upnpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* $Id: upnpc.c,v 1.65 2008/10/14 18:05:27 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2005-2008 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. * */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #ifdef WIN32
  12. #include <winsock2.h>
  13. #define snprintf _snprintf
  14. #endif
  15. #include "miniwget.h"
  16. #include "miniupnpc.h"
  17. #include "upnpcommands.h"
  18. #include "upnperrors.h"
  19. /* protofix() checks if protocol is "UDP" or "TCP"
  20. * returns NULL if not */
  21. const char * protofix(const char * proto)
  22. {
  23. static const char proto_tcp[4] = { 'T', 'C', 'P', 0};
  24. static const char proto_udp[4] = { 'U', 'D', 'P', 0};
  25. int i, b;
  26. for(i=0, b=1; i<4; i++)
  27. b = b && ( (proto[i] == proto_tcp[i])
  28. || (proto[i] == (proto_tcp[i] | 32)) );
  29. if(b)
  30. return proto_tcp;
  31. for(i=0, b=1; i<4; i++)
  32. b = b && ( (proto[i] == proto_udp[i])
  33. || (proto[i] == (proto_udp[i] | 32)) );
  34. if(b)
  35. return proto_udp;
  36. return 0;
  37. }
  38. static void DisplayInfos(struct UPNPUrls * urls,
  39. struct IGDdatas * data)
  40. {
  41. char externalIPAddress[16];
  42. char connectionType[64];
  43. char status[64];
  44. char lastconnerr[64];
  45. unsigned int uptime;
  46. unsigned int brUp, brDown;
  47. int r;
  48. UPNP_GetConnectionTypeInfo(urls->controlURL,
  49. data->servicetype,
  50. connectionType);
  51. if(connectionType[0])
  52. printf("Connection Type : %s\n", connectionType);
  53. else
  54. printf("GetConnectionTypeInfo failed.\n");
  55. UPNP_GetStatusInfo(urls->controlURL, data->servicetype,
  56. status, &uptime, lastconnerr);
  57. printf("Status : %s, uptime=%u, LastConnectionError : %s\n",
  58. status, uptime, lastconnerr);
  59. UPNP_GetLinkLayerMaxBitRates(urls->controlURL_CIF, data->servicetype_CIF,
  60. &brDown, &brUp);
  61. printf("MaxBitRateDown : %u bps MaxBitRateUp %u bps\n", brDown, brUp);
  62. r = UPNP_GetExternalIPAddress(urls->controlURL,
  63. data->servicetype,
  64. externalIPAddress);
  65. if(r != UPNPCOMMAND_SUCCESS)
  66. printf("GetExternalIPAddress() returned %d\n", r);
  67. if(externalIPAddress[0])
  68. printf("ExternalIPAddress = %s\n", externalIPAddress);
  69. else
  70. printf("GetExternalIPAddress failed.\n");
  71. }
  72. static void GetConnectionStatus(struct UPNPUrls * urls,
  73. struct IGDdatas * data)
  74. {
  75. unsigned int bytessent, bytesreceived, packetsreceived, packetssent;
  76. DisplayInfos(urls, data);
  77. bytessent = UPNP_GetTotalBytesSent(urls->controlURL_CIF, data->servicetype_CIF);
  78. bytesreceived = UPNP_GetTotalBytesReceived(urls->controlURL_CIF, data->servicetype_CIF);
  79. packetssent = UPNP_GetTotalPacketsSent(urls->controlURL_CIF, data->servicetype_CIF);
  80. packetsreceived = UPNP_GetTotalPacketsReceived(urls->controlURL_CIF, data->servicetype_CIF);
  81. printf("Bytes: Sent: %8u\tRecv: %8u\n", bytessent, bytesreceived);
  82. printf("Packets: Sent: %8u\tRecv: %8u\n", packetssent, packetsreceived);
  83. }
  84. static void ListRedirections(struct UPNPUrls * urls,
  85. struct IGDdatas * data)
  86. {
  87. int r;
  88. int i = 0;
  89. char index[6];
  90. char intClient[16];
  91. char intPort[6];
  92. char extPort[6];
  93. char protocol[4];
  94. char desc[80];
  95. char enabled[6];
  96. char rHost[64];
  97. char duration[16];
  98. /*unsigned int num=0;
  99. UPNP_GetPortMappingNumberOfEntries(urls->controlURL, data->servicetype, &num);
  100. printf("PortMappingNumberOfEntries : %u\n", num);*/
  101. do {
  102. snprintf(index, 6, "%d", i);
  103. rHost[0] = '\0'; enabled[0] = '\0';
  104. duration[0] = '\0'; desc[0] = '\0';
  105. extPort[0] = '\0'; intPort[0] = '\0'; intClient[0] = '\0';
  106. r = UPNP_GetGenericPortMappingEntry(urls->controlURL, data->servicetype,
  107. index,
  108. extPort, intClient, intPort,
  109. protocol, desc, enabled,
  110. rHost, duration);
  111. if(r==0)
  112. /*
  113. printf("%02d - %s %s->%s:%s\tenabled=%s leaseDuration=%s\n"
  114. " desc='%s' rHost='%s'\n",
  115. i, protocol, extPort, intClient, intPort,
  116. enabled, duration,
  117. desc, rHost);
  118. */
  119. printf("%2d %s %5s->%s:%-5s '%s' '%s'\n",
  120. i, protocol, extPort, intClient, intPort,
  121. desc, rHost);
  122. else
  123. printf("GetGenericPortMappingEntry() returned %d (%s)\n",
  124. r, strupnperror(r));
  125. i++;
  126. } while(r==0);
  127. }
  128. /* Test function
  129. * 1 - get connection type
  130. * 2 - get extenal ip address
  131. * 3 - Add port mapping
  132. * 4 - get this port mapping from the IGD */
  133. static void SetRedirectAndTest(struct UPNPUrls * urls,
  134. struct IGDdatas * data,
  135. const char * iaddr,
  136. const char * iport,
  137. const char * eport,
  138. const char * proto)
  139. {
  140. char externalIPAddress[16];
  141. char intClient[16];
  142. char intPort[6];
  143. int r;
  144. if(!iaddr || !iport || !eport || !proto)
  145. {
  146. fprintf(stderr, "Wrong arguments\n");
  147. return;
  148. }
  149. proto = protofix(proto);
  150. if(!proto)
  151. {
  152. fprintf(stderr, "invalid protocol\n");
  153. return;
  154. }
  155. UPNP_GetExternalIPAddress(urls->controlURL,
  156. data->servicetype,
  157. externalIPAddress);
  158. if(externalIPAddress[0])
  159. printf("ExternalIPAddress = %s\n", externalIPAddress);
  160. else
  161. printf("GetExternalIPAddress failed.\n");
  162. r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
  163. eport, iport, iaddr, 0, proto, 0);
  164. if(r!=UPNPCOMMAND_SUCCESS)
  165. printf("AddPortMapping(%s, %s, %s) failed with code %d\n",
  166. eport, iport, iaddr, r);
  167. r = UPNP_GetSpecificPortMappingEntry(urls->controlURL,
  168. data->servicetype,
  169. eport, proto,
  170. intClient, intPort);
  171. if(r!=UPNPCOMMAND_SUCCESS)
  172. printf("GetSpecificPortMappingEntry() failed with code %d\n", r);
  173. if(intClient[0]) {
  174. printf("InternalIP:Port = %s:%s\n", intClient, intPort);
  175. printf("external %s:%s %s is redirected to internal %s:%s\n",
  176. externalIPAddress, eport, proto, intClient, intPort);
  177. }
  178. }
  179. static void
  180. RemoveRedirect(struct UPNPUrls * urls,
  181. struct IGDdatas * data,
  182. const char * eport,
  183. const char * proto)
  184. {
  185. int r;
  186. if(!proto || !eport)
  187. {
  188. fprintf(stderr, "invalid arguments\n");
  189. return;
  190. }
  191. proto = protofix(proto);
  192. if(!proto)
  193. {
  194. fprintf(stderr, "protocol invalid\n");
  195. return;
  196. }
  197. r = UPNP_DeletePortMapping(urls->controlURL, data->servicetype, eport, proto, 0);
  198. printf("UPNP_DeletePortMapping() returned : %d\n", r);
  199. }
  200. /* sample upnp client program */
  201. int main(int argc, char ** argv)
  202. {
  203. char command = 0;
  204. char ** commandargv = 0;
  205. int commandargc = 0;
  206. struct UPNPDev * devlist = 0;
  207. char lanaddr[16]; /* my ip address on the LAN */
  208. int i;
  209. const char * rootdescurl = 0;
  210. const char * multicastif = 0;
  211. const char * minissdpdpath = 0;
  212. #ifdef WIN32
  213. WSADATA wsaData;
  214. int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  215. if(nResult != NO_ERROR)
  216. {
  217. fprintf(stderr, "WSAStartup() failed.\n");
  218. return -1;
  219. }
  220. #endif
  221. printf("upnpc : miniupnpc library test client. (c) 2006-2008 Thomas Bernard\n");
  222. printf("Go to http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/\n"
  223. "for more information.\n");
  224. /* command line processing */
  225. for(i=1; i<argc; i++)
  226. {
  227. if(argv[i][0] == '-')
  228. {
  229. if(argv[i][1] == 'u')
  230. rootdescurl = argv[++i];
  231. else if(argv[i][1] == 'm')
  232. multicastif = argv[++i];
  233. else if(argv[i][1] == 'p')
  234. minissdpdpath = argv[++i];
  235. else
  236. {
  237. command = argv[i][1];
  238. i++;
  239. commandargv = argv + i;
  240. commandargc = argc - i;
  241. break;
  242. }
  243. }
  244. else
  245. {
  246. fprintf(stderr, "option '%s' invalid\n", argv[i]);
  247. }
  248. }
  249. if(!command || (command == 'a' && commandargc<4)
  250. || (command == 'd' && argc<2)
  251. || (command == 'r' && argc<2))
  252. {
  253. fprintf(stderr, "Usage :\t%s [options] -a ip port external_port protocol\n\t\tAdd port redirection\n", argv[0]);
  254. fprintf(stderr, " \t%s [options] -d external_port protocol\n\t\tDelete port redirection\n", argv[0]);
  255. fprintf(stderr, " \t%s [options] -s\n\t\tGet Connection status\n", argv[0]);
  256. fprintf(stderr, " \t%s [options] -l\n\t\tList redirections\n", argv[0]);
  257. fprintf(stderr, " \t%s [options] -r port1 protocol1 [port2 protocol2] [...]\n\t\tAdd all redirections to the current host\n", argv[0]);
  258. fprintf(stderr, "\nprotocol is UDP or TCP\n");
  259. fprintf(stderr, "Options:\n");
  260. fprintf(stderr, " -u url : bypass discovery process by providing the XML root description url.\n");
  261. fprintf(stderr, " -m address : provide ip address of the interface to use for sending SSDP multicast packets.\n");
  262. fprintf(stderr, " -p path : use this path for MiniSSDPd socket.\n");
  263. return 1;
  264. }
  265. if( rootdescurl
  266. || (devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0)))
  267. {
  268. struct UPNPDev * device;
  269. struct UPNPUrls urls;
  270. struct IGDdatas data;
  271. if(devlist)
  272. {
  273. printf("List of UPNP devices found on the network :\n");
  274. for(device = devlist; device; device = device->pNext)
  275. {
  276. printf(" desc: %s\n st: %s\n\n",
  277. device->descURL, device->st);
  278. }
  279. }
  280. i = 1;
  281. if( (rootdescurl && UPNP_GetIGDFromUrl(rootdescurl, &urls, &data, lanaddr, sizeof(lanaddr)))
  282. || (i = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr))))
  283. {
  284. switch(i) {
  285. case 1:
  286. printf("Found valid IGD : %s\n", urls.controlURL);
  287. break;
  288. case 2:
  289. printf("Found a (not connected?) IGD : %s\n", urls.controlURL);
  290. printf("Trying to continue anyway\n");
  291. break;
  292. case 3:
  293. printf("UPnP device found. Is it an IGD ? : %s\n", urls.controlURL);
  294. printf("Trying to continue anyway\n");
  295. break;
  296. default:
  297. printf("Found device (igd ?) : %s\n", urls.controlURL);
  298. printf("Trying to continue anyway\n");
  299. }
  300. printf("Local LAN ip address : %s\n", lanaddr);
  301. #if 0
  302. printf("getting \"%s\"\n", urls.ipcondescURL);
  303. descXML = miniwget(urls.ipcondescURL, &descXMLsize);
  304. if(descXML)
  305. {
  306. /*fwrite(descXML, 1, descXMLsize, stdout);*/
  307. free(descXML); descXML = NULL;
  308. }
  309. #endif
  310. switch(command)
  311. {
  312. case 'l':
  313. DisplayInfos(&urls, &data);
  314. ListRedirections(&urls, &data);
  315. break;
  316. case 'a':
  317. SetRedirectAndTest(&urls, &data,
  318. commandargv[0], commandargv[1],
  319. commandargv[2], commandargv[3]);
  320. break;
  321. case 'd':
  322. RemoveRedirect(&urls, &data, commandargv[0], commandargv[1]);
  323. break;
  324. case 's':
  325. GetConnectionStatus(&urls, &data);
  326. break;
  327. case 'r':
  328. for(i=0; i<commandargc; i+=2)
  329. {
  330. /*printf("port %s protocol %s\n", argv[i], argv[i+1]);*/
  331. SetRedirectAndTest(&urls, &data,
  332. lanaddr, commandargv[i],
  333. commandargv[i], commandargv[i+1]);
  334. }
  335. break;
  336. default:
  337. fprintf(stderr, "Unknown switch -%c\n", command);
  338. }
  339. FreeUPNPUrls(&urls);
  340. }
  341. else
  342. {
  343. fprintf(stderr, "No valid UPNP Internet Gateway Device found.\n");
  344. }
  345. freeUPNPDevlist(devlist); devlist = 0;
  346. }
  347. else
  348. {
  349. fprintf(stderr, "No IGD UPnP Device found on the network !\n");
  350. }
  351. /*puts("************* HOP ***************");*/
  352. return 0;
  353. }