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

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

update to libnatpmp-20071213

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