1 /* $Id: natpmp.h,v 1.11 2009/02/27 22:38:05 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 #ifndef _MSC_VER
25 #include <sys/time.h>
26 #endif
27 #ifdef WIN32
28 #include <winsock2.h>
29 #ifdef _MSC_VER
30 typedef unsigned __int32 uint32_t;
31 typedef unsigned __int16 uint16_t;
32 #else
33 #include <stdint.h>
34 #endif
35 #define in_addr_t uint32_t
36 #include "declspec.h"
37 #else
38 #define LIBSPEC
39 #include <netinet/in.h>
40 #endif
41 
42 typedef struct {
43 	int s;	/* socket */
44 	in_addr_t gateway;	/* default gateway (IPv4) */
45 	int has_pending_request;
46 	char pending_request[12];
47 	int pending_request_len;
48 	int try_number;
49 	struct timeval retry_time;
50 } natpmp_t;
51 
52 typedef struct {
53 	uint16_t type;	/* NATPMP_RESPTYPE_* */
54 	uint16_t resultcode;	/* NAT-PMP response code */
55 	uint32_t epoch;	/* Seconds since start of epoch */
56 	union {
57 		struct {
58 			//in_addr_t addr;
59 			struct in_addr addr;
60 		} publicaddress;
61 		struct {
62 			uint16_t privateport;
63 			uint16_t mappedpublicport;
64 			uint32_t lifetime;
65 		} newportmapping;
66 	} pnu;
67 } natpmpresp_t;
68 
69 /* possible values for type field of natpmpresp_t */
70 #define NATPMP_RESPTYPE_PUBLICADDRESS (0)
71 #define NATPMP_RESPTYPE_UDPPORTMAPPING (1)
72 #define NATPMP_RESPTYPE_TCPPORTMAPPING (2)
73 
74 /* Values to pass to sendnewportmappingrequest() */
75 #define NATPMP_PROTOCOL_UDP (1)
76 #define NATPMP_PROTOCOL_TCP (2)
77 
78 /* return values */
79 /* NATPMP_ERR_INVALIDARGS : invalid arguments passed to the function */
80 #define NATPMP_ERR_INVALIDARGS (-1)
81 /* NATPMP_ERR_SOCKETERROR : socket() failed. check errno for details */
82 #define NATPMP_ERR_SOCKETERROR (-2)
83 /* NATPMP_ERR_CANNOTGETGATEWAY : can't get default gateway IP */
84 #define NATPMP_ERR_CANNOTGETGATEWAY (-3)
85 /* NATPMP_ERR_CLOSEERR : close() failed. check errno for details */
86 #define NATPMP_ERR_CLOSEERR (-4)
87 /* NATPMP_ERR_RECVFROM : recvfrom() failed. check errno for details */
88 #define NATPMP_ERR_RECVFROM (-5)
89 /* NATPMP_ERR_NOPENDINGREQ : readnatpmpresponseorretry() called while
90  * no NAT-PMP request was pending */
91 #define NATPMP_ERR_NOPENDINGREQ (-6)
92 /* NATPMP_ERR_NOGATEWAYSUPPORT : the gateway does not support NAT-PMP */
93 #define NATPMP_ERR_NOGATEWAYSUPPORT (-7)
94 /* NATPMP_ERR_CONNECTERR : connect() failed. check errno for details */
95 #define NATPMP_ERR_CONNECTERR (-8)
96 /* NATPMP_ERR_WRONGPACKETSOURCE : packet not received from the network gateway */
97 #define NATPMP_ERR_WRONGPACKETSOURCE (-9)
98 /* NATPMP_ERR_SENDERR : send() failed. check errno for details */
99 #define NATPMP_ERR_SENDERR (-10)
100 /* NATPMP_ERR_FCNTLERROR : fcntl() failed. check errno for details */
101 #define NATPMP_ERR_FCNTLERROR (-11)
102 /* NATPMP_ERR_GETTIMEOFDAYERR : gettimeofday() failed. check errno for details */
103 #define NATPMP_ERR_GETTIMEOFDAYERR (-12)
104 
105 /* */
106 #define NATPMP_ERR_UNSUPPORTEDVERSION (-14)
107 #define NATPMP_ERR_UNSUPPORTEDOPCODE (-15)
108 
109 /* Errors from the server : */
110 #define NATPMP_ERR_UNDEFINEDERROR (-49)
111 #define NATPMP_ERR_NOTAUTHORIZED (-51)
112 #define NATPMP_ERR_NETWORKFAILURE (-52)
113 #define NATPMP_ERR_OUTOFRESOURCES (-53)
114 
115 /* NATPMP_TRYAGAIN : no data available for the moment. try again later */
116 #define NATPMP_TRYAGAIN (-100)
117 
118 /* initnatpmp()
119  * initialize a natpmp_t object
120  * Return values :
121  * 0 = OK
122  * NATPMP_ERR_INVALIDARGS
123  * NATPMP_ERR_SOCKETERROR
124  * NATPMP_ERR_FCNTLERROR
125  * NATPMP_ERR_CANNOTGETGATEWAY
126  * NATPMP_ERR_CONNECTERR */
127 LIBSPEC int initnatpmp(natpmp_t * p);
128 
129 /* closenatpmp()
130  * close resources associated with a natpmp_t object
131  * Return values :
132  * 0 = OK
133  * NATPMP_ERR_INVALIDARGS
134  * NATPMP_ERR_CLOSEERR */
135 LIBSPEC int closenatpmp(natpmp_t * p);
136 
137 /* sendpublicaddressrequest()
138  * send a public address NAT-PMP request to the network gateway
139  * Return values :
140  * 2 = OK (size of the request)
141  * NATPMP_ERR_INVALIDARGS
142  * NATPMP_ERR_SENDERR */
143 LIBSPEC int sendpublicaddressrequest(natpmp_t * p);
144 
145 /* sendnewportmappingrequest()
146  * send a new port mapping NAT-PMP request to the network gateway
147  * Arguments :
148  * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP,
149  * lifetime is in seconds.
150  * To remove a port mapping, set lifetime to zero.
151  * To remove all port mappings to the host, set lifetime and both ports
152  * to zero.
153  * Return values :
154  * 12 = OK (size of the request)
155  * NATPMP_ERR_INVALIDARGS
156  * NATPMP_ERR_SENDERR */
157 LIBSPEC int sendnewportmappingrequest(natpmp_t * p, int protocol,
158                               uint16_t privateport, uint16_t publicport,
159 							  uint32_t lifetime);
160 
161 /* getnatpmprequesttimeout()
162  * fills the timeval structure with the timeout duration of the
163  * currently pending NAT-PMP request.
164  * Return values :
165  * 0 = OK
166  * NATPMP_ERR_INVALIDARGS
167  * NATPMP_ERR_GETTIMEOFDAYERR
168  * NATPMP_ERR_NOPENDINGREQ */
169 LIBSPEC int getnatpmprequesttimeout(natpmp_t * p, struct timeval * timeout);
170 
171 /* readnatpmpresponseorretry()
172  * fills the natpmpresp_t structure if possible
173  * Return values :
174  * 0 = OK
175  * NATPMP_TRYAGAIN
176  * NATPMP_ERR_INVALIDARGS
177  * NATPMP_ERR_NOPENDINGREQ
178  * NATPMP_ERR_NOGATEWAYSUPPORT
179  * NATPMP_ERR_RECVFROM
180  * NATPMP_ERR_WRONGPACKETSOURCE
181  * NATPMP_ERR_UNSUPPORTEDVERSION
182  * NATPMP_ERR_UNSUPPORTEDOPCODE
183  * NATPMP_ERR_NOTAUTHORIZED
184  * NATPMP_ERR_NETWORKFAILURE
185  * NATPMP_ERR_OUTOFRESOURCES
186  * NATPMP_ERR_UNSUPPORTEDOPCODE
187  * NATPMP_ERR_UNDEFINEDERROR */
188 LIBSPEC int readnatpmpresponseorretry(natpmp_t * p, natpmpresp_t * response);
189 
190 #ifdef ENABLE_STRNATPMPERR
191 LIBSPEC const char * strnatpmperr(int t);
192 #endif
193 
194 #endif
195