Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002 - 2003 |
| 3 | * NetGroup, Politecnico di Torino (Italy) |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of the Politecnico di Torino nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #ifndef __SOCKUTILS_H__ |
| 34 | #define __SOCKUTILS_H__ |
| 35 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 36 | #ifdef _MSC_VER |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 37 | #pragma once |
| 38 | #endif |
| 39 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 40 | #include "pcap/socket.h" |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 41 | |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 42 | #ifndef _WIN32 |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 43 | /* UN*X */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 44 | #include <unistd.h> /* close() */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 45 | |
| 46 | /*! |
| 47 | * \brief In Winsock, the close() call cannot be used on a socket; |
| 48 | * closesocket() must be used. |
| 49 | * We define closesocket() to be a wrapper around close() on UN*X, |
| 50 | * so that it can be used on both platforms. |
| 51 | */ |
| 52 | #define closesocket(a) close(a) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 55 | #include "sslutils.h" // for SSL type, whatever that turns out to be |
| 56 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 57 | /* |
| 58 | * MingW headers include this definition, but only for Windows XP and above. |
| 59 | * MSDN states that this function is available for most versions on Windows. |
| 60 | */ |
| 61 | #if ((defined(__MINGW32__)) && (_WIN32_WINNT < 0x0501)) |
| 62 | int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD, |
| 63 | char*,DWORD,int); |
| 64 | #endif |
| 65 | |
| 66 | /* |
| 67 | * \defgroup SockUtils Cross-platform socket utilities (IPv4-IPv6) |
| 68 | */ |
| 69 | |
| 70 | /* |
| 71 | * \addtogroup SockUtils |
| 72 | * \{ |
| 73 | */ |
| 74 | |
| 75 | /* |
| 76 | * \defgroup ExportedStruct Exported Structures and Definitions |
| 77 | */ |
| 78 | |
| 79 | /* |
| 80 | * \addtogroup ExportedStruct |
| 81 | * \{ |
| 82 | */ |
| 83 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 84 | /**************************************************** |
| 85 | * * |
| 86 | * Exported functions / definitions * |
| 87 | * * |
| 88 | ****************************************************/ |
| 89 | |
| 90 | /* 'checkonly' flag, into the rpsock_bufferize() */ |
| 91 | #define SOCKBUF_CHECKONLY 1 |
| 92 | /* no 'checkonly' flag, into the rpsock_bufferize() */ |
| 93 | #define SOCKBUF_BUFFERIZE 0 |
| 94 | |
| 95 | /* no 'server' flag; it opens a client socket */ |
| 96 | #define SOCKOPEN_CLIENT 0 |
| 97 | /* 'server' flag; it opens a server socket */ |
| 98 | #define SOCKOPEN_SERVER 1 |
| 99 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Flags for sock_recv(). |
| 102 | */ |
| 103 | #define SOCK_RECEIVEALL_NO 0x00000000 /* Don't wait to receive all data */ |
| 104 | #define SOCK_RECEIVEALL_YES 0x00000001 /* Wait to receive all data */ |
| 105 | |
| 106 | #define SOCK_EOF_ISNT_ERROR 0x00000000 /* Return 0 on EOF */ |
| 107 | #define SOCK_EOF_IS_ERROR 0x00000002 /* Return an error on EOF */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 108 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 109 | #define SOCK_MSG_PEEK 0x00000004 /* Return data but leave it in the socket queue */ |
| 110 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 111 | /* |
| 112 | * \} |
| 113 | */ |
| 114 | |
| 115 | #ifdef __cplusplus |
| 116 | extern "C" { |
| 117 | #endif |
| 118 | |
| 119 | /* |
| 120 | * \defgroup ExportedFunc Exported Functions |
| 121 | */ |
| 122 | |
| 123 | /* |
| 124 | * \addtogroup ExportedFunc |
| 125 | * \{ |
| 126 | */ |
| 127 | |
| 128 | int sock_init(char *errbuf, int errbuflen); |
| 129 | void sock_cleanup(void); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 130 | void sock_fmterror(const char *caller, int errcode, char *errbuf, int errbuflen); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 131 | void sock_geterror(const char *caller, char *errbuf, int errbufsize); |
| 132 | int sock_initaddress(const char *address, const char *port, |
| 133 | struct addrinfo *hints, struct addrinfo **addrinfo, |
| 134 | char *errbuf, int errbuflen); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 135 | int sock_recv(SOCKET sock, SSL *, void *buffer, size_t size, int receiveall, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 136 | char *errbuf, int errbuflen); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 137 | int sock_recv_dgram(SOCKET sock, SSL *, void *buffer, size_t size, |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 138 | char *errbuf, int errbuflen); |
| 139 | SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf, int errbuflen); |
| 140 | int sock_close(SOCKET sock, char *errbuf, int errbuflen); |
| 141 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 142 | int sock_send(SOCKET sock, SSL *, const char *buffer, size_t size, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 143 | char *errbuf, int errbuflen); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 144 | int sock_bufferize(const char *buffer, int size, char *tempbuf, int *offset, int totsize, int checkonly, char *errbuf, int errbuflen); |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 145 | int sock_discard(SOCKET sock, SSL *, int size, char *errbuf, int errbuflen); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 146 | int sock_check_hostlist(char *hostlist, const char *sep, struct sockaddr_storage *from, char *errbuf, int errbuflen); |
| 147 | int sock_cmpaddr(struct sockaddr_storage *first, struct sockaddr_storage *second); |
| 148 | |
| 149 | int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen); |
| 150 | |
| 151 | int sock_getascii_addrport(const struct sockaddr_storage *sockaddr, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen); |
| 152 | int sock_present2network(const char *address, struct sockaddr_storage *sockaddr, int addr_family, char *errbuf, int errbuflen); |
| 153 | |
| 154 | #ifdef __cplusplus |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | /* |
| 159 | * \} |
| 160 | */ |
| 161 | |
| 162 | /* |
| 163 | * \} |
| 164 | */ |
| 165 | |
| 166 | #endif |