natpmp.h 7.2 KB

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