source: trunk/libtransmission/net.h

Last change on this file was 14724, checked in by jordan, 7 years ago

use '#pragma once' instead of #ifndef..#define..#endif guards

  • Property svn:keywords set to Date Rev Author Id
File size: 5.6 KB
Line 
1/******************************************************************************
2 * $Id: net.h 14724 2016-03-29 16:37:21Z 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#pragma once
30
31#ifdef _WIN32
32 #include <inttypes.h>
33 #include <ws2tcpip.h>
34#else
35 #include <errno.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38#endif
39
40#ifdef _WIN32
41 typedef SOCKET tr_socket_t;
42 #define TR_BAD_SOCKET INVALID_SOCKET
43 #define TR_PRI_SOCK "Id" /* intentionally signed to print -1 nicely. */
44
45 #undef  EADDRINUSE
46 #define EADDRINUSE              WSAEADDRINUSE
47 #undef  ECONNREFUSED
48 #define ECONNREFUSED            WSAECONNREFUSED
49 #undef  ECONNRESET
50 #define ECONNRESET              WSAECONNRESET
51 #undef  EHOSTUNREACH
52 #define EHOSTUNREACH            WSAEHOSTUNREACH
53 #undef  EINPROGRESS
54 #define EINPROGRESS             WSAEINPROGRESS
55 #undef  ENOTCONN
56 #define ENOTCONN                WSAENOTCONN
57 #undef  EWOULDBLOCK
58 #define EWOULDBLOCK             WSAEWOULDBLOCK
59 #undef  EAFNOSUPPORT
60 #define EAFNOSUPPORT            WSAEAFNOSUPPORT
61 #undef  ENETUNREACH
62 #define ENETUNREACH             WSAENETUNREACH
63
64 #define sockerrno               WSAGetLastError ()
65#else
66 /** @brief Platform-specific socket descriptor type. */
67 typedef int tr_socket_t;
68 /** @brief Platform-specific invalid socket descriptor constant. */
69 #define TR_BAD_SOCKET (-1)
70 #define TR_PRI_SOCK "d"
71
72 #define sockerrno errno
73#endif
74
75/****
76*****
77*****  tr_address
78*****
79****/
80
81typedef enum tr_address_type
82{
83    TR_AF_INET,
84    TR_AF_INET6,
85    NUM_TR_AF_INET_TYPES
86}
87tr_address_type;
88
89typedef struct tr_address
90{
91    tr_address_type type;
92    union {
93        /* The order here is important for tr_in{,6}addr_any initialization,
94         * since we can't use C99 designated initializers */
95        struct in6_addr addr6;
96        struct in_addr  addr4;
97    } addr;
98} tr_address;
99
100extern const tr_address tr_inaddr_any;
101extern const tr_address tr_in6addr_any;
102
103const char* tr_address_to_string (const tr_address * addr);
104
105const char* tr_address_to_string_with_buf (const tr_address  * addr,
106                                           char              * buf,
107                                           size_t              buflen);
108
109bool tr_address_from_string (tr_address  * setme,
110                              const char  * string);
111
112bool tr_address_from_sockaddr_storage (tr_address                     * setme,
113                                       tr_port                        * port,
114                                       const struct sockaddr_storage  * src);
115
116int tr_address_compare (const tr_address * a,
117                        const tr_address * b);
118
119bool tr_address_is_valid_for_peers (const tr_address  * addr,
120                                    tr_port             port);
121
122static inline bool
123tr_address_is_valid (const tr_address * a)
124{
125    return (a != NULL) && (a->type==TR_AF_INET || a->type==TR_AF_INET6);
126}
127
128/***********************************************************************
129 * Sockets
130 **********************************************************************/
131
132struct tr_session;
133
134tr_socket_t tr_netOpenPeerSocket (tr_session       * session,
135                                  const tr_address * addr,
136                                  tr_port            port,
137                                  bool               clientIsSeed);
138
139struct UTPSocket *
140tr_netOpenPeerUTPSocket (tr_session        * session,
141                         const tr_address  * addr,
142                         tr_port             port,
143                         bool                clientIsSeed);
144
145tr_socket_t tr_netBindTCP (const tr_address * addr,
146                           tr_port            port,
147                           bool               suppressMsgs);
148
149tr_socket_t tr_netAccept (tr_session  * session,
150                          tr_socket_t   bound,
151                          tr_address  * setme_addr,
152                          tr_port     * setme_port);
153
154void tr_netSetTOS (tr_socket_t s,
155                   int         tos);
156
157void tr_netSetCongestionControl (tr_socket_t   s,
158                                 const char  * algorithm);
159
160void tr_netClose (tr_session  * session,
161                  tr_socket_t   s);
162
163void tr_netCloseSocket (tr_socket_t fd);
164
165bool tr_net_hasIPv6 (tr_port);
166
167
168/**
169 * @brief get a human-representable string representing the network error.
170 * @param err an errno on Unix/Linux and an WSAError on win32)
171 */
172char* tr_net_strerror (char * buf, size_t buflen, int err);
173
174const unsigned char *tr_globalIPv6 (void);
175
Note: See TracBrowser for help on using the repository browser.