460204e9f3cc732cd2309a2183fa3348c7c3c19e
[IRC.git] / Robust / src / Runtime / socket.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <sys/socket.h>
4 #include <fcntl.h>
5 #include <arpa/inet.h>
6 #include <strings.h>
7 #include <errno.h>
8 #include "SimpleHash.h"
9 #include "GenericHashtable.h"
10
11 extern struct RuntimeHash *fdtoobject;
12
13 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
14   int fd;
15
16   int n=1;
17   struct sockaddr_in sin;
18
19   bzero (&sin, sizeof (sin));
20   sin.sin_family = AF_INET;
21   sin.sin_port = htons (port);
22   sin.sin_addr.s_addr = htonl (INADDR_ANY);
23   fd=socket(AF_INET, SOCK_STREAM, 0);
24   if (fd<0) {
25 #ifdef DEBUG
26     perror(NULL);
27     printf("createSocket error #1\n");
28 #endif
29     longjmp(error_handler,5);
30   }
31
32   if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
33     close(fd);
34 #ifdef DEBUG
35     perror(NULL);
36     printf("createSocket error #2\n");
37 #endif
38     longjmp(error_handler, 6);
39   }
40   fcntl(fd, F_SETFD, 1);
41   fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
42
43   /* bind to port */
44   if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) { 
45     close (fd);
46 #ifdef DEBUG
47     perror(NULL);
48     printf("createSocket error #3\n");
49 #endif
50     longjmp(error_handler, 7);
51   }
52
53   /* listen */
54   if (listen(fd, 5)<0) { 
55     close (fd);
56 #ifdef DEBUG
57     perror(NULL);
58     printf("createSocket error #4\n");
59 #endif
60     longjmp(error_handler, 8);
61   }
62
63   /* Store the fd/socket object mapping */
64   RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
65   addreadfd(fd);
66   return fd;
67 }
68
69 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
70   struct sockaddr_in sin;
71   unsigned int sinlen=sizeof(sin);
72   int fd=VAR(___this___)->___fd___;
73   int newfd;
74   newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
75
76
77   if (newfd<0) { 
78 #ifdef DEBUG
79     perror(NULL);
80     printf("acceptSocket error #1\n");
81 #endif
82     longjmp(error_handler, 9);
83   }
84   fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
85
86   RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
87   addreadfd(newfd);
88   flagorand(VAR(___this___),0,0xFFFFFFFE);
89   return newfd;
90 }
91
92
93 void CALL02(___Socket______nativeWrite_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
94   int fd=VAR(___this___)->___fd___;
95   int length=VAR(___b___)->___length___;
96   char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
97   while(1) {
98     int bytewritten=write(fd, charstr, length);
99     if (bytewritten==-1&&errno==EAGAIN)
100       continue;
101
102     if (bytewritten!=length) {
103       perror("ERROR IN NATIVEWRITE");
104     }
105     break;
106   }
107   //  flagorand(VAR(___this___),0,0xFFFFFFFE);
108 }
109
110 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
111   int fd=VAR(___this___)->___fd___;
112   int length=VAR(___b___)->___length___;
113   char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
114   int byteread=read(fd, charstr, length);
115   
116   if (byteread<0) {
117     printf("ERROR IN NATIVEREAD\n");
118   }
119   flagorand(VAR(___this___),0,0xFFFFFFFE);
120   return byteread;
121 }
122
123 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
124   int fd=VAR(___this___)->___fd___;
125   int data;
126   RuntimeHashget(fdtoobject, fd, &data);
127   RuntimeHashremove(fdtoobject, fd, data);
128   removereadfd(fd);
129   close(fd);
130   flagorand(VAR(___this___),0,0xFFFFFFFE);
131 }