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.

196 lines
5.0 KiB

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