source: trunk/libtransmission/net.h @ 14528

Last change on this file since 14528 was 14528, checked in by mikedld, 6 years ago

Fix some issues revealed by coverity

  • Property svn:keywords set to Date Rev Author Id
File size: 5.8 KB
Line 
1/******************************************************************************
2 * $Id: net.h 14528 2015-05-09 14:10:55Z mikedld $
3 *
4 * Copyright (c) Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef __TRANSMISSION__
26#error only libtransmission should #include this header.
27#endif
28
29#ifndef _TR_NET_H_
30#define _TR_NET_H_
31
32#ifdef _WIN32
33 #include <inttypes.h>
34 #include <ws2tcpip.h>
35#else
36 #include <errno.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39#endif
40
41#ifdef _WIN32
42 typedef SOCKET tr_socket_t;
43 #define TR_BAD_SOCKET INVALID_SOCKET
44 #define TR_PRI_SOCK "Id" /* intentionally signed to print -1 nicely. */
45
46 #undef  EADDRINUSE
47 #define EADDRINUSE              WSAEADDRINUSE
48 #undef  ECONNREFUSED
49 #define ECONNREFUSED            WSAECONNREFUSED
50 #undef  ECONNRESET
51 #define ECONNRESET              WSAECONNRESET
52 #undef  EHOSTUNREACH
53 #define EHOSTUNREACH            WSAEHOSTUNREACH
54 #undef  EINPROGRESS
55 #define EINPROGRESS             WSAEINPROGRESS
56 #undef  ENOTCONN
57 #define ENOTCONN                WSAENOTCONN
58 #undef  EWOULDBLOCK
59 #define EWOULDBLOCK             WSAEWOULDBLOCK
60 #undef  EAFNOSUPPORT
61 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
62 #undef  ENETUNREACH
63 #define ENETUNREACH             WSAENETUNREACH
64
65 #define sockerrno               WSAGetLastError ()
66#else
67 /** @brief Platform-specific socket descriptor type. */
68 typedef int tr_socket_t;
69 /** @brief Platform-specific invalid socket descriptor constant. */
70 #define TR_BAD_SOCKET (-1)
71 #define TR_PRI_SOCK "d"
72
73 #define sockerrno errno
74#endif
75
76/****
77*****
78*****  tr_address
79*****
80****/
81
82typedef enum tr_address_type
83{
84    TR_AF_INET,
85    TR_AF_INET6,
86    NUM_TR_AF_INET_TYPES
87}
88tr_address_type;
89
90typedef struct tr_address
91{
92    tr_address_type type;
93    union {
94        /* The order here is important for tr_in{,6}addr_any initialization,
95         * since we can't use C99 designated initializers */
96        struct in6_addr addr6;
97        struct in_addr  addr4;
98    } addr;
99} tr_address;
100
101extern const tr_address tr_inaddr_any;
102extern const tr_address tr_in6addr_any;
103
104const char* tr_address_to_string (const tr_address * addr);
105
106const char* tr_address_to_string_with_buf (const tr_address  * addr,
107                                           char              * buf,
108                                           size_t              buflen);
109
110bool tr_address_from_string (tr_address  * setme,
111                              const char  * string);
112
113bool tr_address_from_sockaddr_storage (tr_address                     * setme,
114                                       tr_port                        * port,
115                                       const struct sockaddr_storage  * src);
116
117int tr_address_compare (const tr_address * a,
118                        const tr_address * b);
119
120bool tr_address_is_valid_for_peers (const tr_address  * addr,
121                                    tr_port             port);
122
123static inline bool
124tr_address_is_valid (const tr_address * a)
125{
126    return (a != NULL) && (a->type==TR_AF_INET || a->type==TR_AF_INET6);
127}
128
129/***********************************************************************
130 * Sockets
131 **********************************************************************/
132
133struct tr_session;
134
135tr_socket_t tr_netOpenPeerSocket (tr_session       * session,
136                                  const tr_address * addr,
137                                  tr_port            port,
138                                  bool               clientIsSeed);
139
140struct UTPSocket *
141tr_netOpenPeerUTPSocket (tr_session        * session,
142                         const tr_address  * addr,
143                         tr_port             port,
144                         bool                clientIsSeed);
145
146tr_socket_t tr_netBindTCP (const tr_address * addr,
147                           tr_port            port,
148                           bool               suppressMsgs);
149
150tr_socket_t tr_netAccept (tr_session  * session,
151                          tr_socket_t   bound,
152                          tr_address  * setme_addr,
153                          tr_port     * setme_port);
154
155void tr_netSetTOS (tr_socket_t s,
156                   int         tos);
157
158void tr_netSetCongestionControl (tr_socket_t   s,
159                                 const char  * algorithm);
160
161void tr_netClose (tr_session  * session,
162                  tr_socket_t   s);
163
164void tr_netCloseSocket (tr_socket_t fd);
165
166void tr_netInit (void);
167
168bool tr_net_hasIPv6 (tr_port);
169
170
171/**
172 * @brief get a human-representable string representing the network error.
173 * @param err an errno on Unix/Linux and an WSAError on win32)
174 */
175char* tr_net_strerror (char * buf, size_t buflen, int err);
176
177const unsigned char *tr_globalIPv6 (void);
178
179#if defined (_WIN32) && !defined (QT_DLL)
180/* The QT exclusion is because something clashes whith the next include */
181#include <ws2tcpip.h> /* socklen_t */
182
183#endif
184
185#endif /* _TR_NET_H_ */
Note: See TracBrowser for help on using the repository browser.