/[classpath]/classpath/native/jni/java-net/javanet.c
ViewVC logotype

Diff of /classpath/native/jni/java-net/javanet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cbj, Mon Nov 5 02:56:47 2001 UTC revision 1.2 by cbj, Tue Nov 27 03:12:02 2001 UTC
# Line 63  _javanet_set_int_field(JNIEnv *env, jobj Line 63  _javanet_set_int_field(JNIEnv *env, jobj
63    jfieldID fid;    jfieldID fid;
64    
65    cls = (*env)->FindClass(env, class);    cls = (*env)->FindClass(env, class);
66      if (cls == NULL)
67        return;
68    
69    fid = (*env)->GetFieldID(env, cls, field, "I");    fid = (*env)->GetFieldID(env, cls, field, "I");
70    if (!fid)    if (fid == NULL)
71      return;      return;
72    
73    (*env)->SetIntField(env, obj, fid, val);    (*env)->SetIntField(env, obj, fid, val);
# Line 85  _javanet_get_int_field(JNIEnv *env, jobj Line 88  _javanet_get_int_field(JNIEnv *env, jobj
88    jfieldID fid;    jfieldID fid;
89    int fd;    int fd;
90    
91    DBG("Entered _javanet_get_int_field\n");    DBG("_javanet_get_int_field(): Entered _javanet_get_int_field\n");
92    
93    cls = (*env)->GetObjectClass(env, obj);    cls = (*env)->GetObjectClass(env, obj);
94      if (cls == NULL)
95        return -1;
96    
97    fid = (*env)->GetFieldID(env, cls, field, "I");    fid = (*env)->GetFieldID(env, cls, field, "I");
98    if (!fid)    if (fid == NULL)
99      return(-1);      return -1;
100    DBG("Found field id\n");    DBG("_javanet_get_int_field(): Found field id\n");
101    
102    fd = (*env)->GetIntField(env, obj, fid);    fd = (*env)->GetIntField(env, obj, fid);
103    
104    return(fd);    return fd;
105  }  }
106    
107  /*************************************************************************/  /*************************************************************************/
# Line 113  _javanet_create_localfd(JNIEnv *env, job Line 119  _javanet_create_localfd(JNIEnv *env, job
119    jmethodID mid;    jmethodID mid;
120    jobject fd_obj;    jobject fd_obj;
121    
122    DBG("Entered _javanet_create_localfd\n");    DBG("_javanet_create_localfd(): Entered _javanet_create_localfd\n");
123    
124    /* Look up the fd field */    /* Look up the fd field */
125    this_cls = (*env)->FindClass(env, "java/net/SocketImpl");    this_cls = (*env)->FindClass(env, "java/net/SocketImpl");
126      if (this_cls == NULL)
127        return;
128    
129    fid = (*env)->GetFieldID(env, this_cls, "fd", "Ljava/io/FileDescriptor;");    fid = (*env)->GetFieldID(env, this_cls, "fd", "Ljava/io/FileDescriptor;");
130    if (!fid)    if (fid == NULL)
131      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      return;
132    DBG("Found fd variable\n");  
133      DBG("_javanet_create_localfd(): Found fd variable\n");
134    
135    /* Create a FileDescriptor */    /* Create a FileDescriptor */
136    fd_cls = (*env)->FindClass(env, "java/io/FileDescriptor");    fd_cls = (*env)->FindClass(env, "java/io/FileDescriptor");
137    if (!fd_cls)    if (fd_cls == NULL)
138      {      return;
139        JCL_ThrowException(env, IO_EXCEPTION, "Can't load FileDescriptor class");  
140        return;    DBG("_javanet_create_localfd(): Found FileDescriptor class\n");
     }  
   DBG("Found FileDescriptor class\n");  
