natpmpc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* $Id: natpmpc.c,v 1.13 2012/08/21 17:23:38 nanard Exp $ */
  2. /* libnatpmp
  3. Copyright (c) 2007-2011, 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. #include <stdio.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #if defined(_MSC_VER)
  30. #if _MSC_VER >= 1400
  31. #define strcasecmp _stricmp
  32. #else
  33. #define strcasecmp stricmp
  34. #endif
  35. #else
  36. #include <unistd.h>
  37. #endif
  38. #ifdef WIN32
  39. #include <winsock2.h>
  40. #else
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #endif
  44. #include "natpmp.h"
  45. void usage(FILE * out, const char * argv0)
  46. {
  47. fprintf(out, "Usage :\n");
  48. fprintf(out, " %s [options]\n", argv0);
  49. fprintf(out, "\tdisplay the public IP address.\n");
  50. fprintf(out, " %s -h\n", argv0);
  51. fprintf(out, "\tdisplay this help screen.\n");
  52. fprintf(out, " %s [options] -a <public port> <private port> <protocol> [lifetime]\n", argv0);
  53. fprintf(out, "\tadd a port mapping.\n");
  54. fprintf(out, "\nOption available :\n");
  55. fprintf(out, " -g ipv4address\n");
  56. fprintf(out, "\tforce the gateway to be used as destination for NAT-PMP commands.\n");
  57. fprintf(out, "\n In order to remove a mapping, set it with a lifetime of 0 seconds.\n");
  58. fprintf(out, " To remove all mappings for your machine, use 0 as private port and lifetime.\n");
  59. }
  60. /* sample code for using libnatpmp */
  61. int main(int argc, char * * argv)
  62. {
  63. natpmp_t natpmp;
  64. natpmpresp_t response;
  65. int r;
  66. int sav_errno;
  67. struct timeval timeout;
  68. fd_set fds;
  69. int i;
  70. int protocol = 0;
  71. uint16_t privateport = 0;
  72. uint16_t publicport = 0;
  73. uint32_t lifetime = 3600;
  74. int command = 0;
  75. int forcegw = 0;
  76. in_addr_t gateway = 0;
  77. struct in_addr gateway_in_use;
  78. #ifdef WIN32
  79. WSADATA wsaData;
  80. int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  81. if(nResult != NO_ERROR)
  82. {
  83. fprintf(stderr, "WSAStartup() failed.\n");
  84. return -1;
  85. }
  86. #endif
  87. /* argument parsing */
  88. for(i=1; i<argc; i++) {
  89. if(argv[i][0] == '-') {
  90. switch(argv[i][1]) {
  91. case 'h':
  92. usage(stdout, argv[0]);
  93. return 0;
  94. case 'g':
  95. forcegw = 1;
  96. if(argc < i + 1) {
  97. fprintf(stderr, "Not enough arguments for option -%c\n", argv[i][1]);
  98. return 1;
  99. }
  100. gateway = inet_addr(argv[++i]);
  101. break;
  102. case 'a':
  103. command = 'a';
  104. if(argc < i + 4) {
  105. fprintf(stderr, "Not enough arguments for option -%c\n", argv[i][1]);
  106. return 1;
  107. }
  108. i++;
  109. if(1 != sscanf(argv[i], "%hu", &publicport)) {
  110. fprintf(stderr, "%s is not a correct 16bits unsigned integer\n", argv[i]);
  111. return 1;
  112. }
  113. i++;
  114. if(1 != sscanf(argv[i], "%hu", &privateport)) {
  115. fprintf(stderr, "%s is not a correct 16bits unsigned integer\n", argv[i]);
  116. return 1;
  117. }
  118. i++;
  119. if(0 == strcasecmp(argv[i], "tcp"))
  120. protocol = NATPMP_PROTOCOL_TCP;
  121. else if(0 == strcasecmp(argv[i], "udp"))
  122. protocol = NATPMP_PROTOCOL_UDP;
  123. else {
  124. fprintf(stderr, "%s is not a valid protocol\n", argv[i]);
  125. return 1;
  126. }
  127. if(argc > i + 1) {
  128. if(1 != sscanf(argv[i+1], "%u", &lifetime)) {
  129. fprintf(stderr, "%s is not a correct 32bits unsigned integer\n", argv[i]);
  130. } else {
  131. i++;
  132. }
  133. }
  134. break;
  135. default:
  136. fprintf(stderr, "Unknown option %s\n", argv[i]);
  137. usage(stderr, argv[0]);
  138. return 1;
  139. }
  140. } else {
  141. fprintf(stderr, "Unknown option %s\n", argv[i]);
  142. usage(stderr, argv[0]);
  143. return 1;
  144. }
  145. }
  146. /* initnatpmp() */
  147. r = initnatpmp(&natpmp, forcegw, gateway);
  148. printf("initnatpmp() returned %d (%s)\n", r, r?"FAILED":"SUCCESS");
  149. if(r<0)
  150. return 1;
  151. gateway_in_use.s_addr = natpmp.gateway;
  152. printf("using gateway : %s\n", inet_ntoa(gateway_in_use));
  153. /* sendpublicaddressrequest() */
  154. r = sendpublicaddressrequest(&natpmp);
  155. printf("sendpublicaddressrequest returned %d (%s)\n",
  156. r, r==2?"SUCCESS":"FAILED");
  157. if(r<0)
  158. return 1;
  159. do {
  160. FD_ZERO(&fds);
  161. FD_SET(natpmp.s, &fds);
  162. getnatpmprequesttimeout(&natpmp, &timeout);
  163. r = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  164. if(r<0) {
  165. fprintf(stderr, "select()");
  166. return 1;
  167. }
  168. r = readnatpmpresponseorretry(&natpmp, &response);
  169. sav_errno = errno;
  170. printf("readnatpmpresponseorretry returned %d (%s)\n",
  171. r, r==0?"OK":(r==NATPMP_TRYAGAIN?"TRY AGAIN":"FAILED"));
  172. if(r<0 && r!=NATPMP_TRYAGAIN) {
  173. #ifdef ENABLE_STRNATPMPERR
  174. fprintf(stderr, "readnatpmpresponseorretry() failed : %s\n",
  175. strnatpmperr(r));
  176. #endif
  177. fprintf(stderr, " errno=%d '%s'\n",
  178. sav_errno, strerror(sav_errno));
  179. }
  180. } while(r==NATPMP_TRYAGAIN);
  181. if(r<0)
  182. return 1;
  183. /* TODO : check that response.type == 0 */
  184. printf("Public IP address : %s\n", inet_ntoa(response.pnu.publicaddress.addr));
  185. printf("epoch = %u\n", response.epoch);
  186. if(command == 'a') {
  187. /* sendnewportmappingrequest() */
  188. r = sendnewportmappingrequest(&natpmp, protocol,
  189. privateport, publicport,
  190. lifetime);
  191. printf("sendnewportmappingrequest returned %d (%s)\n",
  192. r, r==12?"SUCCESS":"FAILED");
  193. if(r < 0)
  194. return 1;
  195. do {
  196. FD_ZERO(&fds);
  197. FD_SET(natpmp.s, &fds);
  198. getnatpmprequesttimeout(&natpmp, &timeout);
  199. select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
  200. r = readnatpmpresponseorretry(&natpmp, &response);
  201. printf("readnatpmpresponseorretry returned %d (%s)\n",
  202. r, r==0?"OK":(r==NATPMP_TRYAGAIN?"TRY AGAIN":"FAILED"));
  203. } while(r==NATPMP_TRYAGAIN);
  204. if(r<0) {
  205. #ifdef ENABLE_STRNATPMPERR
  206. fprintf(stderr, "readnatpmpresponseorretry() failed : %s\n",
  207. strnatpmperr(r));
  208. #endif
  209. return 1;
  210. }
  211. printf("Mapped public port %hu protocol %s to local port %hu "
  212. "liftime %u\n",
  213. response.pnu.newportmapping.mappedpublicport,
  214. response.type == NATPMP_RESPTYPE_UDPPORTMAPPING ? "UDP" :
  215. (response.type == NATPMP_RESPTYPE_TCPPORTMAPPING ? "TCP" :
  216. "UNKNOWN"),
  217. response.pnu.newportmapping.privateport,
  218. response.pnu.newportmapping.lifetime);
  219. printf("epoch = %u\n", response.epoch);
  220. }
  221. r = closenatpmp(&natpmp);
  222. printf("closenatpmp() returned %d (%s)\n", r, r==0?"SUCCESS":"FAILED");
  223. if(r<0)
  224. return 1;
  225. return 0;
  226. }