2018-01-16 01:06:46 +00:00
|
|
|
/* $Id: minisoap.c,v 1.25 2017/04/21 10:03:24 nanard Exp $ */
|
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* Project : miniupnp
|
2011-09-27 20:25:35 +00:00
|
|
|
* Author : Thomas Bernard
|
2018-01-16 01:06:46 +00:00
|
|
|
* Copyright (c) 2005-2018 Thomas Bernard
|
2011-09-27 20:25:35 +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>
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <io.h>
|
|
|
|
#include <winsock2.h>
|
2020-10-02 20:32:36 +00:00
|
|
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
|
|
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
|
|
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
|
|
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
|
|
|
#endif
|
2011-09-27 20:25:35 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include "minisoap.h"
|
|
|
|
#include "miniupnpcstrings.h"
|
|
|
|
|
|
|
|
/* only for malloc */
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* httpWrite sends the headers and the body to the socket
|
|
|
|
* and returns the number of bytes sent */
|
|
|
|
static int
|
2018-04-06 10:21:36 +00:00
|
|
|
httpWrite(SOCKET fd, const char * body, int bodysize,
|
2011-09-27 20:25:35 +00:00
|
|
|
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;
|
2015-10-26 10:27:10 +00:00
|
|
|
/* TODO: AVOID MALLOC, we could use writev() for that */
|
2011-09-27 20:25:35 +00:00
|
|
|
p = malloc(headerssize+bodysize);
|
|
|
|
if(!p)
|
2015-10-26 10:27:10 +00:00
|
|
|
return -1;
|
2011-09-27 20:25:35 +00:00
|
|
|
memcpy(p, headers, headerssize);
|
|
|
|
memcpy(p+headerssize, body, bodysize);
|
|
|
|
/*n = write(fd, p, headerssize+bodysize);*/
|
|
|
|
n = send(fd, p, headerssize+bodysize, 0);
|
|
|
|
if(n<0) {
|
|
|
|
PRINT_SOCKET_ERROR("send");
|
|
|
|
}
|
|
|
|
/* disable send on the socket */
|
2018-01-16 01:06:46 +00:00
|
|
|
/* draytek routers don't seem to like that... */
|
2011-09-27 20:25:35 +00:00
|
|
|
#if 0
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
if(shutdown(fd, SD_SEND)<0) {
|
|
|
|
#else
|
|
|
|
if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/
|
|
|
|
#endif
|
|
|
|
PRINT_SOCKET_ERROR("shutdown");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
free(p);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* self explanatory */
|
2018-04-06 10:21:36 +00:00
|
|
|
int soapPostSubmit(SOCKET fd,
|
2011-09-27 20:25:35 +00:00
|
|
|
const char * url,
|
|
|
|
const char * host,
|
|
|
|
unsigned short port,
|
|
|
|
const char * action,
|
|
|
|
const char * body,
|
|
|
|
const char * httpversion)
|
|
|
|
{
|
|
|
|
char headerbuf[512];
|
|
|
|
int headerssize;
|
|
|
|
char portstr[8];
|
2018-06-18 20:35:59 +00:00
|
|
|
int bodysize = (int)strlen(body);
|
2011-09-27 20:25:35 +00:00
|
|
|
/* We are not using keep-alive HTTP connections.
|
|
|
|
* HTTP/1.1 needs the header Connection: close to do that.
|
|
|
|
* 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. */
|
|
|
|
/* Connection: Close is normally there only in HTTP/1.1 but who knows */
|
|
|
|
portstr[0] = '\0';
|
|
|
|
if(port != 80)
|
|
|
|
snprintf(portstr, sizeof(portstr), ":%hu", port);
|
|
|
|
headerssize = snprintf(headerbuf, sizeof(headerbuf),
|
|
|
|
"POST %s HTTP/%s\r\n"
|
|
|
|
"Host: %s%s\r\n"
|
2014-11-05 05:34:51 +00:00
|
|
|
"User-Agent: " OS_STRING ", " UPNP_VERSION_STRING ", MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n"
|
2011-09-27 20:25:35 +00:00
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Content-Type: text/xml\r\n"
|
|
|
|
"SOAPAction: \"%s\"\r\n"
|
|
|
|
"Connection: Close\r\n"
|
|
|
|
"Cache-Control: no-cache\r\n" /* ??? */
|
|
|
|
"Pragma: no-cache\r\n"
|
|
|
|
"\r\n",
|
|
|
|
url, httpversion, host, portstr, bodysize, action);
|
2015-10-25 20:05:14 +00:00
|
|
|
if ((unsigned int)headerssize >= sizeof(headerbuf))
|
2015-10-26 10:27:10 +00:00
|
|
|
return -1;
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/*printf("SOAP request : headersize=%d bodysize=%d\n",
|
|
|
|
headerssize, bodysize);
|
|
|
|
*/
|
|
|
|
printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n",
|
|
|
|
url, httpversion, host, portstr);
|
|
|
|
printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize);
|
|
|
|
printf("Headers :\n%s", headerbuf);
|
|
|
|
printf("Body :\n%s\n", body);
|
|
|
|
#endif
|
|
|
|
return httpWrite(fd, body, bodysize, headerbuf, headerssize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|