1/* $Id: natpmp.h,v 1.15 2011/07/15 08:30:11 nanard Exp $ */ 2/* libnatpmp 3Copyright (c) 2007-2011, Thomas BERNARD 4All rights reserved. 5 6Redistribution and use in source and binary forms, with or without 7modification, are permitted provided that the following conditions are met: 8 9 * Redistributions of source code must retain the above copyright notice, 10 this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, 12 this list of conditions and the following disclaimer in the documentation 13 and/or other materials provided with the distribution. 14 * The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27POSSIBILITY OF SUCH DAMAGE. 28*/ 29#ifndef __NATPMP_H__ 30#define __NATPMP_H__ 31 32/* NAT-PMP Port as defined by the NAT-PMP draft */ 33#define NATPMP_PORT (5351) 34 35#include <time.h> 36#if !defined(_MSC_VER) 37#include <sys/time.h> 38#endif 39#ifdef WIN32 40#include <winsock2.h> 41#if !defined(_MSC_VER) 42#include <stdint.h> 43#else 44typedef unsigned long uint32_t; 45typedef unsigned short uint16_t; 46#endif 47#define in_addr_t uint32_t 48#include "declspec.h" 49#else 50#define LIBSPEC 51#include <netinet/in.h> 52#endif 53 54typedef struct { 55 int s; /* socket */ 56 in_addr_t gateway; /* default gateway (IPv4) */ 57 int has_pending_request; 58 unsigned char pending_request[12]; 59 int pending_request_len; 60 int try_number; 61 struct timeval retry_time; 62} natpmp_t; 63 64typedef struct { 65 uint16_t type; /* NATPMP_RESPTYPE_* */ 66 uint16_t resultcode; /* NAT-PMP response code */ 67 uint32_t epoch; /* Seconds since start of epoch */ 68 union { 69 struct { 70 //in_addr_t addr; 71 struct in_addr addr; 72 } publicaddress; 73 struct { 74 uint16_t privateport; 75 uint16_t mappedpublicport; 76 uint32_t lifetime; 77 } newportmapping; 78 } pnu; 79} natpmpresp_t; 80 81/* possible values for type field of natpmpresp_t */ 82#define NATPMP_RESPTYPE_PUBLICADDRESS (0) 83#define NATPMP_RESPTYPE_UDPPORTMAPPING (1) 84#define NATPMP_RESPTYPE_TCPPORTMAPPING (2) 85 86/* Values to pass to sendnewportmappingrequest() */ 87#define NATPMP_PROTOCOL_UDP (1) 88#define NATPMP_PROTOCOL_TCP (2) 89 90/* return values */ 91/* NATPMP_ERR_INVALIDARGS : invalid arguments passed to the function */ 92#define NATPMP_ERR_INVALIDARGS (-1) 93/* NATPMP_ERR_SOCKETERROR : socket() failed. check errno for details */ 94#define NATPMP_ERR_SOCKETERROR (-2) 95/* NATPMP_ERR_CANNOTGETGATEWAY : can't get default gateway IP */ 96#define NATPMP_ERR_CANNOTGETGATEWAY (-3) 97/* NATPMP_ERR_CLOSEERR : close() failed. check errno for details */ 98#define NATPMP_ERR_CLOSEERR (-4) 99/* NATPMP_ERR_RECVFROM : recvfrom() failed. check errno for details */ 100#define NATPMP_ERR_RECVFROM (-5) 101/* NATPMP_ERR_NOPENDINGREQ : readnatpmpresponseorretry() called while 102 * no NAT-PMP request was pending */ 103#define NATPMP_ERR_NOPENDINGREQ (-6) 104/* NATPMP_ERR_NOGATEWAYSUPPORT : the gateway does not support NAT-PMP */ 105#define NATPMP_ERR_NOGATEWAYSUPPORT (-7) 106/* NATPMP_ERR_CONNECTERR : connect() failed. check errno for details */ 107#define NATPMP_ERR_CONNECTERR (-8) 108/* NATPMP_ERR_WRONGPACKETSOURCE : packet not received from the network gateway */ 109#define NATPMP_ERR_WRONGPACKETSOURCE (-9) 110/* NATPMP_ERR_SENDERR : send() failed. check errno for details */ 111#define NATPMP_ERR_SENDERR (-10) 112/* NATPMP_ERR_FCNTLERROR : fcntl() failed. check errno for details */ 113#define NATPMP_ERR_FCNTLERROR (-11) 114/* NATPMP_ERR_GETTIMEOFDAYERR : gettimeofday() failed. check errno for details */ 115#define NATPMP_ERR_GETTIMEOFDAYERR (-12) 116 117/* */ 118#define NATPMP_ERR_UNSUPPORTEDVERSION (-14) 119#define NATPMP_ERR_UNSUPPORTEDOPCODE (-15) 120 121/* Errors from the server : */ 122#define NATPMP_ERR_UNDEFINEDERROR (-49) 123#define NATPMP_ERR_NOTAUTHORIZED (-51) 124#define NATPMP_ERR_NETWORKFAILURE (-52) 125#define NATPMP_ERR_OUTOFRESOURCES (-53) 126 127/* NATPMP_TRYAGAIN : no data available for the moment. try again later */ 128#define NATPMP_TRYAGAIN (-100) 129 130#ifdef __cplusplus 131extern "C" { 132#endif 133 134/* initnatpmp() 135 * initialize a natpmp_t object 136 * With forcegw=1 the gateway is not detected automaticaly. 137 * Return values : 138 * 0 = OK 139 * NATPMP_ERR_INVALIDARGS 140 * NATPMP_ERR_SOCKETERROR 141 * NATPMP_ERR_FCNTLERROR 142 * NATPMP_ERR_CANNOTGETGATEWAY 143 * NATPMP_ERR_CONNECTERR */ 144LIBSPEC int initnatpmp(natpmp_t * p, int forcegw, in_addr_t forcedgw); 145 146/* closenatpmp() 147 * close resources associated with a natpmp_t object 148 * Return values : 149 * 0 = OK 150 * NATPMP_ERR_INVALIDARGS 151 * NATPMP_ERR_CLOSEERR */ 152LIBSPEC int closenatpmp(natpmp_t * p); 153 154/* sendpublicaddressrequest() 155 * send a public address NAT-PMP request to the network gateway 156 * Return values : 157 * 2 = OK (size of the request) 158 * NATPMP_ERR_INVALIDARGS 159 * NATPMP_ERR_SENDERR */ 160LIBSPEC int sendpublicaddressrequest(natpmp_t * p); 161 162/* sendnewportmappingrequest() 163 * send a new port mapping NAT-PMP request to the network gateway 164 * Arguments : 165 * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP, 166 * lifetime is in seconds. 167 * To remove a port mapping, set lifetime to zero. 168 * To remove all port mappings to the host, set lifetime and both ports 169 * to zero. 170 * Return values : 171 * 12 = OK (size of the request) 172 * NATPMP_ERR_INVALIDARGS 173 * NATPMP_ERR_SENDERR */ 174LIBSPEC int sendnewportmappingrequest(natpmp_t * p, int protocol, 175 uint16_t privateport, uint16_t publicport, 176 uint32_t lifetime); 177 178/* getnatpmprequesttimeout() 179 * fills the timeval structure with the timeout duration of the 180 * currently pending NAT-PMP request. 181 * Return values : 182 * 0 = OK 183 * NATPMP_ERR_INVALIDARGS 184 * NATPMP_ERR_GETTIMEOFDAYERR 185 * NATPMP_ERR_NOPENDINGREQ */ 186LIBSPEC int getnatpmprequesttimeout(natpmp_t * p, struct timeval * timeout); 187 188/* readnatpmpresponseorretry() 189 * fills the natpmpresp_t structure if possible 190 * Return values : 191 * 0 = OK 192 * NATPMP_TRYAGAIN 193 * NATPMP_ERR_INVALIDARGS 194 * NATPMP_ERR_NOPENDINGREQ 195 * NATPMP_ERR_NOGATEWAYSUPPORT 196 * NATPMP_ERR_RECVFROM 197 * NATPMP_ERR_WRONGPACKETSOURCE 198 * NATPMP_ERR_UNSUPPORTEDVERSION 199 * NATPMP_ERR_UNSUPPORTEDOPCODE 200 * NATPMP_ERR_NOTAUTHORIZED 201 * NATPMP_ERR_NETWORKFAILURE 202 * NATPMP_ERR_OUTOFRESOURCES 203 * NATPMP_ERR_UNSUPPORTEDOPCODE 204 * NATPMP_ERR_UNDEFINEDERROR */ 205LIBSPEC int readnatpmpresponseorretry(natpmp_t * p, natpmpresp_t * response); 206 207#ifdef ENABLE_STRNATPMPERR 208LIBSPEC const char * strnatpmperr(int t); 209#endif 210 211#ifdef __cplusplus 212} 213#endif 214 215#endif 216