source: trunk/libtransmission/net.h @ 12545

Last change on this file since 12545 was 12229, checked in by jordan, 11 years ago

(trunk libT) API cleanup of the tr_address functions to make them more consistent.

This is loosely related to #2910, but only in the sense of laying the groundwork for #2910's fix...

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