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