2010-12-22 16:10:14 +00:00
|
|
|
/* $Id: minisoap.c,v 1.20 2010/12/11 17:56:51 nanard Exp $ */
|
2007-11-06 16:02:50 +00:00
|
|
|
/* Project : miniupnp
|
|
|
|
* Author : Thomas Bernard
|
2010-02-25 12:39:22 +00:00
|
|
|
* Copyright (c) 2005-2009 Thomas Bernard
|
2007-11-06 16:02:50 +00:00
|
|
|
* This software is subject to the conditions detailed in the
|
|
|
|
* LICENCE file provided in this distribution.
|
|
|
|
*
|
|
|
|
* Minimal SOAP implementation for UPnP protocol.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include "minisoap.h"
|
2009-02-07 00:11:58 +00:00
|
|
|
#include "miniupnpcstrings.h"
|
2007-11-06 16:02:50 +00:00
|
|
|
|
|
|
|
/* only for malloc */
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-02-21 14:38:01 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
|
|
|
|
#else
|
|
|
|
#define PRINT_SOCKET_ERROR(x) perror(x)
|
|
|
|
#endif
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
/* httpWrite sends the headers and the body to the socket
|
|
|
|
* and returns the number of bytes sent */
|
|
|
|
static int
|
|
|
|
httpWrite(int fd, const char * body, int bodysize,
|
|
|
|
const char * headers, int headerssize)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
/*n = write(fd, headers, headerssize);*/
|
|
|
|
/*if(bodysize>0)
|
|
|
|
n += write(fd, body, bodysize);*/
|
|
|
|
/* Note : my old linksys router only took into account
|
|
|
|
* soap request that are sent into only one packet */
|
|
|
|
char * p;
|
2008-02-21 14:38:01 +00:00
|
|
|
/* TODO: AVOID MALLOC */
|
2007-11-06 16:02:50 +00:00
|
|
|
p = malloc(headerssize+bodysize);
|
2008-02-23 15:31:39 +00:00
|
|
|
if(!p)
|
|
|
|
return 0;
|
2007-11-06 16:02:50 +00:00
|
|
|
memcpy(p, headers, headerssize);
|
|
|
|
memcpy(p+headerssize, body, bodysize);
|
|
|
|
/*n = write(fd, p, headerssize+bodysize);*/
|
|
|
|
n = send(fd, p, headerssize+bodysize, 0);
|
2008-02-21 14:38:01 +00:00
|
|
|
if(n<0) {
|
|
|
|
PRINT_SOCKET_ERROR("send");
|
|
|
|
}
|
|
|
|
/* disable send on the socket */
|
|
|
|
/* draytek routers dont seems to like that... */
|
|
|
|
#if 0
|
2007-11-06 16:02:50 +00:00
|
|
|
#ifdef WIN32
|
2008-02-21 14:38:01 +00:00
|
|
|
if(shutdown(fd, SD_SEND)<0) {
|
2007-11-06 16:02:50 +00:00
|
|
|
#else
|
2008-02-21 14:38:01 +00:00
|
|
|
if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/
|
|
|
|
#endif
|
|
|
|
PRINT_SOCKET_ERROR("shutdown");
|
|
|
|
}
|
2007-11-06 16:02:50 +00:00
|
|
|
#endif
|
|
|
|
free(p);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* self explanatory */
|
|
|
|
int soapPostSubmit(int fd,
|
|
|
|
const char * url,
|
|
|
|
const char * host,
|
|
|
|
unsigned short port,
|
|
|
|
const char * action,
|
2010-04-13 00:11:34 +00:00
|
|
|
const char * body,
|
|
|
|
const char * httpversion)
|
2007-11-06 16:02:50 +00:00
|
|
|
{
|
|
|
|
int bodysize;
|
2008-02-23 15:31:39 +00:00
|
|
|
char headerbuf[512];
|
2007-11-06 16:02:50 +00:00
|
|
|
int headerssize;
|
2008-02-23 15:31:39 +00:00
|
|
|
char portstr[8];
|
2007-11-06 16:02:50 +00:00
|
|
|
bodysize = (int)strlen(body);
|
2007-12-20 20:18:22 +00:00
|
|
|
/* We are not using keep-alive HTTP connections.
|
|
|
|
* HTTP/1.1 needs the header Connection: close to do that.
|
2010-02-25 12:39:22 +00:00
|
|
|
* This is the default with HTTP/1.0
|
|
|
|
* Using HTTP/1.1 means we need to support chunked transfer-encoding :
|
|
|
|
* When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked
|
|
|
|
* transfer encoding. */
|
2008-02-21 14:38:01 +00:00
|
|
|
/* Connection: Close is normally there only in HTTP/1.1 but who knows */
|
2008-02-23 15:31:39 +00:00
|
|
|
portstr[0] = '\0';
|
|
|
|
if(port != 80)
|
|
|
|
snprintf(portstr, sizeof(portstr), ":%hu", port);
|
2007-11-06 16:02:50 +00:00
|
|
|
headerssize = snprintf(headerbuf, sizeof(headerbuf),
|
2010-04-13 00:11:34 +00:00
|
|
|
"POST %s HTTP/%s\r\n"
|
2008-02-23 15:31:39 +00:00
|
|
|
"Host: %s%s\r\n"
|
2009-02-07 00:11:58 +00:00
|
|
|
"User-Agent: " OS_STRING ", UPnP/1.0, MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
|
2007-12-20 20:18:22 +00:00
|
|
|
"Content-Length: %d\r\n"
|
2007-11-06 16:02:50 +00:00
|
|
|
"Content-Type: text/xml\r\n"
|
|
|
|
"SOAPAction: \"%s\"\r\n"
|
2008-02-21 14:38:01 +00:00
|
|
|
"Connection: Close\r\n"
|
2008-02-23 15:31:39 +00:00
|
|
|
"Cache-Control: no-cache\r\n" /* ??? */
|
|
|
|
"Pragma: no-cache\r\n"
|
2007-11-06 16:02:50 +00:00
|
|
|
"\r\n",
|
2010-04-13 00:11:34 +00:00
|
|
|
url, httpversion, host, portstr, bodysize, action);
|
2008-02-23 15:31:39 +00:00
|
|
|
#ifdef DEBUG
|
2010-12-22 16:10:14 +00:00
|
|
|
/*printf("SOAP request : headersize=%d bodysize=%d\n",
|
2008-02-23 15:31:39 +00:00
|
|
|
headerssize, bodysize);
|
2010-12-22 16:10:14 +00:00
|
|
|
*/
|
|
|
|
printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
|
|
|
|
url, httpversion, host, portstr);
|
|
|
|
printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
|
2008-02-23 15:31:39 +00:00
|
|
|
/*printf("%s", headerbuf);*/
|
|
|
|
#endif
|
2007-11-06 16:02:50 +00:00
|
|
|
return httpWrite(fd, body, bodysize, headerbuf, headerssize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|