source: trunk/third-party/libnatpmp/natpmp.h @ 5970

Last change on this file since 5970 was 5970, checked in by charles, 14 years ago

#972: Update to the 2008/05/29 snapshot of libnatpmp

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