source: trunk/libtransmission/net.h @ 7652

Last change on this file since 7652 was 7652, checked in by charles, 13 years ago

(trunk libT) win32 portability fixes, reported by Alexey

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