1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 2002 - 2003
3*8b26181fSAndroid Build Coastguard Worker * NetGroup, Politecnico di Torino (Italy)
4*8b26181fSAndroid Build Coastguard Worker * All rights reserved.
5*8b26181fSAndroid Build Coastguard Worker *
6*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*8b26181fSAndroid Build Coastguard Worker * are met:
9*8b26181fSAndroid Build Coastguard Worker *
10*8b26181fSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
11*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
12*8b26181fSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
13*8b26181fSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
14*8b26181fSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
15*8b26181fSAndroid Build Coastguard Worker * 3. Neither the name of the Politecnico di Torino nor the names of its
16*8b26181fSAndroid Build Coastguard Worker * contributors may be used to endorse or promote products derived from
17*8b26181fSAndroid Build Coastguard Worker * this software without specific prior written permission.
18*8b26181fSAndroid Build Coastguard Worker *
19*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*8b26181fSAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*8b26181fSAndroid Build Coastguard Worker * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*8b26181fSAndroid Build Coastguard Worker * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*8b26181fSAndroid Build Coastguard Worker * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*8b26181fSAndroid Build Coastguard Worker * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*8b26181fSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*8b26181fSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*8b26181fSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*8b26181fSAndroid Build Coastguard Worker * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*8b26181fSAndroid Build Coastguard Worker *
31*8b26181fSAndroid Build Coastguard Worker */
32*8b26181fSAndroid Build Coastguard Worker
33*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
34*8b26181fSAndroid Build Coastguard Worker #include <config.h>
35*8b26181fSAndroid Build Coastguard Worker #endif
36*8b26181fSAndroid Build Coastguard Worker
37*8b26181fSAndroid Build Coastguard Worker /*
38*8b26181fSAndroid Build Coastguard Worker * \file sockutils.c
39*8b26181fSAndroid Build Coastguard Worker *
40*8b26181fSAndroid Build Coastguard Worker * The goal of this file is to provide a common set of primitives for socket
41*8b26181fSAndroid Build Coastguard Worker * manipulation.
42*8b26181fSAndroid Build Coastguard Worker *
43*8b26181fSAndroid Build Coastguard Worker * Although the socket interface defined in the RFC 2553 (and its updates)
44*8b26181fSAndroid Build Coastguard Worker * is excellent, there are still differences between the behavior of those
45*8b26181fSAndroid Build Coastguard Worker * routines on UN*X and Windows, and between UN*Xes.
46*8b26181fSAndroid Build Coastguard Worker *
47*8b26181fSAndroid Build Coastguard Worker * These calls provide an interface similar to the socket interface, but
48*8b26181fSAndroid Build Coastguard Worker * that hides the differences between operating systems. It does not
49*8b26181fSAndroid Build Coastguard Worker * attempt to significantly improve on the socket interface in other
50*8b26181fSAndroid Build Coastguard Worker * ways.
51*8b26181fSAndroid Build Coastguard Worker */
52*8b26181fSAndroid Build Coastguard Worker
53*8b26181fSAndroid Build Coastguard Worker #include "ftmacros.h"
54*8b26181fSAndroid Build Coastguard Worker
55*8b26181fSAndroid Build Coastguard Worker #include <string.h>
56*8b26181fSAndroid Build Coastguard Worker #include <errno.h> /* for the errno variable */
57*8b26181fSAndroid Build Coastguard Worker #include <stdio.h> /* for the stderr file */
58*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h> /* for malloc() and free() */
59*8b26181fSAndroid Build Coastguard Worker #include <limits.h> /* for INT_MAX */
60*8b26181fSAndroid Build Coastguard Worker
61*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker #include "sockutils.h"
64*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
65*8b26181fSAndroid Build Coastguard Worker
66*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
67*8b26181fSAndroid Build Coastguard Worker /*
68*8b26181fSAndroid Build Coastguard Worker * Winsock initialization.
69*8b26181fSAndroid Build Coastguard Worker *
70*8b26181fSAndroid Build Coastguard Worker * Ask for Winsock 2.2.
71*8b26181fSAndroid Build Coastguard Worker */
72*8b26181fSAndroid Build Coastguard Worker #define WINSOCK_MAJOR_VERSION 2
73*8b26181fSAndroid Build Coastguard Worker #define WINSOCK_MINOR_VERSION 2
74*8b26181fSAndroid Build Coastguard Worker
75*8b26181fSAndroid Build Coastguard Worker static int sockcount = 0; /*!< Variable that allows calling the WSAStartup() only one time */
76*8b26181fSAndroid Build Coastguard Worker #endif
77*8b26181fSAndroid Build Coastguard Worker
78*8b26181fSAndroid Build Coastguard Worker /* Some minor differences between UNIX and Win32 */
79*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
80*8b26181fSAndroid Build Coastguard Worker #define SHUT_WR SD_SEND /* The control code for shutdown() is different in Win32 */
81*8b26181fSAndroid Build Coastguard Worker #endif
82*8b26181fSAndroid Build Coastguard Worker
83*8b26181fSAndroid Build Coastguard Worker /* Size of the buffer that has to keep error messages */
84*8b26181fSAndroid Build Coastguard Worker #define SOCK_ERRBUF_SIZE 1024
85*8b26181fSAndroid Build Coastguard Worker
86*8b26181fSAndroid Build Coastguard Worker /* Constants; used in order to keep strings here */
87*8b26181fSAndroid Build Coastguard Worker #define SOCKET_NO_NAME_AVAILABLE "No name available"
88*8b26181fSAndroid Build Coastguard Worker #define SOCKET_NO_PORT_AVAILABLE "No port available"
89*8b26181fSAndroid Build Coastguard Worker #define SOCKET_NAME_NULL_DAD "Null address (possibly DAD Phase)"
90*8b26181fSAndroid Build Coastguard Worker
91*8b26181fSAndroid Build Coastguard Worker /*
92*8b26181fSAndroid Build Coastguard Worker * On UN*X, send() and recv() return ssize_t.
93*8b26181fSAndroid Build Coastguard Worker *
94*8b26181fSAndroid Build Coastguard Worker * On Windows, send() and recv() return an int.
95*8b26181fSAndroid Build Coastguard Worker *
96*8b26181fSAndroid Build Coastguard Worker * With MSVC, there *is* no ssize_t.
97*8b26181fSAndroid Build Coastguard Worker *
98*8b26181fSAndroid Build Coastguard Worker * With MinGW, there is an ssize_t type; it is either an int (32 bit)
99*8b26181fSAndroid Build Coastguard Worker * or a long long (64 bit).
100*8b26181fSAndroid Build Coastguard Worker *
101*8b26181fSAndroid Build Coastguard Worker * So, on Windows, if we don't have ssize_t defined, define it as an
102*8b26181fSAndroid Build Coastguard Worker * int, so we can use it, on all platforms, as the type of variables
103*8b26181fSAndroid Build Coastguard Worker * that hold the return values from send() and recv().
104*8b26181fSAndroid Build Coastguard Worker */
105*8b26181fSAndroid Build Coastguard Worker #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
106*8b26181fSAndroid Build Coastguard Worker typedef int ssize_t;
107*8b26181fSAndroid Build Coastguard Worker #endif
108*8b26181fSAndroid Build Coastguard Worker
109*8b26181fSAndroid Build Coastguard Worker /****************************************************
110*8b26181fSAndroid Build Coastguard Worker * *
111*8b26181fSAndroid Build Coastguard Worker * Locally defined functions *
112*8b26181fSAndroid Build Coastguard Worker * *
113*8b26181fSAndroid Build Coastguard Worker ****************************************************/
114*8b26181fSAndroid Build Coastguard Worker
115*8b26181fSAndroid Build Coastguard Worker static int sock_ismcastaddr(const struct sockaddr *saddr);
116*8b26181fSAndroid Build Coastguard Worker
117*8b26181fSAndroid Build Coastguard Worker /****************************************************
118*8b26181fSAndroid Build Coastguard Worker * *
119*8b26181fSAndroid Build Coastguard Worker * Function bodies *
120*8b26181fSAndroid Build Coastguard Worker * *
121*8b26181fSAndroid Build Coastguard Worker ****************************************************/
122*8b26181fSAndroid Build Coastguard Worker
123*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
124*8b26181fSAndroid Build Coastguard Worker const uint8_t *fuzzBuffer;
125*8b26181fSAndroid Build Coastguard Worker size_t fuzzSize;
126*8b26181fSAndroid Build Coastguard Worker size_t fuzzPos;
127*8b26181fSAndroid Build Coastguard Worker
sock_initfuzz(const uint8_t * Data,size_t Size)128*8b26181fSAndroid Build Coastguard Worker void sock_initfuzz(const uint8_t *Data, size_t Size) {
129*8b26181fSAndroid Build Coastguard Worker fuzzPos = 0;
130*8b26181fSAndroid Build Coastguard Worker fuzzSize = Size;
131*8b26181fSAndroid Build Coastguard Worker fuzzBuffer = Data;
132*8b26181fSAndroid Build Coastguard Worker }
133*8b26181fSAndroid Build Coastguard Worker
fuzz_recv(char * bufp,int remaining)134*8b26181fSAndroid Build Coastguard Worker static int fuzz_recv(char *bufp, int remaining) {
135*8b26181fSAndroid Build Coastguard Worker if (remaining > fuzzSize - fuzzPos) {
136*8b26181fSAndroid Build Coastguard Worker remaining = fuzzSize - fuzzPos;
137*8b26181fSAndroid Build Coastguard Worker }
138*8b26181fSAndroid Build Coastguard Worker if (fuzzPos < fuzzSize) {
139*8b26181fSAndroid Build Coastguard Worker memcpy(bufp, fuzzBuffer + fuzzPos, remaining);
140*8b26181fSAndroid Build Coastguard Worker }
141*8b26181fSAndroid Build Coastguard Worker fuzzPos += remaining;
142*8b26181fSAndroid Build Coastguard Worker return remaining;
143*8b26181fSAndroid Build Coastguard Worker }
144*8b26181fSAndroid Build Coastguard Worker #endif
145*8b26181fSAndroid Build Coastguard Worker
sock_geterrcode(void)146*8b26181fSAndroid Build Coastguard Worker int sock_geterrcode(void)
147*8b26181fSAndroid Build Coastguard Worker {
148*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
149*8b26181fSAndroid Build Coastguard Worker return GetLastError();
150*8b26181fSAndroid Build Coastguard Worker #else
151*8b26181fSAndroid Build Coastguard Worker return errno;
152*8b26181fSAndroid Build Coastguard Worker #endif
153*8b26181fSAndroid Build Coastguard Worker }
154*8b26181fSAndroid Build Coastguard Worker
155*8b26181fSAndroid Build Coastguard Worker /*
156*8b26181fSAndroid Build Coastguard Worker * Format an error message given an errno value (UN*X) or a Winsock error
157*8b26181fSAndroid Build Coastguard Worker * (Windows).
158*8b26181fSAndroid Build Coastguard Worker */
sock_vfmterrmsg(char * errbuf,size_t errbuflen,int errcode,const char * fmt,va_list ap)159*8b26181fSAndroid Build Coastguard Worker void sock_vfmterrmsg(char *errbuf, size_t errbuflen, int errcode,
160*8b26181fSAndroid Build Coastguard Worker const char *fmt, va_list ap)
161*8b26181fSAndroid Build Coastguard Worker {
162*8b26181fSAndroid Build Coastguard Worker if (errbuf == NULL)
163*8b26181fSAndroid Build Coastguard Worker return;
164*8b26181fSAndroid Build Coastguard Worker
165*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
166*8b26181fSAndroid Build Coastguard Worker pcap_vfmt_errmsg_for_win32_err(errbuf, errbuflen, errcode,
167*8b26181fSAndroid Build Coastguard Worker fmt, ap);
168*8b26181fSAndroid Build Coastguard Worker #else
169*8b26181fSAndroid Build Coastguard Worker pcap_vfmt_errmsg_for_errno(errbuf, errbuflen, errcode,
170*8b26181fSAndroid Build Coastguard Worker fmt, ap);
171*8b26181fSAndroid Build Coastguard Worker #endif
172*8b26181fSAndroid Build Coastguard Worker }
173*8b26181fSAndroid Build Coastguard Worker
sock_fmterrmsg(char * errbuf,size_t errbuflen,int errcode,const char * fmt,...)174*8b26181fSAndroid Build Coastguard Worker void sock_fmterrmsg(char *errbuf, size_t errbuflen, int errcode,
175*8b26181fSAndroid Build Coastguard Worker const char *fmt, ...)
176*8b26181fSAndroid Build Coastguard Worker {
177*8b26181fSAndroid Build Coastguard Worker va_list ap;
178*8b26181fSAndroid Build Coastguard Worker
179*8b26181fSAndroid Build Coastguard Worker va_start(ap, fmt);
180*8b26181fSAndroid Build Coastguard Worker sock_vfmterrmsg(errbuf, errbuflen, errcode, fmt, ap);
181*8b26181fSAndroid Build Coastguard Worker va_end(ap);
182*8b26181fSAndroid Build Coastguard Worker }
183*8b26181fSAndroid Build Coastguard Worker
184*8b26181fSAndroid Build Coastguard Worker /*
185*8b26181fSAndroid Build Coastguard Worker * Format an error message for the last socket error.
186*8b26181fSAndroid Build Coastguard Worker */
sock_geterrmsg(char * errbuf,size_t errbuflen,const char * fmt,...)187*8b26181fSAndroid Build Coastguard Worker void sock_geterrmsg(char *errbuf, size_t errbuflen, const char *fmt, ...)
188*8b26181fSAndroid Build Coastguard Worker {
189*8b26181fSAndroid Build Coastguard Worker va_list ap;
190*8b26181fSAndroid Build Coastguard Worker
191*8b26181fSAndroid Build Coastguard Worker va_start(ap, fmt);
192*8b26181fSAndroid Build Coastguard Worker sock_vfmterrmsg(errbuf, errbuflen, sock_geterrcode(), fmt, ap);
193*8b26181fSAndroid Build Coastguard Worker va_end(ap);
194*8b26181fSAndroid Build Coastguard Worker }
195*8b26181fSAndroid Build Coastguard Worker
196*8b26181fSAndroid Build Coastguard Worker /*
197*8b26181fSAndroid Build Coastguard Worker * Types of error.
198*8b26181fSAndroid Build Coastguard Worker *
199*8b26181fSAndroid Build Coastguard Worker * These are sorted by how likely they are to be the "underlying" problem,
200*8b26181fSAndroid Build Coastguard Worker * so that lower-rated errors for a given address in a given family
201*8b26181fSAndroid Build Coastguard Worker * should not overwrite higher-rated errors for another address in that
202*8b26181fSAndroid Build Coastguard Worker * family, and higher-rated errors should overwrit elower-rated errors.
203*8b26181fSAndroid Build Coastguard Worker */
204*8b26181fSAndroid Build Coastguard Worker typedef enum {
205*8b26181fSAndroid Build Coastguard Worker SOCK_CONNERR, /* connection error */
206*8b26181fSAndroid Build Coastguard Worker SOCK_HOSTERR, /* host error */
207*8b26181fSAndroid Build Coastguard Worker SOCK_NETERR, /* network error */
208*8b26181fSAndroid Build Coastguard Worker SOCK_AFNOTSUPERR, /* address family not supported */
209*8b26181fSAndroid Build Coastguard Worker SOCK_UNKNOWNERR, /* unknown error */
210*8b26181fSAndroid Build Coastguard Worker SOCK_NOERR /* no error */
211*8b26181fSAndroid Build Coastguard Worker } sock_errtype;
212*8b26181fSAndroid Build Coastguard Worker
sock_geterrtype(int errcode)213*8b26181fSAndroid Build Coastguard Worker static sock_errtype sock_geterrtype(int errcode)
214*8b26181fSAndroid Build Coastguard Worker {
215*8b26181fSAndroid Build Coastguard Worker switch (errcode) {
216*8b26181fSAndroid Build Coastguard Worker
217*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
218*8b26181fSAndroid Build Coastguard Worker case WSAECONNRESET:
219*8b26181fSAndroid Build Coastguard Worker case WSAECONNABORTED:
220*8b26181fSAndroid Build Coastguard Worker case WSAECONNREFUSED:
221*8b26181fSAndroid Build Coastguard Worker #else
222*8b26181fSAndroid Build Coastguard Worker case ECONNRESET:
223*8b26181fSAndroid Build Coastguard Worker case ECONNABORTED:
224*8b26181fSAndroid Build Coastguard Worker case ECONNREFUSED:
225*8b26181fSAndroid Build Coastguard Worker #endif
226*8b26181fSAndroid Build Coastguard Worker /*
227*8b26181fSAndroid Build Coastguard Worker * Connection error; this means the problem is probably
228*8b26181fSAndroid Build Coastguard Worker * that there's no server set up on the remote machine,
229*8b26181fSAndroid Build Coastguard Worker * or that it is set up, but it's IPv4-only or IPv6-only
230*8b26181fSAndroid Build Coastguard Worker * and we're trying the wrong address family.
231*8b26181fSAndroid Build Coastguard Worker *
232*8b26181fSAndroid Build Coastguard Worker * These overwrite all other errors, as they indicate
233*8b26181fSAndroid Build Coastguard Worker * that, even if somethng else went wrong in another
234*8b26181fSAndroid Build Coastguard Worker * attempt, this probably wouldn't work even if the
235*8b26181fSAndroid Build Coastguard Worker * other problems were fixed.
236*8b26181fSAndroid Build Coastguard Worker */
237*8b26181fSAndroid Build Coastguard Worker return (SOCK_CONNERR);
238*8b26181fSAndroid Build Coastguard Worker
239*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
240*8b26181fSAndroid Build Coastguard Worker case WSAENETUNREACH:
241*8b26181fSAndroid Build Coastguard Worker case WSAETIMEDOUT:
242*8b26181fSAndroid Build Coastguard Worker case WSAEHOSTDOWN:
243*8b26181fSAndroid Build Coastguard Worker case WSAEHOSTUNREACH:
244*8b26181fSAndroid Build Coastguard Worker #else
245*8b26181fSAndroid Build Coastguard Worker case ENETUNREACH:
246*8b26181fSAndroid Build Coastguard Worker case ETIMEDOUT:
247*8b26181fSAndroid Build Coastguard Worker case EHOSTDOWN:
248*8b26181fSAndroid Build Coastguard Worker case EHOSTUNREACH:
249*8b26181fSAndroid Build Coastguard Worker #endif
250*8b26181fSAndroid Build Coastguard Worker /*
251*8b26181fSAndroid Build Coastguard Worker * Network errors that could be IPv4-specific, IPv6-
252*8b26181fSAndroid Build Coastguard Worker * specific, or present with both.
253*8b26181fSAndroid Build Coastguard Worker *
254*8b26181fSAndroid Build Coastguard Worker * Don't overwrite connection errors, but overwrite
255*8b26181fSAndroid Build Coastguard Worker * everything else.
256*8b26181fSAndroid Build Coastguard Worker */
257*8b26181fSAndroid Build Coastguard Worker return (SOCK_HOSTERR);
258*8b26181fSAndroid Build Coastguard Worker
259*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
260*8b26181fSAndroid Build Coastguard Worker case WSAENETDOWN:
261*8b26181fSAndroid Build Coastguard Worker case WSAENETRESET:
262*8b26181fSAndroid Build Coastguard Worker #else
263*8b26181fSAndroid Build Coastguard Worker case ENETDOWN:
264*8b26181fSAndroid Build Coastguard Worker case ENETRESET:
265*8b26181fSAndroid Build Coastguard Worker #endif
266*8b26181fSAndroid Build Coastguard Worker /*
267*8b26181fSAndroid Build Coastguard Worker * Network error; this means we don't know whether
268*8b26181fSAndroid Build Coastguard Worker * there's a server set up on the remote machine,
269*8b26181fSAndroid Build Coastguard Worker * and we don't have a reason to believe that IPv6
270*8b26181fSAndroid Build Coastguard Worker * any worse or better than IPv4.
271*8b26181fSAndroid Build Coastguard Worker *
272*8b26181fSAndroid Build Coastguard Worker * These probably indicate a local failure, e.g.
273*8b26181fSAndroid Build Coastguard Worker * an interface is down.
274*8b26181fSAndroid Build Coastguard Worker *
275*8b26181fSAndroid Build Coastguard Worker * Don't overwrite connection errors or host errors,
276*8b26181fSAndroid Build Coastguard Worker * but overwrite everything else.
277*8b26181fSAndroid Build Coastguard Worker */
278*8b26181fSAndroid Build Coastguard Worker return (SOCK_NETERR);
279*8b26181fSAndroid Build Coastguard Worker
280*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
281*8b26181fSAndroid Build Coastguard Worker case WSAEAFNOSUPPORT:
282*8b26181fSAndroid Build Coastguard Worker #else
283*8b26181fSAndroid Build Coastguard Worker case EAFNOSUPPORT:
284*8b26181fSAndroid Build Coastguard Worker #endif
285*8b26181fSAndroid Build Coastguard Worker /*
286*8b26181fSAndroid Build Coastguard Worker * "Address family not supported" probably means
287*8b26181fSAndroid Build Coastguard Worker * "No soup^WIPv6 for you!".
288*8b26181fSAndroid Build Coastguard Worker *
289*8b26181fSAndroid Build Coastguard Worker * Don't overwrite connection errors, host errors, or
290*8b26181fSAndroid Build Coastguard Worker * network errors (none of which we should get for this
291*8b26181fSAndroid Build Coastguard Worker * address family if it's not supported), but overwrite
292*8b26181fSAndroid Build Coastguard Worker * everything else.
293*8b26181fSAndroid Build Coastguard Worker */
294*8b26181fSAndroid Build Coastguard Worker return (SOCK_AFNOTSUPERR);
295*8b26181fSAndroid Build Coastguard Worker
296*8b26181fSAndroid Build Coastguard Worker default:
297*8b26181fSAndroid Build Coastguard Worker /*
298*8b26181fSAndroid Build Coastguard Worker * Anything else.
299*8b26181fSAndroid Build Coastguard Worker *
300*8b26181fSAndroid Build Coastguard Worker * Don't overwrite any errors.
301*8b26181fSAndroid Build Coastguard Worker */
302*8b26181fSAndroid Build Coastguard Worker return (SOCK_UNKNOWNERR);
303*8b26181fSAndroid Build Coastguard Worker }
304*8b26181fSAndroid Build Coastguard Worker }
305*8b26181fSAndroid Build Coastguard Worker
306*8b26181fSAndroid Build Coastguard Worker /*
307*8b26181fSAndroid Build Coastguard Worker * \brief This function initializes the socket mechanism if it hasn't
308*8b26181fSAndroid Build Coastguard Worker * already been initialized or reinitializes it after it has been
309*8b26181fSAndroid Build Coastguard Worker * cleaned up.
310*8b26181fSAndroid Build Coastguard Worker *
311*8b26181fSAndroid Build Coastguard Worker * On UN*Xes, it doesn't need to do anything; on Windows, it needs to
312*8b26181fSAndroid Build Coastguard Worker * initialize Winsock.
313*8b26181fSAndroid Build Coastguard Worker *
314*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain
315*8b26181fSAndroid Build Coastguard Worker * the complete error message. This buffer has to be at least 'errbuflen'
316*8b26181fSAndroid Build Coastguard Worker * in length. It can be NULL; in this case no error message is supplied.
317*8b26181fSAndroid Build Coastguard Worker *
318*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error.
319*8b26181fSAndroid Build Coastguard Worker * The error message cannot be larger than 'errbuflen - 1' because the
320*8b26181fSAndroid Build Coastguard Worker * last char is reserved for the string terminator.
321*8b26181fSAndroid Build Coastguard Worker *
322*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred. The
323*8b26181fSAndroid Build Coastguard Worker * error message is returned in the buffer pointed to by 'errbuf' variable.
324*8b26181fSAndroid Build Coastguard Worker */
325*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
sock_init(char * errbuf,int errbuflen)326*8b26181fSAndroid Build Coastguard Worker int sock_init(char *errbuf, int errbuflen)
327*8b26181fSAndroid Build Coastguard Worker {
328*8b26181fSAndroid Build Coastguard Worker if (sockcount == 0)
329*8b26181fSAndroid Build Coastguard Worker {
330*8b26181fSAndroid Build Coastguard Worker WSADATA wsaData; /* helper variable needed to initialize Winsock */
331*8b26181fSAndroid Build Coastguard Worker
332*8b26181fSAndroid Build Coastguard Worker if (WSAStartup(MAKEWORD(WINSOCK_MAJOR_VERSION,
333*8b26181fSAndroid Build Coastguard Worker WINSOCK_MINOR_VERSION), &wsaData) != 0)
334*8b26181fSAndroid Build Coastguard Worker {
335*8b26181fSAndroid Build Coastguard Worker if (errbuf)
336*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "Failed to initialize Winsock\n");
337*8b26181fSAndroid Build Coastguard Worker
338*8b26181fSAndroid Build Coastguard Worker WSACleanup();
339*8b26181fSAndroid Build Coastguard Worker
340*8b26181fSAndroid Build Coastguard Worker return -1;
341*8b26181fSAndroid Build Coastguard Worker }
342*8b26181fSAndroid Build Coastguard Worker }
343*8b26181fSAndroid Build Coastguard Worker
344*8b26181fSAndroid Build Coastguard Worker sockcount++;
345*8b26181fSAndroid Build Coastguard Worker return 0;
346*8b26181fSAndroid Build Coastguard Worker }
347*8b26181fSAndroid Build Coastguard Worker #else
sock_init(char * errbuf _U_,int errbuflen _U_)348*8b26181fSAndroid Build Coastguard Worker int sock_init(char *errbuf _U_, int errbuflen _U_)
349*8b26181fSAndroid Build Coastguard Worker {
350*8b26181fSAndroid Build Coastguard Worker /*
351*8b26181fSAndroid Build Coastguard Worker * Nothing to do on UN*Xes.
352*8b26181fSAndroid Build Coastguard Worker */
353*8b26181fSAndroid Build Coastguard Worker return 0;
354*8b26181fSAndroid Build Coastguard Worker }
355*8b26181fSAndroid Build Coastguard Worker #endif
356*8b26181fSAndroid Build Coastguard Worker
357*8b26181fSAndroid Build Coastguard Worker /*
358*8b26181fSAndroid Build Coastguard Worker * \brief This function cleans up the socket mechanism if we have no
359*8b26181fSAndroid Build Coastguard Worker * sockets left open.
360*8b26181fSAndroid Build Coastguard Worker *
361*8b26181fSAndroid Build Coastguard Worker * On UN*Xes, it doesn't need to do anything; on Windows, it needs
362*8b26181fSAndroid Build Coastguard Worker * to clean up Winsock.
363*8b26181fSAndroid Build Coastguard Worker *
364*8b26181fSAndroid Build Coastguard Worker * \return No error values.
365*8b26181fSAndroid Build Coastguard Worker */
sock_cleanup(void)366*8b26181fSAndroid Build Coastguard Worker void sock_cleanup(void)
367*8b26181fSAndroid Build Coastguard Worker {
368*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
369*8b26181fSAndroid Build Coastguard Worker sockcount--;
370*8b26181fSAndroid Build Coastguard Worker
371*8b26181fSAndroid Build Coastguard Worker if (sockcount == 0)
372*8b26181fSAndroid Build Coastguard Worker WSACleanup();
373*8b26181fSAndroid Build Coastguard Worker #endif
374*8b26181fSAndroid Build Coastguard Worker }
375*8b26181fSAndroid Build Coastguard Worker
376*8b26181fSAndroid Build Coastguard Worker /*
377*8b26181fSAndroid Build Coastguard Worker * \brief It checks if the sockaddr variable contains a multicast address.
378*8b26181fSAndroid Build Coastguard Worker *
379*8b26181fSAndroid Build Coastguard Worker * \return '0' if the address is multicast, '-1' if it is not.
380*8b26181fSAndroid Build Coastguard Worker */
sock_ismcastaddr(const struct sockaddr * saddr)381*8b26181fSAndroid Build Coastguard Worker static int sock_ismcastaddr(const struct sockaddr *saddr)
382*8b26181fSAndroid Build Coastguard Worker {
383*8b26181fSAndroid Build Coastguard Worker if (saddr->sa_family == PF_INET)
384*8b26181fSAndroid Build Coastguard Worker {
385*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in *saddr4 = (struct sockaddr_in *) saddr;
386*8b26181fSAndroid Build Coastguard Worker if (IN_MULTICAST(ntohl(saddr4->sin_addr.s_addr))) return 0;
387*8b26181fSAndroid Build Coastguard Worker else return -1;
388*8b26181fSAndroid Build Coastguard Worker }
389*8b26181fSAndroid Build Coastguard Worker else
390*8b26181fSAndroid Build Coastguard Worker {
391*8b26181fSAndroid Build Coastguard Worker struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) saddr;
392*8b26181fSAndroid Build Coastguard Worker if (IN6_IS_ADDR_MULTICAST(&saddr6->sin6_addr)) return 0;
393*8b26181fSAndroid Build Coastguard Worker else return -1;
394*8b26181fSAndroid Build Coastguard Worker }
395*8b26181fSAndroid Build Coastguard Worker }
396*8b26181fSAndroid Build Coastguard Worker
397*8b26181fSAndroid Build Coastguard Worker struct addr_status {
398*8b26181fSAndroid Build Coastguard Worker struct addrinfo *info;
399*8b26181fSAndroid Build Coastguard Worker int errcode;
400*8b26181fSAndroid Build Coastguard Worker sock_errtype errtype;
401*8b26181fSAndroid Build Coastguard Worker };
402*8b26181fSAndroid Build Coastguard Worker
403*8b26181fSAndroid Build Coastguard Worker /*
404*8b26181fSAndroid Build Coastguard Worker * Sort by IPv4 address vs. IPv6 address.
405*8b26181fSAndroid Build Coastguard Worker */
compare_addrs_to_try_by_address_family(const void * a,const void * b)406*8b26181fSAndroid Build Coastguard Worker static int compare_addrs_to_try_by_address_family(const void *a, const void *b)
407*8b26181fSAndroid Build Coastguard Worker {
408*8b26181fSAndroid Build Coastguard Worker const struct addr_status *addr_a = (const struct addr_status *)a;
409*8b26181fSAndroid Build Coastguard Worker const struct addr_status *addr_b = (const struct addr_status *)b;
410*8b26181fSAndroid Build Coastguard Worker
411*8b26181fSAndroid Build Coastguard Worker return addr_a->info->ai_family - addr_b->info->ai_family;
412*8b26181fSAndroid Build Coastguard Worker }
413*8b26181fSAndroid Build Coastguard Worker
414*8b26181fSAndroid Build Coastguard Worker /*
415*8b26181fSAndroid Build Coastguard Worker * Sort by error type and, within a given error type, by error code and,
416*8b26181fSAndroid Build Coastguard Worker * within a given error code, by IPv4 address vs. IPv6 address.
417*8b26181fSAndroid Build Coastguard Worker */
compare_addrs_to_try_by_status(const void * a,const void * b)418*8b26181fSAndroid Build Coastguard Worker static int compare_addrs_to_try_by_status(const void *a, const void *b)
419*8b26181fSAndroid Build Coastguard Worker {
420*8b26181fSAndroid Build Coastguard Worker const struct addr_status *addr_a = (const struct addr_status *)a;
421*8b26181fSAndroid Build Coastguard Worker const struct addr_status *addr_b = (const struct addr_status *)b;
422*8b26181fSAndroid Build Coastguard Worker
423*8b26181fSAndroid Build Coastguard Worker if (addr_a->errtype == addr_b->errtype)
424*8b26181fSAndroid Build Coastguard Worker {
425*8b26181fSAndroid Build Coastguard Worker if (addr_a->errcode == addr_b->errcode)
426*8b26181fSAndroid Build Coastguard Worker {
427*8b26181fSAndroid Build Coastguard Worker return addr_a->info->ai_family - addr_b->info->ai_family;
428*8b26181fSAndroid Build Coastguard Worker }
429*8b26181fSAndroid Build Coastguard Worker return addr_a->errcode - addr_b->errcode;
430*8b26181fSAndroid Build Coastguard Worker }
431*8b26181fSAndroid Build Coastguard Worker
432*8b26181fSAndroid Build Coastguard Worker return addr_a->errtype - addr_b->errtype;
433*8b26181fSAndroid Build Coastguard Worker }
434*8b26181fSAndroid Build Coastguard Worker
sock_create_socket(struct addrinfo * addrinfo,char * errbuf,int errbuflen)435*8b26181fSAndroid Build Coastguard Worker static SOCKET sock_create_socket(struct addrinfo *addrinfo, char *errbuf,
436*8b26181fSAndroid Build Coastguard Worker int errbuflen)
437*8b26181fSAndroid Build Coastguard Worker {
438*8b26181fSAndroid Build Coastguard Worker SOCKET sock;
439*8b26181fSAndroid Build Coastguard Worker #ifdef SO_NOSIGPIPE
440*8b26181fSAndroid Build Coastguard Worker int on = 1;
441*8b26181fSAndroid Build Coastguard Worker #endif
442*8b26181fSAndroid Build Coastguard Worker
443*8b26181fSAndroid Build Coastguard Worker sock = socket(addrinfo->ai_family, addrinfo->ai_socktype,
444*8b26181fSAndroid Build Coastguard Worker addrinfo->ai_protocol);
445*8b26181fSAndroid Build Coastguard Worker if (sock == INVALID_SOCKET)
446*8b26181fSAndroid Build Coastguard Worker {
447*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "socket() failed");
448*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
449*8b26181fSAndroid Build Coastguard Worker }
450*8b26181fSAndroid Build Coastguard Worker
451*8b26181fSAndroid Build Coastguard Worker /*
452*8b26181fSAndroid Build Coastguard Worker * Disable SIGPIPE, if we have SO_NOSIGPIPE. We don't want to
453*8b26181fSAndroid Build Coastguard Worker * have to deal with signals if the peer closes the connection,
454*8b26181fSAndroid Build Coastguard Worker * especially in client programs, which may not even be aware that
455*8b26181fSAndroid Build Coastguard Worker * they're sending to sockets.
456*8b26181fSAndroid Build Coastguard Worker */
457*8b26181fSAndroid Build Coastguard Worker #ifdef SO_NOSIGPIPE
458*8b26181fSAndroid Build Coastguard Worker if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (char *)&on,
459*8b26181fSAndroid Build Coastguard Worker sizeof (int)) == -1)
460*8b26181fSAndroid Build Coastguard Worker {
461*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen,
462*8b26181fSAndroid Build Coastguard Worker "setsockopt(SO_NOSIGPIPE) failed");
463*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
464*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
465*8b26181fSAndroid Build Coastguard Worker }
466*8b26181fSAndroid Build Coastguard Worker #endif
467*8b26181fSAndroid Build Coastguard Worker return sock;
468*8b26181fSAndroid Build Coastguard Worker }
469*8b26181fSAndroid Build Coastguard Worker
470*8b26181fSAndroid Build Coastguard Worker /*
471*8b26181fSAndroid Build Coastguard Worker * \brief It initializes a network connection both from the client and the server side.
472*8b26181fSAndroid Build Coastguard Worker *
473*8b26181fSAndroid Build Coastguard Worker * In case of a client socket, this function calls socket() and connect().
474*8b26181fSAndroid Build Coastguard Worker * In the meanwhile, it checks for any socket error.
475*8b26181fSAndroid Build Coastguard Worker * If an error occurs, it writes the error message into 'errbuf'.
476*8b26181fSAndroid Build Coastguard Worker *
477*8b26181fSAndroid Build Coastguard Worker * In case of a server socket, the function calls socket(), bind() and listen().
478*8b26181fSAndroid Build Coastguard Worker *
479*8b26181fSAndroid Build Coastguard Worker * This function is usually preceded by the sock_initaddress().
480*8b26181fSAndroid Build Coastguard Worker *
481*8b26181fSAndroid Build Coastguard Worker * \param host: for client sockets, the host name to which we're trying
482*8b26181fSAndroid Build Coastguard Worker * to connect.
483*8b26181fSAndroid Build Coastguard Worker *
484*8b26181fSAndroid Build Coastguard Worker * \param addrinfo: pointer to an addrinfo variable which will be used to
485*8b26181fSAndroid Build Coastguard Worker * open the socket and such. This variable is the one returned by the previous call to
486*8b26181fSAndroid Build Coastguard Worker * sock_initaddress().
487*8b26181fSAndroid Build Coastguard Worker *
488*8b26181fSAndroid Build Coastguard Worker * \param server: '1' if this is a server socket, '0' otherwise.
489*8b26181fSAndroid Build Coastguard Worker *
490*8b26181fSAndroid Build Coastguard Worker * \param nconn: number of the connections that are allowed to wait into the listen() call.
491*8b26181fSAndroid Build Coastguard Worker * This value has no meanings in case of a client socket.
492*8b26181fSAndroid Build Coastguard Worker *
493*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
494*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
495*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
496*8b26181fSAndroid Build Coastguard Worker *
497*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
498*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
499*8b26181fSAndroid Build Coastguard Worker *
500*8b26181fSAndroid Build Coastguard Worker * \return the socket that has been opened (that has to be used in the following sockets calls)
501*8b26181fSAndroid Build Coastguard Worker * if everything is fine, INVALID_SOCKET if some errors occurred. The error message is returned
502*8b26181fSAndroid Build Coastguard Worker * in the 'errbuf' variable.
503*8b26181fSAndroid Build Coastguard Worker */
sock_open(const char * host,struct addrinfo * addrinfo,int server,int nconn,char * errbuf,int errbuflen)504*8b26181fSAndroid Build Coastguard Worker SOCKET sock_open(const char *host, struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen)
505*8b26181fSAndroid Build Coastguard Worker {
506*8b26181fSAndroid Build Coastguard Worker SOCKET sock;
507*8b26181fSAndroid Build Coastguard Worker
508*8b26181fSAndroid Build Coastguard Worker /* This is a server socket */
509*8b26181fSAndroid Build Coastguard Worker if (server)
510*8b26181fSAndroid Build Coastguard Worker {
511*8b26181fSAndroid Build Coastguard Worker int on;
512*8b26181fSAndroid Build Coastguard Worker
513*8b26181fSAndroid Build Coastguard Worker /*
514*8b26181fSAndroid Build Coastguard Worker * Attempt to create the socket.
515*8b26181fSAndroid Build Coastguard Worker */
516*8b26181fSAndroid Build Coastguard Worker sock = sock_create_socket(addrinfo, errbuf, errbuflen);
517*8b26181fSAndroid Build Coastguard Worker if (sock == INVALID_SOCKET)
518*8b26181fSAndroid Build Coastguard Worker {
519*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
520*8b26181fSAndroid Build Coastguard Worker }
521*8b26181fSAndroid Build Coastguard Worker
522*8b26181fSAndroid Build Coastguard Worker /*
523*8b26181fSAndroid Build Coastguard Worker * Allow a new server to bind the socket after the old one
524*8b26181fSAndroid Build Coastguard Worker * exited, even if lingering sockets are still present.
525*8b26181fSAndroid Build Coastguard Worker *
526*8b26181fSAndroid Build Coastguard Worker * Don't treat an error as a failure.
527*8b26181fSAndroid Build Coastguard Worker */
528*8b26181fSAndroid Build Coastguard Worker on = 1;
529*8b26181fSAndroid Build Coastguard Worker (void)setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
530*8b26181fSAndroid Build Coastguard Worker (char *)&on, sizeof (on));
531*8b26181fSAndroid Build Coastguard Worker
532*8b26181fSAndroid Build Coastguard Worker #if defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
533*8b26181fSAndroid Build Coastguard Worker /*
534*8b26181fSAndroid Build Coastguard Worker * Force the use of IPv6-only addresses.
535*8b26181fSAndroid Build Coastguard Worker *
536*8b26181fSAndroid Build Coastguard Worker * RFC 3493 indicates that you can support IPv4 on an
537*8b26181fSAndroid Build Coastguard Worker * IPv6 socket:
538*8b26181fSAndroid Build Coastguard Worker *
539*8b26181fSAndroid Build Coastguard Worker * https://tools.ietf.org/html/rfc3493#section-3.7
540*8b26181fSAndroid Build Coastguard Worker *
541*8b26181fSAndroid Build Coastguard Worker * and that this is the default behavior. This means
542*8b26181fSAndroid Build Coastguard Worker * that if we first create an IPv6 socket bound to the
543*8b26181fSAndroid Build Coastguard Worker * "any" address, it is, in effect, also bound to the
544*8b26181fSAndroid Build Coastguard Worker * IPv4 "any" address, so when we create an IPv4 socket
545*8b26181fSAndroid Build Coastguard Worker * and try to bind it to the IPv4 "any" address, it gets
546*8b26181fSAndroid Build Coastguard Worker * EADDRINUSE.
547*8b26181fSAndroid Build Coastguard Worker *
548*8b26181fSAndroid Build Coastguard Worker * Not all network stacks support IPv4 on IPv6 sockets;
549*8b26181fSAndroid Build Coastguard Worker * pre-NT 6 Windows stacks don't support it, and the
550*8b26181fSAndroid Build Coastguard Worker * OpenBSD stack doesn't support it for security reasons
551*8b26181fSAndroid Build Coastguard Worker * (see the OpenBSD inet6(4) man page). Therefore, we
552*8b26181fSAndroid Build Coastguard Worker * don't want to rely on this behavior.
553*8b26181fSAndroid Build Coastguard Worker *
554*8b26181fSAndroid Build Coastguard Worker * So we try to disable it, using either the IPV6_V6ONLY
555*8b26181fSAndroid Build Coastguard Worker * option from RFC 3493:
556*8b26181fSAndroid Build Coastguard Worker *
557*8b26181fSAndroid Build Coastguard Worker * https://tools.ietf.org/html/rfc3493#section-5.3
558*8b26181fSAndroid Build Coastguard Worker *
559*8b26181fSAndroid Build Coastguard Worker * or the IPV6_BINDV6ONLY option from older UN*Xes.
560*8b26181fSAndroid Build Coastguard Worker */
561*8b26181fSAndroid Build Coastguard Worker #ifndef IPV6_V6ONLY
562*8b26181fSAndroid Build Coastguard Worker /* For older systems */
563*8b26181fSAndroid Build Coastguard Worker #define IPV6_V6ONLY IPV6_BINDV6ONLY
564*8b26181fSAndroid Build Coastguard Worker #endif /* IPV6_V6ONLY */
565*8b26181fSAndroid Build Coastguard Worker if (addrinfo->ai_family == PF_INET6)
566*8b26181fSAndroid Build Coastguard Worker {
567*8b26181fSAndroid Build Coastguard Worker on = 1;
568*8b26181fSAndroid Build Coastguard Worker if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
569*8b26181fSAndroid Build Coastguard Worker (char *)&on, sizeof (int)) == -1)
570*8b26181fSAndroid Build Coastguard Worker {
571*8b26181fSAndroid Build Coastguard Worker if (errbuf)
572*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "setsockopt(IPV6_V6ONLY)");
573*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
574*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
575*8b26181fSAndroid Build Coastguard Worker }
576*8b26181fSAndroid Build Coastguard Worker }
577*8b26181fSAndroid Build Coastguard Worker #endif /* defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY) */
578*8b26181fSAndroid Build Coastguard Worker
579*8b26181fSAndroid Build Coastguard Worker /* WARNING: if the address is a mcast one, I should place the proper Win32 code here */
580*8b26181fSAndroid Build Coastguard Worker if (bind(sock, addrinfo->ai_addr, (int) addrinfo->ai_addrlen) != 0)
581*8b26181fSAndroid Build Coastguard Worker {
582*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "bind() failed");
583*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
584*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
585*8b26181fSAndroid Build Coastguard Worker }
586*8b26181fSAndroid Build Coastguard Worker
587*8b26181fSAndroid Build Coastguard Worker if (addrinfo->ai_socktype == SOCK_STREAM)
588*8b26181fSAndroid Build Coastguard Worker if (listen(sock, nconn) == -1)
589*8b26181fSAndroid Build Coastguard Worker {
590*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen,
591*8b26181fSAndroid Build Coastguard Worker "listen() failed");
592*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
593*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
594*8b26181fSAndroid Build Coastguard Worker }
595*8b26181fSAndroid Build Coastguard Worker
596*8b26181fSAndroid Build Coastguard Worker /* server side ended */
597*8b26181fSAndroid Build Coastguard Worker return sock;
598*8b26181fSAndroid Build Coastguard Worker }
599*8b26181fSAndroid Build Coastguard Worker else /* we're the client */
600*8b26181fSAndroid Build Coastguard Worker {
601*8b26181fSAndroid Build Coastguard Worker struct addr_status *addrs_to_try;
602*8b26181fSAndroid Build Coastguard Worker struct addrinfo *tempaddrinfo;
603*8b26181fSAndroid Build Coastguard Worker size_t numaddrinfos;
604*8b26181fSAndroid Build Coastguard Worker size_t i;
605*8b26181fSAndroid Build Coastguard Worker int current_af = AF_UNSPEC;
606*8b26181fSAndroid Build Coastguard Worker
607*8b26181fSAndroid Build Coastguard Worker /*
608*8b26181fSAndroid Build Coastguard Worker * We have to loop though all the addrinfos returned.
609*8b26181fSAndroid Build Coastguard Worker * For instance, we can have both IPv6 and IPv4 addresses,
610*8b26181fSAndroid Build Coastguard Worker * but the service we're trying to connect to is unavailable
611*8b26181fSAndroid Build Coastguard Worker * in IPv6, so we have to try in IPv4 as well.
612*8b26181fSAndroid Build Coastguard Worker *
613*8b26181fSAndroid Build Coastguard Worker * How many addrinfos do we have?
614*8b26181fSAndroid Build Coastguard Worker */
615*8b26181fSAndroid Build Coastguard Worker numaddrinfos = 0;
616*8b26181fSAndroid Build Coastguard Worker for (tempaddrinfo = addrinfo; tempaddrinfo != NULL;
617*8b26181fSAndroid Build Coastguard Worker tempaddrinfo = tempaddrinfo->ai_next)
618*8b26181fSAndroid Build Coastguard Worker {
619*8b26181fSAndroid Build Coastguard Worker numaddrinfos++;
620*8b26181fSAndroid Build Coastguard Worker }
621*8b26181fSAndroid Build Coastguard Worker
622*8b26181fSAndroid Build Coastguard Worker if (numaddrinfos == 0)
623*8b26181fSAndroid Build Coastguard Worker {
624*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
625*8b26181fSAndroid Build Coastguard Worker "There are no addresses in the address list");
626*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
627*8b26181fSAndroid Build Coastguard Worker }
628*8b26181fSAndroid Build Coastguard Worker
629*8b26181fSAndroid Build Coastguard Worker /*
630*8b26181fSAndroid Build Coastguard Worker * Allocate an array of struct addr_status and fill it in.
631*8b26181fSAndroid Build Coastguard Worker */
632*8b26181fSAndroid Build Coastguard Worker addrs_to_try = calloc(numaddrinfos, sizeof *addrs_to_try);
633*8b26181fSAndroid Build Coastguard Worker if (addrs_to_try == NULL)
634*8b26181fSAndroid Build Coastguard Worker {
635*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
636*8b26181fSAndroid Build Coastguard Worker "Out of memory connecting to %s", host);
637*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
638*8b26181fSAndroid Build Coastguard Worker }
639*8b26181fSAndroid Build Coastguard Worker
640*8b26181fSAndroid Build Coastguard Worker for (tempaddrinfo = addrinfo, i = 0; tempaddrinfo != NULL;
641*8b26181fSAndroid Build Coastguard Worker tempaddrinfo = tempaddrinfo->ai_next, i++)
642*8b26181fSAndroid Build Coastguard Worker {
643*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].info = tempaddrinfo;
644*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errcode = 0;
645*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errtype = SOCK_NOERR;
646*8b26181fSAndroid Build Coastguard Worker }
647*8b26181fSAndroid Build Coastguard Worker
648*8b26181fSAndroid Build Coastguard Worker /*
649*8b26181fSAndroid Build Coastguard Worker * Sort the structures to put the IPv4 addresses before the
650*8b26181fSAndroid Build Coastguard Worker * IPv6 addresses; we will have to create an IPv4 socket
651*8b26181fSAndroid Build Coastguard Worker * for the IPv4 addresses and an IPv6 socket for the IPv6
652*8b26181fSAndroid Build Coastguard Worker * addresses (one of the arguments to socket() is the
653*8b26181fSAndroid Build Coastguard Worker * address/protocol family to use, and IPv4 and IPv6 are
654*8b26181fSAndroid Build Coastguard Worker * separate address/protocol families).
655*8b26181fSAndroid Build Coastguard Worker */
656*8b26181fSAndroid Build Coastguard Worker qsort(addrs_to_try, numaddrinfos, sizeof *addrs_to_try,
657*8b26181fSAndroid Build Coastguard Worker compare_addrs_to_try_by_address_family);
658*8b26181fSAndroid Build Coastguard Worker
659*8b26181fSAndroid Build Coastguard Worker /* Start out with no socket. */
660*8b26181fSAndroid Build Coastguard Worker sock = INVALID_SOCKET;
661*8b26181fSAndroid Build Coastguard Worker
662*8b26181fSAndroid Build Coastguard Worker /*
663*8b26181fSAndroid Build Coastguard Worker * Now try them all.
664*8b26181fSAndroid Build Coastguard Worker */
665*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < numaddrinfos; i++)
666*8b26181fSAndroid Build Coastguard Worker {
667*8b26181fSAndroid Build Coastguard Worker tempaddrinfo = addrs_to_try[i].info;
668*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
669*8b26181fSAndroid Build Coastguard Worker break;
670*8b26181fSAndroid Build Coastguard Worker #endif
671*8b26181fSAndroid Build Coastguard Worker /*
672*8b26181fSAndroid Build Coastguard Worker * If we have a socket, but it's for a
673*8b26181fSAndroid Build Coastguard Worker * different address family, close it.
674*8b26181fSAndroid Build Coastguard Worker */
675*8b26181fSAndroid Build Coastguard Worker if (sock != INVALID_SOCKET &&
676*8b26181fSAndroid Build Coastguard Worker current_af != tempaddrinfo->ai_family)
677*8b26181fSAndroid Build Coastguard Worker {
678*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
679*8b26181fSAndroid Build Coastguard Worker sock = INVALID_SOCKET;
680*8b26181fSAndroid Build Coastguard Worker }
681*8b26181fSAndroid Build Coastguard Worker
682*8b26181fSAndroid Build Coastguard Worker /*
683*8b26181fSAndroid Build Coastguard Worker * If we don't have a socket, open one
684*8b26181fSAndroid Build Coastguard Worker * for *this* address's address family.
685*8b26181fSAndroid Build Coastguard Worker */
686*8b26181fSAndroid Build Coastguard Worker if (sock == INVALID_SOCKET)
687*8b26181fSAndroid Build Coastguard Worker {
688*8b26181fSAndroid Build Coastguard Worker sock = sock_create_socket(tempaddrinfo,
689*8b26181fSAndroid Build Coastguard Worker errbuf, errbuflen);
690*8b26181fSAndroid Build Coastguard Worker if (sock == INVALID_SOCKET)
691*8b26181fSAndroid Build Coastguard Worker {
692*8b26181fSAndroid Build Coastguard Worker free(addrs_to_try);
693*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
694*8b26181fSAndroid Build Coastguard Worker }
695*8b26181fSAndroid Build Coastguard Worker }
696*8b26181fSAndroid Build Coastguard Worker if (connect(sock, tempaddrinfo->ai_addr, (int) tempaddrinfo->ai_addrlen) == -1)
697*8b26181fSAndroid Build Coastguard Worker {
698*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errcode = sock_geterrcode();
699*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errtype =
700*8b26181fSAndroid Build Coastguard Worker sock_geterrtype(addrs_to_try[i].errcode);
701*8b26181fSAndroid Build Coastguard Worker }
702*8b26181fSAndroid Build Coastguard Worker else
703*8b26181fSAndroid Build Coastguard Worker break;
704*8b26181fSAndroid Build Coastguard Worker }
705*8b26181fSAndroid Build Coastguard Worker
706*8b26181fSAndroid Build Coastguard Worker /*
707*8b26181fSAndroid Build Coastguard Worker * Check how we exited from the previous loop.
708*8b26181fSAndroid Build Coastguard Worker * If tempaddrinfo is equal to NULL, it means that all
709*8b26181fSAndroid Build Coastguard Worker * the connect() attempts failed. Construct an
710*8b26181fSAndroid Build Coastguard Worker * error message.
711*8b26181fSAndroid Build Coastguard Worker */
712*8b26181fSAndroid Build Coastguard Worker if (i == numaddrinfos)
713*8b26181fSAndroid Build Coastguard Worker {
714*8b26181fSAndroid Build Coastguard Worker int same_error_for_all;
715*8b26181fSAndroid Build Coastguard Worker int first_error;
716*8b26181fSAndroid Build Coastguard Worker
717*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
718*8b26181fSAndroid Build Coastguard Worker
719*8b26181fSAndroid Build Coastguard Worker /*
720*8b26181fSAndroid Build Coastguard Worker * Sort the statuses to group together categories
721*8b26181fSAndroid Build Coastguard Worker * of errors, errors within categories, and
722*8b26181fSAndroid Build Coastguard Worker * address families within error sets.
723*8b26181fSAndroid Build Coastguard Worker */
724*8b26181fSAndroid Build Coastguard Worker qsort(addrs_to_try, numaddrinfos, sizeof *addrs_to_try,
725*8b26181fSAndroid Build Coastguard Worker compare_addrs_to_try_by_status);
726*8b26181fSAndroid Build Coastguard Worker
727*8b26181fSAndroid Build Coastguard Worker /*
728*8b26181fSAndroid Build Coastguard Worker * Are all the errors the same?
729*8b26181fSAndroid Build Coastguard Worker */
730*8b26181fSAndroid Build Coastguard Worker same_error_for_all = 1;
731*8b26181fSAndroid Build Coastguard Worker first_error = addrs_to_try[0].errcode;
732*8b26181fSAndroid Build Coastguard Worker for (i = 1; i < numaddrinfos; i++)
733*8b26181fSAndroid Build Coastguard Worker {
734*8b26181fSAndroid Build Coastguard Worker if (addrs_to_try[i].errcode != first_error)
735*8b26181fSAndroid Build Coastguard Worker {
736*8b26181fSAndroid Build Coastguard Worker same_error_for_all = 0;
737*8b26181fSAndroid Build Coastguard Worker break;
738*8b26181fSAndroid Build Coastguard Worker }
739*8b26181fSAndroid Build Coastguard Worker }
740*8b26181fSAndroid Build Coastguard Worker
741*8b26181fSAndroid Build Coastguard Worker if (same_error_for_all) {
742*8b26181fSAndroid Build Coastguard Worker /*
743*8b26181fSAndroid Build Coastguard Worker * Yes. No need to show the IP
744*8b26181fSAndroid Build Coastguard Worker * addresses.
745*8b26181fSAndroid Build Coastguard Worker */
746*8b26181fSAndroid Build Coastguard Worker if (addrs_to_try[0].errtype == SOCK_CONNERR) {
747*8b26181fSAndroid Build Coastguard Worker /*
748*8b26181fSAndroid Build Coastguard Worker * Connection error; note that
749*8b26181fSAndroid Build Coastguard Worker * the daemon might not be set
750*8b26181fSAndroid Build Coastguard Worker * up correctly, or set up at all.
751*8b26181fSAndroid Build Coastguard Worker */
752*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbuf, errbuflen,
753*8b26181fSAndroid Build Coastguard Worker addrs_to_try[0].errcode,
754*8b26181fSAndroid Build Coastguard Worker "Is the server properly installed? Cannot connect to %s",
755*8b26181fSAndroid Build Coastguard Worker host);
756*8b26181fSAndroid Build Coastguard Worker } else {
757*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbuf, errbuflen,
758*8b26181fSAndroid Build Coastguard Worker addrs_to_try[0].errcode,
759*8b26181fSAndroid Build Coastguard Worker "Cannot connect to %s", host);
760*8b26181fSAndroid Build Coastguard Worker }
761*8b26181fSAndroid Build Coastguard Worker } else {
762*8b26181fSAndroid Build Coastguard Worker /*
763*8b26181fSAndroid Build Coastguard Worker * Show all the errors and the IP addresses
764*8b26181fSAndroid Build Coastguard Worker * to which they apply.
765*8b26181fSAndroid Build Coastguard Worker */
766*8b26181fSAndroid Build Coastguard Worker char *errbufptr;
767*8b26181fSAndroid Build Coastguard Worker size_t bufspaceleft;
768*8b26181fSAndroid Build Coastguard Worker size_t msglen;
769*8b26181fSAndroid Build Coastguard Worker
770*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
771*8b26181fSAndroid Build Coastguard Worker "Connect to %s failed: ", host);
772*8b26181fSAndroid Build Coastguard Worker
773*8b26181fSAndroid Build Coastguard Worker msglen = strlen(errbuf);
774*8b26181fSAndroid Build Coastguard Worker errbufptr = errbuf + msglen;
775*8b26181fSAndroid Build Coastguard Worker bufspaceleft = errbuflen - msglen;
776*8b26181fSAndroid Build Coastguard Worker
777*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < numaddrinfos &&
778*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errcode != SOCK_NOERR;
779*8b26181fSAndroid Build Coastguard Worker i++)
780*8b26181fSAndroid Build Coastguard Worker {
781*8b26181fSAndroid Build Coastguard Worker /*
782*8b26181fSAndroid Build Coastguard Worker * Get the string for the address
783*8b26181fSAndroid Build Coastguard Worker * and port that got this error.
784*8b26181fSAndroid Build Coastguard Worker */
785*8b26181fSAndroid Build Coastguard Worker sock_getascii_addrport((struct sockaddr_storage *) addrs_to_try[i].info->ai_addr,
786*8b26181fSAndroid Build Coastguard Worker errbufptr, (int)bufspaceleft,
787*8b26181fSAndroid Build Coastguard Worker NULL, 0, NI_NUMERICHOST, NULL, 0);
788*8b26181fSAndroid Build Coastguard Worker msglen = strlen(errbuf);
789*8b26181fSAndroid Build Coastguard Worker errbufptr = errbuf + msglen;
790*8b26181fSAndroid Build Coastguard Worker bufspaceleft = errbuflen - msglen;
791*8b26181fSAndroid Build Coastguard Worker
792*8b26181fSAndroid Build Coastguard Worker if (i + 1 < numaddrinfos &&
793*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i + 1].errcode == addrs_to_try[i].errcode)
794*8b26181fSAndroid Build Coastguard Worker {
795*8b26181fSAndroid Build Coastguard Worker /*
796*8b26181fSAndroid Build Coastguard Worker * There's another error
797*8b26181fSAndroid Build Coastguard Worker * after this, and it has
798*8b26181fSAndroid Build Coastguard Worker * the same error code.
799*8b26181fSAndroid Build Coastguard Worker *
800*8b26181fSAndroid Build Coastguard Worker * Append a comma, as the
801*8b26181fSAndroid Build Coastguard Worker * list of addresses with
802*8b26181fSAndroid Build Coastguard Worker * this error has another
803*8b26181fSAndroid Build Coastguard Worker * entry.
804*8b26181fSAndroid Build Coastguard Worker */
805*8b26181fSAndroid Build Coastguard Worker snprintf(errbufptr, bufspaceleft,
806*8b26181fSAndroid Build Coastguard Worker ", ");
807*8b26181fSAndroid Build Coastguard Worker }
808*8b26181fSAndroid Build Coastguard Worker else
809*8b26181fSAndroid Build Coastguard Worker {
810*8b26181fSAndroid Build Coastguard Worker /*
811*8b26181fSAndroid Build Coastguard Worker * Either there are no
812*8b26181fSAndroid Build Coastguard Worker * more errors after this,
813*8b26181fSAndroid Build Coastguard Worker * or the next error is
814*8b26181fSAndroid Build Coastguard Worker * different.
815*8b26181fSAndroid Build Coastguard Worker *
816*8b26181fSAndroid Build Coastguard Worker * Append a colon and
817*8b26181fSAndroid Build Coastguard Worker * the message for tis
818*8b26181fSAndroid Build Coastguard Worker * error, followed by a
819*8b26181fSAndroid Build Coastguard Worker * comma if there are
820*8b26181fSAndroid Build Coastguard Worker * more errors.
821*8b26181fSAndroid Build Coastguard Worker */
822*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbufptr,
823*8b26181fSAndroid Build Coastguard Worker bufspaceleft,
824*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i].errcode,
825*8b26181fSAndroid Build Coastguard Worker "%s", "");
826*8b26181fSAndroid Build Coastguard Worker msglen = strlen(errbuf);
827*8b26181fSAndroid Build Coastguard Worker errbufptr = errbuf + msglen;
828*8b26181fSAndroid Build Coastguard Worker bufspaceleft = errbuflen - msglen;
829*8b26181fSAndroid Build Coastguard Worker
830*8b26181fSAndroid Build Coastguard Worker if (i + 1 < numaddrinfos &&
831*8b26181fSAndroid Build Coastguard Worker addrs_to_try[i + 1].errcode != SOCK_NOERR)
832*8b26181fSAndroid Build Coastguard Worker {
833*8b26181fSAndroid Build Coastguard Worker /*
834*8b26181fSAndroid Build Coastguard Worker * More to come.
835*8b26181fSAndroid Build Coastguard Worker */
836*8b26181fSAndroid Build Coastguard Worker snprintf(errbufptr,
837*8b26181fSAndroid Build Coastguard Worker bufspaceleft,
838*8b26181fSAndroid Build Coastguard Worker ", ");
839*8b26181fSAndroid Build Coastguard Worker }
840*8b26181fSAndroid Build Coastguard Worker }
841*8b26181fSAndroid Build Coastguard Worker msglen = strlen(errbuf);
842*8b26181fSAndroid Build Coastguard Worker errbufptr = errbuf + msglen;
843*8b26181fSAndroid Build Coastguard Worker bufspaceleft = errbuflen - msglen;
844*8b26181fSAndroid Build Coastguard Worker }
845*8b26181fSAndroid Build Coastguard Worker }
846*8b26181fSAndroid Build Coastguard Worker free(addrs_to_try);
847*8b26181fSAndroid Build Coastguard Worker return INVALID_SOCKET;
848*8b26181fSAndroid Build Coastguard Worker }
849*8b26181fSAndroid Build Coastguard Worker else
850*8b26181fSAndroid Build Coastguard Worker {
851*8b26181fSAndroid Build Coastguard Worker free(addrs_to_try);
852*8b26181fSAndroid Build Coastguard Worker return sock;
853*8b26181fSAndroid Build Coastguard Worker }
854*8b26181fSAndroid Build Coastguard Worker }
855*8b26181fSAndroid Build Coastguard Worker }
856*8b26181fSAndroid Build Coastguard Worker
857*8b26181fSAndroid Build Coastguard Worker /*
858*8b26181fSAndroid Build Coastguard Worker * \brief Closes the present (TCP and UDP) socket connection.
859*8b26181fSAndroid Build Coastguard Worker *
860*8b26181fSAndroid Build Coastguard Worker * This function sends a shutdown() on the socket in order to disable send() calls
861*8b26181fSAndroid Build Coastguard Worker * (while recv() ones are still allowed). Then, it closes the socket.
862*8b26181fSAndroid Build Coastguard Worker *
863*8b26181fSAndroid Build Coastguard Worker * \param sock: the socket identifier of the connection that has to be closed.
864*8b26181fSAndroid Build Coastguard Worker *
865*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
866*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
867*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
868*8b26181fSAndroid Build Coastguard Worker *
869*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
870*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
871*8b26181fSAndroid Build Coastguard Worker *
872*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
873*8b26181fSAndroid Build Coastguard Worker * in the 'errbuf' variable.
874*8b26181fSAndroid Build Coastguard Worker */
sock_close(SOCKET sock,char * errbuf,int errbuflen)875*8b26181fSAndroid Build Coastguard Worker int sock_close(SOCKET sock, char *errbuf, int errbuflen)
876*8b26181fSAndroid Build Coastguard Worker {
877*8b26181fSAndroid Build Coastguard Worker /*
878*8b26181fSAndroid Build Coastguard Worker * SHUT_WR: subsequent calls to the send function are disallowed.
879*8b26181fSAndroid Build Coastguard Worker * For TCP sockets, a FIN will be sent after all data is sent and
880*8b26181fSAndroid Build Coastguard Worker * acknowledged by the Server.
881*8b26181fSAndroid Build Coastguard Worker */
882*8b26181fSAndroid Build Coastguard Worker if (shutdown(sock, SHUT_WR))
883*8b26181fSAndroid Build Coastguard Worker {
884*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "shutdown() feiled");
885*8b26181fSAndroid Build Coastguard Worker /* close the socket anyway */
886*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
887*8b26181fSAndroid Build Coastguard Worker return -1;
888*8b26181fSAndroid Build Coastguard Worker }
889*8b26181fSAndroid Build Coastguard Worker
890*8b26181fSAndroid Build Coastguard Worker closesocket(sock);
891*8b26181fSAndroid Build Coastguard Worker return 0;
892*8b26181fSAndroid Build Coastguard Worker }
893*8b26181fSAndroid Build Coastguard Worker
894*8b26181fSAndroid Build Coastguard Worker /*
895*8b26181fSAndroid Build Coastguard Worker * gai_strerror() has some problems:
896*8b26181fSAndroid Build Coastguard Worker *
897*8b26181fSAndroid Build Coastguard Worker * 1) on Windows, Microsoft explicitly says it's not thread-safe;
898*8b26181fSAndroid Build Coastguard Worker * 2) on UN*X, the Single UNIX Specification doesn't say it *is*
899*8b26181fSAndroid Build Coastguard Worker * thread-safe, so an implementation might use a static buffer
900*8b26181fSAndroid Build Coastguard Worker * for unknown error codes;
901*8b26181fSAndroid Build Coastguard Worker * 3) the error message for the most likely error, EAI_NONAME, is
902*8b26181fSAndroid Build Coastguard Worker * truly horrible on several platforms ("nodename nor servname
903*8b26181fSAndroid Build Coastguard Worker * provided, or not known"? It's typically going to be "not
904*8b26181fSAndroid Build Coastguard Worker * known", not "oopsie, I passed null pointers for the host name
905*8b26181fSAndroid Build Coastguard Worker * and service name", not to mention they forgot the "neither");
906*8b26181fSAndroid Build Coastguard Worker *
907*8b26181fSAndroid Build Coastguard Worker * so we roll our own.
908*8b26181fSAndroid Build Coastguard Worker */
909*8b26181fSAndroid Build Coastguard Worker static void
get_gai_errstring(char * errbuf,int errbuflen,const char * prefix,int err,const char * hostname,const char * portname)910*8b26181fSAndroid Build Coastguard Worker get_gai_errstring(char *errbuf, int errbuflen, const char *prefix, int err,
911*8b26181fSAndroid Build Coastguard Worker const char *hostname, const char *portname)
912*8b26181fSAndroid Build Coastguard Worker {
913*8b26181fSAndroid Build Coastguard Worker char hostport[PCAP_ERRBUF_SIZE];
914*8b26181fSAndroid Build Coastguard Worker
915*8b26181fSAndroid Build Coastguard Worker if (hostname != NULL && portname != NULL)
916*8b26181fSAndroid Build Coastguard Worker snprintf(hostport, PCAP_ERRBUF_SIZE, "host and port %s:%s",
917*8b26181fSAndroid Build Coastguard Worker hostname, portname);
918*8b26181fSAndroid Build Coastguard Worker else if (hostname != NULL)
919*8b26181fSAndroid Build Coastguard Worker snprintf(hostport, PCAP_ERRBUF_SIZE, "host %s",
920*8b26181fSAndroid Build Coastguard Worker hostname);
921*8b26181fSAndroid Build Coastguard Worker else if (portname != NULL)
922*8b26181fSAndroid Build Coastguard Worker snprintf(hostport, PCAP_ERRBUF_SIZE, "port %s",
923*8b26181fSAndroid Build Coastguard Worker portname);
924*8b26181fSAndroid Build Coastguard Worker else
925*8b26181fSAndroid Build Coastguard Worker snprintf(hostport, PCAP_ERRBUF_SIZE, "<no host or port!>");
926*8b26181fSAndroid Build Coastguard Worker switch (err)
927*8b26181fSAndroid Build Coastguard Worker {
928*8b26181fSAndroid Build Coastguard Worker #ifdef EAI_ADDRFAMILY
929*8b26181fSAndroid Build Coastguard Worker case EAI_ADDRFAMILY:
930*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
931*8b26181fSAndroid Build Coastguard Worker "%sAddress family for %s not supported",
932*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
933*8b26181fSAndroid Build Coastguard Worker break;
934*8b26181fSAndroid Build Coastguard Worker #endif
935*8b26181fSAndroid Build Coastguard Worker
936*8b26181fSAndroid Build Coastguard Worker case EAI_AGAIN:
937*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
938*8b26181fSAndroid Build Coastguard Worker "%s%s could not be resolved at this time",
939*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
940*8b26181fSAndroid Build Coastguard Worker break;
941*8b26181fSAndroid Build Coastguard Worker
942*8b26181fSAndroid Build Coastguard Worker case EAI_BADFLAGS:
943*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
944*8b26181fSAndroid Build Coastguard Worker "%sThe ai_flags parameter for looking up %s had an invalid value",
945*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
946*8b26181fSAndroid Build Coastguard Worker break;
947*8b26181fSAndroid Build Coastguard Worker
948*8b26181fSAndroid Build Coastguard Worker case EAI_FAIL:
949*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
950*8b26181fSAndroid Build Coastguard Worker "%sA non-recoverable error occurred when attempting to resolve %s",
951*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
952*8b26181fSAndroid Build Coastguard Worker break;
953*8b26181fSAndroid Build Coastguard Worker
954*8b26181fSAndroid Build Coastguard Worker case EAI_FAMILY:
955*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
956*8b26181fSAndroid Build Coastguard Worker "%sThe address family for looking up %s was not recognized",
957*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
958*8b26181fSAndroid Build Coastguard Worker break;
959*8b26181fSAndroid Build Coastguard Worker
960*8b26181fSAndroid Build Coastguard Worker case EAI_MEMORY:
961*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
962*8b26181fSAndroid Build Coastguard Worker "%sOut of memory trying to allocate storage when looking up %s",
963*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
964*8b26181fSAndroid Build Coastguard Worker break;
965*8b26181fSAndroid Build Coastguard Worker
966*8b26181fSAndroid Build Coastguard Worker /*
967*8b26181fSAndroid Build Coastguard Worker * RFC 2553 had both EAI_NODATA and EAI_NONAME.
968*8b26181fSAndroid Build Coastguard Worker *
969*8b26181fSAndroid Build Coastguard Worker * RFC 3493 has only EAI_NONAME.
970*8b26181fSAndroid Build Coastguard Worker *
971*8b26181fSAndroid Build Coastguard Worker * Some implementations define EAI_NODATA and EAI_NONAME
972*8b26181fSAndroid Build Coastguard Worker * to the same value, others don't. If EAI_NODATA is
973*8b26181fSAndroid Build Coastguard Worker * defined and isn't the same as EAI_NONAME, we handle
974*8b26181fSAndroid Build Coastguard Worker * EAI_NODATA.
975*8b26181fSAndroid Build Coastguard Worker */
976*8b26181fSAndroid Build Coastguard Worker #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
977*8b26181fSAndroid Build Coastguard Worker case EAI_NODATA:
978*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
979*8b26181fSAndroid Build Coastguard Worker "%sNo address associated with %s",
980*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
981*8b26181fSAndroid Build Coastguard Worker break;
982*8b26181fSAndroid Build Coastguard Worker #endif
983*8b26181fSAndroid Build Coastguard Worker
984*8b26181fSAndroid Build Coastguard Worker case EAI_NONAME:
985*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
986*8b26181fSAndroid Build Coastguard Worker "%sThe %s couldn't be resolved",
987*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
988*8b26181fSAndroid Build Coastguard Worker break;
989*8b26181fSAndroid Build Coastguard Worker
990*8b26181fSAndroid Build Coastguard Worker case EAI_SERVICE:
991*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
992*8b26181fSAndroid Build Coastguard Worker "%sThe service value specified when looking up %s as not recognized for the socket type",
993*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
994*8b26181fSAndroid Build Coastguard Worker break;
995*8b26181fSAndroid Build Coastguard Worker
996*8b26181fSAndroid Build Coastguard Worker case EAI_SOCKTYPE:
997*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
998*8b26181fSAndroid Build Coastguard Worker "%sThe socket type specified when looking up %s as not recognized",
999*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
1000*8b26181fSAndroid Build Coastguard Worker break;
1001*8b26181fSAndroid Build Coastguard Worker
1002*8b26181fSAndroid Build Coastguard Worker #ifdef EAI_SYSTEM
1003*8b26181fSAndroid Build Coastguard Worker case EAI_SYSTEM:
1004*8b26181fSAndroid Build Coastguard Worker /*
1005*8b26181fSAndroid Build Coastguard Worker * Assumed to be UN*X.
1006*8b26181fSAndroid Build Coastguard Worker */
1007*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, errbuflen, errno,
1008*8b26181fSAndroid Build Coastguard Worker "%sAn error occurred when looking up %s",
1009*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
1010*8b26181fSAndroid Build Coastguard Worker break;
1011*8b26181fSAndroid Build Coastguard Worker #endif
1012*8b26181fSAndroid Build Coastguard Worker
1013*8b26181fSAndroid Build Coastguard Worker #ifdef EAI_BADHINTS
1014*8b26181fSAndroid Build Coastguard Worker case EAI_BADHINTS:
1015*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1016*8b26181fSAndroid Build Coastguard Worker "%sInvalid value for hints when looking up %s",
1017*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
1018*8b26181fSAndroid Build Coastguard Worker break;
1019*8b26181fSAndroid Build Coastguard Worker #endif
1020*8b26181fSAndroid Build Coastguard Worker
1021*8b26181fSAndroid Build Coastguard Worker #ifdef EAI_PROTOCOL
1022*8b26181fSAndroid Build Coastguard Worker case EAI_PROTOCOL:
1023*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1024*8b26181fSAndroid Build Coastguard Worker "%sResolved protocol when looking up %s is unknown",
1025*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
1026*8b26181fSAndroid Build Coastguard Worker break;
1027*8b26181fSAndroid Build Coastguard Worker #endif
1028*8b26181fSAndroid Build Coastguard Worker
1029*8b26181fSAndroid Build Coastguard Worker #ifdef EAI_OVERFLOW
1030*8b26181fSAndroid Build Coastguard Worker case EAI_OVERFLOW:
1031*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1032*8b26181fSAndroid Build Coastguard Worker "%sArgument buffer overflow when looking up %s",
1033*8b26181fSAndroid Build Coastguard Worker prefix, hostport);
1034*8b26181fSAndroid Build Coastguard Worker break;
1035*8b26181fSAndroid Build Coastguard Worker #endif
1036*8b26181fSAndroid Build Coastguard Worker
1037*8b26181fSAndroid Build Coastguard Worker default:
1038*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1039*8b26181fSAndroid Build Coastguard Worker "%sgetaddrinfo() error %d when looking up %s",
1040*8b26181fSAndroid Build Coastguard Worker prefix, err, hostport);
1041*8b26181fSAndroid Build Coastguard Worker break;
1042*8b26181fSAndroid Build Coastguard Worker }
1043*8b26181fSAndroid Build Coastguard Worker }
1044*8b26181fSAndroid Build Coastguard Worker
1045*8b26181fSAndroid Build Coastguard Worker /*
1046*8b26181fSAndroid Build Coastguard Worker * \brief Checks that the address, port and flags given are valids and it returns an 'addrinfo' structure.
1047*8b26181fSAndroid Build Coastguard Worker *
1048*8b26181fSAndroid Build Coastguard Worker * This function basically calls the getaddrinfo() calls, and it performs a set of sanity checks
1049*8b26181fSAndroid Build Coastguard Worker * to control that everything is fine (e.g. a TCP socket cannot have a mcast address, and such).
1050*8b26181fSAndroid Build Coastguard Worker * If an error occurs, it writes the error message into 'errbuf'.
1051*8b26181fSAndroid Build Coastguard Worker *
1052*8b26181fSAndroid Build Coastguard Worker * \param host: a pointer to a string identifying the host. It can be
1053*8b26181fSAndroid Build Coastguard Worker * a host name, a numeric literal address, or NULL or "" (useful
1054*8b26181fSAndroid Build Coastguard Worker * in case of a server socket which has to bind to all addresses).
1055*8b26181fSAndroid Build Coastguard Worker *
1056*8b26181fSAndroid Build Coastguard Worker * \param port: a pointer to a user-allocated buffer containing the network port to use.
1057*8b26181fSAndroid Build Coastguard Worker *
1058*8b26181fSAndroid Build Coastguard Worker * \param hints: an addrinfo variable (passed by reference) containing the flags needed to create the
1059*8b26181fSAndroid Build Coastguard Worker * addrinfo structure appropriately.
1060*8b26181fSAndroid Build Coastguard Worker *
1061*8b26181fSAndroid Build Coastguard Worker * \param addrinfo: it represents the true returning value. This is a pointer to an addrinfo variable
1062*8b26181fSAndroid Build Coastguard Worker * (passed by reference), which will be allocated by this function and returned back to the caller.
1063*8b26181fSAndroid Build Coastguard Worker * This variable will be used in the next sockets calls.
1064*8b26181fSAndroid Build Coastguard Worker *
1065*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1066*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1067*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1068*8b26181fSAndroid Build Coastguard Worker *
1069*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1070*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1071*8b26181fSAndroid Build Coastguard Worker *
1072*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred. The error message is returned
1073*8b26181fSAndroid Build Coastguard Worker * in the 'errbuf' variable. The addrinfo variable that has to be used in the following sockets calls is
1074*8b26181fSAndroid Build Coastguard Worker * returned into the addrinfo parameter.
1075*8b26181fSAndroid Build Coastguard Worker *
1076*8b26181fSAndroid Build Coastguard Worker * \warning The 'addrinfo' variable has to be deleted by the programmer by calling freeaddrinfo() when
1077*8b26181fSAndroid Build Coastguard Worker * it is no longer needed.
1078*8b26181fSAndroid Build Coastguard Worker *
1079*8b26181fSAndroid Build Coastguard Worker * \warning This function requires the 'hints' variable as parameter. The semantic of this variable is the same
1080*8b26181fSAndroid Build Coastguard Worker * of the one of the corresponding variable used into the standard getaddrinfo() socket function. We suggest
1081*8b26181fSAndroid Build Coastguard Worker * the programmer to look at that function in order to set the 'hints' variable appropriately.
1082*8b26181fSAndroid Build Coastguard Worker */
sock_initaddress(const char * host,const char * port,struct addrinfo * hints,struct addrinfo ** addrinfo,char * errbuf,int errbuflen)1083*8b26181fSAndroid Build Coastguard Worker int sock_initaddress(const char *host, const char *port,
1084*8b26181fSAndroid Build Coastguard Worker struct addrinfo *hints, struct addrinfo **addrinfo, char *errbuf, int errbuflen)
1085*8b26181fSAndroid Build Coastguard Worker {
1086*8b26181fSAndroid Build Coastguard Worker int retval;
1087*8b26181fSAndroid Build Coastguard Worker
1088*8b26181fSAndroid Build Coastguard Worker /*
1089*8b26181fSAndroid Build Coastguard Worker * We allow both the host and port to be null, but getaddrinfo()
1090*8b26181fSAndroid Build Coastguard Worker * is not guaranteed to do so; to handle that, if port is null,
1091*8b26181fSAndroid Build Coastguard Worker * we provide "0" as the port number.
1092*8b26181fSAndroid Build Coastguard Worker *
1093*8b26181fSAndroid Build Coastguard Worker * This results in better error messages from get_gai_errstring(),
1094*8b26181fSAndroid Build Coastguard Worker * as those messages won't talk about a problem with the port if
1095*8b26181fSAndroid Build Coastguard Worker * no port was specified.
1096*8b26181fSAndroid Build Coastguard Worker */
1097*8b26181fSAndroid Build Coastguard Worker retval = getaddrinfo(host, port == NULL ? "0" : port, hints, addrinfo);
1098*8b26181fSAndroid Build Coastguard Worker if (retval != 0)
1099*8b26181fSAndroid Build Coastguard Worker {
1100*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1101*8b26181fSAndroid Build Coastguard Worker {
1102*8b26181fSAndroid Build Coastguard Worker if (host != NULL && port != NULL) {
1103*8b26181fSAndroid Build Coastguard Worker /*
1104*8b26181fSAndroid Build Coastguard Worker * Try with just a host, to distinguish
1105*8b26181fSAndroid Build Coastguard Worker * between "host is bad" and "port is
1106*8b26181fSAndroid Build Coastguard Worker * bad".
1107*8b26181fSAndroid Build Coastguard Worker */
1108*8b26181fSAndroid Build Coastguard Worker int try_retval;
1109*8b26181fSAndroid Build Coastguard Worker
1110*8b26181fSAndroid Build Coastguard Worker try_retval = getaddrinfo(host, NULL, hints,
1111*8b26181fSAndroid Build Coastguard Worker addrinfo);
1112*8b26181fSAndroid Build Coastguard Worker if (try_retval == 0) {
1113*8b26181fSAndroid Build Coastguard Worker /*
1114*8b26181fSAndroid Build Coastguard Worker * Worked with just the host,
1115*8b26181fSAndroid Build Coastguard Worker * so assume the problem is
1116*8b26181fSAndroid Build Coastguard Worker * with the port.
1117*8b26181fSAndroid Build Coastguard Worker *
1118*8b26181fSAndroid Build Coastguard Worker * Free up the address info first.
1119*8b26181fSAndroid Build Coastguard Worker */
1120*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(*addrinfo);
1121*8b26181fSAndroid Build Coastguard Worker get_gai_errstring(errbuf, errbuflen,
1122*8b26181fSAndroid Build Coastguard Worker "", retval, NULL, port);
1123*8b26181fSAndroid Build Coastguard Worker } else {
1124*8b26181fSAndroid Build Coastguard Worker /*
1125*8b26181fSAndroid Build Coastguard Worker * Didn't work with just the host,
1126*8b26181fSAndroid Build Coastguard Worker * so assume the problem is
1127*8b26181fSAndroid Build Coastguard Worker * with the host.
1128*8b26181fSAndroid Build Coastguard Worker */
1129*8b26181fSAndroid Build Coastguard Worker get_gai_errstring(errbuf, errbuflen,
1130*8b26181fSAndroid Build Coastguard Worker "", retval, host, NULL);
1131*8b26181fSAndroid Build Coastguard Worker }
1132*8b26181fSAndroid Build Coastguard Worker } else {
1133*8b26181fSAndroid Build Coastguard Worker /*
1134*8b26181fSAndroid Build Coastguard Worker * Either the host or port was null, so
1135*8b26181fSAndroid Build Coastguard Worker * there's nothing to determine.
1136*8b26181fSAndroid Build Coastguard Worker */
1137*8b26181fSAndroid Build Coastguard Worker get_gai_errstring(errbuf, errbuflen, "",
1138*8b26181fSAndroid Build Coastguard Worker retval, host, port);
1139*8b26181fSAndroid Build Coastguard Worker }
1140*8b26181fSAndroid Build Coastguard Worker }
1141*8b26181fSAndroid Build Coastguard Worker return -1;
1142*8b26181fSAndroid Build Coastguard Worker }
1143*8b26181fSAndroid Build Coastguard Worker /*
1144*8b26181fSAndroid Build Coastguard Worker * \warning SOCKET: I should check all the accept() in order to bind to all addresses in case
1145*8b26181fSAndroid Build Coastguard Worker * addrinfo has more han one pointers
1146*8b26181fSAndroid Build Coastguard Worker */
1147*8b26181fSAndroid Build Coastguard Worker
1148*8b26181fSAndroid Build Coastguard Worker /*
1149*8b26181fSAndroid Build Coastguard Worker * This software only supports PF_INET and PF_INET6.
1150*8b26181fSAndroid Build Coastguard Worker *
1151*8b26181fSAndroid Build Coastguard Worker * XXX - should we just check that at least *one* address is
1152*8b26181fSAndroid Build Coastguard Worker * either PF_INET or PF_INET6, and, when using the list,
1153*8b26181fSAndroid Build Coastguard Worker * ignore all addresses that are neither? (What, no IPX
1154*8b26181fSAndroid Build Coastguard Worker * support? :-))
1155*8b26181fSAndroid Build Coastguard Worker */
1156*8b26181fSAndroid Build Coastguard Worker if (((*addrinfo)->ai_family != PF_INET) &&
1157*8b26181fSAndroid Build Coastguard Worker ((*addrinfo)->ai_family != PF_INET6))
1158*8b26181fSAndroid Build Coastguard Worker {
1159*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1160*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "getaddrinfo(): socket type not supported");
1161*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(*addrinfo);
1162*8b26181fSAndroid Build Coastguard Worker *addrinfo = NULL;
1163*8b26181fSAndroid Build Coastguard Worker return -1;
1164*8b26181fSAndroid Build Coastguard Worker }
1165*8b26181fSAndroid Build Coastguard Worker
1166*8b26181fSAndroid Build Coastguard Worker /*
1167*8b26181fSAndroid Build Coastguard Worker * You can't do multicast (or broadcast) TCP.
1168*8b26181fSAndroid Build Coastguard Worker */
1169*8b26181fSAndroid Build Coastguard Worker if (((*addrinfo)->ai_socktype == SOCK_STREAM) &&
1170*8b26181fSAndroid Build Coastguard Worker (sock_ismcastaddr((*addrinfo)->ai_addr) == 0))
1171*8b26181fSAndroid Build Coastguard Worker {
1172*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1173*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "getaddrinfo(): multicast addresses are not valid when using TCP streams");
1174*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(*addrinfo);
1175*8b26181fSAndroid Build Coastguard Worker *addrinfo = NULL;
1176*8b26181fSAndroid Build Coastguard Worker return -1;
1177*8b26181fSAndroid Build Coastguard Worker }
1178*8b26181fSAndroid Build Coastguard Worker
1179*8b26181fSAndroid Build Coastguard Worker return 0;
1180*8b26181fSAndroid Build Coastguard Worker }
1181*8b26181fSAndroid Build Coastguard Worker
1182*8b26181fSAndroid Build Coastguard Worker /*
1183*8b26181fSAndroid Build Coastguard Worker * \brief It sends the amount of data contained into 'buffer' on the given socket.
1184*8b26181fSAndroid Build Coastguard Worker *
1185*8b26181fSAndroid Build Coastguard Worker * This function basically calls the send() socket function and it checks that all
1186*8b26181fSAndroid Build Coastguard Worker * the data specified in 'buffer' (of size 'size') will be sent. If an error occurs,
1187*8b26181fSAndroid Build Coastguard Worker * it writes the error message into 'errbuf'.
1188*8b26181fSAndroid Build Coastguard Worker * In case the socket buffer does not have enough space, it loops until all data
1189*8b26181fSAndroid Build Coastguard Worker * has been sent.
1190*8b26181fSAndroid Build Coastguard Worker *
1191*8b26181fSAndroid Build Coastguard Worker * \param socket: the connected socket currently opened.
1192*8b26181fSAndroid Build Coastguard Worker *
1193*8b26181fSAndroid Build Coastguard Worker * \param buffer: a char pointer to a user-allocated buffer in which data is contained.
1194*8b26181fSAndroid Build Coastguard Worker *
1195*8b26181fSAndroid Build Coastguard Worker * \param size: number of bytes that have to be sent.
1196*8b26181fSAndroid Build Coastguard Worker *
1197*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1198*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1199*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1200*8b26181fSAndroid Build Coastguard Worker *
1201*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1202*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1203*8b26181fSAndroid Build Coastguard Worker *
1204*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if an error other than
1205*8b26181fSAndroid Build Coastguard Worker * "connection reset" or "peer has closed the receive side" occurred,
1206*8b26181fSAndroid Build Coastguard Worker * '-2' if we got one of those errors.
1207*8b26181fSAndroid Build Coastguard Worker * For errors, an error message is returned in the 'errbuf' variable.
1208*8b26181fSAndroid Build Coastguard Worker */
sock_send(SOCKET sock,SSL * ssl _U_NOSSL_,const char * buffer,size_t size,char * errbuf,int errbuflen)1209*8b26181fSAndroid Build Coastguard Worker int sock_send(SOCKET sock, SSL *ssl _U_NOSSL_, const char *buffer, size_t size,
1210*8b26181fSAndroid Build Coastguard Worker char *errbuf, int errbuflen)
1211*8b26181fSAndroid Build Coastguard Worker {
1212*8b26181fSAndroid Build Coastguard Worker int remaining;
1213*8b26181fSAndroid Build Coastguard Worker ssize_t nsent;
1214*8b26181fSAndroid Build Coastguard Worker
1215*8b26181fSAndroid Build Coastguard Worker if (size > INT_MAX)
1216*8b26181fSAndroid Build Coastguard Worker {
1217*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1218*8b26181fSAndroid Build Coastguard Worker {
1219*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1220*8b26181fSAndroid Build Coastguard Worker "Can't send more than %u bytes with sock_send",
1221*8b26181fSAndroid Build Coastguard Worker INT_MAX);
1222*8b26181fSAndroid Build Coastguard Worker }
1223*8b26181fSAndroid Build Coastguard Worker return -1;
1224*8b26181fSAndroid Build Coastguard Worker }
1225*8b26181fSAndroid Build Coastguard Worker remaining = (int)size;
1226*8b26181fSAndroid Build Coastguard Worker
1227*8b26181fSAndroid Build Coastguard Worker do {
1228*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1229*8b26181fSAndroid Build Coastguard Worker if (ssl) return ssl_send(ssl, buffer, remaining, errbuf, errbuflen);
1230*8b26181fSAndroid Build Coastguard Worker #endif
1231*8b26181fSAndroid Build Coastguard Worker
1232*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1233*8b26181fSAndroid Build Coastguard Worker nsent = remaining;
1234*8b26181fSAndroid Build Coastguard Worker #else
1235*8b26181fSAndroid Build Coastguard Worker #ifdef MSG_NOSIGNAL
1236*8b26181fSAndroid Build Coastguard Worker /*
1237*8b26181fSAndroid Build Coastguard Worker * Send with MSG_NOSIGNAL, so that we don't get SIGPIPE
1238*8b26181fSAndroid Build Coastguard Worker * on errors on stream-oriented sockets when the other
1239*8b26181fSAndroid Build Coastguard Worker * end breaks the connection.
1240*8b26181fSAndroid Build Coastguard Worker * The EPIPE error is still returned.
1241*8b26181fSAndroid Build Coastguard Worker */
1242*8b26181fSAndroid Build Coastguard Worker nsent = send(sock, buffer, remaining, MSG_NOSIGNAL);
1243*8b26181fSAndroid Build Coastguard Worker #else
1244*8b26181fSAndroid Build Coastguard Worker nsent = send(sock, buffer, remaining, 0);
1245*8b26181fSAndroid Build Coastguard Worker #endif
1246*8b26181fSAndroid Build Coastguard Worker #endif //FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1247*8b26181fSAndroid Build Coastguard Worker
1248*8b26181fSAndroid Build Coastguard Worker if (nsent == -1)
1249*8b26181fSAndroid Build Coastguard Worker {
1250*8b26181fSAndroid Build Coastguard Worker /*
1251*8b26181fSAndroid Build Coastguard Worker * If the client closed the connection out from
1252*8b26181fSAndroid Build Coastguard Worker * under us, there's no need to log that as an
1253*8b26181fSAndroid Build Coastguard Worker * error.
1254*8b26181fSAndroid Build Coastguard Worker */
1255*8b26181fSAndroid Build Coastguard Worker int errcode;
1256*8b26181fSAndroid Build Coastguard Worker
1257*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
1258*8b26181fSAndroid Build Coastguard Worker errcode = GetLastError();
1259*8b26181fSAndroid Build Coastguard Worker if (errcode == WSAECONNRESET ||
1260*8b26181fSAndroid Build Coastguard Worker errcode == WSAECONNABORTED)
1261*8b26181fSAndroid Build Coastguard Worker {
1262*8b26181fSAndroid Build Coastguard Worker /*
1263*8b26181fSAndroid Build Coastguard Worker * WSAECONNABORTED appears to be the error
1264*8b26181fSAndroid Build Coastguard Worker * returned in Winsock when you try to send
1265*8b26181fSAndroid Build Coastguard Worker * on a connection where the peer has closed
1266*8b26181fSAndroid Build Coastguard Worker * the receive side.
1267*8b26181fSAndroid Build Coastguard Worker */
1268*8b26181fSAndroid Build Coastguard Worker return -2;
1269*8b26181fSAndroid Build Coastguard Worker }
1270*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbuf, errbuflen, errcode,
1271*8b26181fSAndroid Build Coastguard Worker "send() failed");
1272*8b26181fSAndroid Build Coastguard Worker #else
1273*8b26181fSAndroid Build Coastguard Worker errcode = errno;
1274*8b26181fSAndroid Build Coastguard Worker if (errcode == ECONNRESET || errcode == EPIPE)
1275*8b26181fSAndroid Build Coastguard Worker {
1276*8b26181fSAndroid Build Coastguard Worker /*
1277*8b26181fSAndroid Build Coastguard Worker * EPIPE is what's returned on UN*X when
1278*8b26181fSAndroid Build Coastguard Worker * you try to send on a connection when
1279*8b26181fSAndroid Build Coastguard Worker * the peer has closed the receive side.
1280*8b26181fSAndroid Build Coastguard Worker */
1281*8b26181fSAndroid Build Coastguard Worker return -2;
1282*8b26181fSAndroid Build Coastguard Worker }
1283*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbuf, errbuflen, errcode,
1284*8b26181fSAndroid Build Coastguard Worker "send() failed");
1285*8b26181fSAndroid Build Coastguard Worker #endif
1286*8b26181fSAndroid Build Coastguard Worker return -1;
1287*8b26181fSAndroid Build Coastguard Worker }
1288*8b26181fSAndroid Build Coastguard Worker
1289*8b26181fSAndroid Build Coastguard Worker remaining -= nsent;
1290*8b26181fSAndroid Build Coastguard Worker buffer += nsent;
1291*8b26181fSAndroid Build Coastguard Worker } while (remaining != 0);
1292*8b26181fSAndroid Build Coastguard Worker
1293*8b26181fSAndroid Build Coastguard Worker return 0;
1294*8b26181fSAndroid Build Coastguard Worker }
1295*8b26181fSAndroid Build Coastguard Worker
1296*8b26181fSAndroid Build Coastguard Worker /*
1297*8b26181fSAndroid Build Coastguard Worker * \brief It copies the amount of data contained in 'data' into 'outbuf'.
1298*8b26181fSAndroid Build Coastguard Worker * and it checks for buffer overflows.
1299*8b26181fSAndroid Build Coastguard Worker *
1300*8b26181fSAndroid Build Coastguard Worker * This function basically copies 'size' bytes of data contained in 'data'
1301*8b26181fSAndroid Build Coastguard Worker * into 'outbuf', starting at offset 'offset'. Before that, it checks that the
1302*8b26181fSAndroid Build Coastguard Worker * resulting buffer will not be larger than 'totsize'. Finally, it updates
1303*8b26181fSAndroid Build Coastguard Worker * the 'offset' variable in order to point to the first empty location of the buffer.
1304*8b26181fSAndroid Build Coastguard Worker *
1305*8b26181fSAndroid Build Coastguard Worker * In case the function is called with 'checkonly' equal to 1, it does not copy
1306*8b26181fSAndroid Build Coastguard Worker * the data into the buffer. It only checks for buffer overflows and it updates the
1307*8b26181fSAndroid Build Coastguard Worker * 'offset' variable. This mode can be useful when the buffer already contains the
1308*8b26181fSAndroid Build Coastguard Worker * data (maybe because the producer writes directly into the target buffer), so
1309*8b26181fSAndroid Build Coastguard Worker * only the buffer overflow check has to be made.
1310*8b26181fSAndroid Build Coastguard Worker * In this case, both 'data' and 'outbuf' can be NULL values.
1311*8b26181fSAndroid Build Coastguard Worker *
1312*8b26181fSAndroid Build Coastguard Worker * This function is useful in case the userland application does not know immediately
1313*8b26181fSAndroid Build Coastguard Worker * all the data it has to write into the socket. This function provides a way to create
1314*8b26181fSAndroid Build Coastguard Worker * the "stream" step by step, appending the new data to the old one. Then, when all the
1315*8b26181fSAndroid Build Coastguard Worker * data has been bufferized, the application can call the sock_send() function.
1316*8b26181fSAndroid Build Coastguard Worker *
1317*8b26181fSAndroid Build Coastguard Worker * \param data: a void pointer to the data that has to be copied.
1318*8b26181fSAndroid Build Coastguard Worker *
1319*8b26181fSAndroid Build Coastguard Worker * \param size: number of bytes that have to be copied.
1320*8b26181fSAndroid Build Coastguard Worker *
1321*8b26181fSAndroid Build Coastguard Worker * \param outbuf: user-allocated buffer (of size 'totsize') into which data
1322*8b26181fSAndroid Build Coastguard Worker * has to be copied.
1323*8b26181fSAndroid Build Coastguard Worker *
1324*8b26181fSAndroid Build Coastguard Worker * \param offset: an index into 'outbuf' which keeps the location of its first
1325*8b26181fSAndroid Build Coastguard Worker * empty location.
1326*8b26181fSAndroid Build Coastguard Worker *
1327*8b26181fSAndroid Build Coastguard Worker * \param totsize: total size of the buffer into which data is being copied.
1328*8b26181fSAndroid Build Coastguard Worker *
1329*8b26181fSAndroid Build Coastguard Worker * \param checkonly: '1' if we do not want to copy data into the buffer and we
1330*8b26181fSAndroid Build Coastguard Worker * want just do a buffer ovreflow control, '0' if data has to be copied as well.
1331*8b26181fSAndroid Build Coastguard Worker *
1332*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1333*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1334*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1335*8b26181fSAndroid Build Coastguard Worker *
1336*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1337*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1338*8b26181fSAndroid Build Coastguard Worker *
1339*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred. The error message
1340*8b26181fSAndroid Build Coastguard Worker * is returned in the 'errbuf' variable. When the function returns, 'outbuf' will
1341*8b26181fSAndroid Build Coastguard Worker * have the new string appended, and 'offset' will keep the length of that buffer.
1342*8b26181fSAndroid Build Coastguard Worker * In case of 'checkonly == 1', data is not copied, but 'offset' is updated in any case.
1343*8b26181fSAndroid Build Coastguard Worker *
1344*8b26181fSAndroid Build Coastguard Worker * \warning This function assumes that the buffer in which data has to be stored is
1345*8b26181fSAndroid Build Coastguard Worker * large 'totbuf' bytes.
1346*8b26181fSAndroid Build Coastguard Worker *
1347*8b26181fSAndroid Build Coastguard Worker * \warning In case of 'checkonly', be carefully to call this function *before* copying
1348*8b26181fSAndroid Build Coastguard Worker * the data into the buffer. Otherwise, the control about the buffer overflow is useless.
1349*8b26181fSAndroid Build Coastguard Worker */
sock_bufferize(const void * data,int size,char * outbuf,int * offset,int totsize,int checkonly,char * errbuf,int errbuflen)1350*8b26181fSAndroid Build Coastguard Worker int sock_bufferize(const void *data, int size, char *outbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen)
1351*8b26181fSAndroid Build Coastguard Worker {
1352*8b26181fSAndroid Build Coastguard Worker if ((*offset + size) > totsize)
1353*8b26181fSAndroid Build Coastguard Worker {
1354*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1355*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "Not enough space in the temporary send buffer.");
1356*8b26181fSAndroid Build Coastguard Worker return -1;
1357*8b26181fSAndroid Build Coastguard Worker }
1358*8b26181fSAndroid Build Coastguard Worker
1359*8b26181fSAndroid Build Coastguard Worker if (!checkonly)
1360*8b26181fSAndroid Build Coastguard Worker memcpy(outbuf + (*offset), data, size);
1361*8b26181fSAndroid Build Coastguard Worker
1362*8b26181fSAndroid Build Coastguard Worker (*offset) += size;
1363*8b26181fSAndroid Build Coastguard Worker
1364*8b26181fSAndroid Build Coastguard Worker return 0;
1365*8b26181fSAndroid Build Coastguard Worker }
1366*8b26181fSAndroid Build Coastguard Worker
1367*8b26181fSAndroid Build Coastguard Worker /*
1368*8b26181fSAndroid Build Coastguard Worker * \brief It waits on a connected socket and it manages to receive data.
1369*8b26181fSAndroid Build Coastguard Worker *
1370*8b26181fSAndroid Build Coastguard Worker * This function basically calls the recv() socket function and it checks that no
1371*8b26181fSAndroid Build Coastguard Worker * error occurred. If that happens, it writes the error message into 'errbuf'.
1372*8b26181fSAndroid Build Coastguard Worker *
1373*8b26181fSAndroid Build Coastguard Worker * This function changes its behavior according to the 'receiveall' flag: if we
1374*8b26181fSAndroid Build Coastguard Worker * want to receive exactly 'size' byte, it loops on the recv() until all the requested
1375*8b26181fSAndroid Build Coastguard Worker * data is arrived. Otherwise, it returns the data currently available.
1376*8b26181fSAndroid Build Coastguard Worker *
1377*8b26181fSAndroid Build Coastguard Worker * In case the socket does not have enough data available, it cycles on the recv()
1378*8b26181fSAndroid Build Coastguard Worker * until the requested data (of size 'size') is arrived.
1379*8b26181fSAndroid Build Coastguard Worker * In this case, it blocks until the number of bytes read is equal to 'size'.
1380*8b26181fSAndroid Build Coastguard Worker *
1381*8b26181fSAndroid Build Coastguard Worker * \param sock: the connected socket currently opened.
1382*8b26181fSAndroid Build Coastguard Worker *
1383*8b26181fSAndroid Build Coastguard Worker * \param buffer: a char pointer to a user-allocated buffer in which data has to be stored
1384*8b26181fSAndroid Build Coastguard Worker *
1385*8b26181fSAndroid Build Coastguard Worker * \param size: size of the allocated buffer. WARNING: this indicates the number of bytes
1386*8b26181fSAndroid Build Coastguard Worker * that we are expecting to be read.
1387*8b26181fSAndroid Build Coastguard Worker *
1388*8b26181fSAndroid Build Coastguard Worker * \param flags:
1389*8b26181fSAndroid Build Coastguard Worker *
1390*8b26181fSAndroid Build Coastguard Worker * SOCK_RECEIVALL_XXX:
1391*8b26181fSAndroid Build Coastguard Worker *
1392*8b26181fSAndroid Build Coastguard Worker * if SOCK_RECEIVEALL_NO, return as soon as some data is ready
1393*8b26181fSAndroid Build Coastguard Worker * if SOCK_RECEIVALL_YES, wait until 'size' data has been
1394*8b26181fSAndroid Build Coastguard Worker * received (in case the socket does not have enough data available).
1395*8b26181fSAndroid Build Coastguard Worker *
1396*8b26181fSAndroid Build Coastguard Worker * SOCK_EOF_XXX:
1397*8b26181fSAndroid Build Coastguard Worker *
1398*8b26181fSAndroid Build Coastguard Worker * if SOCK_EOF_ISNT_ERROR, if the first read returns 0, just return 0,
1399*8b26181fSAndroid Build Coastguard Worker * and return an error on any subsequent read that returns 0;
1400*8b26181fSAndroid Build Coastguard Worker * if SOCK_EOF_IS_ERROR, if any read returns 0, return an error.
1401*8b26181fSAndroid Build Coastguard Worker *
1402*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1403*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1404*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1405*8b26181fSAndroid Build Coastguard Worker *
1406*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1407*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1408*8b26181fSAndroid Build Coastguard Worker *
1409*8b26181fSAndroid Build Coastguard Worker * \return the number of bytes read if everything is fine, '-1' if some errors occurred.
1410*8b26181fSAndroid Build Coastguard Worker * The error message is returned in the 'errbuf' variable.
1411*8b26181fSAndroid Build Coastguard Worker */
1412*8b26181fSAndroid Build Coastguard Worker
sock_recv(SOCKET sock,SSL * ssl _U_NOSSL_,void * buffer,size_t size,int flags,char * errbuf,int errbuflen)1413*8b26181fSAndroid Build Coastguard Worker int sock_recv(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
1414*8b26181fSAndroid Build Coastguard Worker int flags, char *errbuf, int errbuflen)
1415*8b26181fSAndroid Build Coastguard Worker {
1416*8b26181fSAndroid Build Coastguard Worker int recv_flags = 0;
1417*8b26181fSAndroid Build Coastguard Worker char *bufp = buffer;
1418*8b26181fSAndroid Build Coastguard Worker int remaining;
1419*8b26181fSAndroid Build Coastguard Worker ssize_t nread;
1420*8b26181fSAndroid Build Coastguard Worker
1421*8b26181fSAndroid Build Coastguard Worker if (size == 0)
1422*8b26181fSAndroid Build Coastguard Worker {
1423*8b26181fSAndroid Build Coastguard Worker return 0;
1424*8b26181fSAndroid Build Coastguard Worker }
1425*8b26181fSAndroid Build Coastguard Worker if (size > INT_MAX)
1426*8b26181fSAndroid Build Coastguard Worker {
1427*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1428*8b26181fSAndroid Build Coastguard Worker {
1429*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1430*8b26181fSAndroid Build Coastguard Worker "Can't read more than %u bytes with sock_recv",
1431*8b26181fSAndroid Build Coastguard Worker INT_MAX);
1432*8b26181fSAndroid Build Coastguard Worker }
1433*8b26181fSAndroid Build Coastguard Worker return -1;
1434*8b26181fSAndroid Build Coastguard Worker }
1435*8b26181fSAndroid Build Coastguard Worker
1436*8b26181fSAndroid Build Coastguard Worker if (flags & SOCK_MSG_PEEK)
1437*8b26181fSAndroid Build Coastguard Worker recv_flags |= MSG_PEEK;
1438*8b26181fSAndroid Build Coastguard Worker
1439*8b26181fSAndroid Build Coastguard Worker bufp = (char *) buffer;
1440*8b26181fSAndroid Build Coastguard Worker remaining = (int) size;
1441*8b26181fSAndroid Build Coastguard Worker
1442*8b26181fSAndroid Build Coastguard Worker /*
1443*8b26181fSAndroid Build Coastguard Worker * We don't use MSG_WAITALL because it's not supported in
1444*8b26181fSAndroid Build Coastguard Worker * Win32.
1445*8b26181fSAndroid Build Coastguard Worker */
1446*8b26181fSAndroid Build Coastguard Worker for (;;) {
1447*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1448*8b26181fSAndroid Build Coastguard Worker nread = fuzz_recv(bufp, remaining);
1449*8b26181fSAndroid Build Coastguard Worker #elif defined(HAVE_OPENSSL)
1450*8b26181fSAndroid Build Coastguard Worker if (ssl)
1451*8b26181fSAndroid Build Coastguard Worker {
1452*8b26181fSAndroid Build Coastguard Worker /*
1453*8b26181fSAndroid Build Coastguard Worker * XXX - what about MSG_PEEK?
1454*8b26181fSAndroid Build Coastguard Worker */
1455*8b26181fSAndroid Build Coastguard Worker nread = ssl_recv(ssl, bufp, remaining, errbuf, errbuflen);
1456*8b26181fSAndroid Build Coastguard Worker if (nread == -2) return -1;
1457*8b26181fSAndroid Build Coastguard Worker }
1458*8b26181fSAndroid Build Coastguard Worker else
1459*8b26181fSAndroid Build Coastguard Worker nread = recv(sock, bufp, remaining, recv_flags);
1460*8b26181fSAndroid Build Coastguard Worker #else
1461*8b26181fSAndroid Build Coastguard Worker nread = recv(sock, bufp, remaining, recv_flags);
1462*8b26181fSAndroid Build Coastguard Worker #endif
1463*8b26181fSAndroid Build Coastguard Worker
1464*8b26181fSAndroid Build Coastguard Worker if (nread == -1)
1465*8b26181fSAndroid Build Coastguard Worker {
1466*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
1467*8b26181fSAndroid Build Coastguard Worker if (errno == EINTR)
1468*8b26181fSAndroid Build Coastguard Worker return -3;
1469*8b26181fSAndroid Build Coastguard Worker #endif
1470*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "recv() failed");
1471*8b26181fSAndroid Build Coastguard Worker return -1;
1472*8b26181fSAndroid Build Coastguard Worker }
1473*8b26181fSAndroid Build Coastguard Worker
1474*8b26181fSAndroid Build Coastguard Worker if (nread == 0)
1475*8b26181fSAndroid Build Coastguard Worker {
1476*8b26181fSAndroid Build Coastguard Worker if ((flags & SOCK_EOF_IS_ERROR) ||
1477*8b26181fSAndroid Build Coastguard Worker (remaining != (int) size))
1478*8b26181fSAndroid Build Coastguard Worker {
1479*8b26181fSAndroid Build Coastguard Worker /*
1480*8b26181fSAndroid Build Coastguard Worker * Either we've already read some data,
1481*8b26181fSAndroid Build Coastguard Worker * or we're always supposed to return
1482*8b26181fSAndroid Build Coastguard Worker * an error on EOF.
1483*8b26181fSAndroid Build Coastguard Worker */
1484*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1485*8b26181fSAndroid Build Coastguard Worker {
1486*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1487*8b26181fSAndroid Build Coastguard Worker "The other host terminated the connection.");
1488*8b26181fSAndroid Build Coastguard Worker }
1489*8b26181fSAndroid Build Coastguard Worker return -1;
1490*8b26181fSAndroid Build Coastguard Worker }
1491*8b26181fSAndroid Build Coastguard Worker else
1492*8b26181fSAndroid Build Coastguard Worker return 0;
1493*8b26181fSAndroid Build Coastguard Worker }
1494*8b26181fSAndroid Build Coastguard Worker
1495*8b26181fSAndroid Build Coastguard Worker /*
1496*8b26181fSAndroid Build Coastguard Worker * Do we want to read the amount requested, or just return
1497*8b26181fSAndroid Build Coastguard Worker * what we got?
1498*8b26181fSAndroid Build Coastguard Worker */
1499*8b26181fSAndroid Build Coastguard Worker if (!(flags & SOCK_RECEIVEALL_YES))
1500*8b26181fSAndroid Build Coastguard Worker {
1501*8b26181fSAndroid Build Coastguard Worker /*
1502*8b26181fSAndroid Build Coastguard Worker * Just return what we got.
1503*8b26181fSAndroid Build Coastguard Worker */
1504*8b26181fSAndroid Build Coastguard Worker return (int) nread;
1505*8b26181fSAndroid Build Coastguard Worker }
1506*8b26181fSAndroid Build Coastguard Worker
1507*8b26181fSAndroid Build Coastguard Worker bufp += nread;
1508*8b26181fSAndroid Build Coastguard Worker remaining -= nread;
1509*8b26181fSAndroid Build Coastguard Worker
1510*8b26181fSAndroid Build Coastguard Worker if (remaining == 0)
1511*8b26181fSAndroid Build Coastguard Worker return (int) size;
1512*8b26181fSAndroid Build Coastguard Worker }
1513*8b26181fSAndroid Build Coastguard Worker }
1514*8b26181fSAndroid Build Coastguard Worker
1515*8b26181fSAndroid Build Coastguard Worker /*
1516*8b26181fSAndroid Build Coastguard Worker * Receives a datagram from a socket.
1517*8b26181fSAndroid Build Coastguard Worker *
1518*8b26181fSAndroid Build Coastguard Worker * Returns the size of the datagram on success or -1 on error.
1519*8b26181fSAndroid Build Coastguard Worker */
sock_recv_dgram(SOCKET sock,SSL * ssl _U_NOSSL_,void * buffer,size_t size,char * errbuf,int errbuflen)1520*8b26181fSAndroid Build Coastguard Worker int sock_recv_dgram(SOCKET sock, SSL *ssl _U_NOSSL_, void *buffer, size_t size,
1521*8b26181fSAndroid Build Coastguard Worker char *errbuf, int errbuflen)
1522*8b26181fSAndroid Build Coastguard Worker {
1523*8b26181fSAndroid Build Coastguard Worker ssize_t nread;
1524*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
1525*8b26181fSAndroid Build Coastguard Worker struct msghdr message;
1526*8b26181fSAndroid Build Coastguard Worker struct iovec iov;
1527*8b26181fSAndroid Build Coastguard Worker #endif
1528*8b26181fSAndroid Build Coastguard Worker
1529*8b26181fSAndroid Build Coastguard Worker if (size == 0)
1530*8b26181fSAndroid Build Coastguard Worker {
1531*8b26181fSAndroid Build Coastguard Worker return 0;
1532*8b26181fSAndroid Build Coastguard Worker }
1533*8b26181fSAndroid Build Coastguard Worker if (size > INT_MAX)
1534*8b26181fSAndroid Build Coastguard Worker {
1535*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1536*8b26181fSAndroid Build Coastguard Worker {
1537*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen,
1538*8b26181fSAndroid Build Coastguard Worker "Can't read more than %u bytes with sock_recv_dgram",
1539*8b26181fSAndroid Build Coastguard Worker INT_MAX);
1540*8b26181fSAndroid Build Coastguard Worker }
1541*8b26181fSAndroid Build Coastguard Worker return -1;
1542*8b26181fSAndroid Build Coastguard Worker }
1543*8b26181fSAndroid Build Coastguard Worker
1544*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OPENSSL
1545*8b26181fSAndroid Build Coastguard Worker // TODO: DTLS
1546*8b26181fSAndroid Build Coastguard Worker if (ssl)
1547*8b26181fSAndroid Build Coastguard Worker {
1548*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "DTLS not implemented yet");
1549*8b26181fSAndroid Build Coastguard Worker return -1;
1550*8b26181fSAndroid Build Coastguard Worker }
1551*8b26181fSAndroid Build Coastguard Worker #endif
1552*8b26181fSAndroid Build Coastguard Worker
1553*8b26181fSAndroid Build Coastguard Worker /*
1554*8b26181fSAndroid Build Coastguard Worker * This should be a datagram socket, so we should get the
1555*8b26181fSAndroid Build Coastguard Worker * entire datagram in one recv() or recvmsg() call, and
1556*8b26181fSAndroid Build Coastguard Worker * don't need to loop.
1557*8b26181fSAndroid Build Coastguard Worker */
1558*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
1559*8b26181fSAndroid Build Coastguard Worker nread = recv(sock, buffer, (int)size, 0);
1560*8b26181fSAndroid Build Coastguard Worker if (nread == SOCKET_ERROR)
1561*8b26181fSAndroid Build Coastguard Worker {
1562*8b26181fSAndroid Build Coastguard Worker /*
1563*8b26181fSAndroid Build Coastguard Worker * To quote the MSDN documentation for recv(),
1564*8b26181fSAndroid Build Coastguard Worker * "If the datagram or message is larger than
1565*8b26181fSAndroid Build Coastguard Worker * the buffer specified, the buffer is filled
1566*8b26181fSAndroid Build Coastguard Worker * with the first part of the datagram, and recv
1567*8b26181fSAndroid Build Coastguard Worker * generates the error WSAEMSGSIZE. For unreliable
1568*8b26181fSAndroid Build Coastguard Worker * protocols (for example, UDP) the excess data is
1569*8b26181fSAndroid Build Coastguard Worker * lost..."
1570*8b26181fSAndroid Build Coastguard Worker *
1571*8b26181fSAndroid Build Coastguard Worker * So if the message is bigger than the buffer
1572*8b26181fSAndroid Build Coastguard Worker * supplied to us, the excess data is discarded,
1573*8b26181fSAndroid Build Coastguard Worker * and we'll report an error.
1574*8b26181fSAndroid Build Coastguard Worker */
1575*8b26181fSAndroid Build Coastguard Worker sock_fmterrmsg(errbuf, errbuflen, sock_geterrcode(),
1576*8b26181fSAndroid Build Coastguard Worker "recv() failed");
1577*8b26181fSAndroid Build Coastguard Worker return -1;
1578*8b26181fSAndroid Build Coastguard Worker }
1579*8b26181fSAndroid Build Coastguard Worker #else /* _WIN32 */
1580*8b26181fSAndroid Build Coastguard Worker /*
1581*8b26181fSAndroid Build Coastguard Worker * The Single UNIX Specification says that a recv() on
1582*8b26181fSAndroid Build Coastguard Worker * a socket for a message-oriented protocol will discard
1583*8b26181fSAndroid Build Coastguard Worker * the excess data. It does *not* indicate that the
1584*8b26181fSAndroid Build Coastguard Worker * receive will fail with, for example, EMSGSIZE.
1585*8b26181fSAndroid Build Coastguard Worker *
1586*8b26181fSAndroid Build Coastguard Worker * Therefore, we use recvmsg(), which appears to be
1587*8b26181fSAndroid Build Coastguard Worker * the only way to get a "message truncated" indication
1588*8b26181fSAndroid Build Coastguard Worker * when receiving a message for a message-oriented
1589*8b26181fSAndroid Build Coastguard Worker * protocol.
1590*8b26181fSAndroid Build Coastguard Worker */
1591*8b26181fSAndroid Build Coastguard Worker message.msg_name = NULL; /* we don't care who it's from */
1592*8b26181fSAndroid Build Coastguard Worker message.msg_namelen = 0;
1593*8b26181fSAndroid Build Coastguard Worker iov.iov_base = buffer;
1594*8b26181fSAndroid Build Coastguard Worker iov.iov_len = size;
1595*8b26181fSAndroid Build Coastguard Worker message.msg_iov = &iov;
1596*8b26181fSAndroid Build Coastguard Worker message.msg_iovlen = 1;
1597*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
1598*8b26181fSAndroid Build Coastguard Worker message.msg_control = NULL; /* we don't care about control information */
1599*8b26181fSAndroid Build Coastguard Worker message.msg_controllen = 0;
1600*8b26181fSAndroid Build Coastguard Worker #endif
1601*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_STRUCT_MSGHDR_MSG_FLAGS
1602*8b26181fSAndroid Build Coastguard Worker message.msg_flags = 0;
1603*8b26181fSAndroid Build Coastguard Worker #endif
1604*8b26181fSAndroid Build Coastguard Worker #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1605*8b26181fSAndroid Build Coastguard Worker nread = fuzz_recv(buffer, size);
1606*8b26181fSAndroid Build Coastguard Worker #else
1607*8b26181fSAndroid Build Coastguard Worker nread = recvmsg(sock, &message, 0);
1608*8b26181fSAndroid Build Coastguard Worker #endif
1609*8b26181fSAndroid Build Coastguard Worker if (nread == -1)
1610*8b26181fSAndroid Build Coastguard Worker {
1611*8b26181fSAndroid Build Coastguard Worker if (errno == EINTR)
1612*8b26181fSAndroid Build Coastguard Worker return -3;
1613*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "recv() failed");
1614*8b26181fSAndroid Build Coastguard Worker return -1;
1615*8b26181fSAndroid Build Coastguard Worker }
1616*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_STRUCT_MSGHDR_MSG_FLAGS
1617*8b26181fSAndroid Build Coastguard Worker /*
1618*8b26181fSAndroid Build Coastguard Worker * XXX - Solaris supports this, but only if you ask for the
1619*8b26181fSAndroid Build Coastguard Worker * X/Open version of recvmsg(); should we use that, or will
1620*8b26181fSAndroid Build Coastguard Worker * that cause other problems?
1621*8b26181fSAndroid Build Coastguard Worker */
1622*8b26181fSAndroid Build Coastguard Worker if (message.msg_flags & MSG_TRUNC)
1623*8b26181fSAndroid Build Coastguard Worker {
1624*8b26181fSAndroid Build Coastguard Worker /*
1625*8b26181fSAndroid Build Coastguard Worker * Message was bigger than the specified buffer size.
1626*8b26181fSAndroid Build Coastguard Worker *
1627*8b26181fSAndroid Build Coastguard Worker * Report this as an error, as the Microsoft documentation
1628*8b26181fSAndroid Build Coastguard Worker * implies we'd do in a similar case on Windows.
1629*8b26181fSAndroid Build Coastguard Worker */
1630*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "recv(): Message too long");
1631*8b26181fSAndroid Build Coastguard Worker return -1;
1632*8b26181fSAndroid Build Coastguard Worker }
1633*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_STRUCT_MSGHDR_MSG_FLAGS */
1634*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
1635*8b26181fSAndroid Build Coastguard Worker
1636*8b26181fSAndroid Build Coastguard Worker /*
1637*8b26181fSAndroid Build Coastguard Worker * The size we're reading fits in an int, so the return value
1638*8b26181fSAndroid Build Coastguard Worker * will fit in an int.
1639*8b26181fSAndroid Build Coastguard Worker */
1640*8b26181fSAndroid Build Coastguard Worker return (int)nread;
1641*8b26181fSAndroid Build Coastguard Worker }
1642*8b26181fSAndroid Build Coastguard Worker
1643*8b26181fSAndroid Build Coastguard Worker /*
1644*8b26181fSAndroid Build Coastguard Worker * \brief It discards N bytes that are currently waiting to be read on the current socket.
1645*8b26181fSAndroid Build Coastguard Worker *
1646*8b26181fSAndroid Build Coastguard Worker * This function is useful in case we receive a message we cannot understand (e.g.
1647*8b26181fSAndroid Build Coastguard Worker * wrong version number when receiving a network packet), so that we have to discard all
1648*8b26181fSAndroid Build Coastguard Worker * data before reading a new message.
1649*8b26181fSAndroid Build Coastguard Worker *
1650*8b26181fSAndroid Build Coastguard Worker * This function will read 'size' bytes from the socket and discard them.
1651*8b26181fSAndroid Build Coastguard Worker * It defines an internal buffer in which data will be copied; however, in case
1652*8b26181fSAndroid Build Coastguard Worker * this buffer is not large enough, it will cycle in order to read everything as well.
1653*8b26181fSAndroid Build Coastguard Worker *
1654*8b26181fSAndroid Build Coastguard Worker * \param sock: the connected socket currently opened.
1655*8b26181fSAndroid Build Coastguard Worker *
1656*8b26181fSAndroid Build Coastguard Worker * \param size: number of bytes that have to be discarded.
1657*8b26181fSAndroid Build Coastguard Worker *
1658*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1659*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1660*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1661*8b26181fSAndroid Build Coastguard Worker *
1662*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1663*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1664*8b26181fSAndroid Build Coastguard Worker *
1665*8b26181fSAndroid Build Coastguard Worker * \return '0' if everything is fine, '-1' if some errors occurred.
1666*8b26181fSAndroid Build Coastguard Worker * The error message is returned in the 'errbuf' variable.
1667*8b26181fSAndroid Build Coastguard Worker */
sock_discard(SOCKET sock,SSL * ssl,int size,char * errbuf,int errbuflen)1668*8b26181fSAndroid Build Coastguard Worker int sock_discard(SOCKET sock, SSL *ssl, int size, char *errbuf, int errbuflen)
1669*8b26181fSAndroid Build Coastguard Worker {
1670*8b26181fSAndroid Build Coastguard Worker #define TEMP_BUF_SIZE 32768
1671*8b26181fSAndroid Build Coastguard Worker
1672*8b26181fSAndroid Build Coastguard Worker char buffer[TEMP_BUF_SIZE]; /* network buffer, to be used when the message is discarded */
1673*8b26181fSAndroid Build Coastguard Worker
1674*8b26181fSAndroid Build Coastguard Worker /*
1675*8b26181fSAndroid Build Coastguard Worker * A static allocation avoids the need of a 'malloc()' each time we want to discard a message
1676*8b26181fSAndroid Build Coastguard Worker * Our feeling is that a buffer if 32KB is enough for most of the application;
1677*8b26181fSAndroid Build Coastguard Worker * in case this is not enough, the "while" loop discards the message by calling the
1678*8b26181fSAndroid Build Coastguard Worker * sockrecv() several times.
1679*8b26181fSAndroid Build Coastguard Worker * We do not want to create a bigger variable because this causes the program to exit on
1680*8b26181fSAndroid Build Coastguard Worker * some platforms (e.g. BSD)
1681*8b26181fSAndroid Build Coastguard Worker */
1682*8b26181fSAndroid Build Coastguard Worker while (size > TEMP_BUF_SIZE)
1683*8b26181fSAndroid Build Coastguard Worker {
1684*8b26181fSAndroid Build Coastguard Worker if (sock_recv(sock, ssl, buffer, TEMP_BUF_SIZE, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
1685*8b26181fSAndroid Build Coastguard Worker return -1;
1686*8b26181fSAndroid Build Coastguard Worker
1687*8b26181fSAndroid Build Coastguard Worker size -= TEMP_BUF_SIZE;
1688*8b26181fSAndroid Build Coastguard Worker }
1689*8b26181fSAndroid Build Coastguard Worker
1690*8b26181fSAndroid Build Coastguard Worker /*
1691*8b26181fSAndroid Build Coastguard Worker * If there is still data to be discarded
1692*8b26181fSAndroid Build Coastguard Worker * In this case, the data can fit into the temporary buffer
1693*8b26181fSAndroid Build Coastguard Worker */
1694*8b26181fSAndroid Build Coastguard Worker if (size)
1695*8b26181fSAndroid Build Coastguard Worker {
1696*8b26181fSAndroid Build Coastguard Worker if (sock_recv(sock, ssl, buffer, size, SOCK_RECEIVEALL_YES, errbuf, errbuflen) == -1)
1697*8b26181fSAndroid Build Coastguard Worker return -1;
1698*8b26181fSAndroid Build Coastguard Worker }
1699*8b26181fSAndroid Build Coastguard Worker
1700*8b26181fSAndroid Build Coastguard Worker return 0;
1701*8b26181fSAndroid Build Coastguard Worker }
1702*8b26181fSAndroid Build Coastguard Worker
1703*8b26181fSAndroid Build Coastguard Worker /*
1704*8b26181fSAndroid Build Coastguard Worker * \brief Checks that one host (identified by the sockaddr_storage structure) belongs to an 'allowed list'.
1705*8b26181fSAndroid Build Coastguard Worker *
1706*8b26181fSAndroid Build Coastguard Worker * This function is useful after an accept() call in order to check if the connecting
1707*8b26181fSAndroid Build Coastguard Worker * host is allowed to connect to me. To do that, we have a buffer that keeps the list of the
1708*8b26181fSAndroid Build Coastguard Worker * allowed host; this function checks the sockaddr_storage structure of the connecting host
1709*8b26181fSAndroid Build Coastguard Worker * against this host list, and it returns '0' is the host is included in this list.
1710*8b26181fSAndroid Build Coastguard Worker *
1711*8b26181fSAndroid Build Coastguard Worker * \param hostlist: pointer to a string that contains the list of the allowed host.
1712*8b26181fSAndroid Build Coastguard Worker *
1713*8b26181fSAndroid Build Coastguard Worker * \param sep: a string that keeps the separators used between the hosts (for example the
1714*8b26181fSAndroid Build Coastguard Worker * space character) in the host list.
1715*8b26181fSAndroid Build Coastguard Worker *
1716*8b26181fSAndroid Build Coastguard Worker * \param from: a sockaddr_storage structure, as it is returned by the accept() call.
1717*8b26181fSAndroid Build Coastguard Worker *
1718*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1719*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1720*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1721*8b26181fSAndroid Build Coastguard Worker *
1722*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1723*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1724*8b26181fSAndroid Build Coastguard Worker *
1725*8b26181fSAndroid Build Coastguard Worker * \return It returns:
1726*8b26181fSAndroid Build Coastguard Worker * - '1' if the host list is empty
1727*8b26181fSAndroid Build Coastguard Worker * - '0' if the host belongs to the host list (and therefore it is allowed to connect)
1728*8b26181fSAndroid Build Coastguard Worker * - '-1' in case the host does not belong to the host list (and therefore it is not allowed to connect
1729*8b26181fSAndroid Build Coastguard Worker * - '-2' in case or error. The error message is returned in the 'errbuf' variable.
1730*8b26181fSAndroid Build Coastguard Worker */
sock_check_hostlist(char * hostlist,const char * sep,struct sockaddr_storage * from,char * errbuf,int errbuflen)1731*8b26181fSAndroid Build Coastguard Worker int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen)
1732*8b26181fSAndroid Build Coastguard Worker {
1733*8b26181fSAndroid Build Coastguard Worker /* checks if the connecting host is among the ones allowed */
1734*8b26181fSAndroid Build Coastguard Worker if ((hostlist) && (hostlist[0]))
1735*8b26181fSAndroid Build Coastguard Worker {
1736*8b26181fSAndroid Build Coastguard Worker char *token; /* temp, needed to separate items into the hostlist */
1737*8b26181fSAndroid Build Coastguard Worker struct addrinfo *addrinfo, *ai_next;
1738*8b26181fSAndroid Build Coastguard Worker char *temphostlist;
1739*8b26181fSAndroid Build Coastguard Worker char *lasts;
1740*8b26181fSAndroid Build Coastguard Worker int getaddrinfo_failed = 0;
1741*8b26181fSAndroid Build Coastguard Worker
1742*8b26181fSAndroid Build Coastguard Worker /*
1743*8b26181fSAndroid Build Coastguard Worker * The problem is that strtok modifies the original variable by putting '0' at the end of each token
1744*8b26181fSAndroid Build Coastguard Worker * So, we have to create a new temporary string in which the original content is kept
1745*8b26181fSAndroid Build Coastguard Worker */
1746*8b26181fSAndroid Build Coastguard Worker temphostlist = strdup(hostlist);
1747*8b26181fSAndroid Build Coastguard Worker if (temphostlist == NULL)
1748*8b26181fSAndroid Build Coastguard Worker {
1749*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen,
1750*8b26181fSAndroid Build Coastguard Worker "sock_check_hostlist(), malloc() failed");
1751*8b26181fSAndroid Build Coastguard Worker return -2;
1752*8b26181fSAndroid Build Coastguard Worker }
1753*8b26181fSAndroid Build Coastguard Worker
1754*8b26181fSAndroid Build Coastguard Worker token = pcap_strtok_r(temphostlist, sep, &lasts);
1755*8b26181fSAndroid Build Coastguard Worker
1756*8b26181fSAndroid Build Coastguard Worker /* it avoids a warning in the compilation ('addrinfo used but not initialized') */
1757*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1758*8b26181fSAndroid Build Coastguard Worker
1759*8b26181fSAndroid Build Coastguard Worker while (token != NULL)
1760*8b26181fSAndroid Build Coastguard Worker {
1761*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints;
1762*8b26181fSAndroid Build Coastguard Worker int retval;
1763*8b26181fSAndroid Build Coastguard Worker
1764*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1765*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(struct addrinfo));
1766*8b26181fSAndroid Build Coastguard Worker hints.ai_family = PF_UNSPEC;
1767*8b26181fSAndroid Build Coastguard Worker hints.ai_socktype = SOCK_STREAM;
1768*8b26181fSAndroid Build Coastguard Worker
1769*8b26181fSAndroid Build Coastguard Worker retval = getaddrinfo(token, NULL, &hints, &addrinfo);
1770*8b26181fSAndroid Build Coastguard Worker if (retval != 0)
1771*8b26181fSAndroid Build Coastguard Worker {
1772*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1773*8b26181fSAndroid Build Coastguard Worker get_gai_errstring(errbuf, errbuflen,
1774*8b26181fSAndroid Build Coastguard Worker "Allowed host list error: ",
1775*8b26181fSAndroid Build Coastguard Worker retval, token, NULL);
1776*8b26181fSAndroid Build Coastguard Worker
1777*8b26181fSAndroid Build Coastguard Worker /*
1778*8b26181fSAndroid Build Coastguard Worker * Note that at least one call to getaddrinfo()
1779*8b26181fSAndroid Build Coastguard Worker * failed.
1780*8b26181fSAndroid Build Coastguard Worker */
1781*8b26181fSAndroid Build Coastguard Worker getaddrinfo_failed = 1;
1782*8b26181fSAndroid Build Coastguard Worker
1783*8b26181fSAndroid Build Coastguard Worker /* Get next token */
1784*8b26181fSAndroid Build Coastguard Worker token = pcap_strtok_r(NULL, sep, &lasts);
1785*8b26181fSAndroid Build Coastguard Worker continue;
1786*8b26181fSAndroid Build Coastguard Worker }
1787*8b26181fSAndroid Build Coastguard Worker
1788*8b26181fSAndroid Build Coastguard Worker /* ai_next is required to preserve the content of addrinfo, in order to deallocate it properly */
1789*8b26181fSAndroid Build Coastguard Worker ai_next = addrinfo;
1790*8b26181fSAndroid Build Coastguard Worker while (ai_next)
1791*8b26181fSAndroid Build Coastguard Worker {
1792*8b26181fSAndroid Build Coastguard Worker if (sock_cmpaddr(from, (struct sockaddr_storage *) ai_next->ai_addr) == 0)
1793*8b26181fSAndroid Build Coastguard Worker {
1794*8b26181fSAndroid Build Coastguard Worker free(temphostlist);
1795*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1796*8b26181fSAndroid Build Coastguard Worker return 0;
1797*8b26181fSAndroid Build Coastguard Worker }
1798*8b26181fSAndroid Build Coastguard Worker
1799*8b26181fSAndroid Build Coastguard Worker /*
1800*8b26181fSAndroid Build Coastguard Worker * If we are here, it means that the current address does not matches
1801*8b26181fSAndroid Build Coastguard Worker * Let's try with the next one in the header chain
1802*8b26181fSAndroid Build Coastguard Worker */
1803*8b26181fSAndroid Build Coastguard Worker ai_next = ai_next->ai_next;
1804*8b26181fSAndroid Build Coastguard Worker }
1805*8b26181fSAndroid Build Coastguard Worker
1806*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1807*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1808*8b26181fSAndroid Build Coastguard Worker
1809*8b26181fSAndroid Build Coastguard Worker /* Get next token */
1810*8b26181fSAndroid Build Coastguard Worker token = pcap_strtok_r(NULL, sep, &lasts);
1811*8b26181fSAndroid Build Coastguard Worker }
1812*8b26181fSAndroid Build Coastguard Worker
1813*8b26181fSAndroid Build Coastguard Worker if (addrinfo)
1814*8b26181fSAndroid Build Coastguard Worker {
1815*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
1816*8b26181fSAndroid Build Coastguard Worker addrinfo = NULL;
1817*8b26181fSAndroid Build Coastguard Worker }
1818*8b26181fSAndroid Build Coastguard Worker
1819*8b26181fSAndroid Build Coastguard Worker free(temphostlist);
1820*8b26181fSAndroid Build Coastguard Worker
1821*8b26181fSAndroid Build Coastguard Worker if (getaddrinfo_failed) {
1822*8b26181fSAndroid Build Coastguard Worker /*
1823*8b26181fSAndroid Build Coastguard Worker * At least one getaddrinfo() call failed;
1824*8b26181fSAndroid Build Coastguard Worker * treat that as an error, so rpcapd knows
1825*8b26181fSAndroid Build Coastguard Worker * that it should log it locally as well
1826*8b26181fSAndroid Build Coastguard Worker * as telling the client about it.
1827*8b26181fSAndroid Build Coastguard Worker */
1828*8b26181fSAndroid Build Coastguard Worker return -2;
1829*8b26181fSAndroid Build Coastguard Worker } else {
1830*8b26181fSAndroid Build Coastguard Worker /*
1831*8b26181fSAndroid Build Coastguard Worker * All getaddrinfo() calls succeeded, but
1832*8b26181fSAndroid Build Coastguard Worker * the host wasn't in the list.
1833*8b26181fSAndroid Build Coastguard Worker */
1834*8b26181fSAndroid Build Coastguard Worker if (errbuf)
1835*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "The host is not in the allowed host list. Connection refused.");
1836*8b26181fSAndroid Build Coastguard Worker return -1;
1837*8b26181fSAndroid Build Coastguard Worker }
1838*8b26181fSAndroid Build Coastguard Worker }
1839*8b26181fSAndroid Build Coastguard Worker
1840*8b26181fSAndroid Build Coastguard Worker /* No hostlist, so we have to return 'empty list' */
1841*8b26181fSAndroid Build Coastguard Worker return 1;
1842*8b26181fSAndroid Build Coastguard Worker }
1843*8b26181fSAndroid Build Coastguard Worker
1844*8b26181fSAndroid Build Coastguard Worker /*
1845*8b26181fSAndroid Build Coastguard Worker * \brief Compares two addresses contained into two sockaddr_storage structures.
1846*8b26181fSAndroid Build Coastguard Worker *
1847*8b26181fSAndroid Build Coastguard Worker * This function is useful to compare two addresses, given their internal representation,
1848*8b26181fSAndroid Build Coastguard Worker * i.e. an sockaddr_storage structure.
1849*8b26181fSAndroid Build Coastguard Worker *
1850*8b26181fSAndroid Build Coastguard Worker * The two structures do not need to be sockaddr_storage; you can have both 'sockaddr_in' and
1851*8b26181fSAndroid Build Coastguard Worker * sockaddr_in6, properly acsted in order to be compliant to the function interface.
1852*8b26181fSAndroid Build Coastguard Worker *
1853*8b26181fSAndroid Build Coastguard Worker * This function will return '0' if the two addresses matches, '-1' if not.
1854*8b26181fSAndroid Build Coastguard Worker *
1855*8b26181fSAndroid Build Coastguard Worker * \param first: a sockaddr_storage structure, (for example the one that is returned by an
1856*8b26181fSAndroid Build Coastguard Worker * accept() call), containing the first address to compare.
1857*8b26181fSAndroid Build Coastguard Worker *
1858*8b26181fSAndroid Build Coastguard Worker * \param second: a sockaddr_storage structure containing the second address to compare.
1859*8b26181fSAndroid Build Coastguard Worker *
1860*8b26181fSAndroid Build Coastguard Worker * \return '0' if the addresses are equal, '-1' if they are different.
1861*8b26181fSAndroid Build Coastguard Worker */
sock_cmpaddr(struct sockaddr_storage * first,struct sockaddr_storage * second)1862*8b26181fSAndroid Build Coastguard Worker int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second)
1863*8b26181fSAndroid Build Coastguard Worker {
1864*8b26181fSAndroid Build Coastguard Worker if (first->ss_family == second->ss_family)
1865*8b26181fSAndroid Build Coastguard Worker {
1866*8b26181fSAndroid Build Coastguard Worker if (first->ss_family == AF_INET)
1867*8b26181fSAndroid Build Coastguard Worker {
1868*8b26181fSAndroid Build Coastguard Worker if (memcmp(&(((struct sockaddr_in *) first)->sin_addr),
1869*8b26181fSAndroid Build Coastguard Worker &(((struct sockaddr_in *) second)->sin_addr),
1870*8b26181fSAndroid Build Coastguard Worker sizeof(struct in_addr)) == 0)
1871*8b26181fSAndroid Build Coastguard Worker return 0;
1872*8b26181fSAndroid Build Coastguard Worker }
1873*8b26181fSAndroid Build Coastguard Worker else /* address family is AF_INET6 */
1874*8b26181fSAndroid Build Coastguard Worker {
1875*8b26181fSAndroid Build Coastguard Worker if (memcmp(&(((struct sockaddr_in6 *) first)->sin6_addr),
1876*8b26181fSAndroid Build Coastguard Worker &(((struct sockaddr_in6 *) second)->sin6_addr),
1877*8b26181fSAndroid Build Coastguard Worker sizeof(struct in6_addr)) == 0)
1878*8b26181fSAndroid Build Coastguard Worker return 0;
1879*8b26181fSAndroid Build Coastguard Worker }
1880*8b26181fSAndroid Build Coastguard Worker }
1881*8b26181fSAndroid Build Coastguard Worker
1882*8b26181fSAndroid Build Coastguard Worker return -1;
1883*8b26181fSAndroid Build Coastguard Worker }
1884*8b26181fSAndroid Build Coastguard Worker
1885*8b26181fSAndroid Build Coastguard Worker /*
1886*8b26181fSAndroid Build Coastguard Worker * \brief It gets the address/port the system picked for this socket (on connected sockets).
1887*8b26181fSAndroid Build Coastguard Worker *
1888*8b26181fSAndroid Build Coastguard Worker * It is used to return the address and port the server picked for our socket on the local machine.
1889*8b26181fSAndroid Build Coastguard Worker * It works only on:
1890*8b26181fSAndroid Build Coastguard Worker * - connected sockets
1891*8b26181fSAndroid Build Coastguard Worker * - server sockets
1892*8b26181fSAndroid Build Coastguard Worker *
1893*8b26181fSAndroid Build Coastguard Worker * On unconnected client sockets it does not work because the system dynamically chooses a port
1894*8b26181fSAndroid Build Coastguard Worker * only when the socket calls a send() call.
1895*8b26181fSAndroid Build Coastguard Worker *
1896*8b26181fSAndroid Build Coastguard Worker * \param sock: the connected socket currently opened.
1897*8b26181fSAndroid Build Coastguard Worker *
1898*8b26181fSAndroid Build Coastguard Worker * \param address: it contains the address that will be returned by the function. This buffer
1899*8b26181fSAndroid Build Coastguard Worker * must be properly allocated by the user. The address can be either literal or numeric depending
1900*8b26181fSAndroid Build Coastguard Worker * on the value of 'Flags'.
1901*8b26181fSAndroid Build Coastguard Worker *
1902*8b26181fSAndroid Build Coastguard Worker * \param addrlen: the length of the 'address' buffer.
1903*8b26181fSAndroid Build Coastguard Worker *
1904*8b26181fSAndroid Build Coastguard Worker * \param port: it contains the port that will be returned by the function. This buffer
1905*8b26181fSAndroid Build Coastguard Worker * must be properly allocated by the user.
1906*8b26181fSAndroid Build Coastguard Worker *
1907*8b26181fSAndroid Build Coastguard Worker * \param portlen: the length of the 'port' buffer.
1908*8b26181fSAndroid Build Coastguard Worker *
1909*8b26181fSAndroid Build Coastguard Worker * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
1910*8b26181fSAndroid Build Coastguard Worker * that determine if the resulting address must be in numeric / literal form, and so on.
1911*8b26181fSAndroid Build Coastguard Worker *
1912*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1913*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1914*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1915*8b26181fSAndroid Build Coastguard Worker *
1916*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1917*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1918*8b26181fSAndroid Build Coastguard Worker *
1919*8b26181fSAndroid Build Coastguard Worker * \return It returns '-1' if this function succeeds, '0' otherwise.
1920*8b26181fSAndroid Build Coastguard Worker * The address and port corresponding are returned back in the buffers 'address' and 'port'.
1921*8b26181fSAndroid Build Coastguard Worker * In any case, the returned strings are '0' terminated.
1922*8b26181fSAndroid Build Coastguard Worker *
1923*8b26181fSAndroid Build Coastguard Worker * \warning If the socket is using a connectionless protocol, the address may not be available
1924*8b26181fSAndroid Build Coastguard Worker * until I/O occurs on the socket.
1925*8b26181fSAndroid Build Coastguard Worker */
sock_getmyinfo(SOCKET sock,char * address,int addrlen,char * port,int portlen,int flags,char * errbuf,int errbuflen)1926*8b26181fSAndroid Build Coastguard Worker int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen)
1927*8b26181fSAndroid Build Coastguard Worker {
1928*8b26181fSAndroid Build Coastguard Worker struct sockaddr_storage mysockaddr;
1929*8b26181fSAndroid Build Coastguard Worker socklen_t sockaddrlen;
1930*8b26181fSAndroid Build Coastguard Worker
1931*8b26181fSAndroid Build Coastguard Worker
1932*8b26181fSAndroid Build Coastguard Worker sockaddrlen = sizeof(struct sockaddr_storage);
1933*8b26181fSAndroid Build Coastguard Worker
1934*8b26181fSAndroid Build Coastguard Worker if (getsockname(sock, (struct sockaddr *) &mysockaddr, &sockaddrlen) == -1)
1935*8b26181fSAndroid Build Coastguard Worker {
1936*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen, "getsockname() failed");
1937*8b26181fSAndroid Build Coastguard Worker return 0;
1938*8b26181fSAndroid Build Coastguard Worker }
1939*8b26181fSAndroid Build Coastguard Worker
1940*8b26181fSAndroid Build Coastguard Worker /* Returns the numeric address of the host that triggered the error */
1941*8b26181fSAndroid Build Coastguard Worker return sock_getascii_addrport(&mysockaddr, address, addrlen, port, portlen, flags, errbuf, errbuflen);
1942*8b26181fSAndroid Build Coastguard Worker }
1943*8b26181fSAndroid Build Coastguard Worker
1944*8b26181fSAndroid Build Coastguard Worker /*
1945*8b26181fSAndroid Build Coastguard Worker * \brief It retrieves two strings containing the address and the port of a given 'sockaddr' variable.
1946*8b26181fSAndroid Build Coastguard Worker *
1947*8b26181fSAndroid Build Coastguard Worker * This function is basically an extended version of the inet_ntop(), which does not exist in
1948*8b26181fSAndroid Build Coastguard Worker * Winsock because the same result can be obtained by using the getnameinfo().
1949*8b26181fSAndroid Build Coastguard Worker * However, differently from inet_ntop(), this function is able to return also literal names
1950*8b26181fSAndroid Build Coastguard Worker * (e.g. 'localhost') dependently from the 'Flags' parameter.
1951*8b26181fSAndroid Build Coastguard Worker *
1952*8b26181fSAndroid Build Coastguard Worker * The function accepts a sockaddr_storage variable (which can be returned by several functions
1953*8b26181fSAndroid Build Coastguard Worker * like bind(), connect(), accept(), and more) and it transforms its content into a 'human'
1954*8b26181fSAndroid Build Coastguard Worker * form. So, for instance, it is able to translate an hex address (stored in binary form) into
1955*8b26181fSAndroid Build Coastguard Worker * a standard IPv6 address like "::1".
1956*8b26181fSAndroid Build Coastguard Worker *
1957*8b26181fSAndroid Build Coastguard Worker * The behavior of this function depends on the parameters we have in the 'Flags' variable, which
1958*8b26181fSAndroid Build Coastguard Worker * are the ones allowed in the standard getnameinfo() socket function.
1959*8b26181fSAndroid Build Coastguard Worker *
1960*8b26181fSAndroid Build Coastguard Worker * \param sockaddr: a 'sockaddr_in' or 'sockaddr_in6' structure containing the address that
1961*8b26181fSAndroid Build Coastguard Worker * need to be translated from network form into the presentation form. This structure must be
1962*8b26181fSAndroid Build Coastguard Worker * zero-ed prior using it, and the address family field must be filled with the proper value.
1963*8b26181fSAndroid Build Coastguard Worker * The user must cast any 'sockaddr_in' or 'sockaddr_in6' structures to 'sockaddr_storage' before
1964*8b26181fSAndroid Build Coastguard Worker * calling this function.
1965*8b26181fSAndroid Build Coastguard Worker *
1966*8b26181fSAndroid Build Coastguard Worker * \param address: it contains the address that will be returned by the function. This buffer
1967*8b26181fSAndroid Build Coastguard Worker * must be properly allocated by the user. The address can be either literal or numeric depending
1968*8b26181fSAndroid Build Coastguard Worker * on the value of 'Flags'.
1969*8b26181fSAndroid Build Coastguard Worker *
1970*8b26181fSAndroid Build Coastguard Worker * \param addrlen: the length of the 'address' buffer.
1971*8b26181fSAndroid Build Coastguard Worker *
1972*8b26181fSAndroid Build Coastguard Worker * \param port: it contains the port that will be returned by the function. This buffer
1973*8b26181fSAndroid Build Coastguard Worker * must be properly allocated by the user.
1974*8b26181fSAndroid Build Coastguard Worker *
1975*8b26181fSAndroid Build Coastguard Worker * \param portlen: the length of the 'port' buffer.
1976*8b26181fSAndroid Build Coastguard Worker *
1977*8b26181fSAndroid Build Coastguard Worker * \param flags: a set of flags (the ones defined into the getnameinfo() standard socket function)
1978*8b26181fSAndroid Build Coastguard Worker * that determine if the resulting address must be in numeric / literal form, and so on.
1979*8b26181fSAndroid Build Coastguard Worker *
1980*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
1981*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
1982*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
1983*8b26181fSAndroid Build Coastguard Worker *
1984*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
1985*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
1986*8b26181fSAndroid Build Coastguard Worker *
1987*8b26181fSAndroid Build Coastguard Worker * \return It returns '-1' if this function succeeds, '0' otherwise.
1988*8b26181fSAndroid Build Coastguard Worker * The address and port corresponding to the given SockAddr are returned back in the buffers 'address'
1989*8b26181fSAndroid Build Coastguard Worker * and 'port'.
1990*8b26181fSAndroid Build Coastguard Worker * In any case, the returned strings are '0' terminated.
1991*8b26181fSAndroid Build Coastguard Worker */
sock_getascii_addrport(const struct sockaddr_storage * sockaddr,char * address,int addrlen,char * port,int portlen,int flags,char * errbuf,size_t errbuflen)1992*8b26181fSAndroid Build Coastguard Worker int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, size_t errbuflen)
1993*8b26181fSAndroid Build Coastguard Worker {
1994*8b26181fSAndroid Build Coastguard Worker socklen_t sockaddrlen;
1995*8b26181fSAndroid Build Coastguard Worker int retval; /* Variable that keeps the return value; */
1996*8b26181fSAndroid Build Coastguard Worker
1997*8b26181fSAndroid Build Coastguard Worker retval = -1;
1998*8b26181fSAndroid Build Coastguard Worker
1999*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2000*8b26181fSAndroid Build Coastguard Worker if (sockaddr->ss_family == AF_INET)
2001*8b26181fSAndroid Build Coastguard Worker sockaddrlen = sizeof(struct sockaddr_in);
2002*8b26181fSAndroid Build Coastguard Worker else
2003*8b26181fSAndroid Build Coastguard Worker sockaddrlen = sizeof(struct sockaddr_in6);
2004*8b26181fSAndroid Build Coastguard Worker #else
2005*8b26181fSAndroid Build Coastguard Worker sockaddrlen = sizeof(struct sockaddr_storage);
2006*8b26181fSAndroid Build Coastguard Worker #endif
2007*8b26181fSAndroid Build Coastguard Worker
2008*8b26181fSAndroid Build Coastguard Worker if ((flags & NI_NUMERICHOST) == 0) /* Check that we want literal names */
2009*8b26181fSAndroid Build Coastguard Worker {
2010*8b26181fSAndroid Build Coastguard Worker if ((sockaddr->ss_family == AF_INET6) &&
2011*8b26181fSAndroid Build Coastguard Worker (memcmp(&((struct sockaddr_in6 *) sockaddr)->sin6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sizeof(struct in6_addr)) == 0))
2012*8b26181fSAndroid Build Coastguard Worker {
2013*8b26181fSAndroid Build Coastguard Worker if (address)
2014*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(address, SOCKET_NAME_NULL_DAD, addrlen);
2015*8b26181fSAndroid Build Coastguard Worker return retval;
2016*8b26181fSAndroid Build Coastguard Worker }
2017*8b26181fSAndroid Build Coastguard Worker }
2018*8b26181fSAndroid Build Coastguard Worker
2019*8b26181fSAndroid Build Coastguard Worker if (getnameinfo((struct sockaddr *) sockaddr, sockaddrlen, address, addrlen, port, portlen, flags) != 0)
2020*8b26181fSAndroid Build Coastguard Worker {
2021*8b26181fSAndroid Build Coastguard Worker /* If the user wants to receive an error message */
2022*8b26181fSAndroid Build Coastguard Worker if (errbuf)
2023*8b26181fSAndroid Build Coastguard Worker {
2024*8b26181fSAndroid Build Coastguard Worker sock_geterrmsg(errbuf, errbuflen,
2025*8b26181fSAndroid Build Coastguard Worker "getnameinfo() failed");
2026*8b26181fSAndroid Build Coastguard Worker errbuf[errbuflen - 1] = 0;
2027*8b26181fSAndroid Build Coastguard Worker }
2028*8b26181fSAndroid Build Coastguard Worker
2029*8b26181fSAndroid Build Coastguard Worker if (address)
2030*8b26181fSAndroid Build Coastguard Worker {
2031*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(address, SOCKET_NO_NAME_AVAILABLE, addrlen);
2032*8b26181fSAndroid Build Coastguard Worker address[addrlen - 1] = 0;
2033*8b26181fSAndroid Build Coastguard Worker }
2034*8b26181fSAndroid Build Coastguard Worker
2035*8b26181fSAndroid Build Coastguard Worker if (port)
2036*8b26181fSAndroid Build Coastguard Worker {
2037*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(port, SOCKET_NO_PORT_AVAILABLE, portlen);
2038*8b26181fSAndroid Build Coastguard Worker port[portlen - 1] = 0;
2039*8b26181fSAndroid Build Coastguard Worker }
2040*8b26181fSAndroid Build Coastguard Worker
2041*8b26181fSAndroid Build Coastguard Worker retval = 0;
2042*8b26181fSAndroid Build Coastguard Worker }
2043*8b26181fSAndroid Build Coastguard Worker
2044*8b26181fSAndroid Build Coastguard Worker return retval;
2045*8b26181fSAndroid Build Coastguard Worker }
2046*8b26181fSAndroid Build Coastguard Worker
2047*8b26181fSAndroid Build Coastguard Worker /*
2048*8b26181fSAndroid Build Coastguard Worker * \brief It translates an address from the 'presentation' form into the 'network' form.
2049*8b26181fSAndroid Build Coastguard Worker *
2050*8b26181fSAndroid Build Coastguard Worker * This function basically replaces inet_pton(), which does not exist in Winsock because
2051*8b26181fSAndroid Build Coastguard Worker * the same result can be obtained by using the getaddrinfo().
2052*8b26181fSAndroid Build Coastguard Worker * An additional advantage is that 'Address' can be both a numeric address (e.g. '127.0.0.1',
2053*8b26181fSAndroid Build Coastguard Worker * like in inet_pton() ) and a literal name (e.g. 'localhost').
2054*8b26181fSAndroid Build Coastguard Worker *
2055*8b26181fSAndroid Build Coastguard Worker * This function does the reverse job of sock_getascii_addrport().
2056*8b26181fSAndroid Build Coastguard Worker *
2057*8b26181fSAndroid Build Coastguard Worker * \param address: a zero-terminated string which contains the name you have to
2058*8b26181fSAndroid Build Coastguard Worker * translate. The name can be either literal (e.g. 'localhost') or numeric (e.g. '::1').
2059*8b26181fSAndroid Build Coastguard Worker *
2060*8b26181fSAndroid Build Coastguard Worker * \param sockaddr: a user-allocated sockaddr_storage structure which will contains the
2061*8b26181fSAndroid Build Coastguard Worker * 'network' form of the requested address.
2062*8b26181fSAndroid Build Coastguard Worker *
2063*8b26181fSAndroid Build Coastguard Worker * \param addr_family: a constant which can assume the following values:
2064*8b26181fSAndroid Build Coastguard Worker * - 'AF_INET' if we want to ping an IPv4 host
2065*8b26181fSAndroid Build Coastguard Worker * - 'AF_INET6' if we want to ping an IPv6 host
2066*8b26181fSAndroid Build Coastguard Worker * - 'AF_UNSPEC' if we do not have preferences about the protocol used to ping the host
2067*8b26181fSAndroid Build Coastguard Worker *
2068*8b26181fSAndroid Build Coastguard Worker * \param errbuf: a pointer to an user-allocated buffer that will contain the complete
2069*8b26181fSAndroid Build Coastguard Worker * error message. This buffer has to be at least 'errbuflen' in length.
2070*8b26181fSAndroid Build Coastguard Worker * It can be NULL; in this case the error cannot be printed.
2071*8b26181fSAndroid Build Coastguard Worker *
2072*8b26181fSAndroid Build Coastguard Worker * \param errbuflen: length of the buffer that will contains the error. The error message cannot be
2073*8b26181fSAndroid Build Coastguard Worker * larger than 'errbuflen - 1' because the last char is reserved for the string terminator.
2074*8b26181fSAndroid Build Coastguard Worker *
2075*8b26181fSAndroid Build Coastguard Worker * \return '-1' if the translation succeeded, '-2' if there was some non critical error, '0'
2076*8b26181fSAndroid Build Coastguard Worker * otherwise. In case it fails, the content of the SockAddr variable remains unchanged.
2077*8b26181fSAndroid Build Coastguard Worker * A 'non critical error' can occur in case the 'Address' is a literal name, which can be mapped
2078*8b26181fSAndroid Build Coastguard Worker * to several network addresses (e.g. 'foo.bar.com' => '10.2.2.2' and '10.2.2.3'). In this case
2079*8b26181fSAndroid Build Coastguard Worker * the content of the SockAddr parameter will be the address corresponding to the first mapping.
2080*8b26181fSAndroid Build Coastguard Worker *
2081*8b26181fSAndroid Build Coastguard Worker * \warning The sockaddr_storage structure MUST be allocated by the user.
2082*8b26181fSAndroid Build Coastguard Worker */
sock_present2network(const char * address,struct sockaddr_storage * sockaddr,int addr_family,char * errbuf,int errbuflen)2083*8b26181fSAndroid Build Coastguard Worker int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen)
2084*8b26181fSAndroid Build Coastguard Worker {
2085*8b26181fSAndroid Build Coastguard Worker int retval;
2086*8b26181fSAndroid Build Coastguard Worker struct addrinfo *addrinfo;
2087*8b26181fSAndroid Build Coastguard Worker struct addrinfo hints;
2088*8b26181fSAndroid Build Coastguard Worker
2089*8b26181fSAndroid Build Coastguard Worker memset(&hints, 0, sizeof(hints));
2090*8b26181fSAndroid Build Coastguard Worker
2091*8b26181fSAndroid Build Coastguard Worker hints.ai_family = addr_family;
2092*8b26181fSAndroid Build Coastguard Worker
2093*8b26181fSAndroid Build Coastguard Worker if ((retval = sock_initaddress(address, "22222" /* fake port */, &hints, &addrinfo, errbuf, errbuflen)) == -1)
2094*8b26181fSAndroid Build Coastguard Worker return 0;
2095*8b26181fSAndroid Build Coastguard Worker
2096*8b26181fSAndroid Build Coastguard Worker if (addrinfo->ai_family == PF_INET)
2097*8b26181fSAndroid Build Coastguard Worker memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in));
2098*8b26181fSAndroid Build Coastguard Worker else
2099*8b26181fSAndroid Build Coastguard Worker memcpy(sockaddr, addrinfo->ai_addr, sizeof(struct sockaddr_in6));
2100*8b26181fSAndroid Build Coastguard Worker
2101*8b26181fSAndroid Build Coastguard Worker if (addrinfo->ai_next != NULL)
2102*8b26181fSAndroid Build Coastguard Worker {
2103*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2104*8b26181fSAndroid Build Coastguard Worker
2105*8b26181fSAndroid Build Coastguard Worker if (errbuf)
2106*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbuflen, "More than one socket requested; using the first one returned");
2107*8b26181fSAndroid Build Coastguard Worker return -2;
2108*8b26181fSAndroid Build Coastguard Worker }
2109*8b26181fSAndroid Build Coastguard Worker
2110*8b26181fSAndroid Build Coastguard Worker freeaddrinfo(addrinfo);
2111*8b26181fSAndroid Build Coastguard Worker return -1;
2112*8b26181fSAndroid Build Coastguard Worker }
2113