chiark / gitweb /
Commit 2.4.5-5 as unpacked
[innduct.git] / include / portable / socket.h
1 /*  $Id: socket.h 5388 2002-04-01 07:03:01Z rra $
2 **
3 **  Portability wrapper around <sys/socket.h> and friends.
4 **
5 **  This header file is the equivalent of:
6 **
7 **      #include <arpa/inet.h>
8 **      #include <netinet/in.h>
9 **      #include <sys/socket.h>
10 **
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
13 **  prototyped.
14 */
15
16 #ifndef PORTABLE_SOCKET_H
17 #define PORTABLE_SOCKET_H 1
18
19 #include "config.h"
20 #include <sys/types.h>
21
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>
26
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
29    prototypes. */
30 #if NEED_DECLARATION_INET_ATON
31 extern int              inet_aton(const char *, struct in_addr *);
32 #endif
33 #if NEED_DECLARATION_INET_NTOA
34 extern const char *     inet_ntoa(const struct in_addr);
35 #endif
36
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 {
43     unsigned char ss_len;
44     unsigned char ss_family;
45     unsigned char __padding[128 - 2];
46 };
47 # else
48 struct sockaddr_storage {
49     unsigned short ss_family;
50     unsigned char __padding[128 - 2];
51 };
52 # endif
53 #endif
54
55 /* Use convenient, non-uglified names for the fields since we use them quite a
56    bit in code. */
57 #if HAVE_2553_STYLE_SS_FAMILY
58 # define ss_family __ss_family
59 # define ss_len    __ss_len
60 #endif
61
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)
66 # else
67 /* Hack courtesy of the USAGI project. */
68 #  if HAVE_INET6
69 #   define SA_LEN(s) \
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)))
75 #  else
76 #   define SA_LEN(s) \
77     ((((struct sockaddr *)(s))->sa_family == AF_INET)           \
78         ? sizeof(struct sockaddr_in)                            \
79         : sizeof(struct sockaddr))
80 #  endif
81 # endif /* HAVE_SOCKADDR_LEN */
82 #endif /* !HAVE_SA_LEN_MACRO */
83
84 #endif /* PORTABLE_SOCKET_H */