2
0

minisoap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* $Id: minisoap.c,v 1.16 2008/10/11 16:39:29 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2005 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. #if _MSC_VER < 1900
  16. #define snprintf _snprintf
  17. #endif
  18. #else
  19. #include <unistd.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #endif
  23. #include "minisoap.h"
  24. #include "miniupnpcstrings.h"
  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. {
  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. /* Connection: Close is normally there only in HTTP/1.1 but who knows */
  87. portstr[0] = '\0';
  88. if(port != 80)
  89. snprintf(portstr, sizeof(portstr), ":%hu", port);
  90. headerssize = snprintf(headerbuf, sizeof(headerbuf),
  91. "POST %s HTTP/1.1\r\n"
  92. /* "POST %s HTTP/1.0\r\n"*/
  93. "Host: %s%s\r\n"
  94. "User-Agent: " OS_STRING ", UPnP/1.0, MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
  95. "Content-Length: %d\r\n"
  96. "Content-Type: text/xml\r\n"
  97. "SOAPAction: \"%s\"\r\n"
  98. "Connection: Close\r\n"
  99. "Cache-Control: no-cache\r\n" /* ??? */
  100. "Pragma: no-cache\r\n"
  101. "\r\n",
  102. url, host, portstr, bodysize, action);
  103. #ifdef DEBUG
  104. printf("SOAP request : headersize=%d bodysize=%d\n",
  105. headerssize, bodysize);
  106. /*printf("%s", headerbuf);*/
  107. #endif
  108. return httpWrite(fd, body, bodysize, headerbuf, headerssize);
  109. }