minisoap.c 3.8 KB

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