1 | /* $Id: natpmp.h,v 1.10 2008/07/02 22:33:06 nanard Exp $ */ |
---|
2 | /* libnatpmp |
---|
3 | * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr> |
---|
4 | * http://miniupnp.free.fr/libnatpmp.html |
---|
5 | * |
---|
6 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
7 | * purpose with or without fee is hereby granted, provided that the above |
---|
8 | * copyright notice and this permission notice appear in all copies. |
---|
9 | * |
---|
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
---|
17 | #ifndef __NATPMP_H__ |
---|
18 | #define __NATPMP_H__ |
---|
19 | |
---|
20 | /* NAT-PMP Port as defined by the NAT-PMP draft */ |
---|
21 | #define NATPMP_PORT (5351) |
---|
22 | |
---|
23 | #include <time.h> |
---|
24 | #include <sys/time.h> |
---|
25 | #ifdef WIN32 |
---|
26 | #include <winsock2.h> |
---|
27 | #include <stdint.h> |
---|
28 | #define in_addr_t uint32_t |
---|
29 | #else |
---|
30 | #include <netinet/in.h> |
---|
31 | #endif |
---|
32 | #include "declspec.h" |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | int s; /* socket */ |
---|
36 | in_addr_t gateway; /* default gateway (IPv4) */ |
---|
37 | int has_pending_request; |
---|
38 | unsigned char pending_request[12]; |
---|
39 | int pending_request_len; |
---|
40 | int try_number; |
---|
41 | struct timeval retry_time; |
---|
42 | } natpmp_t; |
---|
43 | |
---|
44 | typedef struct { |
---|
45 | uint16_t type; /* NATPMP_RESPTYPE_* */ |
---|
46 | uint16_t resultcode; /* NAT-PMP response code */ |
---|
47 | uint32_t epoch; /* Seconds since start of epoch */ |
---|
48 | union { |
---|
49 | struct { |
---|
50 | //in_addr_t addr; |
---|
51 | struct in_addr addr; |
---|
52 | } publicaddress; |
---|
53 | struct { |
---|
54 | uint16_t privateport; |
---|
55 | uint16_t mappedpublicport; |
---|
56 | uint32_t lifetime; |
---|
57 | } newportmapping; |
---|
58 | } pnu; |
---|
59 | } natpmpresp_t; |
---|
60 | |
---|
61 | /* possible values for type field of natpmpresp_t */ |
---|
62 | #define NATPMP_RESPTYPE_PUBLICADDRESS (0) |
---|
63 | #define NATPMP_RESPTYPE_UDPPORTMAPPING (1) |
---|
64 | #define NATPMP_RESPTYPE_TCPPORTMAPPING (2) |
---|
65 | |
---|
66 | /* Values to pass to sendnewportmappingrequest() */ |
---|
67 | #define NATPMP_PROTOCOL_UDP (1) |
---|
68 | #define NATPMP_PROTOCOL_TCP (2) |
---|
69 | |
---|
70 | /* return values */ |
---|
71 | /* NATPMP_ERR_INVALIDARGS : invalid arguments passed to the function */ |
---|
72 | #define NATPMP_ERR_INVALIDARGS (-1) |
---|
73 | /* NATPMP_ERR_SOCKETERROR : socket() failed. check errno for details */ |
---|
74 | #define NATPMP_ERR_SOCKETERROR (-2) |
---|
75 | /* NATPMP_ERR_CANNOTGETGATEWAY : can't get default gateway IP */ |
---|
76 | #define NATPMP_ERR_CANNOTGETGATEWAY (-3) |
---|
77 | /* NATPMP_ERR_CLOSEERR : close() failed. check errno for details */ |
---|
78 | #define NATPMP_ERR_CLOSEERR (-4) |
---|
79 | /* NATPMP_ERR_RECVFROM : recvfrom() failed. check errno for details */ |
---|
80 | #define NATPMP_ERR_RECVFROM (-5) |
---|
81 | /* NATPMP_ERR_NOPENDINGREQ : readnatpmpresponseorretry() called while |
---|
82 | * no NAT-PMP request was pending */ |
---|
83 | #define NATPMP_ERR_NOPENDINGREQ (-6) |
---|
84 | /* NATPMP_ERR_NOGATEWAYSUPPORT : the gateway does not support NAT-PMP */ |
---|
85 | #define NATPMP_ERR_NOGATEWAYSUPPORT (-7) |
---|
86 | /* NATPMP_ERR_CONNECTERR : connect() failed. check errno for details */ |
---|
87 | #define NATPMP_ERR_CONNECTERR (-8) |
---|
88 | /* NATPMP_ERR_WRONGPACKETSOURCE : packet not received from the network gateway */ |
---|
89 | #define NATPMP_ERR_WRONGPACKETSOURCE (-9) |
---|
90 | /* NATPMP_ERR_SENDERR : send() failed. check errno for details */ |
---|
91 | #define NATPMP_ERR_SENDERR (-10) |
---|
92 | /* NATPMP_ERR_FCNTLERROR : fcntl() failed. check errno for details */ |
---|
93 | #define NATPMP_ERR_FCNTLERROR (-11) |
---|
94 | /* NATPMP_ERR_GETTIMEOFDAYERR : gettimeofday() failed. check errno for details */ |
---|
95 | #define NATPMP_ERR_GETTIMEOFDAYERR (-12) |
---|
96 | |
---|
97 | /* */ |
---|
98 | #define NATPMP_ERR_UNSUPPORTEDVERSION (-14) |
---|
99 | #define NATPMP_ERR_UNSUPPORTEDOPCODE (-15) |
---|
100 | |
---|
101 | /* Errors from the server : */ |
---|
102 | #define NATPMP_ERR_UNDEFINEDERROR (-49) |
---|
103 | #define NATPMP_ERR_NOTAUTHORIZED (-51) |
---|
104 | #define NATPMP_ERR_NETWORKFAILURE (-52) |
---|
105 | #define NATPMP_ERR_OUTOFRESOURCES (-53) |
---|
106 | |
---|
107 | /* NATPMP_TRYAGAIN : no data available for the moment. try again later */ |
---|
108 | #define NATPMP_TRYAGAIN (-100) |
---|
109 | |
---|
110 | /* initnatpmp() |
---|
111 | * initialize a natpmp_t object |
---|
112 | * Return values : |
---|
113 | * 0 = OK |
---|
114 | * NATPMP_ERR_INVALIDARGS |
---|
115 | * NATPMP_ERR_SOCKETERROR |
---|
116 | * NATPMP_ERR_FCNTLERROR |
---|
117 | * NATPMP_ERR_CANNOTGETGATEWAY |
---|
118 | * NATPMP_ERR_CONNECTERR */ |
---|
119 | LIBSPEC int initnatpmp(natpmp_t * p); |
---|
120 | |
---|
121 | /* closenatpmp() |
---|
122 | * close resources associated with a natpmp_t object |
---|
123 | * Return values : |
---|
124 | * 0 = OK |
---|
125 | * NATPMP_ERR_INVALIDARGS |
---|
126 | * NATPMP_ERR_CLOSEERR */ |
---|
127 | LIBSPEC int closenatpmp(natpmp_t * p); |
---|
128 | |
---|
129 | /* sendpublicaddressrequest() |
---|
130 | * send a public address NAT-PMP request to the network gateway |
---|
131 | * Return values : |
---|
132 | * 2 = OK (size of the request) |
---|
133 | * NATPMP_ERR_INVALIDARGS |
---|
134 | * NATPMP_ERR_SENDERR */ |
---|
135 | LIBSPEC int sendpublicaddressrequest(natpmp_t * p); |
---|
136 | |
---|
137 | /* sendnewportmappingrequest() |
---|
138 | * send a new port mapping NAT-PMP request to the network gateway |
---|
139 | * Arguments : |
---|
140 | * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP, |
---|
141 | * lifetime is in seconds. |
---|
142 | * To remove a port mapping, set lifetime to zero. |
---|
143 | * To remove all port mappings to the host, set lifetime and both ports |
---|
144 | * to zero. |
---|
145 | * Return values : |
---|
146 | * 12 = OK (size of the request) |
---|
147 | * NATPMP_ERR_INVALIDARGS |
---|
148 | * NATPMP_ERR_SENDERR */ |
---|
149 | LIBSPEC int sendnewportmappingrequest(natpmp_t * p, int protocol, |
---|
150 | uint16_t privateport, uint16_t publicport, |
---|
151 | uint32_t lifetime); |
---|
152 | |
---|
153 | /* getnatpmprequesttimeout() |
---|
154 | * fills the timeval structure with the timeout duration of the |
---|
155 | * currently pending NAT-PMP request. |
---|
156 | * Return values : |
---|
157 | * 0 = OK |
---|
158 | * NATPMP_ERR_INVALIDARGS |
---|
159 | * NATPMP_ERR_GETTIMEOFDAYERR |
---|
160 | * NATPMP_ERR_NOPENDINGREQ */ |
---|
161 | LIBSPEC int getnatpmprequesttimeout(natpmp_t * p, struct timeval * timeout); |
---|
162 | |
---|
163 | /* readnatpmpresponseorretry() |
---|
164 | * fills the natpmpresp_t structure if possible |
---|
165 | * Return values : |
---|
166 | * 0 = OK |
---|
167 | * NATPMP_TRYAGAIN |
---|
168 | * NATPMP_ERR_INVALIDARGS |
---|
169 | * NATPMP_ERR_NOPENDINGREQ |
---|
170 | * NATPMP_ERR_NOGATEWAYSUPPORT |
---|
171 | * NATPMP_ERR_RECVFROM |
---|
172 | * NATPMP_ERR_WRONGPACKETSOURCE |
---|
173 | * NATPMP_ERR_UNSUPPORTEDVERSION |
---|
174 | * NATPMP_ERR_UNSUPPORTEDOPCODE |
---|
175 | * NATPMP_ERR_NOTAUTHORIZED |
---|
176 | * NATPMP_ERR_NETWORKFAILURE |
---|
177 | * NATPMP_ERR_OUTOFRESOURCES |
---|
178 | * NATPMP_ERR_UNSUPPORTEDOPCODE |
---|
179 | * NATPMP_ERR_UNDEFINEDERROR */ |
---|
180 | LIBSPEC int readnatpmpresponseorretry(natpmp_t * p, natpmpresp_t * response); |
---|
181 | |
---|
182 | #ifdef ENABLE_STRNATPMPERR |
---|
183 | LIBSPEC const char * strnatpmperr(int t); |
---|
184 | #endif |
---|
185 | |
---|
186 | #endif |
---|