socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_COMPAT_SOCKET_H
10 #define SQUID_COMPAT_SOCKET_H
11 
12 #if HAVE_SYS_SOCKET_H
13 #include <sys/socket.h>
14 #endif
15 
17 int xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength);
18 
20 int xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength);
21 
23 int xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength);
24 
26 int xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength);
27 
29 int xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength);
30 
32 int xlisten(int socketFd, int backlog);
33 
35 ssize_t xrecv(int socketFd, void * buf, size_t bufLength, int flags);
36 
38 ssize_t xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength);
39 
41 ssize_t xsend(int socketFd, const void * buf, size_t bufLength, int flags);
42 
44 ssize_t xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t toLength);
45 
47 int xsetsockopt(int socketFd, int level, int option, const void * value, socklen_t valueLength);
48 
50 int xsocket(int domain, int type, int protocol);
51 
52 // Solaris and possibly others lack MSG_NOSIGNAL optimization
53 // TODO: move this into compat/? Use a dedicated compat file to avoid dragging
54 // sys/socket.h into the rest of Squid??
55 #ifndef MSG_NOSIGNAL
56 #define MSG_NOSIGNAL 0
57 #endif
58 
59 #if !(_SQUID_WINDOWS_ || _SQUID_MINGW_)
60 
61 inline int
62 xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength)
63 {
64  return accept(socketFd, sa, saLength);
65 }
66 
67 inline int
68 xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
69 {
70  return bind(socketFd, sa, saLength);
71 }
72 
73 inline int
74 xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength)
75 {
76  return connect(socketFd, sa, saLength);
77 }
78 
79 inline int
80 xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength)
81 {
82  return getsockname(socketFd, sa, saLength);
83 }
84 
85 inline int
86 xlisten(int socketFd, int backlog)
87 {
88  return listen(socketFd, backlog);
89 }
90 
91 inline int
92 xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength)
93 {
94  return getsockopt(socketFd, level, optionName, optionValue, optionLength);
95 }
96 
97 inline ssize_t
98 xrecv(int socketFd, void * buf, size_t bufLength, int flags)
99 {
100  return recv(socketFd, buf, bufLength, flags);
101 }
102 
103 inline ssize_t
104 xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength)
105 {
106  return recvfrom(socketFd, buf, bufLength, flags, from, fromLength);
107 }
108 
109 inline ssize_t
110 xsend(int socketFd, const void * buf, size_t bufLength, int flags)
111 {
112  return send(socketFd, buf, bufLength, flags);
113 }
114 
115 inline ssize_t
116 xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t l)
117 {
118  return sendto(socketFd, buf, bufLength, flags, to, l);
119 }
120 
121 inline int
122 xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
123 {
124  return setsockopt(socketFd, level, option, value, valueLength);
125 }
126 
127 inline int
128 xsocket(int domain, int type, int protocol)
129 {
130  return socket(domain, type, protocol);
131 }
132 
133 #else
134 
135 static_assert(SOCKET_ERROR == -1);
136 
137 inline bool
138 isValidSocketHandle(intptr_t handle)
139 {
140  return handle != intptr_t(INVALID_HANDLE_VALUE);
141 }
142 
143 #endif /* !(_SQUID_WINDOWS_ || _SQUID_MINGW_) */
144 
145 #endif /* SQUID_COMPAT_SOCKET_H */
int xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength)
POSIX accept(2) equivalent.
Definition: socket.h:62
int socklen_t
Definition: types.h:137
ssize_t xsend(int socketFd, const void *buf, size_t bufLength, int flags)
POSIX send(2) equivalent.
Definition: socket.h:110
ssize_t xrecvfrom(int socketFd, void *buf, size_t bufLength, int flags, struct sockaddr *from, socklen_t *fromLength)
POSIX recvfrom(2) equivalent.
Definition: socket.h:104
int xgetsockname(int socketFd, struct sockaddr *sa, socklen_t *saLength)
POSIX getsockname(2) equivalent.
Definition: socket.h:80
int xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
POSIX setsockopt(2) equivalent.
Definition: socket.h:122
int xgetsockopt(int socketFd, int level, int optionName, void *optionValue, socklen_t *optionLength)
POSIX getsockopt(2) equivalent.
Definition: socket.h:92
int xsocket(int domain, int type, int protocol)
POSIX socket(2) equivalent.
Definition: socket.h:128
ssize_t xrecv(int socketFd, void *buf, size_t bufLength, int flags)
POSIX recv(2) equivalent.
Definition: socket.h:98
ssize_t xsendto(int socketFd, const void *buf, size_t bufLength, int flags, const struct sockaddr *to, socklen_t toLength)
POSIX sendto(2) equivalent.
Definition: socket.h:116
int xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
POSIX bind(2) equivalent.
Definition: socket.h:68
int xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength)
POSIX connect(2) equivalent.
Definition: socket.h:74
int xlisten(int socketFd, int backlog)
POSIX listen(2) equivalent.
Definition: socket.h:86

 

Introduction

Documentation

Support

Miscellaneous