1 /* $Id: socket.h 5388 2002-04-01 07:03:01Z rra $
3 ** Portability wrapper around <sys/socket.h> and friends.
5 ** This header file is the equivalent of:
7 ** #include <arpa/inet.h>
8 ** #include <netinet/in.h>
9 ** #include <sys/socket.h>
11 ** but also cleans up various messes, mostly related to IPv6 support. It
12 ** ensures that inet_aton and inet_ntoa are available and properly
16 #ifndef PORTABLE_SOCKET_H
17 #define PORTABLE_SOCKET_H 1
20 #include <sys/types.h>
22 /* BSDI needs <netinet/in.h> before <arpa/inet.h>. */
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
27 /* Provide prototypes for inet_aton and inet_ntoa if not prototyped in the
28 system header files since they're occasionally available without proper
30 #if NEED_DECLARATION_INET_ATON
31 extern int inet_aton(const char *, struct in_addr *);
33 #if NEED_DECLARATION_INET_NTOA
34 extern const char * inet_ntoa(const struct in_addr);
37 /* Defined by RFC 2553, used to store a generic address. Note that this
38 doesn't do the alignment mangling that RFC 2553 does; it's not clear if
39 that should be added.... */
40 #if !HAVE_SOCKADDR_STORAGE
41 # if HAVE_SOCKADDR_LEN
42 struct sockaddr_storage {
44 unsigned char ss_family;
45 unsigned char __padding[128 - 2];
48 struct sockaddr_storage {
49 unsigned short ss_family;
50 unsigned char __padding[128 - 2];
55 /* Use convenient, non-uglified names for the fields since we use them quite a
57 #if HAVE_2553_STYLE_SS_FAMILY
58 # define ss_family __ss_family
59 # define ss_len __ss_len
62 /* Define an SA_LEN macro that gives us the length of a sockaddr. */
63 #if !HAVE_SA_LEN_MACRO
64 # if HAVE_SOCKADDR_LEN
65 # define SA_LEN(s) ((s)->sa_len)
67 /* Hack courtesy of the USAGI project. */
70 ((((struct sockaddr *)(s))->sa_family == AF_INET6) \
71 ? sizeof(struct sockaddr_in6) \
72 : ((((struct sockaddr *)(s))->sa_family == AF_INET) \
73 ? sizeof(struct sockaddr_in) \
74 : sizeof(struct sockaddr)))
77 ((((struct sockaddr *)(s))->sa_family == AF_INET) \
78 ? sizeof(struct sockaddr_in) \
79 : sizeof(struct sockaddr))
81 # endif /* HAVE_SOCKADDR_LEN */
82 #endif /* !HAVE_SA_LEN_MACRO */
84 #endif /* PORTABLE_SOCKET_H */