1 /* $Id: minisoap.c,v 1.20 2010/12/11 17:56:51 nanard Exp $ */
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.
8 * Minimal SOAP implementation for UPnP protocol.
15 #define snprintf _snprintf
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 #include "miniupnpcstrings.h"
28 #define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
30 #define PRINT_SOCKET_ERROR(x) perror(x)
33 /* httpWrite sends the headers and the body to the socket
34 * and returns the number of bytes sent */
36 httpWrite(int fd
, const char * body
, int bodysize
,
37 const char * headers
, int headerssize
)
40 /*n = write(fd, headers, headerssize);*/
42 n += write(fd, body, bodysize);*/
43 /* Note : my old linksys router only took into account
44 * soap request that are sent into only one packet */
46 /* TODO: AVOID MALLOC */
47 p
= malloc(headerssize
+bodysize
);
50 memcpy(p
, headers
, headerssize
);
51 memcpy(p
+headerssize
, body
, bodysize
);
52 /*n = write(fd, p, headerssize+bodysize);*/
53 n
= send(fd
, p
, headerssize
+bodysize
, 0);
55 PRINT_SOCKET_ERROR("send");
57 /* disable send on the socket */
58 /* draytek routers dont seems to like that... */
61 if(shutdown(fd
, SD_SEND
)<0) {
63 if(shutdown(fd
, SHUT_WR
)<0) { /*SD_SEND*/
65 PRINT_SOCKET_ERROR("shutdown");
72 /* self explanatory */
73 int soapPostSubmit(int fd
,
79 const char * httpversion
)
85 bodysize
= (int)strlen(body
);
86 /* We are not using keep-alive HTTP connections.
87 * HTTP/1.1 needs the header Connection: close to do that.
88 * This is the default with HTTP/1.0
89 * Using HTTP/1.1 means we need to support chunked transfer-encoding :
90 * When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked
91 * transfer encoding. */
92 /* Connection: Close is normally there only in HTTP/1.1 but who knows */
95 snprintf(portstr
, sizeof(portstr
), ":%hu", port
);
96 headerssize
= snprintf(headerbuf
, sizeof(headerbuf
),
99 "User-Agent: " OS_STRING
", UPnP/1.0, MiniUPnPc/" MINIUPNPC_VERSION_STRING
"\r\n"
100 "Content-Length: %d\r\n"
101 "Content-Type: text/xml\r\n"
102 "SOAPAction: \"%s\"\r\n"
103 "Connection: Close\r\n"
104 "Cache-Control: no-cache\r\n" /* ??? */
105 "Pragma: no-cache\r\n"
107 url
, httpversion
, host
, portstr
, bodysize
, action
);
109 /*printf("SOAP request : headersize=%d bodysize=%d\n",
110 headerssize, bodysize);
112 printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
113 url
, httpversion
, host
, portstr
);
114 printf("SOAPAction: \"%s\" - Content-Length: %d\n", action
, bodysize
);
115 /*printf("%s", headerbuf);*/
117 return httpWrite(fd
, body
, bodysize
, headerbuf
, headerssize
);