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