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

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

(trunk libnatpmp) update to new version

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