2 #include "structdefs.h"
5 #include <sys/socket.h>
9 #include <netinet/tcp.h>
12 #include "SimpleHash.h"
13 #include "GenericHashtable.h"
15 struct RuntimeHash *fdtoobject;
17 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___,int ___port___) {
19 // not supported in RAW version
22 struct sockaddr_in sin;
25 bzero(&sin, sizeof(sin));
26 sin.sin_family= AF_INET;
27 sin.sin_port=htons(___port___);
28 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
29 #if defined(THREADS)||defined(DSTM)
31 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
35 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
36 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
37 #if defined(THREADS)||defined(DSTM)
48 fcntl(___fd___, F_SETFD, 1);
49 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
50 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
63 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
64 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
65 RuntimeHashremovekey(fdtoobject, ___fd___);
66 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
72 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
74 // not supported in RAW version
80 struct sockaddr_in sin;
81 bzero(&sin, sizeof(sin));
82 sin.sin_family= AF_INET;
84 sin.sin_addr.s_addr=INADDR_ANY;
86 fd=socket(AF_INET, SOCK_STREAM, 0);
90 printf("createSocket error in nativeBind\n");
93 longjmp(error_handler,12);
103 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
104 if (rc<0) goto error;
106 sa_size = sizeof(sin);
107 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
108 if (rc<0) goto error;
116 printf("createSocket error #2 in nativeBind\n");
119 longjmp(error_handler,13);
130 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
132 // not supported in RAW version
135 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
136 int length=VAR(___hostname___)->___length___;
138 char * str=malloc(length+1);
140 struct ArrayObject * arraybytearray;
142 for(i=0; i<length; i++) {
143 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
146 h=gethostbyname(str);
149 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
152 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
154 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
157 struct ArrayObject *bytearray;
160 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
161 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
162 arraybytearray=(struct ArrayObject *) ptrarray[2];
165 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
167 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
169 int ha=ntohl(*(int *)h->h_addr_list[i]);
170 (&bytearray->___length___)[1]=ha;
174 return arraybytearray;
179 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
181 // not supported in RAW version
187 struct sockaddr_in sin;
189 bzero(&sin, sizeof(sin));
190 sin.sin_family = AF_INET;
191 sin.sin_port = htons(port);
192 sin.sin_addr.s_addr = htonl(INADDR_ANY);
193 fd=socket(AF_INET, SOCK_STREAM, 0);
197 printf("createSocket error #1\n");
200 longjmp(error_handler,5);
210 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
214 printf("createSocket error #2\n");
217 longjmp(error_handler,6);
228 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
235 fcntl(fd, F_SETFD, 1);
236 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
240 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
244 printf("createSocket error #3\n");
247 longjmp(error_handler,7);
258 if (listen(fd, 5)<0) {
262 printf("createSocket error #4\n");
265 longjmp(error_handler,8);
275 /* Store the fd/socket object mapping */
277 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
284 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
286 // not supported in RAW version
289 struct sockaddr_in sin;
290 unsigned int sinlen=sizeof(sin);
291 int fd=VAR(___this___)->___fd___;
293 #if defined(THREADS)||defined(DSTM)
295 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
298 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
300 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
301 #if defined(THREADS)||defined(DSTM)
309 printf("acceptSocket error #1\n");
312 longjmp(error_handler,9);
322 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
323 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
326 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
327 enqueueObject(VAR(___this___), NULL, 0);
328 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
329 //enqueueObject(VAR(___this___), objq4socketobj[corenum], numqueues4socketobj[corenum]);
331 flagorand(VAR(___this___),0,0xFFFFFFFE);
332 enqueueObject(VAR(___this___));
339 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
340 int fd=VAR(___this___)->___fd___;
341 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
346 bytewritten=write(fd, &charstr[offset], length);
347 if (bytewritten==-1&&errno!=EAGAIN)
355 perror("ERROR IN NATIVEWRITE");
356 printf("error=%d remaining bytes %d\n",errno, length);
363 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
364 int fd=VAR(___this___)->___fd___;
365 int length=VAR(___b___)->___length___;
367 char * charstr=malloc(length);
369 #if defined(THREADS)||defined(DSTM)
371 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
376 // printf("Doing read on %d\n",fd);
378 byteread=read(fd, charstr, length);
379 } while(byteread==-1&&errno==EINTR);
380 #if defined(THREADS)||defined(DSTM)
388 for(i=0; i<byteread; i++) {
389 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
397 printf("ERROR IN NATIVEREAD\n");
403 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
404 enqueueObject(VAR(___this___), NULL, 0);
405 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
406 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
408 flagorand(VAR(___this___),0,0xFFFFFFFE);
409 enqueueObject(VAR(___this___));
415 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
416 int fd=VAR(___this___)->___fd___;
419 RuntimeHashget(fdtoobject, fd, &data);
420 RuntimeHashremove(fdtoobject, fd, data);
423 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
424 enqueueObject(VAR(___this___), NULL, 0);
425 //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
426 //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
428 flagorand(VAR(___this___),0,0xFFFFFFFE);
429 enqueueObject(VAR(___this___));