2 #include "structdefs.h"
3 #include <sys/socket.h>
9 #include "SimpleHash.h"
10 #include "GenericHashtable.h"
12 extern struct RuntimeHash *fdtoobject;
14 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___ ,int ___port___) {
15 struct sockaddr_in sin;
18 bzero(&sin, sizeof(sin));
19 sin.sin_family= AF_INET;
20 sin.sin_port=htons(___port___);
21 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
24 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
28 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
29 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
41 fcntl(___fd___, F_SETFD, 1);
42 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
43 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
55 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
56 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
57 RuntimeHashremovekey(fdtoobject, ___fd___);
58 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
64 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
68 struct sockaddr_in sin;
69 bzero(&sin, sizeof(sin));
70 sin.sin_family= AF_INET;
72 sin.sin_addr.s_addr=INADDR_ANY;
74 fd=socket(AF_INET, SOCK_STREAM, 0);
78 printf("createSocket error in nativeBind\n");
81 longjmp(error_handler,12);
91 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
94 sa_size = sizeof(sin);
95 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
104 printf("createSocket error #2 in nativeBind\n");
107 longjmp(error_handler,13);
117 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
118 int length=VAR(___hostname___)->___length___;
120 char * str=malloc(length+1);
122 struct ArrayObject * arraybytearray;
124 for(i=0;i<length;i++) {
125 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
128 h=gethostbyname(str);
131 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
134 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
136 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
139 struct ArrayObject *bytearray;
142 int ptrarray[]={1, (int) ___params___, (int)arraybytearray};
143 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
144 arraybytearray=(struct ArrayObject *) ptrarray[2];
147 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
149 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
151 int ha=ntohl(*(int *)h->h_addr_list[i]);
152 (&bytearray->___length___)[1]=ha;
156 return arraybytearray;
160 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
164 struct sockaddr_in sin;
166 bzero(&sin, sizeof(sin));
167 sin.sin_family = AF_INET;
168 sin.sin_port = htons (port);
169 sin.sin_addr.s_addr = htonl (INADDR_ANY);
170 fd=socket(AF_INET, SOCK_STREAM, 0);
174 printf("createSocket error #1\n");
177 longjmp(error_handler,5);
187 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
191 printf("createSocket error #2\n");
194 longjmp(error_handler,6);
205 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
212 fcntl(fd, F_SETFD, 1);
213 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
217 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
221 printf("createSocket error #3\n");
224 longjmp(error_handler,7);
235 if (listen(fd, 5)<0) {
239 printf("createSocket error #4\n");
242 longjmp(error_handler,8);
252 /* Store the fd/socket object mapping */
254 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
260 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
261 struct sockaddr_in sin;
262 unsigned int sinlen=sizeof(sin);
263 int fd=VAR(___this___)->___fd___;
267 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
270 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
279 printf("acceptSocket error #1\n");
282 longjmp(error_handler,9);
292 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
293 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
295 flagorand(VAR(___this___),0,0xFFFFFFFE);
300 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
301 int fd=VAR(___this___)->___fd___;
302 char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int)+offset;
307 bytewritten=write(fd, &charstr[offset], length);
308 if (bytewritten==-1&&errno!=EAGAIN)
315 perror("ERROR IN NATIVEWRITE");
316 printf("error=%d remaining bytes %d\n",errno, length);
322 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
323 int fd=VAR(___this___)->___fd___;
324 int length=VAR(___b___)->___length___;
326 char * charstr=malloc(length);
330 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
335 // printf("Doing read on %d\n",fd);
337 byteread=read(fd, charstr, length);
338 } while(byteread==-1&&errno==EINTR);
347 for(i=0;i<byteread;i++) {
348 (((char *)& VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
355 printf("ERROR IN NATIVEREAD\n");
359 flagorand(VAR(___this___),0,0xFFFFFFFE);
364 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
365 int fd=VAR(___this___)->___fd___;
368 RuntimeHashget(fdtoobject, fd, &data);
369 RuntimeHashremove(fdtoobject, fd, data);
371 flagorand(VAR(___this___),0,0xFFFFFFFE);