1 | /****************************************************************************** |
---|
2 | * $Id: net.h 7468 2008-12-22 19:14:43Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 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 <winsock2.h> |
---|
35 | typedef int socklen_t; |
---|
36 | #else |
---|
37 | #include <sys/types.h> |
---|
38 | #include <sys/socket.h> |
---|
39 | #include <netinet/in.h> |
---|
40 | #include <arpa/inet.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifdef WIN32 |
---|
44 | #define ECONNREFUSED WSAECONNREFUSED |
---|
45 | #define ECONNRESET WSAECONNRESET |
---|
46 | #define EHOSTUNREACH WSAEHOSTUNREACH |
---|
47 | #define EINPROGRESS WSAEINPROGRESS |
---|
48 | #define ENOTCONN WSAENOTCONN |
---|
49 | #define EWOULDBLOCK WSAEWOULDBLOCK |
---|
50 | #define sockerrno WSAGetLastError( ) |
---|
51 | #else |
---|
52 | #include <errno.h> |
---|
53 | #define sockerrno errno |
---|
54 | #endif |
---|
55 | |
---|
56 | struct tr_session; |
---|
57 | |
---|
58 | typedef enum tr_address_type |
---|
59 | { |
---|
60 | TR_AF_INET, |
---|
61 | TR_AF_INET6 |
---|
62 | } tr_address_type; |
---|
63 | |
---|
64 | typedef struct tr_address |
---|
65 | { |
---|
66 | tr_address_type type; |
---|
67 | union { |
---|
68 | /* The order here is important for tr_in{,6}addr_any initialization, |
---|
69 | * since we can't use C99 designated initializers */ |
---|
70 | struct in6_addr addr6; |
---|
71 | struct in_addr addr4; |
---|
72 | } addr; |
---|
73 | } tr_address; |
---|
74 | |
---|
75 | extern const tr_address tr_inaddr_any; |
---|
76 | extern const tr_address tr_in6addr_any; |
---|
77 | |
---|
78 | const char *tr_ntop( const tr_address * src, |
---|
79 | char * dst, |
---|
80 | int size ); |
---|
81 | const char *tr_ntop_non_ts( const tr_address * src ); |
---|
82 | tr_address *tr_pton( const char * src, |
---|
83 | tr_address * dst ); |
---|
84 | int tr_compareAddresses( const tr_address * a, |
---|
85 | const tr_address * b); |
---|
86 | void tr_normalizeV4Mapped( tr_address * const addr ); |
---|
87 | |
---|
88 | void tr_suspectAddress( const tr_address * a, const char * source ); |
---|
89 | tr_bool tr_isAddress( const tr_address * a ); |
---|
90 | |
---|
91 | typedef struct tr_net_af_support |
---|
92 | { |
---|
93 | tr_bool has_inet6; |
---|
94 | tr_bool needs_inet4; |
---|
95 | } tr_net_af_support; |
---|
96 | |
---|
97 | tr_net_af_support tr_net_getAFSupport( tr_port ); |
---|
98 | |
---|
99 | /*********************************************************************** |
---|
100 | * Socket list housekeeping |
---|
101 | **********************************************************************/ |
---|
102 | typedef struct tr_socketList tr_socketList; |
---|
103 | tr_socketList *tr_socketListAppend( tr_socketList * const head, |
---|
104 | const tr_address * const addr ); |
---|
105 | tr_socketList *tr_socketListNew( const tr_address * const addr ); |
---|
106 | void tr_socketListFree( tr_socketList * const head ); |
---|
107 | void tr_socketListRemove( tr_socketList * const head, |
---|
108 | tr_socketList * const el); |
---|
109 | void tr_socketListTruncate( tr_socketList * const head, |
---|
110 | tr_socketList * const start ); |
---|
111 | int tr_socketListGetSocket( const tr_socketList * const el ); |
---|
112 | const tr_address *tr_socketListGetAddress( const tr_socketList * const el ); |
---|
113 | void tr_socketListForEach( tr_socketList * const head, |
---|
114 | void ( * cb ) ( int * const, |
---|
115 | tr_address * const, |
---|
116 | void * const ), |
---|
117 | void * const userData); |
---|
118 | |
---|
119 | /*********************************************************************** |
---|
120 | * Sockets |
---|
121 | **********************************************************************/ |
---|
122 | int tr_netOpenTCP( tr_session * session, |
---|
123 | const tr_address * addr, |
---|
124 | tr_port port ); |
---|
125 | |
---|
126 | int tr_netBindTCP( const tr_address * addr, |
---|
127 | tr_port port, |
---|
128 | tr_bool suppressMsgs ); |
---|
129 | |
---|
130 | int tr_netAccept( tr_session * session, |
---|
131 | int bound, |
---|
132 | tr_address * setme_addr, |
---|
133 | tr_port * setme_port ); |
---|
134 | |
---|
135 | int tr_netSetTOS( int s, |
---|
136 | int tos ); |
---|
137 | |
---|
138 | void tr_netClose( int s ); |
---|
139 | |
---|
140 | void tr_netInit( void ); |
---|
141 | |
---|
142 | #endif /* _TR_NET_H_ */ |
---|