141    
142    mid  = (*env)->GetMethodID(env, fd_cls, "<init>", "()V");    mid  = (*env)->GetMethodID(env, fd_cls, "<init>", "()V");
143    if (!mid)    if (mid == NULL)
144      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      return;
145    DBG("Found FileDescriptor constructor\n");  
146      DBG("_javanet_create_localfd(): Found FileDescriptor constructor\n");
147    
148    fd_obj = (*env)->NewObject(env, fd_cls, mid);    fd_obj = (*env)->NewObject(env, fd_cls, mid);
149    if (!fd_obj)    if (fd_obj == NULL)
150      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      return;
151    DBG("Created FileDescriptor\n");  
152      DBG("_javanet_create_localfd(): Created FileDescriptor\n");
153    
154    /* Now set the pointer to the new FileDescriptor */    /* Now set the pointer to the new FileDescriptor */
155    (*env)->SetObjectField(env, this, fid, fd_obj);    (*env)->SetObjectField(env, this, fid, fd_obj);
156    DBG("Set fd field\n");    DBG("_javanet_create_localfd(): Set fd field\n");
157    
158    return;    return;
159  }  }
# Line 161  _javanet_create_boolean(JNIEnv *env, jbo Line 171  _javanet_create_boolean(JNIEnv *env, jbo
171    jobject obj;    jobject obj;
172    
173    cls = (*env)->FindClass(env, "java/lang/Boolean");    cls = (*env)->FindClass(env, "java/lang/Boolean");
174    if (!cls)    if (cls == NULL)
175      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
176    
177    mid = (*env)->GetMethodID(env, cls, "<init>", "(Z)V");    mid = (*env)->GetMethodID(env, cls, "<init>", "(Z)V");
178    if (!mid)    if (mid == NULL)
179      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
180    
181    obj = (*env)->NewObject(env, cls, mid, val);    obj = (*env)->NewObject(env, cls, mid, val);
182    if (!obj)    if (obj == NULL)
183      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
184    
185    return(obj);    return obj;
186  }  }
187    
188  /*************************************************************************/  /*************************************************************************/
# Line 188  _javanet_create_integer(JNIEnv *env, jin Line 198  _javanet_create_integer(JNIEnv *env, jin
198    jobject obj;    jobject obj;
199    
200    cls = (*env)->FindClass(env, "java/lang/Integer");    cls = (*env)->FindClass(env, "java/lang/Integer");
201    if (!cls)    if (cls == NULL)
202      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
203    
204    mid = (*env)->GetMethodID(env, cls, "<init>", "(I)V");    mid = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
205    if (!mid)    if (mid == NULL)
206      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
207    
208    obj = (*env)->NewObject(env, cls, mid, val);    obj = (*env)->NewObject(env, cls, mid, val);
209    if (!obj)    if (obj == NULL)
210      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
211    
212    return(obj);    return obj;
213  }  }
214    
215  /*************************************************************************/  /*************************************************************************/
# Line 220  _javanet_create_inetaddress(JNIEnv *env, Line 230  _javanet_create_inetaddress(JNIEnv *env,
230    sprintf(buf, "%d.%d.%d.%d", ((netaddr & 0xFF000000) >> 24),    sprintf(buf, "%d.%d.%d.%d", ((netaddr & 0xFF000000) >> 24),
231            ((netaddr & 0x00FF0000) >> 16), ((netaddr &0x0000FF00) >> 8),            ((netaddr & 0x00FF0000) >> 16), ((netaddr &0x0000FF00) >> 8),
232            (netaddr & 0x000000FF));            (netaddr & 0x000000FF));
233    DBG("Created ip addr string\n");    DBG("_javanet_create_inetaddress(): Created ip addr string\n");
234    
235    /* Get an InetAddress object for this IP */    /* Get an InetAddress object for this IP */
236    ia_cls = (*env)->FindClass(env, "java/net/InetAddress");    ia_cls = (*env)->FindClass(env, "java/net/InetAddress");
237    if (!ia_cls)    if (ia_cls == NULL)
238      {      return NULL;
239        JCL_ThrowException(env, IO_EXCEPTION, "Can't load InetAddress class");  
240        return(0);    DBG("_javanet_create_inetaddress(): Found InetAddress class\n");
     }  
   DBG("Found InetAddress class\n");  
