minisoap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* $Id: minisoap.c,v 1.25 2017/04/21 10:03:24 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2005-2015 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. *
  8. * Minimal SOAP implementation for UPnP protocol.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #ifdef _WIN32
  13. #include <io.h>
  14. #include <winsock2.h>
  15. #define snprintf _snprintf
  16. #else
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #endif
  21. #include "minisoap.h"
  22. #ifdef _WIN32
  23. #undef OS_STRING
  24. #define OS_STRING "Win32"
  25. #define MINIUPNPC_VERSION_STRING "2.0"
  26. #define UPNP_VERSION_STRING "UPnP/1.1"
  27. #endif
  28. #ifdef __ANDROID__
  29. #undef OS_STRING
  30. #define OS_STRING "Android"
  31. #define MINIUPNPC_VERSION_STRING "2.0"
  32. #define UPNP_VERSION_STRING "UPnP/1.1"
  33. #endif
  34. /* only for malloc */
  35. #include <stdlib.h>
  36. #ifdef _WIN32
  37. #define PRINT_SOCKET_ERROR(x) fprintf(stderr, "Socket error: %s, %d\n", x, WSAGetLastError());
  38. #else
  39. #define PRINT_SOCKET_ERROR(x) perror(x)
  40. #endif
  41. /* httpWrite sends the headers and the body to the socket
  42. * and returns the number of bytes sent */
  43. static int
  44. httpWrite(int fd, const char * body, int bodysize,
  45. const char * headers, int headerssize)
  46. {
  47. int n = 0;
  48. /*n = write(fd, headers, headerssize);*/
  49. /*if(bodysize>0)
  50. n += write(fd, body, bodysize);*/
  51. /* Note : my old linksys router only took into account
  52. * soap request that are sent into only one packet */
  53. char * p;
  54. /* TODO: AVOID MALLOC, we could use writev() for that */
  55. p = malloc(headerssize+bodysize);
  56. if(!p)
  57. return -1;
  58. memcpy(p, headers, headerssize);
  59. memcpy(p+headerssize, body, bodysize);
  60. /*n = write(fd, p, headerssize+bodysize);*/
  61. n = send(fd, p, headerssize+bodysize, 0);
  62. if(n<0) {
  63. PRINT_SOCKET_ERROR("send");
  64. }
  65. /* disable send on the socket */
  66. /* draytek routers dont seems to like that... */
  67. #if 0
  68. #ifdef _WIN32
  69. if(shutdown(fd, SD_SEND)<0) {
  70. #else
  71. if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/
  72. #endif
  73. PRINT_SOCKET_ERROR("shutdown");
  74. }
  75. #endif
  76. free(p);
  77. return n;
  78. }
  79. /* self explanatory */
  80. int soapPostSubmit(int fd,
  81. const char * url,
  82. const char * host,
  83. unsigned short port,
  84. const char * action,
  85. const char * body,
  86. const char * httpversion)
  87. {
  88. int bodysize;
  89. char headerbuf[512];
  90. int headerssize;
  91. char portstr[8];
  92. bodysize = (int)strlen(body);
  93. /* We are not using keep-alive HTTP connections.
  94. * HTTP/1.1 needs the header Connection: close to do that.
  95. * This is the default with HTTP/1.0
  96. * Using HTTP/1.1 means we need to support chunked transfer-encoding :
  97. * When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked
  98. * transfer encoding. */
  99. /* Connection: Close is normally there only in HTTP/1.1 but who knows */
  100. portstr[0] = '\0';
  101. if(port != 80)
  102. snprintf(portstr, sizeof(portstr), ":%hu", port);
  103. headerssize = snprintf(headerbuf, sizeof(headerbuf),
  104. "POST %s HTTP/%s\r\n"
  105. "Host: %s%s\r\n"
  106. "User-Agent: " OS_STRING ", " UPNP_VERSION_STRING ", MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
  107. "Content-Length: %d\r\n"
  108. "Content-Type: text/xml\r\n"
  109. "SOAPAction: \"%s\"\r\n"
  110. "Connection: Close\r\n"
  111. "Cache-Control: no-cache\r\n" /* ??? */
  112. "Pragma: no-cache\r\n"
  113. "\r\n",
  114. url, httpversion, host, portstr, bodysize, action);
  115. if ((unsigned int)headerssize >= sizeof(headerbuf))
  116. return -1;
  117. #ifdef DEBUG
  118. /*printf("SOAP request : headersize=%d bodysize=%d\n",
  119. headerssize, bodysize);
  120. */
  121. printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
  122. url, httpversion, host, portstr);
  123. printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
  124. printf("Headers :\n%s", headerbuf);
  125. printf("Body :\n%s\n", body);
  126. #endif
  127. return httpWrite(fd, body, bodysize, headerbuf, headerssize);
  128. }