1 /* target_generic_network.h - Native methods for network operations.
2    Copyright (C) 1998, 2004, 2005 Free Software Foundation, Inc.
3 
4 Modifications Copyright (C) 2004 by Etienne Gagnon.
5 Modifications Copyright (C) 2004 by David Belanger.
6 Modifications Copyright (C) 2004, 2005 by Grzegorz Prokopski.
7 
8 This file is part of GNU Classpath.
9 
10 GNU Classpath is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14 
15 GNU Classpath is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with GNU Classpath; see the file COPYING.  If not, write to the
22 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301 USA.
24 
25 Linking this library statically or dynamically with other modules is
26 making a combined work based on this library.  Thus, the terms and
27 conditions of the GNU General Public License cover the whole
28 combination.
29 
30 As a special exception, the copyright holders of this library give you
31 permission to link this library with independent modules to produce an
32 executable, regardless of the license terms of these independent
33 modules, and to copy and distribute the resulting executable under
34 terms of your choice, provided that you also meet, for each linked
35 independent module, the terms and conditions of the license of that
36 module.  An independent module is a module which is not derived from
37 or based on this library.  If you modify this library, you may extend
38 this exception to your version of the library, but you are not
39 obligated to do so.  If you do not wish to do so, delete this
40 exception statement from your version. */
41 
42 /*
43 Description: generic target defintions of network functions
44 Systems    : all
45 */
46 
47 #ifndef __TARGET_GENERIC_NETWORK__
48 #define __TARGET_GENERIC_NETWORK__
49 
50 /* check if target_native_network.h included */
51 #ifndef __TARGET_NATIVE_NETWORK__
52   #error Do NOT INCLUDE generic target files! Include the corresponding native target files instead!
53 #endif
54 
55 /****************************** Includes *******************************/
56 /* do not move; needed here because of some macro definitions */
57 #include "config.h"
58 
59 #include <stdlib.h>
60 
61 #ifndef HAVE_SOCKLEN_T
62 typedef unsigned int socklen_t;
63 #endif /* HAVE_SOCKLEN_T */
64 
65 #include "target_native.h"
66 
67 /****************** Conditional compilation switches *******************/
68 
69 /***************************** Constants *******************************/
70 
71 /***************************** Datatypes *******************************/
72 
73 /***************************** Variables *******************************/
74 
75 /****************************** Macros *********************************/
76 
77 /***********************************************************************\
78 * Name       : TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT
79 * Purpose    : convert IP adddress (4 parts) into integer (host-format
80 *              32bit)
81 * Input      : n0,n1,n2,n3 - IP address parts
82 * Output     : i - integer with IP address in host-format
83 * Return     : -
84 * Side-effect: unknown
85 * Notes      : -
86 \***********************************************************************/
87 
88 #ifndef TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT
89   #define TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(n0,n1,n2,n3,i) \
90     do { \
91       i=(((unsigned char)n0) << 24) | \
92         (((unsigned char)n1) << 16) | \
93         (((unsigned char)n2) <<  8) | \
94         (((unsigned char)n3) <<  0); \
95     } while (0)
96 #endif
97 
98 /***********************************************************************\
99 * Name       : TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES
100 * Purpose    : convert IP adddress (4 parts) into integer (host-format
101 *              32bit)
102 * Input      : n0,n1,n2,n3 - IP address parts
103 * Output     : i - integer with IP address in host-format
104 * Return     : -
105 * Side-effect: unknown
106 * Notes      : -
107 \***********************************************************************/
108 
109 #ifndef TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES
110   #define TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(i,n0,n1,n2,n3) \
111     do { \
112       n0=(i & 0xFF000000) >> 24; \
113       n1=(i & 0x00FF0000) >> 16; \
114       n2=(i & 0x0000FF00) >>  8; \
115       n3=(i & 0x000000FF) >>  0; \
116     } while (0)
117 #endif
118 
119 /***********************************************************************\
120 * Name       : TARGET_NATIVE_NETWORK_GET_HOSTNAME
121 * Purpose    : get hostname
122 * Input      : maxNameLen - max. length of name
123 * Output     : name   - name (NUL terminated)
124 *              result - TARGET_NATIVE_OK if no error occurred,
125 *                       TARGET_NATIVE_ERROR otherwise
126 * Return     : -
127 * Side-effect: unknown
128 * Notes      : -
129 \***********************************************************************/
130 
131 #ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME
132   #include <unistd.h>
133   #define TARGET_NATIVE_NETWORK_GET_HOSTNAME(name,maxNameLen,result) \
134     do { \
135       result=(gethostname(name,maxNameLen-1)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
136       name[maxNameLen-1]='\0'; \
137     } while (0)
138 #endif
139 
140 /***********************************************************************\
141 * Name       : TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS
142 * Purpose    : get hostname by address
143 * Input      : address    - IP address (32bit, NOT network byte order!)
144 *              maxNameLen - max. length of name
145 * Output     : name   - name (NUL terminated)
146 *              result - TARGET_NATIVE_OK if no error occurred,
147 *                       TARGET_NATIVE_ERROR otherwise
148 * Return     : -
149 * Side-effect: unknown
150 * Notes      : -
151 \***********************************************************************/
152 
153 /* XXX NYI??? reentrant? */
154 #ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS
155   #include <netdb.h>
156   #define TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS(address,name,maxNameLen,result) \
157     do { \
158       int            __networkAddress; \
159       struct hostent *__hostEntry; \
160       \
161       __networkAddress=htonl(address); \
162       __hostEntry = gethostbyaddr((char*)&__networkAddress,sizeof(__networkAddress),AF_INET); \
163       if (__hostEntry!=NULL) \
164       { \
165         strncpy(name,__hostEntry->h_name,maxNameLen-1); \
166         name[maxNameLen]='\0'; \
167         result=TARGET_NATIVE_OK; \
168       } \
169       else \
170       { \
171         result=TARGET_NATIVE_ERROR; \
172       } \
173     } while (0)
174 #endif
175 
176 /***********************************************************************\
177 * Name       : TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME
178 * Purpose    : get hostname by name
179 * Input      : name           - hostname
180 *              maxAddressSize - max. size of address array
181 * Output     : addresses     - host adddresses (array, NOT in network
182 *                              byte order!)
183 *              addressCount  - number of entries in address array
184 *              result        - TARGET_NATIVE_OK if no error occurred,
185 *                              TARGET_NATIVE_ERROR otherwise
186 * Return     : -
187 * Side-effect: unknown
188 * Notes      : -
189 \***********************************************************************/
190 
191 /* XXX NYI??? reentrant? */
192 #ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME
193   #include <netdb.h>
194   #define TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME(name,addresses,maxAddressSize,addressCount,result) \
195     do { \
196       struct hostent *__hostEntry; \
197       \
198       addressCount=0; \
199       \
200       __hostEntry = gethostbyname(name); \
201       if (__hostEntry!=NULL) \
202       { \
203         while ((addressCount<maxAddressSize) && (__hostEntry->h_addr_list[addressCount]!=NULL)) \
204         { \
205           addresses[addressCount]=ntohl(*(int*)(__hostEntry->h_addr_list[addressCount])); \
206           addressCount++; \
207         } \
208         result=TARGET_NATIVE_OK; \
209       } \
210       else \
211       { \
212         result=TARGET_NATIVE_ERROR; \
213       } \
214     } while (0)
215 #endif
216 
217 /***********************************************************************\
218 * Name       : TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM
219 * Purpose    : open stream socket
220 * Input      : -
221 * Output     : socketDescriptor - socket descriptor
222 *              result           - TARGET_NATIVE_OK if no error occurred,
223 *                                 TARGET_NATIVE_ERROR otherwise
224 * Return     : -
225 * Side-effect: unknown
226 * Notes      : -
227 \***********************************************************************/
228 
229 #ifndef TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM
230   #include <sys/types.h>
231   #include <sys/socket.h>
232   #include <fcntl.h>
233   #define TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM(socketDescriptor,result) \
234     do { \
235       socketDescriptor=socket(AF_INET,SOCK_STREAM,0); \
236       fcntl(socketDescriptor,F_SETFD,FD_CLOEXEC); \
237       result=(socketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
238     } while (0)
239 #endif
240 
241 /***********************************************************************\
242 * Name       : TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM
243 * Purpose    : open datagram socket
244 * Input      : -
245 * Output     : socketDescriptor - socket descriptor
246 *              result           - TARGET_NATIVE_OK if no error occurred,
247 *                                 TARGET_NATIVE_ERROR otherwise
248 * Return     : -
249 * Side-effect: unknown
250 * Notes      : -
251 \***********************************************************************/
252 
253 #ifndef TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM
254   #include <sys/types.h>
255   #include <sys/socket.h>
256   #include <fcntl.h>
257   #define TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM(socketDescriptor,result) \
258     do { \
259       socketDescriptor=socket(AF_INET,SOCK_DGRAM,0); \
260       fcntl(socketDescriptor,F_SETFD,FD_CLOEXEC); \
261       result=(socketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
262     } while (0)
263 #endif
264 
265 /***********************************************************************\
266 * Name       : TARGET_NATIVE_NETWORK_SOCKET_CLOSE
267 * Purpose    : close socket
268 * Input      : socketDescriptor - socket descriptor
269 * Output     : result - TARGET_NATIVE_OK if no error occurred,
270 *                       TARGET_NATIVE_ERROR otherwise
271 * Return     : -
272 * Side-effect: unknown
273 * Notes      : -
274 \***********************************************************************/
275 
276 #ifndef TARGET_NATIVE_NETWORK_SOCKET_CLOSE
277   #include <unistd.h>
278   #define TARGET_NATIVE_NETWORK_SOCKET_CLOSE(socketDescriptor,result) \
279     do { \
280       result=(close(socketDescriptor)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
281     } while (0)
282 #endif
283 
284 /***********************************************************************\
285 * Name       : TARGET_NATIVE_NETWORK_SOCKET_CONNECT
286 * Purpose    : connect socket
287 * Input      : socketDescriptor - socket descriptor
288 *              address          - address (network format???)
289 *              port             - port number (NOT in network byte order!)
290 * Output     : result - TARGET_NATIVE_OK if no error occurred,
291 *                       TARGET_NATIVE_ERROR otherwise
292 * Return     : -
293 * Side-effect: unknown
294 * Notes      : -
295 \***********************************************************************/
296 
297 #ifndef TARGET_NATIVE_NETWORK_SOCKET_CONNECT
298   #include <sys/types.h>
299   #include <sys/socket.h>
300   #include <netinet/in.h>
301   #define TARGET_NATIVE_NETWORK_SOCKET_CONNECT(socketDescriptor,address,port,result) \
302     do { \
303       struct sockaddr_in __socketAddress; \
304       \
305       memset(&__socketAddress,0,sizeof(__socketAddress)); \
306       __socketAddress.sin_family      = AF_INET; \
307       __socketAddress.sin_addr.s_addr = htonl(address); \
308       __socketAddress.sin_port        = htons(((short)port)); \
309       \
310       result=(connect(socketDescriptor,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
311     } while (0)
312 #endif
313 
314 /***********************************************************************\
315 * Name       : TARGET_NATIVE_NETWORK_SOCKET_BIND
316 * Purpose    : bind socket
317 * Input      : socketDescriptor - socket descriptor
318 *              address          - address (NOT ??? in network byte order!)
319 *              port             - port (NOT in network byte order!)
320 * Output     : result - TARGET_NATIVE_OK if no error occurred,
321 *                       TARGET_NATIVE_ERROR otherwise
322 * Return     : -
323 * Side-effect: unknown
324 * Notes      : -
325 \***********************************************************************/
326 
327 /* XXX ??? address in network byte order? */
328 #ifndef TARGET_NATIVE_NETWORK_SOCKET_BIND
329   #include <sys/types.h>
330   #include <sys/socket.h>
331   #include <netinet/in.h>
332   #define TARGET_NATIVE_NETWORK_SOCKET_BIND(socketDescriptor,address,port,result) \
333     do { \
334       struct sockaddr_in __socketAddress; \
335       \
336       memset(&__socketAddress,0,sizeof(__socketAddress)); \
337       __socketAddress.sin_family      = AF_INET; \
338       __socketAddress.sin_addr.s_addr = htonl(address); \
339       __socketAddress.sin_port        = htons(((short)port)); \
340       \
341       result=(bind(socketDescriptor,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
342     } while (0)
343 #endif
344 
345 /***********************************************************************\
346 * Name       : TARGET_NATIVE_NETWORK_SOCKET_LISTEN
347 * Purpose    : listen socket
348 * Input      : socketDescriptor - socket descriptor
349 *              maxQueueLength   - max. number of pending connections
350 * Output     : result - TARGET_NATIVE_OK if no error occurred,
351 *                       TARGET_NATIVE_ERROR otherwise
352 * Return     : -
353 * Side-effect: unknown
354 * Notes      : -
355 \***********************************************************************/
356 
357 /* XXX ??? address in network byte order? */
358 #ifndef TARGET_NATIVE_NETWORK_SOCKET_LISTEN
359   #include <sys/socket.h>
360   #define TARGET_NATIVE_NETWORK_SOCKET_LISTEN(socketDescriptor,maxQueueLength,result) \
361     do { \
362       result=(listen(socketDescriptor,maxQueueLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
363     } while (0)
364 #endif
365 
366 /***********************************************************************\
367 * Name       : TARGET_NATIVE_NETWORK_SOCKET_ACCEPT
368 * Purpose    : accept socket
369 * Input      : socketDescriptor - socket descriptor
370 * Output     : result - TARGET_NATIVE_OK if no error occurred,
371 *                       TARGET_NATIVE_ERROR otherwise
372 * Return     : -
373 * Side-effect: unknown
374 * Notes      : -
375 \***********************************************************************/
376 
377 /* XXX ??? address in network byte order? */
378 #ifndef TARGET_NATIVE_NETWORK_SOCKET_ACCEPT
379   #include <sys/types.h>
380   #include <sys/socket.h>
381   #include <netinet/in.h>
382   #define TARGET_NATIVE_NETWORK_SOCKET_ACCEPT(socketDescriptor,newSocketDescriptor,result) \
383     do { \
384       struct sockaddr_in __socketAddress; \
385       socklen_t          __socketAddressLength; \
386       \
387       memset(&__socketAddress,0,sizeof(__socketAddress)); \
388       __socketAddressLength=sizeof(__socketAddress); \
389       newSocketDescriptor=accept(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength); \
390       result=(newSocketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
391     } while (0)
392 #endif
393 
394 /***********************************************************************\
395 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO
396 * Purpose    : get local socket data info
397 * Input      : socketDescriptor - socket descriptor
398 * Output     : localAddress     - local address (NOT in network byte order!)
399 *              localPort        - local port number (NOT in network byte order!)
400 *              result           - TARGET_NATIVE_OK if no error occurred,
401 *                                 TARGET_NATIVE_ERROR otherwise
402 * Return     : -
403 * Side-effect: unknown
404 * Notes      : -
405 \***********************************************************************/
406 
407 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO
408   #include <sys/socket.h>
409   #include <netinet/in.h>
410   #define TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO(socketDescriptor,localAddress,localPort,result) \
411     do { \
412       struct sockaddr_in __socketAddress; \
413       socklen_t          __socketAddressLength; \
414       \
415       localAddress=0; \
416       localPort   =0; \
417       \
418       __socketAddressLength=sizeof(__socketAddress); \
419       result=(getsockname(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
420       if (result==TARGET_NATIVE_OK) \
421       { \
422         localAddress=ntohl(__socketAddress.sin_addr.s_addr); \
423         localPort   =ntohs(__socketAddress.sin_port); \
424       } \
425     } while (0)
426 #endif
427 
428 /***********************************************************************\
429 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO
430 * Purpose    : get remote socket data info
431 * Input      : socketDescriptor - socket descriptor
432 * Output     : remoteAddress    - remote address (NOT in network byte order!)
433 *              remotePort       - remote port number (NOT in network byte order!)
434 *            : result           - TARGET_NATIVE_OK if no error occurred,
435 *                                 TARGET_NATIVE_ERROR otherwise
436 * Return     : -
437 * Side-effect: unknown
438 * Notes      : -
439 \***********************************************************************/
440 
441 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO
442   #include <sys/socket.h>
443   #include <netinet/in.h>
444   #define TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO(socketDescriptor,remoteAddress,remotePort,result) \
445     do { \
446       struct sockaddr_in __socketAddress; \
447       socklen_t          __socketAddressLength; \
448       \
449       remoteAddress=0; \
450       remotePort   =0; \
451       \
452       __socketAddressLength=sizeof(__socketAddress); \
453       result=(getpeername(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
454       if (result==TARGET_NATIVE_OK) \
455       { \
456         remoteAddress=ntohl(__socketAddress.sin_addr.s_addr); \
457         remotePort   =ntohs(__socketAddress.sin_port); \
458       } \
459     } while (0)
460 #endif
461 
462 /***********************************************************************\
463 * Name       : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE
464 * Purpose    : get number of available bytes for receive
465 * Input      : socketDescriptor - socket descriptor
466 * Output     : bytesAvailable - available bytes for receive
467 *            : result         - TARGET_NATIVE_OK if no error occurred,
468 *                               TARGET_NATIVE_ERROR otherwise
469 * Return     : -
470 * Side-effect: unknown
471 * Notes      : -
472 \***********************************************************************/
473 
474 #ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE
475   #include <sys/ioctl.h>
476   #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE(socketDescriptor,bytesAvailable,result) \
477     do { \
478       int __value; \
479       \
480       bytesAvailable=0; \
481       \
482       result=(ioctl(socketDescriptor,FIONREAD,&__value)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
483       if (result==TARGET_NATIVE_OK) \
484       { \
485         bytesAvailable=__value; \
486       } \
487     } while (0)
488 #endif
489 
490 /***********************************************************************\
491 * Name       : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE
492 * Purpose    : receive data from socket
493 * Input      : socketDescriptor - socket descriptor
494 *              maxLength - max. size of bfufer
495 * Output     : buffer       - received data
496 *              bytesReceive - length of received data
497 * Return     : -
498 * Side-effect: unknown
499 * Notes      : -
500 \***********************************************************************/
501 
502 #ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE
503   #include <sys/types.h>
504   #include <sys/socket.h>
505   #include <netinet/in.h>
506   #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE(socketDescriptor,buffer,maxLength,bytesReceived) \
507     do { \
508       struct sockaddr_in __socketAddress; \
509       socklen_t          __socketAddressLength; \
510       \
511       memset(&__socketAddress,0,sizeof(__socketAddress)); \
512       __socketAddressLength=sizeof(__socketAddress); \
513       bytesReceived=recv(socketDescriptor,buffer,maxLength,0); \
514     } while (0)
515 #endif
516 
517 /***********************************************************************\
518 * Name       : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT
519 * Purpose    : receive data from socket
520 * Input      : socketDescriptor - socket descriptor
521 *              maxLength - max. size of bfufer
522 * Output     : buffer       - received data
523 *              address      - from address (NOT in network byte order!)
524 *              port         - from port (NOT in network byte order!)
525 *              bytesReceive - length of received data
526 * Return     : -
527 * Side-effect: unknown
528 * Notes      : -
529 \***********************************************************************/
530 
531 #ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT
532   #include <sys/types.h>
533   #include <sys/socket.h>
534   #include <netinet/in.h>
535   #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT(socketDescriptor,buffer,maxLength,address,port,bytesReceived) \
536     do { \
537       struct sockaddr_in __socketAddress; \
538       socklen_t          __socketAddressLength; \
539       \
540       port=0; \
541       \
542       memset(&__socketAddress,0,sizeof(__socketAddress)); \
543       __socketAddressLength=sizeof(__socketAddress); \
544       bytesReceived=recvfrom(socketDescriptor,buffer,maxLength,0,(struct sockaddr*)&__socketAddress,&__socketAddressLength); \
545       if (__socketAddressLength==sizeof(__socketAddress)) \
546       { \
547         address=ntohl(__socketAddress.sin_addr.s_addr); \
548         port   =ntohs(__socketAddress.sin_port); \
549       } \
550     } while (0)
551 #endif
552 
553 /***********************************************************************\
554 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SEND
555 * Purpose    : send data to socket
556 * Input      : socketDescriptor - socket descriptor
557 *            : buffer  - data to send
558 *              length  - length of data to send
559 * Output     : bytesSent - number of bytes sent, -1 otherwise
560 * Side-effect: unknown
561 * Notes      : -
562 \***********************************************************************/
563 
564 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SEND
565   #include <sys/types.h>
566   #include <sys/socket.h>
567   #include <netinet/in.h>
568   #define TARGET_NATIVE_NETWORK_SOCKET_SEND(socketDescriptor,buffer,length,bytesSent) \
569     do { \
570       bytesSent=send(socketDescriptor,buffer,length,0); \
571     } while (0)
572 #endif
573 
574 /***********************************************************************\
575 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT
576 * Purpose    : send data to socket
577 * Input      : socketDescriptor - socket descriptor
578 *            : buffer  - data to send
579 *              length  - length of data to send
580 *              Address - to address (NOT in network byte order!)
581 *              Port    - to port (NOT in network byte order!)
582 * Output     : bytesSent - number of bytes sent, -1 otherwise
583 * Side-effect: unknown
584 * Notes      : -
585 \***********************************************************************/
586 
587 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT
588   #include <sys/types.h>
589   #include <sys/socket.h>
590   #include <netinet/in.h>
591   #define TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT(socketDescriptor,buffer,length,address,port,bytesSent) \
592     do { \
593       struct sockaddr_in __socketAddress; \
594       \
595       memset(&__socketAddress,0,sizeof(__socketAddress)); \
596       __socketAddress.sin_family      = AF_INET; \
597       __socketAddress.sin_addr.s_addr = htonl(address); \
598       __socketAddress.sin_port        = htons((short)port); \
599       bytesSent=sendto(socketDescriptor,buffer,length,0,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress)); \
600     } while (0)
601 #endif
602 
603 /***********************************************************************\
604 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY
605 * Purpose    : set socket option TCP_NODELAY
606 * Input      : socketDescriptor - socket descriptor
607 *              flag             - 1 or 0
608 * Output     : result - TARGET_NATIVE_OK if no error occurred,
609 *                       TARGET_NATIVE_ERROR otherwise
610 * Return     : -
611 * Side-effect: unknown
612 * Notes      : -
613 \***********************************************************************/
614 
615 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY
616   #include <sys/types.h>
617   #include <sys/socket.h>
618   #include <netinet/tcp.h>
619   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY(socketDescriptor,flag,result) \
620     do { \
621       int __value; \
622       \
623       __value=flag; \
624       result=(setsockopt(socketDescriptor,IPPROTO_TCP,TCP_NODELAY,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
625     } while (0)
626 #endif
627 
628 /***********************************************************************\
629 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER
630 * Purpose    : set socket option SO_LINGER
631 * Input      : socketDescriptor - socket descriptor
632 *              flag             - 1 or 0
633 *              value            - linger value
634 * Output     : result - TARGET_NATIVE_OK if no error occurred,
635 *                       TARGET_NATIVE_ERROR otherwise
636 * Return     : -
637 * Side-effect: unknown
638 * Notes      : -
639 \***********************************************************************/
640 
641 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER
642   #include <sys/types.h>
643   #include <sys/socket.h>
644   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER(socketDescriptor,flag,value,result) \
645     do { \
646       struct linger __linger; \
647       \
648       memset(&__linger,0,sizeof(__linger)); \
649       if (flag) \
650       { \
651         __linger.l_onoff=0; \
652       } \
653       else \
654       { \
655         __linger.l_linger=value; \
656         __linger.l_onoff =1; \
657       } \
658       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_LINGER,&__linger,sizeof(__linger))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
659     } while (0)
660 #endif
661 
662 /***********************************************************************\
663 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT
664 * Purpose    : set socket option SO_TIMEOUT
665 * Input      : socketDescriptor - socket descriptor
666 *              flag             - 1 or 0
667 * Output     : result - TARGET_NATIVE_OK if no error occurred,
668 *                       TARGET_NATIVE_ERROR otherwise
669 * Return     : -
670 * Side-effect: unknown
671 * Notes      : -
672 \***********************************************************************/
673 
674 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT
675   #include <sys/types.h>
676   #include <sys/socket.h>
677 #if TIME_WITH_SYS_TIME
678 # include <sys/time.h>
679 # include <time.h>
680 #else
681 # if HAVE_SYS_TIME_H
682 #  include <sys/time.h>
683 # else
684 #  include <time.h>
685 # endif
686 #endif
687   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_TIMEOUT(socketDescriptor,flag,result) \
688     do { \
689       struct timeval __value; \
690       \
691       __value.tv_sec = flag / 1000; \
692       __value.tv_usec = (flag % 1000) * 1000; \
693       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_TIMEOUT,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
694     } while (0)
695 #endif
696 
697 /***********************************************************************\
698 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_SNDBUF
699 * Purpose    : set socket option SO_SNDBUF
700 * Input      : socketDescriptor - socket descriptor
701 *              size             - size of send buffer
702 * Output     : result - TARGET_NATIVE_OK if no error occurred,
703 *                       TARGET_NATIVE_ERROR otherwise
704 * Return     : -
705 * Side-effect: unknown
706 * Notes      : -
707 \***********************************************************************/
708 
709 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_SNDBUF
710   #include <sys/types.h>
711   #include <sys/socket.h>
712   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_SNDBUF(socketDescriptor,size,result) \
713     do { \
714       int __value; \
715       \
716       __value=size; \
717       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_SNDBUF,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
718     } while (0)
719 #endif
720 
721 /***********************************************************************\
722 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_RCDBUF
723 * Purpose    : set socket option SO_RCDBUF
724 * Input      : socketDescriptor - socket descriptor
725 *              size             - size of receive buffer
726 * Output     : result - TARGET_NATIVE_OK if no error occurred,
727 *                       TARGET_NATIVE_ERROR otherwise
728 * Return     : -
729 * Side-effect: unknown
730 * Notes      : -
731 \***********************************************************************/
732 
733 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_RCDBUF
734   #include <sys/types.h>
735   #include <sys/socket.h>
736   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_RCDBUF(socketDescriptor,size,result) \
737     do { \
738       int __value; \
739       \
740       __value=size; \
741       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_RCVBUF,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
742     } while (0)
743 #endif
744 
745 /***********************************************************************\
746 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_TTL
747 * Purpose    : set socket option IP_TTL
748 * Input      : socketDescriptor - socket descriptor
749 *              value            - value
750 * Output     : result - TARGET_NATIVE_OK if no error occurred,
751 *                       TARGET_NATIVE_ERROR otherwise
752 * Return     : -
753 * Side-effect: unknown
754 * Notes      : -
755 \***********************************************************************/
756 
757 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_TTL
758   #include <sys/types.h>
759   #include <sys/socket.h>
760   #include <netinet/in.h>
761   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_TTL(socketDescriptor,value,result) \
762     do { \
763       int __value; \
764       \
765       __value=value; \
766       result=(setsockopt(socketDescriptor,IPPROTO_IP,IP_TTL,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
767     } while (0)
768 #endif
769 
770 /***********************************************************************\
771 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_MULTICAST_IF
772 * Purpose    : set socket option IP_MULTICAST_IF
773 * Input      : socketDescriptor - socket descriptor
774 *              address          - integer with IP address in host-format
775 * Output     : result - TARGET_NATIVE_OK if no error occurred,
776 *                       TARGET_NATIVE_ERROR otherwise
777 * Return     : -
778 * Side-effect: unknown
779 * Notes      : -
780 \***********************************************************************/
781 
782 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_MULTICAST_IF
783   #include <sys/types.h>
784   #include <sys/socket.h>
785   #include <netinet/in.h>
786   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_IP_MULTICAST_IF(socketDescriptor,address,result) \
787     do { \
788       struct sockaddr_in __socketAddress; \
789       \
790       memset(&__socketAddress,0,sizeof(__socketAddress)); \
791       __socketAddress.sin_family      = AF_INET; \
792       __socketAddress.sin_addr.s_addr = htonl(address); \
793       result=(setsockopt(socketDescriptor,IPPROTO_IP,IP_MULTICAST_IF,&__socketAddress,sizeof(__socketAddress))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
794     } while (0)
795 #endif
796 
797 /***********************************************************************\
798 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_REUSE_ADDRESS
799 * Purpose    : set socket option REUSE_ADDRESS
800 * Input      : socketDescriptor - socket descriptor
801 *              flag             - 1 or 0
802 * Output     : result - TARGET_NATIVE_OK if no error occurred,
803 *                       TARGET_NATIVE_ERROR otherwise
804 * Return     : -
805 * Side-effect: unknown
806 * Notes      : -
807 \***********************************************************************/
808 
809 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_REUSE_ADDRESS
810   #include <sys/types.h>
811   #include <sys/socket.h>
812   #include <netinet/in.h>
813   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_REUSE_ADDRESS(socketDescriptor,flag,result) \
814     do { \
815       int __value; \
816       \
817       __value=flag; \
818       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_REUSEADDR,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
819     } while (0)
820 #endif
821 
822 /***********************************************************************\
823 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_ADD_MEMBERSHIP
824 * Purpose    : set socket option IP_ADD_MEMBERSHIP
825 * Input      : socketDescriptor - socket descriptor
826 *              address          - network address (host-format)
827 * Output     : result - TARGET_NATIVE_OK if no error occurred,
828 *                       TARGET_NATIVE_ERROR otherwise
829 * Return     : -
830 * Side-effect: unknown
831 * Notes      : -
832 \***********************************************************************/
833 
834 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_ADD_MEMBERSHIP
835   #include <sys/types.h>
836   #include <sys/socket.h>
837   #include <netinet/in.h>
838   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_ADD_MEMBERSHIP(socketDescriptor,address,result) \
839     do { \
840       struct ip_mreq __request; \
841       \
842       memset(&__request,0,sizeof(__request)); \
843       __request.imr_multiaddr.s_addr=htonl(address); \
844       __request.imr_interface.s_addr=INADDR_ANY; \
845       result=(setsockopt(socketDescriptor,IPPROTO_IP,IP_ADD_MEMBERSHIP,&__request,sizeof(__request))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
846     } while (0)
847 #endif
848 
849 /***********************************************************************\
850 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_DROP_MEMBERSHIP
851 * Purpose    : set socket option IP_DROP_MEMBERSHIP
852 * Input      : socketDescriptor - socket descriptor
853 *              address          - network address (host-format)
854 * Output     : result - TARGET_NATIVE_OK if no error occurred,
855 *                       TARGET_NATIVE_ERROR otherwise
856 * Return     : -
857 * Side-effect: unknown
858 * Notes      : -
859 \***********************************************************************/
860 
861 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_DROP_MEMBERSHIP
862   #include <sys/types.h>
863   #include <sys/socket.h>
864   #include <netinet/in.h>
865   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_DROP_MEMBERSHIP(socketDescriptor,address,result) \
866     do { \
867       struct ip_mreq __request; \
868       \
869       memset(&__request,0,sizeof(__request)); \
870       __request.imr_multiaddr.s_addr=htonl(address); \
871       __request.imr_interface.s_addr=INADDR_ANY; \
872       result=(setsockopt(socketDescriptor,IPPROTO_IP,IP_DROP_MEMBERSHIP,&__request,sizeof(__request))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
873     } while (0)
874 #endif
875 
876 /***********************************************************************\
877 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_KEEP_ALIVE
878 * Purpose    : set socket option KEEP_ALIVE
879 * Input      : socketDescriptor - socket descriptor
880 *              flag             - 1 or 0
881 * Output     : result - TARGET_NATIVE_OK if no error occurred,
882 *                       TARGET_NATIVE_ERROR otherwise
883 * Return     : -
884 * Side-effect: unknown
885 * Notes      : -
886 \***********************************************************************/
887 
888 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_KEEP_ALIVE
889   #include <sys/types.h>
890   #include <sys/socket.h>
891   #include <netinet/in.h>
892   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_KEEP_ALIVE(socketDescriptor,flag,result) \
893     do { \
894       int __value; \
895       \
896       __value=flag; \
897       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_KEEPALIVE,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
898     } while (0)
899 #endif
900 
901 /***********************************************************************\
902 * Name       : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_BROADCAST
903 * Purpose    : set socket option SO_BROADCAST
904 * Input      : socketDescriptor - socket descriptor
905 *              flag             - 1 or 0
906 * Output     : result - TARGET_NATIVE_OK if no error occurred,
907 *                       TARGET_NATIVE_ERROR otherwise
908 * Return     : -
909 * Side-effect: unknown
910 * Notes      : -
911 \***********************************************************************/
912 
913 #ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_BROADCAST
914   #include <sys/types.h>
915   #include <sys/socket.h>
916   #include <netinet/tcp.h>
917   #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_BROADCAST(socketDescriptor,flag,result) \
918     do { \
919       int __value; \
920       \
921       __value=flag; \
922       result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_BROADCAST,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
923     } while (0)
924 #endif
925 
926 /*---------------------------------------------------------------------*/
927 
928 /***********************************************************************\
929 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_TCP_NODELAY
930 * Purpose    : get socket option TCP_NODELAY
931 * Input      : socketDescriptor - socket descriptor
932 * Output     : flag   - 1 or 0
933 *              result - TARGET_NATIVE_OK if no error occurred,
934 *                       TARGET_NATIVE_ERROR otherwise
935 * Return     : -
936 * Side-effect: unknown
937 * Notes      : -
938 \***********************************************************************/
939 
940 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_TCP_NODELAY
941   #include <sys/types.h>
942   #include <sys/socket.h>
943   #include <netinet/tcp.h>
944   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_TCP_NODELAY(socketDescriptor,flag,result) \
945     do { \
946       int       __value; \
947       socklen_t __len; \
948       \
949       flag=0; \
950       \
951       __len=sizeof(__value); \
952       result=(getsockopt(socketDescriptor,IPPROTO_TCP,TCP_NODELAY,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
953       if (result==TARGET_NATIVE_OK) \
954       { \
955         flag=__value; \
956       } \
957     } while (0)
958 #endif
959 
960 /***********************************************************************\
961 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_LINGER
962 * Purpose    : get socket option SO_LINGER
963 * Input      : socketDescriptor - socket descriptor
964 * Output     : flag   - 1 or 0
965 *              value  - linger value
966 *              result - TARGET_NATIVE_OK if no error occurred,
967 *                       TARGET_NATIVE_ERROR otherwise
968 * Return     : -
969 * Side-effect: unknown
970 * Notes      : -
971 \***********************************************************************/
972 
973 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_LINGER
974   #include <sys/types.h>
975   #include <sys/socket.h>
976   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_LINGER(socketDescriptor,flag,value,result) \
977     do { \
978       struct linger __linger; \
979       socklen_t     __len; \
980       \
981       flag =0; \
982       value=0; \
983       \
984       __len=sizeof(__linger); \
985       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_LINGER,&__linger,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
986       if (result==TARGET_NATIVE_OK) \
987       { \
988         flag =__linger.l_onoff; \
989         value=__linger.l_linger; \
990       } \
991     } while (0)
992 #endif
993 
994 /***********************************************************************\
995 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_TIMEOUT
996 * Purpose    : get socket option SO_TIMEOUT
997 * Input      : socketDescriptor - socket descriptor
998 * Output     : flag   - 1 or 0
999 *              result - TARGET_NATIVE_OK if no error occurred,
1000 *                       TARGET_NATIVE_ERROR otherwise
1001 * Return     : -
1002 * Side-effect: unknown
1003 * Notes      : -
1004 \***********************************************************************/
1005 
1006 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_TIMEOUT
1007   #include <sys/types.h>
1008   #include <sys/socket.h>
1009   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_TIMEOUT(socketDescriptor,flag,result) \
1010     do { \
1011       struct timeval   __value; \
1012       socklen_t __len; \
1013       \
1014       flag=0; \
1015       \
1016       __len=sizeof(__value); \
1017       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_TIMEOUT,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1018       if (result==TARGET_NATIVE_OK) \
1019       { \
1020         flag = (__value.tv_sec * 1000LL) + (__value.tv_usec / 1000LL); \
1021       } \
1022     } while (0)
1023 #endif
1024 
1025 /***********************************************************************\
1026 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_SNDBUF
1027 * Purpose    : get socket option SO_SNDBUF
1028 * Input      : socketDescriptor - socket descriptor
1029 * Output     : size   - size of send buffer
1030 *              result - TARGET_NATIVE_OK if no error occurred,
1031 *                       TARGET_NATIVE_ERROR otherwise
1032 * Return     : -
1033 * Side-effect: unknown
1034 * Notes      : -
1035 \***********************************************************************/
1036 
1037 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_SNDBUF
1038   #include <sys/types.h>
1039   #include <sys/socket.h>
1040   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_SNDBUF(socketDescriptor,size,result) \
1041     do { \
1042       int       __value; \
1043       socklen_t __len; \
1044       \
1045       size=0; \
1046       \
1047       __len=sizeof(__value); \
1048       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_SNDBUF,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1049       if (result==TARGET_NATIVE_OK) \
1050       { \
1051         size=__value; \
1052       } \
1053     } while (0)
1054 #endif
1055 
1056 /***********************************************************************\
1057 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_RCDBUF
1058 * Purpose    : get socket option SO_RCDBUF
1059 * Input      : socketDescriptor - socket descriptor
1060 * Output     : size   - size of receive buffer
1061 *              result - TARGET_NATIVE_OK if no error occurred,
1062 *                       TARGET_NATIVE_ERROR otherwise
1063 * Return     : -
1064 * Side-effect: unknown
1065 * Notes      : -
1066 \***********************************************************************/
1067 
1068 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_RCDBUF
1069   #include <sys/types.h>
1070   #include <sys/socket.h>
1071   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_SO_RCDBUF(socketDescriptor,size,result) \
1072     do { \
1073       int       __value; \
1074       socklen_t __len; \
1075       \
1076       size=0; \
1077       \
1078       __len=sizeof(__value); \
1079       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_RCVBUF,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1080       if (result==TARGET_NATIVE_OK) \
1081       { \
1082         size=__value; \
1083       } \
1084     } while (0)
1085 #endif
1086 
1087 /***********************************************************************\
1088 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_TTL
1089 * Purpose    : get socket option IP_TTL
1090 * Input      : socketDescriptor - socket descriptor
1091 * Output     : flag   - 1 or 0
1092 *              result - TARGET_NATIVE_OK if no error occurred,
1093 *                       TARGET_NATIVE_ERROR otherwise
1094 * Return     : -
1095 * Side-effect: unknown
1096 * Notes      : -
1097 \***********************************************************************/
1098 
1099 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_TTL
1100   #include <sys/types.h>
1101   #include <sys/socket.h>
1102   #include <netinet/in.h>
1103   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_TTL(socketDescriptor,flag,result) \
1104     do { \
1105       int       __value; \
1106       socklen_t __len; \
1107       \
1108       flag=0; \
1109       \
1110       __len=sizeof(__value); \
1111       result=(getsockopt(socketDescriptor,IPPROTO_IP,IP_TTL,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1112       if (result==TARGET_NATIVE_OK) \
1113       { \
1114         flag=__value; \
1115       } \
1116     } while (0)
1117 #endif
1118 
1119 /***********************************************************************\
1120 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_MULTICAST_IF
1121 * Purpose    : get socket option IP_MULTICAST_IF
1122 * Input      : socketDescriptor - socket descriptor
1123 * Output     : address - integer with IP address in host-format
1124 *              result  - TARGET_NATIVE_OK if no error occurred,
1125 *                        TARGET_NATIVE_ERROR otherwise
1126 * Return     : -
1127 * Side-effect: unknown
1128 * Notes      : -
1129 \***********************************************************************/
1130 
1131 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_MULTICAST_IF
1132   #include <sys/types.h>
1133   #include <sys/socket.h>
1134   #include <netinet/in.h>
1135   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_IP_MULTICAST_IF(socketDescriptor,address,result) \
1136     do { \
1137       struct sockaddr_in __socketAddress; \
1138       socklen_t          __socketAddressLength; \
1139       \
1140       address=0;\
1141       \
1142       memset(&__socketAddress,0,sizeof(__socketAddress)); \
1143       __socketAddress.sin_family      = AF_INET; \
1144       __socketAddress.sin_addr.s_addr = htonl(address); \
1145       __socketAddressLength=sizeof(__socketAddress); \
1146       result=(getsockopt(socketDescriptor,IPPROTO_IP,IP_MULTICAST_IF,&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1147       if (result==TARGET_NATIVE_OK) \
1148       { \
1149         address=ntohl(__socketAddress.sin_addr.s_addr); \
1150       } \
1151     } while (0)
1152 #endif
1153 
1154 /***********************************************************************\
1155 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BIND_ADDRESS
1156 * Purpose    : get socket option SOCKOPT_SO_BINDADDR
1157 * Input      : socketDescriptor - socket descriptor
1158 * Output     : address - integer with IP address in host-format
1159 *              result  - TARGET_NATIVE_OK if no error occurred,
1160 *                        TARGET_NATIVE_ERROR otherwise
1161 * Return     : -
1162 * Side-effect: unknown
1163 * Notes      : -
1164 \***********************************************************************/
1165 
1166 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BIND_ADDRESS
1167   #include <sys/types.h>
1168   #include <sys/socket.h>
1169   #include <netinet/in.h>
1170   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BIND_ADDRESS(socketDescriptor,address,result) \
1171     do { \
1172       struct sockaddr_in __socketAddress; \
1173       socklen_t          __socketAddressLength; \
1174       \
1175       address=0;\
1176       \
1177       memset(&__socketAddress,0,sizeof(__socketAddress)); \
1178       __socketAddressLength=sizeof(__socketAddress); \
1179       result=(getsockname(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1180       if (result==TARGET_NATIVE_OK) \
1181       { \
1182         address=ntohl(__socketAddress.sin_addr.s_addr); \
1183       } \
1184     } while (0)
1185 #endif
1186 
1187 /***********************************************************************\
1188 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_REUSE_ADDRESS
1189 * Purpose    : get socket option REUSE_ADDRESS
1190 * Input      : socketDescriptor - socket descriptor
1191 * Output     : flag   - 1 or 0
1192 *              result - TARGET_NATIVE_OK if no error occurred,
1193 *                       TARGET_NATIVE_ERROR otherwise
1194 * Return     : -
1195 * Side-effect: unknown
1196 * Notes      : -
1197 \***********************************************************************/
1198 
1199 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_REUSE_ADDRESS
1200   #include <sys/types.h>
1201   #include <sys/socket.h>
1202   #include <netinet/in.h>
1203   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_REUSE_ADDRESS(socketDescriptor,flag,result) \
1204     do { \
1205       int       __value; \
1206       socklen_t __len; \
1207       \
1208       flag=0; \
1209       \
1210       __len=sizeof(__value); \
1211       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_REUSEADDR,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1212       if (result==TARGET_NATIVE_OK) \
1213       { \
1214         flag=__value; \
1215       } \
1216     } while (0)
1217 #endif
1218 
1219 /***********************************************************************\
1220 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_KEEP_ALIVE
1221 * Purpose    : get socket option KEEP_ALIVE
1222 * Input      : socketDescriptor - socket descriptor
1223 * Output     : flag   - 1 or 0
1224 *              result - TARGET_NATIVE_OK if no error occurred,
1225 *                       TARGET_NATIVE_ERROR otherwise
1226 * Return     : -
1227 * Side-effect: unknown
1228 * Notes      : -
1229 \***********************************************************************/
1230 
1231 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_KEEP_ALIVE
1232   #include <sys/types.h>
1233   #include <sys/socket.h>
1234   #include <netinet/in.h>
1235   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_KEEP_ALIVE(socketDescriptor,flag,result) \
1236     do { \
1237       int       __value; \
1238       socklen_t __len; \
1239       \
1240       flag=0; \
1241       \
1242       __len=sizeof(__value); \
1243       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_KEEPALIVE,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1244       if (result==TARGET_NATIVE_OK) \
1245       { \
1246         flag=__value; \
1247       } \
1248     } while (0)
1249 #endif
1250 
1251 /***********************************************************************\
1252 * Name       : TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BROADCAST
1253 * Purpose    : get socket option SO_BROADCAST
1254 * Input      : socketDescriptor - socket descriptor
1255 * Output     : flag   - 1 or 0
1256 *              result - TARGET_NATIVE_OK if no error occurred,
1257 *                       TARGET_NATIVE_ERROR otherwise
1258 * Return     : -
1259 * Side-effect: unknown
1260 * Notes      : -
1261 \***********************************************************************/
1262 
1263 #ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BROADCAST
1264   #include <sys/types.h>
1265   #include <sys/socket.h>
1266   #include <netinet/tcp.h>
1267   #define TARGET_NATIVE_NETWORK_SOCKET_GET_OPTION_BROADCAST(socketDescriptor,flag,result) \
1268     do { \
1269       int __value; \
1270       socklen_t __len; \
1271       \
1272       flag=0; \
1273       \
1274       __len=sizeof(__value); \
1275       result=(getsockopt(socketDescriptor,SOL_SOCKET,SO_BROADCAST,&__value,&__len)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \
1276       if (result==TARGET_NATIVE_OK) \
1277       { \
1278         flag=__value; \
1279       } \
1280     } while (0)
1281 #endif
1282 
1283 /***************************** Functions *******************************/
1284 
1285 #ifdef __cplusplus
1286 extern "C" {
1287 #endif
1288 
1289 #ifdef __cplusplus
1290 }
1291 #endif
1292 
1293 #endif /* __TARGET_GENERIC_NETWORK__ */
1294 
1295 /* end of file */
1296 
1297