2
0

natpmp.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* NAT-PMP Port as defined by the NAT-PMP draft */
  20. #define NATPMP_PORT (5351)
  21. #include <time.h>
  22. #ifndef _MSC_VER
  23. #include <sys/time.h>
  24. #endif
  25. #ifdef WIN32
  26. #include <winsock2.h>
  27. #ifdef _MSC_VER
  28. typedef unsigned __int32 uint32_t;
  29. typedef unsigned __int16 uint16_t;
  30. #else
  31. #include <stdint.h>
  32. #endif
  33. #define in_addr_t uint32_t
  34. #include "declspec.h"
  35. #else
  36. #define LIBSPEC
  37. #include <netinet/in.h>
  38. #endif
  39. typedef struct {
  40. int s; /* socket */
  41. in_addr_t gateway; /* default gateway (IPv4) */
  42. int has_pending_request;
  43. char pending_request[12];
  44. int pending_request_len;
  45. int try_number;
  46. struct timeval retry_time;
  47. } natpmp_t;
  48. typedef struct {
  49. uint16_t type; /* NATPMP_RESPTYPE_* */
  50. uint16_t resultcode; /* NAT-PMP response code */
  51. uint32_t epoch; /* Seconds since start of epoch */
  52. union {
  53. struct {
  54. //in_addr_t addr;
  55. struct in_addr addr;
  56. } publicaddress;
  57. struct {
  58. uint16_t privateport;
  59. uint16_t mappedpublicport;
  60. uint32_t lifetime;
  61. } newportmapping;
  62. } pnu;
  63. } natpmpresp_t;
  64. /* possible values for type field of natpmpresp_t */
  65. #define NATPMP_RESPTYPE_PUBLICADDRESS (0)
  66. #define NATPMP_RESPTYPE_UDPPORTMAPPING (1)
  67. #define NATPMP_RESPTYPE_TCPPORTMAPPING (2)
  68. /* Values to pass to sendnewportmappingrequest() */
  69. #define NATPMP_PROTOCOL_UDP (1)
  70. #define NATPMP_PROTOCOL_TCP (2)
  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. #define NATPMP_ERR_UNSUPPORTEDVERSION (-14)
  99. #define NATPMP_ERR_UNSUPPORTEDOPCODE (-15)
  100. /* Errors from the server : */
  101. #define NATPMP_ERR_UNDEFINEDERROR (-49)
  102. #define NATPMP_ERR_NOTAUTHORIZED (-51)
  103. #define NATPMP_ERR_NETWORKFAILURE (-52)
  104. #define NATPMP_ERR_OUTOFRESOURCES (-53)
  105. /* NATPMP_TRYAGAIN : no data available for the moment. try again later */
  106. #define NATPMP_TRYAGAIN (-100)
  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 */
  116. LIBSPEC int initnatpmp(natpmp_t * p);
  117. /* closenatpmp()
  118. * close resources associated with a natpmp_t object
  119. * Return values :
  120. * 0 = OK
  121. * NATPMP_ERR_INVALIDARGS
  122. * NATPMP_ERR_CLOSEERR */
  123. LIBSPEC int closenatpmp(natpmp_t * p);
  124. /* sendpublicaddressrequest()
  125. * send a public address NAT-PMP request to the network gateway
  126. * Return values :
  127. * 2 = OK (size of the request)
  128. * NATPMP_ERR_INVALIDARGS
  129. * NATPMP_ERR_SENDERR */
  130. LIBSPEC int sendpublicaddressrequest(natpmp_t * p);
  131. /* sendnewportmappingrequest()
  132. * send a new port mapping NAT-PMP request to the network gateway
  133. * Arguments :
  134. * protocol is either NATPMP_PROTOCOL_TCP or NATPMP_PROTOCOL_UDP,
  135. * lifetime is in seconds.
  136. * To remove a port mapping, set lifetime to zero.
  137. * To remove all port mappings to the host, set lifetime and both ports
  138. * to zero.
  139. * Return values :
  140. * 12 = OK (size of the request)
  141. * NATPMP_ERR_INVALIDARGS
  142. * NATPMP_ERR_SENDERR */
  143. LIBSPEC int sendnewportmappingrequest(natpmp_t * p, int protocol,
  144. uint16_t privateport, uint16_t publicport,
  145. uint32_t lifetime);
  146. /* getnatpmprequesttimeout()
  147. * fills the timeval structure with the timeout duration of the
  148. * currently pending NAT-PMP request.
  149. * Return values :
  150. * 0 = OK
  151. * NATPMP_ERR_INVALIDARGS
  152. * NATPMP_ERR_GETTIMEOFDAYERR
  153. * NATPMP_ERR_NOPENDINGREQ */
  154. LIBSPEC int getnatpmprequesttimeout(natpmp_t * p, struct timeval * timeout);
  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 */
  172. LIBSPEC int readnatpmpresponseorretry(natpmp_t * p, natpmpresp_t * response);
  173. #ifdef ENABLE_STRNATPMPERR
  174. LIBSPEC const char * strnatpmperr(int t);
  175. #endif
  176. #endif