minisoap.c 3.1 KB

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