241    
242    mid = (*env)->GetStaticMethodID(env, ia_cls, "getByName",    mid = (*env)->GetStaticMethodID(env, ia_cls, "getByName",
243                                    "(Ljava/lang/String;)Ljava/net/InetAddress;");                                    "(Ljava/lang/String;)Ljava/net/InetAddress;");
244    if (!mid)    if (mid == NULL)
245      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
246    DBG("Found getByName method\n");  
247      DBG("_javanet_create_inetaddress(): Found getByName method\n");
248    
249    ip_str = (*env)->NewStringUTF(env, buf);    ip_str = (*env)->NewStringUTF(env, buf);
250    if (!ip_str)    if (ip_str == NULL)
251      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
252    
253    ia = (*env)->CallStaticObjectMethod(env, ia_cls, mid, ip_str);    ia = (*env)->CallStaticObjectMethod(env, ia_cls, mid, ip_str);
254    if (!ia)    if (ia == NULL)
255      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return(0); }      return NULL;
256    DBG("Called getByName method\n");  
257      DBG("_javanet_create_inetaddress(): Called getByName method\n");
258    
259    return(ia);    return ia;
260  }  }
261    
262  /*************************************************************************/  /*************************************************************************/
# Line 262  _javanet_set_remhost(JNIEnv *env, jobjec Line 272  _javanet_set_remhost(JNIEnv *env, jobjec
272    jfieldID fid;    jfieldID fid;
273    jobject ia;    jobject ia;
274    
275    DBG("Entered _javanet_set_remhost\n");    DBG("_javanet_set_remhost(): Entered _javanet_set_remhost\n");
276    
277    /* Get an InetAddress object */    /* Get an InetAddress object */
278    ia = _javanet_create_inetaddress(env, netaddr);    ia = _javanet_create_inetaddress(env, netaddr);
279    if (!ia)    if (ia == NULL)
280      return;      return;
281    
282    /* Set the variable in the object */    /* Set the variable in the object */
283    this_cls = (*env)->FindClass(env, "java/net/SocketImpl");    this_cls = (*env)->FindClass(env, "java/net/SocketImpl");
284      if (this_cls == NULL)
285        return;
286    
287    fid = (*env)->GetFieldID(env, this_cls, "address", "Ljava/net/InetAddress;");    fid = (*env)->GetFieldID(env, this_cls, "address", "Ljava/net/InetAddress;");
288    if (!fid)    if (fid == NULL)
289      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      return;
290    DBG("Found address field\n");  
291      DBG("_javanet_set_remhost(): Found address field\n");
292    
293    (*env)->SetObjectField(env, this, fid, ia);    (*env)->SetObjectField(env, this, fid, ia);
294    DBG("Set field\n");    DBG("_javanet_set_remhost(): Set field\n");
295  }  }
296    
297  /*************************************************************************/  /*************************************************************************/
# Line 294  _javanet_get_netaddr(JNIEnv *env, jobjec Line 308  _javanet_get_netaddr(JNIEnv *env, jobjec
308    jbyte *octets;    jbyte *octets;
309    int netaddr, len;    int netaddr, len;
310    
311    DBG("Entered _javanet_get_netaddr\n");    DBG("_javanet_get_netaddr(): Entered _javanet_get_netaddr\n");
312    
313    /* Call the getAddress method on the object to retrieve the IP address */    /* Call the getAddress method on the object to retrieve the IP address */
314    cls = (*env)->GetObjectClass(env, addr);    cls = (*env)->GetObjectClass(env, addr);
315      if (cls == NULL)
316        return 0;
317    
318    mid = (*env)->GetMethodID(env, cls, "getAddress", "()[B");    mid = (*env)->GetMethodID(env, cls, "getAddress", "()[B");
319    if (!mid)    if (mid == NULL)
320      {      return 0;
321        JCL_ThrowException(env, IO_EXCEPTION, "Internal Error");  
322        return(0);    DBG("_javanet_get_netaddr(): Got getAddress method\n");
     }  
   DBG("Got getAddress method\n");  
323    
324    arr = (*env)->CallObjectMethod(env, addr, mid);    arr = (*env)->CallObjectMethod(env, addr, mid);
325    if (!arr)    if (arr == NULL)
326      {      return 0;
327        JCL_ThrowException(env, IO_EXCEPTION, "Internal Error");  
328        return(0);    DBG("_javanet_get_netaddr(): Got the address\n");
     }  
   DBG("Got the address\n");  
329    
330    /* Turn the IP address into a 32 bit Internet address in network byte order */    /* Turn the IP address into a 32 bit Internet address in network byte order */
331    len = (*env)->GetArrayLength(env, arr);    len = (*env)->GetArrayLength(env, arr);
332    if (len != 4)    if (len != 4)
333      {      {
334        JCL_ThrowException(env, IO_EXCEPTION, "Internal Error");        JCL_ThrowException(env, IO_EXCEPTION, "Internal Error");
335        return(0);        return 0;
336      }      }
337    DBG("Length ok\n");    DBG("_javanet_get_netaddr(): Length ok\n");
338    
339    octets = (*env)->GetByteArrayElements(env, arr, 0);      octets = (*env)->GetByteArrayElements(env, arr, 0);  
340    if (!octets)    if (octets == NULL)
341      {      return 0;
342        JCL_ThrowException(env, IO_EXCEPTION, "Internal Error");  
343        return(0);    DBG("_javanet_get_netaddr(): Grabbed bytes\n");
     }  
   DBG("Grabbed bytes\n");  
