natpmpc.c 5.8 KB

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