socket.cc
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 #include "squid.h"
10 #include "compat/socket.h"
11 #include "compat/wserrno.h"
12 
13 #if _SQUID_WINDOWS_ || _SQUID_MINGW_
14 #include <unordered_map>
15 
16 int
17 xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength)
18 {
19  const auto handle = _get_osfhandle(socketFd);
20  if (!isValidSocketHandle(handle)) {
21  // errno is already set by _get_osfhandle()
22  return SOCKET_ERROR;
23  }
24  int al = 0;
25  int *alp = nullptr;
26  if (saLength) {
27  assert(*saLength <= INT_MAX);
28  al = static_cast<int>(*saLength);
29  alp = &al;
30  }
31  const auto result = accept(handle, sa, alp);
32  if (result == INVALID_SOCKET) {
33  SetErrnoFromWsaError();
34  return SOCKET_ERROR;
35  }
36  const auto rv = _open_osfhandle(result, 0);
37  if (rv == -1)
38  errno = EBADF;
39  if (saLength)
40  *saLength = static_cast<socklen_t>(al);
41  return rv;
42 }
43 
44 int
45 xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
46 {
47  const auto handle = _get_osfhandle(socketFd);
48  if (!isValidSocketHandle(handle)) {
49  // errno is already set by _get_osfhandle()
50  return SOCKET_ERROR;
51  }
52  assert(saLength <= INT_MAX);
53  const auto result = bind(handle, sa, static_cast<int>(saLength));
54  if (result == SOCKET_ERROR)
55  SetErrnoFromWsaError();
56  return result;
57 }
58 
59 int
60 xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength)
61 {
62  const auto handle = _get_osfhandle(socketFd);
63  if (!isValidSocketHandle(handle)) {
64  // errno is already set by _get_osfhandle()
65  return SOCKET_ERROR;
66  }
67  assert(saLength <= INT_MAX);
68  const auto result = connect(handle, sa, static_cast<int>(saLength));
69  if (result == SOCKET_ERROR)
70  SetErrnoFromWsaError();
71  return result;
72 }
73 
74 int
75 xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength)
76 {
77  const auto handle = _get_osfhandle(socketFd);
78  if (!isValidSocketHandle(handle)) {
79  // errno is already set by _get_osfhandle()
80  return SOCKET_ERROR;
81  }
82  int al = 0;
83  int *alp = nullptr;
84  if (saLength) {
85  assert(*saLength <= INT_MAX);
86  al = static_cast<int>(*saLength);
87  alp = &al;
88  }
89  const auto result = getsockname(handle, sa, alp);
90  if (result == SOCKET_ERROR)
91  SetErrnoFromWsaError();
92  if (saLength)
93  *saLength = static_cast<socklen_t>(al);
94  return result;
95 }
96 
97 int
98 xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength)
99 {
100  const auto handle = _get_osfhandle(socketFd);
101  if (!isValidSocketHandle(handle)) {
102  // errno is already set by _get_osfhandle()
103  return SOCKET_ERROR;
104  }
105  int ol = 0;
106  int *olp = nullptr;
107  if (optionLength) {
108  assert(*optionLength <= INT_MAX);
109  ol = static_cast<int>(*optionLength);
110  olp = &ol;
111  }
112  const auto result = getsockopt(handle, level, optionName, static_cast<char *>(optionValue), olp);
113  if (result == SOCKET_ERROR)
114  SetErrnoFromWsaError();
115  if (optionLength)
116  *optionLength = static_cast<socklen_t>(ol);
117  return result;
118 }
119 
120 int
121 xlisten(int socketFd, int backlog)
122 {
123  const auto handle = _get_osfhandle(socketFd);
124  if (!isValidSocketHandle(handle)) {
125  // errno is already set by _get_osfhandle()
126  return SOCKET_ERROR;
127  }
128  const auto result = listen(handle, backlog);
129  if (result == SOCKET_ERROR)
130  SetErrnoFromWsaError();
131  return result;
132 }
133 
134 ssize_t
135 xrecv(int socketFd, void * buf, size_t bufLength, int flags)
136 {
137  const auto handle = _get_osfhandle(socketFd);
138  if (!isValidSocketHandle(handle)) {
139  // errno is already set by _get_osfhandle()
140  return SOCKET_ERROR;
141  }
142  assert(bufLength <= INT_MAX);
143  const auto result = recv(handle, static_cast<char *>(buf), static_cast<int>(bufLength), flags);
144  if (result == SOCKET_ERROR)
145  SetErrnoFromWsaError();
146  return result;
147 }
148 
149 ssize_t
150 xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength)
151 {
152  const auto handle = _get_osfhandle(socketFd);
153  if (!isValidSocketHandle(handle)) {
154  // errno is already set by _get_osfhandle()
155  return SOCKET_ERROR;
156  }
157  assert(bufLength <= INT_MAX);
158  int fl = 0;
159  int *flp = nullptr;
160  if (fromLength) {
161  assert(*fromLength <= INT_MAX);
162  fl = static_cast<int>(*fromLength);
163  flp = &fl;
164  }
165  const auto result = recvfrom(handle, static_cast<char *>(buf), static_cast<int>(bufLength), flags, from, flp);
166  if (result == SOCKET_ERROR)
167  SetErrnoFromWsaError();
168  if (fromLength)
169  *fromLength = static_cast<socklen_t>(fl);
170  return result;
171 }
172 
173 ssize_t
174 xsend(int socketFd, const void * buf, size_t bufLength, int flags)
175 {
176  const auto handle = _get_osfhandle(socketFd);
177  if (!isValidSocketHandle(handle)) {
178  // errno is already set by _get_osfhandle()
179  return SOCKET_ERROR;
180  }
181  assert(bufLength <= INT_MAX);
182  const auto result = send(handle, static_cast<const char *>(buf), static_cast<int>(bufLength), flags);
183  if (result == SOCKET_ERROR)
184  SetErrnoFromWsaError();
185  return result;
186 }
187 
188 ssize_t
189 xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t toLength)
190 {
191  const auto handle = _get_osfhandle(socketFd);
192  if (!isValidSocketHandle(handle)) {
193  // errno is already set by _get_osfhandle()
194  return SOCKET_ERROR;
195  }
196  assert(bufLength <= INT_MAX);
197  assert(toLength <= INT_MAX);
198  const auto result = sendto(handle, static_cast<const char *>(buf), static_cast<int>(bufLength), flags, to, static_cast<int>(toLength));
199  if (result == SOCKET_ERROR)
200  SetErrnoFromWsaError();
201  return result;
202 }
203 
204 int
205 xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
206 {
207  const auto handle = _get_osfhandle(socketFd);
208  if (!isValidSocketHandle(handle)) {
209  // errno is already set by _get_osfhandle()
210  return SOCKET_ERROR;
211  }
212  assert(option <= INT_MAX);
213  const auto result = setsockopt(handle, level, option, static_cast<const char *>(value), static_cast<int>(valueLength));
214  if (result == SOCKET_ERROR)
215  SetErrnoFromWsaError();
216  return result;
217 }
218 
219 int
220 xsocket(int domain, int type, int protocol)
221 {
222  const auto result = socket(domain, type, protocol);
223  if (result == INVALID_SOCKET) {
224  SetErrnoFromWsaError();
225  return SOCKET_ERROR;
226  }
227  const auto rv = _open_osfhandle(result, 0);
228  if (rv == -1)
229  errno = EBADF;
230  return rv;
231 }
232 
233 #endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
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
#define INT_MAX
Definition: types.h:70
int xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
POSIX setsockopt(2) equivalent.
Definition: socket.h:122
#define assert(EX)
Definition: assert.h:17
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