]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/cmsg.h
merge from trunk
[thirdparty/squid.git] / compat / cmsg.h
1 /*
2 * Compatibility-layer for CMSG_
3 */
4 #ifndef SQUID_COMPAT_CMSG_H
5 #define SQUID_COMPAT_CMSG_H
6
7 // cmsg.h is found through sys/socket.h
8 #if HAVE_SYS_SOCKET_H
9 #include <sys/socket.h>
10 #endif
11
12 // WinSock2.h defines these for Windows
13 #if HAVE_WINSOCK2_H
14 #include <winsock2.h>
15 #endif
16
17 // sockaddr_un might be in sys/un.h if not pulled in already
18 #if HAVE_SYS_UN_H
19 #include <sys/un.h>
20 #endif
21
22 #if !HAVE_CMSGHDR
23 struct cmsghdr {
24 unsigned int cmsg_len;
25 int cmsg_level;
26 int cmsg_type;
27 unsigned char cmsg_data[16]; /* dummy */
28 /* followed by UCHAR cmsg_data[]; */
29 };
30 #endif
31
32 /* lifted off https://metacpan.org/source/SAMPO/Socket-PassAccessRights-0.03/passfd.c */
33 #ifndef CMSG_DATA
34 # define CMSG_DATA(cmsg) ((cmsg)->cmsg_data)
35 #endif
36
37 #ifndef CMSG_NXTHDR
38 # define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr (mhdr, cmsg)
39 #endif
40
41 #ifndef CMSG_FIRSTHDR
42 # define CMSG_FIRSTHDR(mhdr) \
43 ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
44 ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) NULL)
45 #endif
46
47 #ifndef CMSG_ALIGN
48 # define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) \
49 & ~(sizeof (size_t) - 1))
50 #endif
51
52 #ifndef CMSG_SPACE
53 # define CMSG_SPACE(len) (CMSG_ALIGN (len) \
54 + CMSG_ALIGN (sizeof (struct cmsghdr)))
55 #undef HAVE_CONSTANT_CMSG_SPACE
56 #define HAVE_CONSTANT_CMSG_SPACE 1
57 #endif
58
59 #ifndef CMSG_LEN
60 # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
61 #endif
62
63 #if !HAVE_IOVEC
64 struct iovec {
65 void *iov_base;
66 size_t iov_len;
67 };
68 #endif
69
70 #if !HAVE_MSGHDR
71 struct msghdr {
72 void *msg_name; /* Address to send to/receive from. */
73 socklen_t msg_namelen; /* Length of address data. */
74
75 struct iovec *msg_iov; /* Vector of data to send/receive into. */
76 size_t msg_iovlen; /* Number of elements in the vector. */
77
78 void *msg_control; /* Ancillary data (eg BSD filedesc passing). */
79 size_t msg_controllen; /* Ancillary data buffer length.
80 !! The type should be socklen_t but the
81 definition of the kernel is incompatible
82 with this. */
83
84 int msg_flags; /* Flags on received message. */
85 };
86 #endif
87
88 #if !HAVE_SOCKADDR_UN
89 struct sockaddr_un {
90 char sun_family;
91 char sun_path[256]; /* pathname */
92 };
93 #endif
94
95 #ifndef SUN_LEN
96 # define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
97 + strlen ((ptr)->sun_path))
98 #endif
99
100 #ifndef SCM_RIGHTS
101 #define SCM_RIGHTS 1
102 #endif
103 #ifndef SCM_CREDENTIALS
104 #define SCM_CREDENTIALS 2
105 #endif
106 #ifndef SCM_SECURITY
107 #define SCM_SECURITY 3
108 #endif
109
110 #ifndef AF_LOCAL
111 #define AF_LOCAL 1
112 #endif
113
114 // CMSG_SPACE is not constant on some systems (in particular Max OS X),
115 // provide a replacement that can be used at build time in that case
116 // NP: this must go below our replacement definitions.
117
118 #if HAVE_CONSTANT_CMSG_SPACE
119 #define SQUID_CMSG_SPACE CMSG_SPACE
120 #else
121 // add 16 bytes for header and data alignment
122 #define SQUID_CMSG_SPACE(len) (sizeof(struct cmsghdr) + (len) + 16)
123 #endif
124
125 #endif /* SQUID_COMPAT_CMSG_H */