344    
345    netaddr = (((unsigned char)octets[0]) << 24) +    netaddr = (((unsigned char)octets[0]) << 24) +
346              (((unsigned char)octets[1]) << 16) +              (((unsigned char)octets[1]) << 16) +
# Line 339  _javanet_get_netaddr(JNIEnv *env, jobjec Line 350  _javanet_get_netaddr(JNIEnv *env, jobjec
350    netaddr = htonl(netaddr);    netaddr = htonl(netaddr);
351    
352    (*env)->ReleaseByteArrayElements(env, arr, octets, 0);    (*env)->ReleaseByteArrayElements(env, arr, octets, 0);
353    DBG("Done getting addr\n");    DBG("_javanet_get_netaddr(): Done getting addr\n");
354    
355    return(netaddr);    return netaddr;
356  }  }
357    
358  /*************************************************************************/  /*************************************************************************/
# Line 406  _javanet_connect(JNIEnv *env, jobject th Line 417  _javanet_connect(JNIEnv *env, jobject th
417    int netaddr, fd = -1, rc, addrlen;    int netaddr, fd = -1, rc, addrlen;
418    struct sockaddr_in si;    struct sockaddr_in si;
419    
420    DBG("Entered _javanet_connect\n");    DBG("_javanet_connect(): Entered _javanet_connect\n");
421    
422    /* Pre-process input variables */    /* Pre-process input variables */
423    netaddr = _javanet_get_netaddr(env, addr);    netaddr = _javanet_get_netaddr(env, addr);
# Line 415  _javanet_connect(JNIEnv *env, jobject th Line 426  _javanet_connect(JNIEnv *env, jobject th
426    
427    if (port == -1)    if (port == -1)
428      port = 0;      port = 0;
429    DBG("Got network address\n");    DBG("_javanet_connect(): Got network address\n");
430    
431    /* Grab the real socket file descriptor */    /* Grab the real socket file descriptor */
432    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
433    if (fd == -1)    if (fd == -1)
434      { JCL_ThrowException(env, IO_EXCEPTION,      {
435                                 "Socket not yet created"); return; }        JCL_ThrowException(env, IO_EXCEPTION,
436    DBG("Got native fd\n");                           "Internal error: _javanet_connect(): no native file descriptor");
437          return;
438        }
439      DBG("_javanet_connect(): Got native fd\n");
440    
441    /* Connect up */    /* Connect up */
442    memset(&si, 0, sizeof(struct sockaddr_in));    memset(&si, 0, sizeof(struct sockaddr_in));
# Line 433  _javanet_connect(JNIEnv *env, jobject th Line 447  _javanet_connect(JNIEnv *env, jobject th
447    rc = connect(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in));    rc = connect(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in));
448    if (rc == -1)    if (rc == -1)
449      { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; }      { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; }
450    DBG("Connected successfully\n");    DBG("_javanet_connect(): Connected successfully\n");
451    
452    /* Populate instance variables */    /* Populate instance variables */
453    addrlen = sizeof(struct sockaddr_in);    addrlen = sizeof(struct sockaddr_in);
# Line 447  _javanet_connect(JNIEnv *env, jobject th Line 461  _javanet_connect(JNIEnv *env, jobject th
461    
462    _javanet_create_localfd(env, this);    _javanet_create_localfd(env, this);
463    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
464      return;      {
465    DBG("Created fd\n");        close(fd);
466          return;
467        }
468      DBG("_javanet_connect(): Created fd\n");
469    
470    _javanet_set_int_field(env, this, "java/net/SocketImpl", "localport",    _javanet_set_int_field(env, this, "java/net/SocketImpl", "localport",
471                           ntohs(si.sin_port));                           ntohs(si.sin_port));
472    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
473      return;      {
474    DBG("Set the local port\n");        close(fd);
475          return;
476        }
477      DBG("_javanet_connect(): Set the local port\n");
478        
479    addrlen = sizeof(struct sockaddr_in);    addrlen = sizeof(struct sockaddr_in);
480    rc = getpeername(fd, (struct sockaddr *) &si, &addrlen);    rc = getpeername(fd, (struct sockaddr *) &si, &addrlen);
# Line 467  _javanet_connect(JNIEnv *env, jobject th Line 487  _javanet_connect(JNIEnv *env, jobject th
487    
488    _javanet_set_remhost(env, this, ntohl(si.sin_addr.s_addr));    _javanet_set_remhost(env, this, ntohl(si.sin_addr.s_addr));
489    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
490      return;      {
491    DBG("Set the remote host\n");        close(fd);
492          return;
493        }
494      DBG("_javanet_connect(): Set the remote host\n");
495    
496    _javanet_set_int_field(env, this, "java/net/SocketImpl", "port",    _javanet_set_int_field(env, this, "java/net/SocketImpl", "port",
497                           ntohs(si.sin_port));                           ntohs(si.sin_port));
498    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
499      return;      {
500    DBG("Set the remote port\n");        close(fd);
501          return;
502        }
503      DBG("_javanet_connect(): Set the remote port\n");
504  }  }
505    
506  /*************************************************************************/  /*************************************************************************/
# Line 495  _javanet_bind(JNIEnv *env, jobject this, Line 521  _javanet_bind(JNIEnv *env, jobject this,
521    struct sockaddr_in si;    struct sockaddr_in si;
522    int namelen;    int namelen;
523    
524    DBG("Entering native bind()\n");    DBG("_javanet_bind(): Entering native bind()\n");
525    
526    /* Get the address to connect to */    /* Get the address to connect to */
527    cls = (*env)->GetObjectClass(env, addr);    cls = (*env)->GetObjectClass(env, addr);
528      if (cls == NULL)
529        return;
530    
531    mid  = (*env)->GetMethodID(env, cls, "getAddress", "()[B");    mid  = (*env)->GetMethodID(env, cls, "getAddress", "()[B");
532    if (!mid)    if (mid == NULL)
533      { JCL_ThrowException(env, IO_EXCEPTION, "Internal error") ; return; }      return;
534    DBG("Past getAddress method id\n");  
535      DBG("_javanet_bind(): Past getAddress method id\n");
536    
537    arr = (*env)->CallObjectMethod(env, addr, mid);    arr = (*env)->CallObjectMethod(env, addr, mid);
538    if (!arr || (*env)->ExceptionOccurred(env))    if ((arr == NULL) || (*env)->ExceptionOccurred(env))
539      { JCL_ThrowException(env, IO_EXCEPTION, "Internal error") ; return; }      { JCL_ThrowException(env, IO_EXCEPTION, "Internal error: _javanet_bind()"); return; }
540    DBG("Past call object method\n");  
541      DBG("_javanet_bind(): Past call object method\n");
542    
543    octets = (*env)->GetByteArrayElements(env, arr, 0);      octets = (*env)->GetByteArrayElements(env, arr, 0);  
544    if (!octets)    if (octets == NULL)
545      { JCL_ThrowException(env, IO_EXCEPTION, "Internal error") ; return; }      return;
546    DBG("Past grab array\n");  
547      DBG("_javanet_bind(): Past grab array\n");
548    
549    /* Get the native socket file descriptor */    /* Get the native socket file descriptor */
550    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
551    if (fd == -1)    if (fd == -1)
552      {      {
553        (*env)->ReleaseByteArrayElements(env, arr, octets, 0);        (*env)->ReleaseByteArrayElements(env, arr, octets, 0);
554        JCL_ThrowException(env, IO_EXCEPTION, "Internal error");        JCL_ThrowException(env, IO_EXCEPTION,
555                             "Internal error: _javanet_bind(): no native file descriptor");
556        return;        return;
557      }      }
558    DBG("Past native_fd lookup\n");    DBG("_javanet_bind(): Past native_fd lookup\n");
559    
560    /* Bind the socket */    /* Bind the socket */
561    memset(&si, 0, sizeof(struct sockaddr_in));    memset(&si, 0, sizeof(struct sockaddr_in));
# Line 538  _javanet_bind(JNIEnv *env, jobject this, Line 571  _javanet_bind(JNIEnv *env, jobject this,
571    
572    if (bind(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in)) == -1)    if (bind(fd, (struct sockaddr *) &si, sizeof(struct sockaddr_in)) == -1)
573      { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; }      { JCL_ThrowException(env, IO_EXCEPTION, strerror(errno)); return; }
574    DBG("Past bind\n");    DBG("_javanet_bind(): Past bind\n");
575        
576    /* Update instance variables, specifically the local port number */    /* Update instance variables, specifically the local port number */
577    namelen = sizeof(struct sockaddr_in);    namelen = sizeof(struct sockaddr_in);
# Line 550  _javanet_bind(JNIEnv *env, jobject this, Line 583  _javanet_bind(JNIEnv *env, jobject this,
583    else    else
584      _javanet_set_int_field(env, this, "java/net/DatagramSocketImpl",      _javanet_set_int_field(env, this, "java/net/DatagramSocketImpl",
585                             "localPort", ntohs(si.sin_port));                             "localPort", ntohs(si.sin_port));
586    DBG("Past update port number\n");    DBG("_javanet_bind(): Past update port number\n");
587    
588    return;    return;
589  }  }
# Line 569  _javanet_listen(JNIEnv *env, jobject thi Line 602  _javanet_listen(JNIEnv *env, jobject thi
602    /* Get the real file descriptor */    /* Get the real file descriptor */
603    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
604    if (fd == -1)    if (fd == -1)
605      { JCL_ThrowException(env, IO_EXCEPTION,      {
606                                 "Internal Error"); return; }        JCL_ThrowException(env, IO_EXCEPTION,
607                             "Internal error: _javanet_listen(): no native file descriptor");
608          return;
609        }
610    
611    /* Start listening */    /* Start listening */
612    rc = listen(fd, queuelen);    rc = listen(fd, queuelen);
# Line 595  _javanet_accept(JNIEnv *env, jobject thi Line 631  _javanet_accept(JNIEnv *env, jobject thi
631    /* Get the real file descriptor */    /* Get the real file descriptor */
632    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
633    if (fd == -1)    if (fd == -1)
634      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      {
635          JCL_ThrowException(env, IO_EXCEPTION,
636                             "Internal error: _javanet_accept(): no native file descriptor");
637          return;
638        }
639    
640    /* Accept the connection */    /* Accept the connection */
641    addrlen = sizeof(struct sockaddr_in);    addrlen = sizeof(struct sockaddr_in);
# Line 604  _javanet_accept(JNIEnv *env, jobject thi Line 644  _javanet_accept(JNIEnv *env, jobject thi
644    /******* Do we need to look for EINTR? */    /******* Do we need to look for EINTR? */
645    newfd = accept(fd, (struct sockaddr *) &si, &addrlen);    newfd = accept(fd, (struct sockaddr *) &si, &addrlen);
646    if (newfd == -1)    if (newfd == -1)
647      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      { JCL_ThrowException(env, IO_EXCEPTION, "Internal error: _javanet_accept(): "); return; }
648    
649    /* Populate instance variables */    /* Populate instance variables */
650    _javanet_set_int_field(env, impl, "java/net/PlainSocketImpl", "native_fd",    _javanet_set_int_field(env, impl, "java/net/PlainSocketImpl", "native_fd",
651                           newfd);                           newfd);
652    
653      if ((*env)->ExceptionOccurred(env))
654        {
655          close(newfd);
656          return;
657        }
658    
659    rc = getsockname(newfd, (struct sockaddr *) &si, &addrlen);    rc = getsockname(newfd, (struct sockaddr *) &si, &addrlen);
660    if (rc == -1)    if (rc == -1)
661      {      {
# Line 620  _javanet_accept(JNIEnv *env, jobject thi Line 666  _javanet_accept(JNIEnv *env, jobject thi
666    
667    _javanet_create_localfd(env, impl);    _javanet_create_localfd(env, impl);
668    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
669      return;      {
670          close(newfd);
671          return;
672        }
673    
674    _javanet_set_int_field(env, impl, "java/net/SocketImpl", "localport",    _javanet_set_int_field(env, impl, "java/net/SocketImpl", "localport",
675                           ntohs(si.sin_port));                           ntohs(si.sin_port));
676    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
677      return;      {
678          close(newfd);
679          return;
680        }
681        
682    addrlen = sizeof(struct sockaddr_in);    addrlen = sizeof(struct sockaddr_in);
683    rc = getpeername(newfd, (struct sockaddr *) &si, &addrlen);    rc = getpeername(newfd, (struct sockaddr *) &si, &addrlen);
# Line 638  _javanet_accept(JNIEnv *env, jobject thi Line 690  _javanet_accept(JNIEnv *env, jobject thi
690    
691    _javanet_set_remhost(env, impl, ntohl(si.sin_addr.s_addr));    _javanet_set_remhost(env, impl, ntohl(si.sin_addr.s_addr));
692    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
693      return;      {
694          close(newfd);
695          return;
696        }
697    
698    _javanet_set_int_field(env, impl, "java/net/SocketImpl", "port",    _javanet_set_int_field(env, impl, "java/net/SocketImpl", "port",
699                           ntohs(si.sin_port));                           ntohs(si.sin_port));
700    if ((*env)->ExceptionOccurred(env))    if ((*env)->ExceptionOccurred(env))
701      return;      {
702          close(newfd);
703          return;
704        }
705  }  }
706    
707  /*************************************************************************/  /*************************************************************************/
# Line 670  _javanet_recvfrom(JNIEnv *env, jobject t Line 728  _javanet_recvfrom(JNIEnv *env, jobject t
728    jbyte *p;    jbyte *p;
729    struct sockaddr_in si;    struct sockaddr_in si;
730    
731    DBG("Entered _javanet_recvfrom\n");    DBG("_javanet_recvfrom(): Entered _javanet_recvfrom\n");
732    
733    /* Get the real file descriptor */    /* Get the real file descriptor */
734    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
735    if (fd == -1)    if (fd == -1)
736      { JCL_ThrowException(env, IO_EXCEPTION, "No Socket"); return 0; }      {
737    DBG("Got native_fd\n");        JCL_ThrowException(env, IO_EXCEPTION,
738                             "Internal error: _javanet_recvfrom(): no native file descriptor");
739          return 0;
740        }
741      DBG("_javanet_recvfrom(): Got native_fd\n");
742    
743    /* Get a pointer to the buffer */    /* Get a pointer to the buffer */
744    p = (*env)->GetByteArrayElements(env, buf, 0);    p = (*env)->GetByteArrayElements(env, buf, 0);
745    if (!p)    if (p == NULL)
746      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return 0; }      return 0;
747    DBG("Got buffer\n");  
748      DBG("_javanet_recvfrom(): Got buffer\n");
749    
750    /* Read the data */    /* Read the data */
751    for (;;)    for (;;)
752      {      {
753        if (!addr)        if (addr == NULL)
754          rc = recvfrom(fd, p + offset, len, 0, 0, 0);          rc = recvfrom(fd, p + offset, len, 0, 0, 0);
755        else        else
756          {          {
# Line 740  _javanet_sendto(JNIEnv *env, jobject thi Line 803  _javanet_sendto(JNIEnv *env, jobject thi
803    /* Get the real file descriptor */    /* Get the real file descriptor */
804    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
805    if (fd == -1)    if (fd == -1)
806      { JCL_ThrowException(env, IO_EXCEPTION, "No Socket"); return; }      {
807          JCL_ThrowException(env, IO_EXCEPTION,
808                             "Internal error: _javanet_sendto(): no native file descriptor");
809          return;
810        }
811    
812    /* Get a pointer to the buffer */    /* Get a pointer to the buffer */
813    p = (*env)->GetByteArrayElements(env, buf, 0);    p = (*env)->GetByteArrayElements(env, buf, 0);
814    if (!p)    if (p == NULL)
815      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      return;
816    
817    /* Send the data */    /* Send the data */
818    if (!addr)    if (addr == 0)
819      rc = send(fd, p + offset, len, 0);      {
820          DBG("_javanet_sendto(): Sending....\n");
821          rc = send(fd, p + offset, len, 0);
822        }
823    else    else
824      {      {
825        memset(&si, 0, sizeof(struct sockaddr_in));        memset(&si, 0, sizeof(struct sockaddr_in));
# Line 757  _javanet_sendto(JNIEnv *env, jobject thi Line 827  _javanet_sendto(JNIEnv *env, jobject thi
827        si.sin_addr.s_addr = addr;        si.sin_addr.s_addr = addr;
828        si.sin_port = (unsigned short)port;        si.sin_port = (unsigned short)port;
829                
830        DBG("Sending....\n");        DBG("_javanet_sendto(): Sending....\n");
831        rc = sendto(fd, p + offset, len, 0, (struct sockaddr *) &si, sizeof(struct sockaddr_in));        rc = sendto(fd, p + offset, len, 0, (struct sockaddr *) &si, sizeof(struct sockaddr_in));
832      }      }
833    
# Line 788  _javanet_set_option(JNIEnv *env, jobject Line 858  _javanet_set_option(JNIEnv *env, jobject
858    /* Get the real file descriptor */    /* Get the real file descriptor */
859    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
860    if (fd == -1)    if (fd == -1)
861      { JCL_ThrowException(env, IO_EXCEPTION, "Internal Error"); return; }      {
862          JCL_ThrowException(env, IO_EXCEPTION,
863                             "Internal error: _javanet_set_option(): no native file descriptor");
864          return;
865        }
866    
867    /* We need a class object for all cases below */    /* We need a class object for all cases below */
868    cls = (*env)->GetObjectClass(env, val);    cls = (*env)->GetObjectClass(env, val);
869      if (cls == NULL)
870        return;
871    
872    /* Process the option request */    /* Process the option request */
873    switch (option_id)    switch (option_id)
# Line 799  _javanet_set_option(JNIEnv *env, jobject Line 875  _javanet_set_option(JNIEnv *env, jobject
875        /* TCP_NODELAY case.  val is a Boolean that tells us what to do */        /* TCP_NODELAY case.  val is a Boolean that tells us what to do */
876        case SOCKOPT_TCP_NODELAY:        case SOCKOPT_TCP_NODELAY:
877          mid = (*env)->GetMethodID(env, cls, "booleanValue", "()Z");          mid = (*env)->GetMethodID(env, cls, "booleanValue", "()Z");
878          if (!mid)          if (mid == NULL)
879            { JCL_ThrowException(env, IO_EXCEPTION,            { JCL_ThrowException(env, IO_EXCEPTION,
880                                       "Internal Error"); return; }                                       "Internal error: _javanet_set_option()"); return; }
881    
882          /* Should be a 0 or a 1 */          /* Should be a 0 or a 1 */
883          optval = (*env)->CallBooleanMethod(env, val, mid);          optval = (*env)->CallBooleanMethod(env, val, mid);
884            if ((*env)->ExceptionOccurred(env))
885              return;
886    
887          rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int));          rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int));
888          break;          break;
# Line 829  _javanet_set_option(JNIEnv *env, jobject Line 907  _javanet_set_option(JNIEnv *env, jobject
907                (*env)->ExceptionClear(env);                (*env)->ExceptionClear(env);
908    
909              mid = (*env)->GetMethodID(env, cls, "intValue", "()I");              mid = (*env)->GetMethodID(env, cls, "intValue", "()I");
910              if (!mid)              if (mid == NULL)
911                { JCL_ThrowException(env, IO_EXCEPTION,                { JCL_ThrowException(env, IO_EXCEPTION,
912                                           "Internal Error"); return; }                                     "Internal error: _javanet_set_option()"); return; }
913    
914              linger.l_linger = (*env)->CallIntMethod(env, val, mid);              linger.l_linger = (*env)->CallIntMethod(env, val, mid);
915                if ((*env)->ExceptionOccurred(env))
916                  return;
917                
918              linger.l_onoff = 1;              linger.l_onoff = 1;
919            }            }
920          rc = setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger,          rc = setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger,
# Line 844  _javanet_set_option(JNIEnv *env, jobject Line 925  _javanet_set_option(JNIEnv *env, jobject
925        case SOCKOPT_SO_TIMEOUT:        case SOCKOPT_SO_TIMEOUT:
926  #ifdef SO_TIMEOUT  #ifdef SO_TIMEOUT
927          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");
928          if (!mid)          if (mid == NULL)
929            { JCL_ThrowException(env, IO_EXCEPTION,            { JCL_ThrowException(env, IO_EXCEPTION,
930                                       "Internal Error"); return; }                                       "Internal error: _javanet_set_option()"); return; }
931    
932          optval = (*env)->CallIntMethod(env, val, mid);          optval = (*env)->CallIntMethod(env, val, mid);
933                        if ((*env)->ExceptionOccurred(env))
934              return;
935    
936          rc = setsockopt(fd, SOL_SOCKET, SO_TIMEOUT, &optval, sizeof(int));          rc = setsockopt(fd, SOL_SOCKET, SO_TIMEOUT, &optval, sizeof(int));
937  #else  #else
938          JCL_ThrowException(env, SOCKET_EXCEPTION,          JCL_ThrowException(env, SOCKET_EXCEPTION,
939                                   "SO_TIMEOUT not supported on this platform");                                   "SO_TIMEOUT not supported by this platform");
940          return;          return;
941  #endif  #endif
942          break;          break;
# Line 861  _javanet_set_option(JNIEnv *env, jobject Line 944  _javanet_set_option(JNIEnv *env, jobject
944        case SOCKOPT_SO_SNDBUF:        case SOCKOPT_SO_SNDBUF:
945        case SOCKOPT_SO_RCVBUF:        case SOCKOPT_SO_RCVBUF:
946          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");
947          if (!mid)          if (mid == NULL)
948            { JCL_ThrowException(env, IO_EXCEPTION,            { JCL_ThrowException(env, IO_EXCEPTION,
949                                       "Internal Error"); return; }                                       "Internal error: _javanet_set_option()"); return; }
950    
951          optval = (*env)->CallIntMethod(env, val, mid);          optval = (*env)->CallIntMethod(env, val, mid);
952            if ((*env)->ExceptionOccurred(env))
953              return;
954                    
955          if (option_id == SOCKOPT_SO_SNDBUF)          if (option_id == SOCKOPT_SO_SNDBUF)
956            sockopt = SO_SNDBUF;            sockopt = SO_SNDBUF;
# Line 880  _javanet_set_option(JNIEnv *env, jobject Line 965  _javanet_set_option(JNIEnv *env, jobject
965          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");          mid = (*env)->GetMethodID(env, cls, "intValue", "()I");
966          if (!mid)          if (!mid)
967            { JCL_ThrowException(env, IO_EXCEPTION,            { JCL_ThrowException(env, IO_EXCEPTION,
968                                       "Internal Error"); return; }                                       "Internal error: _javanet_set_option()"); return; }
969    
970          optval = (*env)->CallIntMethod(env, val, mid);          optval = (*env)->CallIntMethod(env, val, mid);
971            if ((*env)->ExceptionOccurred(env))
972              return;
973                            
974          rc = setsockopt(fd, IPPROTO_IP, IP_TTL, &optval, sizeof(int));          rc = setsockopt(fd, IPPROTO_IP, IP_TTL, &optval, sizeof(int));
975          break;          break;
# Line 908  _javanet_set_option(JNIEnv *env, jobject Line 995  _javanet_set_option(JNIEnv *env, jobject
995    /* Check to see if above operations succeeded */    /* Check to see if above operations succeeded */
996    if (rc == -1)    if (rc == -1)
997      JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno));      JCL_ThrowException(env, SOCKET_EXCEPTION, strerror(errno));
998      
999    return;    return;
1000  }  }
1001    
# Line 928  _javanet_get_option(JNIEnv *env, jobject Line 1015  _javanet_get_option(JNIEnv *env, jobject
1015    /* Get the real file descriptor */    /* Get the real file descriptor */
1016    fd = _javanet_get_int_field(env, this, "native_fd");    fd = _javanet_get_int_field(env, this, "native_fd");
1017    if (fd == -1)    if (fd == -1)
1018      { JCL_ThrowException(env, SOCKET_EXCEPTION, "Internal Error"); return(0); }      {
1019          JCL_ThrowException(env, SOCKET_EXCEPTION,
1020                             "Internal error: _javanet_get_option(): no native file descriptor");
1021          return(0);
1022        }
1023    
1024    /* Process the option requested */    /* Process the option requested */
1025    switch (option_id)    switch (option_id)

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26