Annotation of embedaddon/curl/lib/socketpair.c, revision 1.1.1.1

1.1       misho       1: /***************************************************************************
                      2:  *                                  _   _ ____  _
                      3:  *  Project                     ___| | | |  _ \| |
                      4:  *                             / __| | | | |_) | |
                      5:  *                            | (__| |_| |  _ <| |___
                      6:  *                             \___|\___/|_| \_\_____|
                      7:  *
                      8:  * Copyright (C) 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
                      9:  *
                     10:  * This software is licensed as described in the file COPYING, which
                     11:  * you should have received as part of this distribution. The terms
                     12:  * are also available at https://curl.haxx.se/docs/copyright.html.
                     13:  *
                     14:  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
                     15:  * copies of the Software, and permit persons to whom the Software is
                     16:  * furnished to do so, under the terms of the COPYING file.
                     17:  *
                     18:  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
                     19:  * KIND, either express or implied.
                     20:  *
                     21:  ***************************************************************************/
                     22: 
                     23: #include "curl_setup.h"
                     24: #include "socketpair.h"
                     25: 
                     26: #ifndef HAVE_SOCKETPAIR
                     27: #ifdef WIN32
                     28: /*
                     29:  * This is a socketpair() implementation for Windows.
                     30:  */
                     31: #include <string.h>
                     32: #include <winsock2.h>
                     33: #include <ws2tcpip.h>
                     34: #include <windows.h>
                     35: #include <io.h>
                     36: #else
                     37: #ifdef HAVE_NETDB_H
                     38: #include <netdb.h>
                     39: #endif
                     40: #ifdef HAVE_NETINET_IN_H
                     41: #include <netinet/in.h> /* IPPROTO_TCP */
                     42: #endif
                     43: #ifdef HAVE_ARPA_INET_H
                     44: #include <arpa/inet.h>
                     45: #endif
                     46: #ifndef INADDR_LOOPBACK
                     47: #define INADDR_LOOPBACK 0x7f000001
                     48: #endif /* !INADDR_LOOPBACK */
                     49: #endif /* !WIN32 */
                     50: 
                     51: /* The last 3 #include files should be in this order */
                     52: #include "curl_printf.h"
                     53: #include "curl_memory.h"
                     54: #include "memdebug.h"
                     55: 
                     56: int Curl_socketpair(int domain, int type, int protocol,
                     57:                     curl_socket_t socks[2])
                     58: {
                     59:   union {
                     60:     struct sockaddr_in inaddr;
                     61:     struct sockaddr addr;
                     62:   } a;
                     63:   curl_socket_t listener;
                     64:   curl_socklen_t addrlen = sizeof(a.inaddr);
                     65:   int reuse = 1;
                     66:   char data[2][12];
                     67:   ssize_t dlen;
                     68:   (void)domain;
                     69:   (void)type;
                     70:   (void)protocol;
                     71: 
                     72:   listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                     73:   if(listener == CURL_SOCKET_BAD)
                     74:     return -1;
                     75: 
                     76:   memset(&a, 0, sizeof(a));
                     77:   a.inaddr.sin_family = AF_INET;
                     78:   a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
                     79:   a.inaddr.sin_port = 0;
                     80: 
                     81:   socks[0] = socks[1] = CURL_SOCKET_BAD;
                     82: 
                     83:   if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
                     84:                 (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
                     85:     goto error;
                     86:   if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
                     87:     goto error;
                     88:   if(getsockname(listener, &a.addr, &addrlen) == -1)
                     89:     goto error;
                     90:   if(listen(listener, 1) == -1)
                     91:     goto error;
                     92:   socks[0] = socket(AF_INET, SOCK_STREAM, 0);
                     93:   if(socks[0] == CURL_SOCKET_BAD)
                     94:     goto error;
                     95:   if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
                     96:     goto error;
                     97:   socks[1] = accept(listener, NULL, NULL);
                     98:   if(socks[1] == CURL_SOCKET_BAD)
                     99:     goto error;
                    100: 
                    101:   /* verify that nothing else connected */
                    102:   msnprintf(data[0], sizeof(data[0]), "%p", socks);
                    103:   dlen = strlen(data[0]);
                    104:   if(swrite(socks[0], data[0], dlen) != dlen)
                    105:     goto error;
                    106:   if(sread(socks[1], data[1], sizeof(data[1])) != dlen)
                    107:     goto error;
                    108:   if(memcmp(data[0], data[1], dlen))
                    109:     goto error;
                    110: 
                    111:   sclose(listener);
                    112:   return 0;
                    113: 
                    114:   error:
                    115:   sclose(listener);
                    116:   sclose(socks[0]);
                    117:   sclose(socks[1]);
                    118:   return -1;
                    119: }
                    120: 
                    121: #endif /* ! HAVE_SOCKETPAIR */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>