minisoap.c 3.6 KB

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