minisoap.c 3.5 KB

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