listdevices.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $Id: listdevices.c,v 1.6 2015/07/23 20:40:08 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2013-2015 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #ifdef _WIN32
  11. #include <winsock2.h>
  12. #endif /* _WIN32 */
  13. #include "miniupnpc.h"
  14. struct upnp_dev_list {
  15. struct upnp_dev_list * next;
  16. char * descURL;
  17. struct UPNPDev * * array;
  18. size_t count;
  19. size_t allocated_count;
  20. };
  21. #define ADD_DEVICE_COUNT_STEP 16
  22. void add_device(struct upnp_dev_list * * list_head, struct UPNPDev * dev)
  23. {
  24. struct upnp_dev_list * elt;
  25. size_t i;
  26. if(dev == NULL)
  27. return;
  28. for(elt = *list_head; elt != NULL; elt = elt->next) {
  29. if(strcmp(elt->descURL, dev->descURL) == 0) {
  30. for(i = 0; i < elt->count; i++) {
  31. if (strcmp(elt->array[i]->st, dev->st) == 0 && strcmp(elt->array[i]->usn, dev->usn) == 0) {
  32. return; /* already found */
  33. }
  34. }
  35. if(elt->count >= elt->allocated_count) {
  36. struct UPNPDev * * tmp;
  37. elt->allocated_count += ADD_DEVICE_COUNT_STEP;
  38. tmp = realloc(elt->array, elt->allocated_count * sizeof(struct UPNPDev *));
  39. if(tmp == NULL) {
  40. fprintf(stderr, "Failed to realloc(%p, %lu)\n", elt->array, (unsigned long)(elt->allocated_count * sizeof(struct UPNPDev *)));
  41. return;
  42. }
  43. elt->array = tmp;
  44. }
  45. elt->array[elt->count++] = dev;
  46. return;
  47. }
  48. }
  49. elt = malloc(sizeof(struct upnp_dev_list));
  50. if(elt == NULL) {
  51. fprintf(stderr, "Failed to malloc(%lu)\n", (unsigned long)sizeof(struct upnp_dev_list));
  52. return;
  53. }
  54. elt->next = *list_head;
  55. elt->descURL = strdup(dev->descURL);
  56. if(elt->descURL == NULL) {
  57. fprintf(stderr, "Failed to strdup(%s)\n", dev->descURL);
  58. free(elt);
  59. return;
  60. }
  61. elt->allocated_count = ADD_DEVICE_COUNT_STEP;
  62. elt->array = malloc(ADD_DEVICE_COUNT_STEP * sizeof(struct UPNPDev *));
  63. if(elt->array == NULL) {
  64. fprintf(stderr, "Failed to malloc(%lu)\n", (unsigned long)(ADD_DEVICE_COUNT_STEP * sizeof(struct UPNPDev *)));
  65. free(elt->descURL);
  66. free(elt);
  67. return;
  68. }
  69. elt->array[0] = dev;
  70. elt->count = 1;
  71. *list_head = elt;
  72. }
  73. void free_device(struct upnp_dev_list * elt)
  74. {
  75. free(elt->descURL);
  76. free(elt->array);
  77. free(elt);
  78. }
  79. int main(int argc, char * * argv)
  80. {
  81. const char * searched_device = NULL;
  82. const char * * searched_devices = NULL;
  83. const char * multicastif = 0;
  84. const char * minissdpdpath = 0;
  85. int ipv6 = 0;
  86. unsigned char ttl = 2;
  87. int error = 0;
  88. struct UPNPDev * devlist = 0;
  89. struct UPNPDev * dev;
  90. struct upnp_dev_list * sorted_list = NULL;
  91. struct upnp_dev_list * dev_array;
  92. int i;
  93. #ifdef _WIN32
  94. WSADATA wsaData;
  95. int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  96. if(nResult != NO_ERROR)
  97. {
  98. fprintf(stderr, "WSAStartup() failed.\n");
  99. return -1;
  100. }
  101. #endif
  102. for(i = 1; i < argc; i++) {
  103. if(strcmp(argv[i], "-6") == 0)
  104. ipv6 = 1;
  105. else if(strcmp(argv[i], "-d") == 0) {
  106. if(++i >= argc) {
  107. fprintf(stderr, "%s option needs one argument\n", "-d");
  108. return 1;
  109. }
  110. searched_device = argv[i];
  111. } else if(strcmp(argv[i], "-t") == 0) {
  112. if(++i >= argc) {
  113. fprintf(stderr, "%s option needs one argument\n", "-t");
  114. return 1;
  115. }
  116. ttl = (unsigned char)atoi(argv[i]);
  117. } else if(strcmp(argv[i], "-l") == 0) {
  118. if(++i >= argc) {
  119. fprintf(stderr, "-l option needs at least one argument\n");
  120. return 1;
  121. }
  122. searched_devices = (const char * *)(argv + i);
  123. break;
  124. } else if(strcmp(argv[i], "-m") == 0) {
  125. if(++i >= argc) {
  126. fprintf(stderr, "-m option needs one argument\n");
  127. return 1;
  128. }
  129. multicastif = argv[i];
  130. } else {
  131. printf("usage : %s [options] [-l <device1> <device2> ...]\n", argv[0]);
  132. printf("options :\n");
  133. printf(" -6 : use IPv6\n");
  134. printf(" -m address/ifname : network interface to use for multicast\n");
  135. printf(" -d <device string> : search only for this type of device\n");
  136. printf(" -l <device1> <device2> ... : search only for theses types of device\n");
  137. printf(" -t ttl : set multicast TTL. Default value is 2.\n");
  138. printf(" -h : this help\n");
  139. return 1;
  140. }
  141. }
  142. if(searched_device) {
  143. printf("searching UPnP device type %s\n", searched_device);
  144. devlist = upnpDiscoverDevice(searched_device,
  145. 2000, multicastif, minissdpdpath,
  146. 0/*localport*/, ipv6, ttl, &error);
  147. } else if(searched_devices) {
  148. printf("searching UPnP device types :\n");
  149. for(i = 0; searched_devices[i]; i++)
  150. printf("\t%s\n", searched_devices[i]);
  151. devlist = upnpDiscoverDevices(searched_devices,
  152. 2000, multicastif, minissdpdpath,
  153. 0/*localport*/, ipv6, ttl, &error, 1);
  154. } else {
  155. printf("searching all UPnP devices\n");
  156. devlist = upnpDiscoverAll(2000, multicastif, minissdpdpath,
  157. 0/*localport*/, ipv6, ttl, &error);
  158. }
  159. if(devlist) {
  160. for(dev = devlist, i = 1; dev != NULL; dev = dev->pNext, i++) {
  161. printf("%3d: %-48s\n", i, dev->st);
  162. printf(" %s\n", dev->descURL);
  163. printf(" %s\n", dev->usn);
  164. add_device(&sorted_list, dev);
  165. }
  166. putchar('\n');
  167. for (dev_array = sorted_list; dev_array != NULL ; dev_array = dev_array->next) {
  168. printf("%s :\n", dev_array->descURL);
  169. for(i = 0; (unsigned)i < dev_array->count; i++) {
  170. printf("%2d: %s\n", i+1, dev_array->array[i]->st);
  171. printf(" %s\n", dev_array->array[i]->usn);
  172. }
  173. putchar('\n');
  174. }
  175. freeUPNPDevlist(devlist);
  176. while(sorted_list != NULL) {
  177. dev_array = sorted_list;
  178. sorted_list = sorted_list->next;
  179. free_device(dev_array);
  180. }
  181. } else {
  182. printf("no device found.\n");
  183. }
  184. return 0;
  185. }