You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.2 KiB

8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
8 years ago
​
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
  1. /* $Id: testminissdpd.c,v 1.14 2016/03/01 17:49:51 nanard Exp $ */
  2. /* Project : miniupnp
  3. * website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * Author : Thomas BERNARD
  5. * copyright (c) 2005-2016 Thomas Bernard
  6. * This software is subjet to the conditions detailed in the
  7. * provided LICENCE file. */
  8. #include "codelength.h"
  9. #include "printresponse.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <sys/types.h>
  15. #include <sys/un.h>
  16. #include <unistd.h>
  17. void printversion(const unsigned char * resp, int n)
  18. {
  19. int l;
  20. const unsigned char * p;
  21. p = resp;
  22. DECODELENGTH(l, p);
  23. if (resp + n < p + l)
  24. {
  25. printf("get version error\n");
  26. }
  27. printf("MiniSSDPd version : %.*s\n", l, p);
  28. }
  29. #define SENDCOMMAND(command, size) \
  30. write(s, command, size); \
  31. printf("Command written type=%u\n", (unsigned char) command[0]);
  32. int connect_unix_socket(const char * sockpath)
  33. {
  34. int s;
  35. struct sockaddr_un addr;
  36. s = socket(AF_UNIX, SOCK_STREAM, 0);
  37. addr.sun_family = AF_UNIX;
  38. strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
  39. if (connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0)
  40. {
  41. fprintf(stderr, "connecting to %s : ", addr.sun_path);
  42. perror("connect");
  43. exit(1);
  44. }
  45. printf("Connected to %s\n", addr.sun_path);
  46. return s;
  47. }
  48. /* test program for minissdpd */
  49. int main(int argc, char ** argv)
  50. {
  51. const char command0[] = {0x00, 0x00};
  52. char command1[] = "\x01\x00urn:schemas-upnp-org:device:InternetGatewayDevice";
  53. char command2[] = "\x02\x00uuid:fc4ec57e-b051-11db-88f8-0060085db3f6::upnp:rootdevice";
  54. const char command3[] = {0x03, 0x00};
  55. /* old versions of minissdpd would reject a command with
  56. * a zero length string argument */
  57. char command3compat[] = "\x03\x00ssdp:all";
  58. char command4[] = "\x04\x00test:test:test";
  59. const char bad_command[] = {0xff, 0xff};
  60. const char overflow[] = {0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  61. const char command5[] = {0x05, 0x00};
  62. const char bad_command4[] = {0x04, 0x01, 0x60, 0x8f, 0xff, 0xff, 0xff, 0x7f};
  63. int s;
  64. int i;
  65. void * tmp;
  66. unsigned char * resp = NULL;
  67. size_t respsize = 0;
  68. unsigned char buf[4096];
  69. ssize_t n;
  70. int total = 0;
  71. const char * sockpath = "/var/run/minissdpd.sock";
  72. for (i = 0; i < argc - 1; i++)
  73. {
  74. if (0 == strcmp(argv[i], "-s"))
  75. {
  76. sockpath = argv[++i];
  77. }
  78. }
  79. command1[1] = sizeof(command1) - 3;
  80. command2[1] = sizeof(command2) - 3;
  81. command3compat[1] = sizeof(command3compat) - 3;
  82. command4[1] = sizeof(command4) - 3;
  83. s = connect_unix_socket(sockpath);
  84. n = SENDCOMMAND(command0, sizeof(command0));
  85. n = read(s, buf, sizeof(buf));
  86. printf("Response received %d bytes\n", (int) n);
  87. if (n > 0)
  88. {
  89. printversion(buf, n);
  90. }
  91. else
  92. {
  93. printf("Command 0 (get version) not supported\n");
  94. close(s);
  95. s = connect_unix_socket(sockpath);
  96. }
  97. n = SENDCOMMAND(command1, sizeof(command1) - 1);
  98. n = read(s, buf, sizeof(buf));
  99. printf("Response received %d bytes\n", (int) n);
  100. printresponse(buf, n);
  101. if (n == 0)
  102. {
  103. close(s);
  104. s = connect_unix_socket(sockpath);
  105. }
  106. n = SENDCOMMAND(command2, sizeof(command2) - 1);
  107. n = read(s, buf, sizeof(buf));
  108. printf("Response received %d bytes\n", (int) n);
  109. printresponse(buf, n);
  110. if (n == 0)
  111. {
  112. close(s);
  113. s = connect_unix_socket(sockpath);
  114. }
  115. buf[0] = 0; /* Slight hack for printing num devices when 0 */
  116. n = SENDCOMMAND(command3, sizeof(command3));
  117. n = read(s, buf, sizeof(buf));
  118. if (n == 0)
  119. {
  120. printf("command3 failed, testing compatible one\n");
  121. close(s);
  122. s = connect_unix_socket(sockpath);
  123. n = SENDCOMMAND(command3compat, sizeof(command3compat) - 1);
  124. n = read(s, buf, sizeof(buf));
  125. }
  126. printf("Response received %d bytes\n", (int) n);
  127. printf("Number of devices %d\n", (int) buf[0]);
  128. while (n > 0)
  129. {
  130. tmp = realloc(resp, respsize + n);
  131. if (tmp == NULL)
  132. {
  133. fprintf(stderr, "memory allocation error\n");
  134. break;
  135. }
  136. resp = tmp;
  137. respsize += n;
  138. if (n > 0)
  139. {
  140. memcpy(resp + total, buf, n);
  141. total += n;
  142. }
  143. if (n < (ssize_t) sizeof(buf))
  144. {
  145. break;
  146. }
  147. n = read(s, buf, sizeof(buf));
  148. printf("response received %d bytes\n", (int) n);
  149. }
  150. if (resp != NULL)
  151. {
  152. printresponse(resp, total);
  153. free(resp);
  154. resp = NULL;
  155. }
  156. if (n == 0)
  157. {
  158. close(s);
  159. s = connect_unix_socket(sockpath);
  160. }
  161. n = SENDCOMMAND(command4, sizeof(command4));
  162. /* no response for request type 4 */
  163. n = SENDCOMMAND(bad_command, sizeof(bad_command));
  164. n = read(s, buf, sizeof(buf));
  165. printf("Response received %d bytes\n", (int) n);
  166. printresponse(buf, n);
  167. if (n == 0)
  168. {
  169. close(s);
  170. s = connect_unix_socket(sockpath);
  171. }
  172. n = SENDCOMMAND(overflow, sizeof(overflow));
  173. n = read(s, buf, sizeof(buf));
  174. printf("Response received %d bytes\n", (int) n);
  175. printresponse(buf, n);
  176. if (n == 0)
  177. {
  178. close(s);
  179. s = connect_unix_socket(sockpath);
  180. }
  181. n = SENDCOMMAND(command5, sizeof(command5));
  182. n = read(s, buf, sizeof(buf));
  183. printf("Response received %d bytes\n", (int) n);
  184. printresponse(buf, n);
  185. if (n == 0)
  186. {
  187. close(s);
  188. s = connect_unix_socket(sockpath);
  189. }
  190. n = SENDCOMMAND(bad_command4, sizeof(bad_command4));
  191. n = read(s, buf, sizeof(buf));
  192. printf("Response received %d bytes\n", (int) n);
  193. printresponse(buf, n);
  194. close(s);
  195. return 0;
  196. }