minisoap.c 3.7 KB

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