2015-12-16 10:26:03 +00:00
|
|
|
/* $Id: upnphttp.c,v 1.103 2015/12/16 10:21:49 nanard Exp $ */
|
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab */
|
2011-09-28 19:13:20 +00:00
|
|
|
/* Project : miniupnp
|
|
|
|
* Website : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
|
|
|
* Author : Thomas Bernard
|
2015-12-16 10:26:03 +00:00
|
|
|
* Copyright (c) 2005-2015 Thomas Bernard
|
2011-09-28 19:13:20 +00:00
|
|
|
* This software is subject to the conditions detailed in the
|
|
|
|
* LICENCE file included in this distribution.
|
|
|
|
* */
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/param.h>
|
2012-01-20 21:55:43 +00:00
|
|
|
#include <netinet/in.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <ctype.h>
|
2011-11-18 11:15:24 +00:00
|
|
|
#include <errno.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "config.h"
|
2012-10-04 22:17:40 +00:00
|
|
|
#ifdef ENABLE_HTTP_DATE
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "upnphttp.h"
|
|
|
|
#include "upnpdescgen.h"
|
|
|
|
#include "miniupnpdpath.h"
|
|
|
|
#include "upnpsoap.h"
|
|
|
|
#include "upnpevents.h"
|
2012-02-07 00:26:15 +00:00
|
|
|
#include "upnputils.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
#include <openssl/err.h>
|
2014-04-09 14:09:31 +00:00
|
|
|
#include <openssl/engine.h>
|
|
|
|
#include <openssl/conf.h>
|
2014-04-09 13:35:06 +00:00
|
|
|
static SSL_CTX *ssl_ctx = NULL;
|
|
|
|
|
|
|
|
#ifndef HTTPS_CERTFILE
|
|
|
|
#define HTTPS_CERTFILE "/etc/ssl/certs/ssl-cert-snakeoil.pem"
|
|
|
|
#endif
|
|
|
|
#ifndef HTTPS_KEYFILE
|
|
|
|
#define HTTPS_KEYFILE "/etc/ssl/private/ssl-cert-snakeoil.key"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
syslogsslerr(void)
|
|
|
|
{
|
|
|
|
unsigned long err;
|
|
|
|
char buffer[256];
|
|
|
|
while((err = ERR_get_error()) != 0) {
|
|
|
|
syslog(LOG_ERR, "%s", ERR_error_string(err, buffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:28:41 +00:00
|
|
|
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
syslog(LOG_DEBUG, "verify_callback(%d, %p)", preverify_ok, ctx);
|
|
|
|
return preverify_ok;
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:35:06 +00:00
|
|
|
int init_ssl(void)
|
|
|
|
{
|
2015-10-30 18:52:41 +00:00
|
|
|
const SSL_METHOD *method;
|
2014-04-09 13:35:06 +00:00
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
method = TLSv1_server_method();
|
|
|
|
if(method == NULL) {
|
|
|
|
syslog(LOG_ERR, "TLSv1_server_method() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ssl_ctx = SSL_CTX_new(method);
|
|
|
|
if(ssl_ctx == NULL) {
|
|
|
|
syslog(LOG_ERR, "SSL_CTX_new() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* set the local certificate */
|
|
|
|
if(!SSL_CTX_use_certificate_file(ssl_ctx, HTTPS_CERTFILE, SSL_FILETYPE_PEM)) {
|
|
|
|
syslog(LOG_ERR, "SSL_CTX_use_certificate_file(%s) failed", HTTPS_CERTFILE);
|
|
|
|
syslogsslerr();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* set the private key */
|
|
|
|
if(!SSL_CTX_use_PrivateKey_file(ssl_ctx, HTTPS_KEYFILE, SSL_FILETYPE_PEM)) {
|
|
|
|
syslog(LOG_ERR, "SSL_CTX_use_PrivateKey_file(%s) failed", HTTPS_KEYFILE);
|
|
|
|
syslogsslerr();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* verify private key */
|
|
|
|
if(!SSL_CTX_check_private_key(ssl_ctx)) {
|
|
|
|
syslog(LOG_ERR, "SSL_CTX_check_private_key() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-10 21:28:41 +00:00
|
|
|
/*SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, verify_callback);*/
|
|
|
|
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, verify_callback);
|
|
|
|
/*SSL_CTX_set_verify_depth(depth);*/
|
2014-04-11 07:48:50 +00:00
|
|
|
syslog(LOG_INFO, "using %s", SSLeay_version(SSLEAY_VERSION));
|
2014-04-09 13:35:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-09 14:09:31 +00:00
|
|
|
|
|
|
|
void free_ssl(void)
|
|
|
|
{
|
|
|
|
/* free context */
|
|
|
|
if(ssl_ctx != NULL) {
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
ssl_ctx = NULL;
|
|
|
|
}
|
|
|
|
ERR_remove_state(0);
|
|
|
|
ENGINE_cleanup();
|
|
|
|
CONF_modules_unload(1);
|
|
|
|
ERR_free_strings();
|
|
|
|
EVP_cleanup();
|
|
|
|
CRYPTO_cleanup_all_ex_data();
|
|
|
|
}
|
2014-04-09 13:35:06 +00:00
|
|
|
#endif /* ENABLE_HTTPS */
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
struct upnphttp *
|
2011-09-28 19:13:20 +00:00
|
|
|
New_upnphttp(int s)
|
|
|
|
{
|
|
|
|
struct upnphttp * ret;
|
|
|
|
if(s<0)
|
|
|
|
return NULL;
|
|
|
|
ret = (struct upnphttp *)malloc(sizeof(struct upnphttp));
|
|
|
|
if(ret == NULL)
|
|
|
|
return NULL;
|
|
|
|
memset(ret, 0, sizeof(struct upnphttp));
|
|
|
|
ret->socket = s;
|
2012-02-07 00:26:15 +00:00
|
|
|
if(!set_non_blocking(s))
|
|
|
|
syslog(LOG_WARNING, "New_upnphttp::set_non_blocking(): %m");
|
2011-09-28 19:13:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
void
|
|
|
|
InitSSL_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
h->ssl = SSL_new(ssl_ctx);
|
|
|
|
if(h->ssl == NULL) {
|
|
|
|
syslog(LOG_ERR, "SSL_new() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if(!SSL_set_fd(h->ssl, h->socket)) {
|
|
|
|
syslog(LOG_ERR, "SSL_set_fd() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
r = SSL_accept(h->ssl); /* start the handshaking */
|
|
|
|
if(r < 0) {
|
|
|
|
int err;
|
|
|
|
err = SSL_get_error(h->ssl, r);
|
|
|
|
syslog(LOG_DEBUG, "SSL_accept() returned %d, SSL_get_error() %d", r, err);
|
|
|
|
if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) {
|
|
|
|
syslog(LOG_ERR, "SSL_accept() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_HTTPS */
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
void
|
|
|
|
CloseSocket_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
/* SSL_shutdown() ? */
|
2011-09-28 19:13:20 +00:00
|
|
|
if(close(h->socket) < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "CloseSocket_upnphttp: close(%d): %m", h->socket);
|
|
|
|
}
|
|
|
|
h->socket = -1;
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EToDelete;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Delete_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
if(h)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl)
|
|
|
|
SSL_free(h->ssl);
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(h->socket >= 0)
|
|
|
|
CloseSocket_upnphttp(h);
|
|
|
|
if(h->req_buf)
|
|
|
|
free(h->req_buf);
|
|
|
|
if(h->res_buf)
|
|
|
|
free(h->res_buf);
|
|
|
|
free(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:56:27 +00:00
|
|
|
/* parse HttpHeaders of the REQUEST
|
|
|
|
* This function is called after the \r\n\r\n character
|
|
|
|
* sequence has been found in h->req_buf */
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
|
|
|
ParseHttpHeaders(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
char * line;
|
|
|
|
char * colon;
|
|
|
|
char * p;
|
|
|
|
int n;
|
2013-01-29 21:56:27 +00:00
|
|
|
if((h->req_buf == NULL) || (h->req_contentoff <= 0))
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
line = h->req_buf;
|
|
|
|
while(line < (h->req_buf + h->req_contentoff))
|
|
|
|
{
|
2013-01-29 21:55:57 +00:00
|
|
|
colon = line;
|
|
|
|
while(*colon != ':')
|
|
|
|
{
|
|
|
|
if(*colon == '\r' || *colon == '\n')
|
|
|
|
{
|
|
|
|
colon = NULL; /* no ':' character found on the line */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
colon++;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
if(colon)
|
|
|
|
{
|
2014-12-09 17:28:23 +00:00
|
|
|
if(strncasecmp(line, "Content-Length:", 15)==0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
p = colon;
|
2014-12-09 10:02:57 +00:00
|
|
|
while((*p < '0' || *p > '9') && (*p != '\r') && (*p != '\n'))
|
2011-09-28 19:13:20 +00:00
|
|
|
p++;
|
|
|
|
h->req_contentlen = atoi(p);
|
2013-01-27 20:07:40 +00:00
|
|
|
if(h->req_contentlen < 0) {
|
|
|
|
syslog(LOG_WARNING, "ParseHttpHeaders() invalid Content-Length %d", h->req_contentlen);
|
|
|
|
h->req_contentlen = 0;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
/*printf("*** Content-Lenght = %d ***\n", h->req_contentlen);
|
|
|
|
printf(" readbufflen=%d contentoff = %d\n",
|
|
|
|
h->req_buflen, h->req_contentoff);*/
|
|
|
|
}
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "Host:", 5)==0)
|
2014-12-09 16:49:02 +00:00
|
|
|
{
|
|
|
|
p = colon;
|
|
|
|
n = 0;
|
|
|
|
while(*p == ':' || *p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
while(p[n]>' ')
|
|
|
|
n++;
|
|
|
|
h->req_HostOff = p - h->req_buf;
|
|
|
|
h->req_HostLen = n;
|
|
|
|
}
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "SOAPAction:", 11)==0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
p = colon;
|
|
|
|
n = 0;
|
|
|
|
while(*p == ':' || *p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
while(p[n]>=' ')
|
|
|
|
n++;
|
|
|
|
if((p[0] == '"' && p[n-1] == '"')
|
|
|
|
|| (p[0] == '\'' && p[n-1] == '\''))
|
|
|
|
{
|
|
|
|
p++; n -= 2;
|
|
|
|
}
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_soapActionOff = p - h->req_buf;
|
2011-09-28 19:13:20 +00:00
|
|
|
h->req_soapActionLen = n;
|
|
|
|
}
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "accept-language:", 16) == 0)
|
2012-09-28 09:10:20 +00:00
|
|
|
{
|
|
|
|
p = colon;
|
|
|
|
n = 0;
|
|
|
|
while(*p == ':' || *p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
while(p[n]>=' ')
|
|
|
|
n++;
|
|
|
|
syslog(LOG_DEBUG, "accept-language HTTP header : '%.*s'", n, p);
|
|
|
|
/* keep only the 1st accepted language */
|
|
|
|
n = 0;
|
|
|
|
while(p[n]>' ' && p[n] != ',')
|
|
|
|
n++;
|
|
|
|
if(n >= (int)sizeof(h->accept_language))
|
|
|
|
n = (int)sizeof(h->accept_language) - 1;
|
|
|
|
memcpy(h->accept_language, p, n);
|
|
|
|
h->accept_language[n] = '\0';
|
|
|
|
}
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "expect:", 7) == 0)
|
2012-09-28 09:12:50 +00:00
|
|
|
{
|
|
|
|
p = colon;
|
|
|
|
n = 0;
|
|
|
|
while(*p == ':' || *p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
while(p[n]>=' ')
|
|
|
|
n++;
|
|
|
|
if(strncasecmp(p, "100-continue", 12) == 0) {
|
|
|
|
h->respflags |= FLAG_CONTINUE;
|
|
|
|
syslog(LOG_DEBUG, "\"Expect: 100-Continue\" header detected");
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_EVENTS
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "Callback:", 9)==0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2014-03-15 09:52:39 +00:00
|
|
|
/* The Callback can contain several urls :
|
|
|
|
* If there is more than one URL, when the service sends
|
|
|
|
* events, it will try these URLs in order until one
|
|
|
|
* succeeds. One or more URLs each enclosed by angle
|
|
|
|
* brackets ("<" and ">") */
|
2011-09-28 19:13:20 +00:00
|
|
|
p = colon;
|
|
|
|
while(*p != '<' && *p != '\r' )
|
|
|
|
p++;
|
|
|
|
n = 0;
|
2014-03-15 09:52:39 +00:00
|
|
|
while(p[n] != '\r')
|
2011-09-28 19:13:20 +00:00
|
|
|
n++;
|
2014-03-15 09:52:39 +00:00
|
|
|
while(n > 0 && p[n] != '>')
|
|
|
|
n--;
|
|
|
|
/* found last > character */
|
|
|
|
h->req_CallbackOff = p - h->req_buf;
|
|
|
|
h->req_CallbackLen = MAX(0, n + 1);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "SID:", 4)==0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
p = colon + 1;
|
2014-12-09 16:48:14 +00:00
|
|
|
while((*p == ' ') || (*p == '\t'))
|
2011-09-28 19:13:20 +00:00
|
|
|
p++;
|
|
|
|
n = 0;
|
|
|
|
while(!isspace(p[n]))
|
|
|
|
n++;
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_SIDOff = p - h->req_buf;
|
2011-09-28 19:13:20 +00:00
|
|
|
h->req_SIDLen = n;
|
|
|
|
}
|
|
|
|
/* Timeout: Seconds-nnnn */
|
|
|
|
/* TIMEOUT
|
|
|
|
Recommended. Requested duration until subscription expires,
|
|
|
|
either number of seconds or infinite. Recommendation
|
|
|
|
by a UPnP Forum working committee. Defined by UPnP vendor.
|
|
|
|
Consists of the keyword "Second-" followed (without an
|
|
|
|
intervening space) by either an integer or the keyword "infinite". */
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "Timeout:", 8)==0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
p = colon + 1;
|
2014-12-09 16:48:14 +00:00
|
|
|
while((*p == ' ') || (*p == '\t'))
|
2011-09-28 19:13:20 +00:00
|
|
|
p++;
|
|
|
|
if(strncasecmp(p, "Second-", 7)==0) {
|
|
|
|
h->req_Timeout = atoi(p+7);
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 22:25:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
2014-12-09 17:28:23 +00:00
|
|
|
else if(strncasecmp(line, "nt:", 3)==0)
|
2012-10-04 22:25:08 +00:00
|
|
|
{
|
|
|
|
p = colon + 1;
|
2014-12-09 10:38:50 +00:00
|
|
|
while((*p == ' ') || (*p == '\t'))
|
2012-10-04 22:25:08 +00:00
|
|
|
p++;
|
|
|
|
n = 0;
|
|
|
|
while(!isspace(p[n]))
|
|
|
|
n++;
|
|
|
|
h->req_NTOff = p - h->req_buf;
|
|
|
|
h->req_NTLen = n;
|
|
|
|
}
|
2014-12-09 10:04:38 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
|
|
|
#endif /* ENABLE_EVENTS */
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2013-01-29 21:56:27 +00:00
|
|
|
/* the loop below won't run off the end of the buffer
|
|
|
|
* because the buffer is guaranteed to contain the \r\n\r\n
|
|
|
|
* character sequence */
|
2011-09-28 19:13:20 +00:00
|
|
|
while(!(line[0] == '\r' && line[1] == '\n'))
|
|
|
|
line++;
|
|
|
|
line += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* very minimalistic 404 error message */
|
|
|
|
static void
|
|
|
|
Send404(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
static const char body404[] =
|
|
|
|
"<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>"
|
|
|
|
"<BODY><H1>Not Found</H1>The requested URL was not found"
|
|
|
|
" on this server.</BODY></HTML>\r\n";
|
2012-02-07 00:26:15 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
h->respflags = FLAG_HTML;
|
|
|
|
BuildResp2_upnphttp(h, 404, "Not Found",
|
|
|
|
body404, sizeof(body404) - 1);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 13:29:18 +00:00
|
|
|
static void
|
|
|
|
Send405(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
static const char body405[] =
|
|
|
|
"<HTML><HEAD><TITLE>405 Method Not Allowed</TITLE></HEAD>"
|
|
|
|
"<BODY><H1>Method Not Allowed</H1>The HTTP Method "
|
|
|
|
"is not allowed on this resource.</BODY></HTML>\r\n";
|
|
|
|
|
|
|
|
h->respflags |= FLAG_HTML;
|
|
|
|
BuildResp2_upnphttp(h, 405, "Method Not Allowed",
|
|
|
|
body405, sizeof(body405) - 1);
|
|
|
|
SendRespAndClose_upnphttp(h);
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
/* very minimalistic 501 error message */
|
|
|
|
static void
|
|
|
|
Send501(struct upnphttp * h)
|
|
|
|
{
|
2012-03-01 01:44:38 +00:00
|
|
|
static const char body501[] =
|
2011-09-28 19:13:20 +00:00
|
|
|
"<HTML><HEAD><TITLE>501 Not Implemented</TITLE></HEAD>"
|
|
|
|
"<BODY><H1>Not Implemented</H1>The HTTP Method "
|
|
|
|
"is not implemented by this server.</BODY></HTML>\r\n";
|
2012-02-07 00:26:15 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
h->respflags = FLAG_HTML;
|
|
|
|
BuildResp2_upnphttp(h, 501, "Not Implemented",
|
|
|
|
body501, sizeof(body501) - 1);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:54:55 +00:00
|
|
|
/* findendheaders() find the \r\n\r\n character sequence and
|
|
|
|
* return a pointer to it.
|
|
|
|
* It returns NULL if not found */
|
2011-09-28 19:13:20 +00:00
|
|
|
static const char *
|
|
|
|
findendheaders(const char * s, int len)
|
|
|
|
{
|
2013-01-29 21:54:55 +00:00
|
|
|
while(len-->3)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
if(s[0]=='\r' && s[1]=='\n' && s[2]=='\r' && s[3]=='\n')
|
|
|
|
return s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_DUMMY_SERVICE
|
|
|
|
static void
|
|
|
|
sendDummyDesc(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
static const char xml_desc[] = "<?xml version=\"1.0\"?>\r\n"
|
|
|
|
"<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">"
|
|
|
|
" <specVersion>"
|
|
|
|
" <major>1</major>"
|
|
|
|
" <minor>0</minor>"
|
|
|
|
" </specVersion>"
|
|
|
|
" <actionList />"
|
|
|
|
" <serviceStateTable />"
|
|
|
|
"</scpd>\r\n";
|
|
|
|
BuildResp_upnphttp(h, xml_desc, sizeof(xml_desc)-1);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Sends the description generated by the parameter */
|
|
|
|
static void
|
|
|
|
sendXMLdesc(struct upnphttp * h, char * (f)(int *))
|
|
|
|
{
|
|
|
|
char * desc;
|
|
|
|
int len;
|
|
|
|
desc = f(&len);
|
|
|
|
if(!desc)
|
|
|
|
{
|
|
|
|
static const char error500[] = "<HTML><HEAD><TITLE>Error 500</TITLE>"
|
|
|
|
"</HEAD><BODY>Internal Server Error</BODY></HTML>\r\n";
|
|
|
|
syslog(LOG_ERR, "Failed to generate XML description");
|
|
|
|
h->respflags = FLAG_HTML;
|
|
|
|
BuildResp2_upnphttp(h, 500, "Internal Server Error",
|
|
|
|
error500, sizeof(error500)-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BuildResp_upnphttp(h, desc, len);
|
|
|
|
}
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
free(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ProcessHTTPPOST_upnphttp()
|
|
|
|
* executes the SOAP query if it is possible */
|
|
|
|
static void
|
|
|
|
ProcessHTTPPOST_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
if((h->req_buflen - h->req_contentoff) >= h->req_contentlen)
|
|
|
|
{
|
2012-09-28 09:12:50 +00:00
|
|
|
/* the request body is received */
|
2012-04-25 22:30:32 +00:00
|
|
|
if(h->req_soapActionOff > 0)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
/* we can process the request */
|
|
|
|
syslog(LOG_INFO, "SOAPAction: %.*s",
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_soapActionLen, h->req_buf + h->req_soapActionOff);
|
2012-03-01 01:44:38 +00:00
|
|
|
ExecuteSoapAction(h,
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_buf + h->req_soapActionOff,
|
2011-09-28 19:13:20 +00:00
|
|
|
h->req_soapActionLen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static const char err400str[] =
|
|
|
|
"<html><body>Bad request</body></html>";
|
|
|
|
syslog(LOG_INFO, "No SOAPAction in HTTP headers");
|
|
|
|
h->respflags = FLAG_HTML;
|
|
|
|
BuildResp2_upnphttp(h, 400, "Bad Request",
|
|
|
|
err400str, sizeof(err400str) - 1);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-28 09:12:50 +00:00
|
|
|
else if(h->respflags & FLAG_CONTINUE)
|
|
|
|
{
|
|
|
|
/* Sending the 100 Continue response */
|
|
|
|
if(!h->res_buf) {
|
|
|
|
h->res_buf = malloc(256);
|
|
|
|
h->res_buf_alloclen = 256;
|
|
|
|
}
|
|
|
|
h->res_buflen = snprintf(h->res_buf, h->res_buf_alloclen,
|
|
|
|
"%s 100 Continue\r\n\r\n", h->HttpVer);
|
|
|
|
h->res_sent = 0;
|
|
|
|
h->state = ESendingContinue;
|
|
|
|
if(SendResp_upnphttp(h))
|
|
|
|
h->state = EWaitingForHttpContent;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* waiting for remaining data */
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EWaitingForHttpContent;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_EVENTS
|
|
|
|
/**
|
2014-03-15 09:52:39 +00:00
|
|
|
* checkCallbackURL()
|
|
|
|
* check that url is on originating IP
|
|
|
|
* extract first correct URL
|
2011-09-28 19:13:20 +00:00
|
|
|
* returns 0 if the callback header value is not valid
|
|
|
|
* 1 if it is valid.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
checkCallbackURL(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
char addrstr[48];
|
|
|
|
int ipv6;
|
|
|
|
const char * p;
|
2012-05-01 09:52:21 +00:00
|
|
|
unsigned int i;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
2014-03-15 09:52:39 +00:00
|
|
|
start_again:
|
|
|
|
if(h->req_CallbackOff <= 0 || h->req_CallbackLen < 10)
|
2011-09-28 19:13:20 +00:00
|
|
|
return 0;
|
2014-03-15 09:52:39 +00:00
|
|
|
if(memcmp(h->req_buf + h->req_CallbackOff, "<http://", 8) != 0) {
|
|
|
|
p = h->req_buf + h->req_CallbackOff + 1;
|
|
|
|
goto invalid;
|
|
|
|
}
|
|
|
|
/* extract host from url to addrstr[] */
|
2011-09-28 19:13:20 +00:00
|
|
|
i = 0;
|
2014-03-15 09:52:39 +00:00
|
|
|
p = h->req_buf + h->req_CallbackOff + 8;
|
2011-09-28 19:13:20 +00:00
|
|
|
if(*p == '[') {
|
|
|
|
p++;
|
|
|
|
ipv6 = 1;
|
2014-03-15 09:52:39 +00:00
|
|
|
while(*p != ']' && *p != '>' && i < (sizeof(addrstr)-1)
|
2012-04-25 22:30:32 +00:00
|
|
|
&& p < (h->req_buf + h->req_CallbackOff + h->req_CallbackLen))
|
2011-09-28 19:13:20 +00:00
|
|
|
addrstr[i++] = *(p++);
|
|
|
|
} else {
|
2014-03-15 09:52:39 +00:00
|
|
|
ipv6 = 0;
|
|
|
|
while(*p != '/' && *p != ':' && *p != '>' && i < (sizeof(addrstr)-1)
|
2012-04-25 22:30:32 +00:00
|
|
|
&& p < (h->req_buf + h->req_CallbackOff + h->req_CallbackLen))
|
2011-09-28 19:13:20 +00:00
|
|
|
addrstr[i++] = *(p++);
|
|
|
|
}
|
|
|
|
addrstr[i] = '\0';
|
2014-03-15 09:52:39 +00:00
|
|
|
/* check addrstr */
|
2011-09-28 19:13:20 +00:00
|
|
|
if(ipv6) {
|
2014-03-15 09:52:39 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
2011-09-28 19:13:20 +00:00
|
|
|
struct in6_addr addr;
|
|
|
|
if(inet_pton(AF_INET6, addrstr, &addr) <= 0)
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
if(!h->ipv6
|
|
|
|
|| (0!=memcmp(&addr, &(h->clientaddr_v6), sizeof(struct in6_addr))))
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
#else
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
struct in_addr addr;
|
|
|
|
if(inet_pton(AF_INET, addrstr, &addr) <= 0)
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(h->ipv6) {
|
|
|
|
if(!IN6_IS_ADDR_V4MAPPED(&(h->clientaddr_v6)))
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
if(0!=memcmp(&addr, ((const char *)&(h->clientaddr_v6) + 12), 4))
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
} else {
|
|
|
|
if(0!=memcmp(&addr, &(h->clientaddr), sizeof(struct in_addr)))
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(0!=memcmp(&addr, &(h->clientaddr), sizeof(struct in_addr)))
|
2014-03-15 09:52:39 +00:00
|
|
|
goto invalid;
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-03-15 09:52:39 +00:00
|
|
|
/* select only the good callback url */
|
|
|
|
while(p < h->req_buf + h->req_CallbackOff + h->req_CallbackLen && *p != '>')
|
|
|
|
p++;
|
|
|
|
h->req_CallbackOff++; /* skip initial '<' */
|
|
|
|
h->req_CallbackLen = (int)(p - h->req_buf - h->req_CallbackOff);
|
2011-09-28 19:13:20 +00:00
|
|
|
return 1;
|
2014-03-15 09:52:39 +00:00
|
|
|
invalid:
|
|
|
|
while(p < h->req_buf + h->req_CallbackOff + h->req_CallbackLen && *p != '>')
|
|
|
|
p++;
|
|
|
|
if(*p != '>') return 0;
|
|
|
|
while(p < h->req_buf + h->req_CallbackOff + h->req_CallbackLen && *p != '<')
|
|
|
|
p++;
|
|
|
|
if(*p != '<') return 0;
|
|
|
|
h->req_CallbackLen -= (int)(p - h->req_buf - h->req_CallbackOff);
|
|
|
|
h->req_CallbackOff = (int)(p - h->req_buf);
|
|
|
|
goto start_again;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ProcessHTTPSubscribe_upnphttp(struct upnphttp * h, const char * path)
|
|
|
|
{
|
|
|
|
const char * sid;
|
|
|
|
syslog(LOG_DEBUG, "ProcessHTTPSubscribe %s", path);
|
|
|
|
syslog(LOG_DEBUG, "Callback '%.*s' Timeout=%d",
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_CallbackLen, h->req_buf + h->req_CallbackOff,
|
|
|
|
h->req_Timeout);
|
|
|
|
syslog(LOG_DEBUG, "SID '%.*s'", h->req_SIDLen, h->req_buf + h->req_SIDOff);
|
2016-02-16 09:57:22 +00:00
|
|
|
#if defined(UPNP_STRICT) && (UPNP_VERSION_MAJOR > 1) || (UPNP_VERSION_MINOR > 0)
|
|
|
|
/*if(h->req_Timeout < 1800) {*/
|
|
|
|
if(h->req_Timeout == 0) {
|
|
|
|
/* Second-infinite is forbidden with UDA v1.1 and later :
|
|
|
|
* (UDA 1.1 : 4.1.1 Subscription)
|
|
|
|
* UPnP 1.1 control points MUST NOT subscribe using keyword infinite,
|
|
|
|
* UPnP 1.1 devices MUST NOT set actual subscription durations to
|
|
|
|
* "infinite". The presence of infinite in a request MUST be silently
|
|
|
|
* ignored by a UPnP 1.1 device (the presence of infinite is handled
|
|
|
|
* by the device as if the TIMEOUT header field in a request was not
|
|
|
|
* present) . The keyword infinite MUST NOT be returned by a UPnP 1.1
|
|
|
|
* device. */
|
|
|
|
h->req_Timeout = 1800; /* default to 30 minutes */
|
|
|
|
}
|
|
|
|
#endif /* UPNP_STRICT */
|
2012-04-25 22:30:32 +00:00
|
|
|
if((h->req_CallbackOff <= 0) && (h->req_SIDOff <= 0)) {
|
2011-09-28 19:13:20 +00:00
|
|
|
/* Missing or invalid CALLBACK : 412 Precondition Failed.
|
|
|
|
* If CALLBACK header is missing or does not contain a valid HTTP URL,
|
|
|
|
* the publisher must respond with HTTP error 412 Precondition Failed*/
|
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
} else {
|
|
|
|
/* - add to the subscriber list
|
2012-03-01 01:44:38 +00:00
|
|
|
* - respond HTTP/x.x 200 OK
|
2011-09-28 19:13:20 +00:00
|
|
|
* - Send the initial event message */
|
|
|
|
/* Server:, SID:; Timeout: Second-(xx|infinite) */
|
|
|
|
/* Check that the callback URL is on the same IP as
|
|
|
|
* the request, and not on the internet, nor on ourself (DOS attack ?) */
|
2012-04-25 22:30:32 +00:00
|
|
|
if(h->req_CallbackOff > 0) {
|
2012-10-04 22:25:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
/* SID: and Callback: are incompatible */
|
|
|
|
if(h->req_SIDOff > 0) {
|
|
|
|
syslog(LOG_WARNING, "Both Callback: and SID: in SUBSCRIBE");
|
|
|
|
BuildResp2_upnphttp(h, 400, "Incompatible header fields", 0, 0);
|
|
|
|
/* "NT: upnp:event" header is mandatory */
|
|
|
|
} else if(h->req_NTOff <= 0 || h->req_NTLen != 10 ||
|
|
|
|
0 != memcmp("upnp:event", h->req_buf + h->req_NTOff, 10)) {
|
|
|
|
syslog(LOG_WARNING, "Invalid NT in SUBSCRIBE %.*s",
|
|
|
|
h->req_NTLen, h->req_buf + h->req_NTOff);
|
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
|
|
|
} else
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(checkCallbackURL(h)) {
|
2012-04-25 22:30:32 +00:00
|
|
|
sid = upnpevents_addSubscriber(path, h->req_buf + h->req_CallbackOff,
|
2011-09-28 19:13:20 +00:00
|
|
|
h->req_CallbackLen, h->req_Timeout);
|
|
|
|
h->respflags = FLAG_TIMEOUT;
|
|
|
|
if(sid) {
|
|
|
|
syslog(LOG_DEBUG, "generated sid=%s", sid);
|
|
|
|
h->respflags |= FLAG_SID;
|
2012-04-25 22:30:32 +00:00
|
|
|
h->res_SID = sid;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
BuildResp_upnphttp(h, 0, 0);
|
|
|
|
} else {
|
|
|
|
syslog(LOG_WARNING, "Invalid Callback in SUBSCRIBE %.*s",
|
2012-04-25 22:30:32 +00:00
|
|
|
h->req_CallbackLen, h->req_buf + h->req_CallbackOff);
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* subscription renew */
|
|
|
|
/* Invalid SID
|
|
|
|
412 Precondition Failed. If a SID does not correspond to a known,
|
|
|
|
un-expired subscription, the publisher must respond
|
|
|
|
with HTTP error 412 Precondition Failed. */
|
2012-10-04 22:25:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
/* SID: and NT: headers are incompatibles */
|
|
|
|
if(h->req_NTOff > 0) {
|
|
|
|
syslog(LOG_WARNING, "Both NT: and SID: in SUBSCRIBE");
|
|
|
|
BuildResp2_upnphttp(h, 400, "Incompatible header fields", 0, 0);
|
2011-09-28 19:13:20 +00:00
|
|
|
} else {
|
2016-02-09 09:17:58 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
|
|
|
sid = upnpevents_renewSubscription(h->req_buf + h->req_SIDOff,
|
|
|
|
h->req_SIDLen, h->req_Timeout);
|
|
|
|
if(!sid) {
|
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
|
|
|
} else {
|
|
|
|
h->respflags = FLAG_TIMEOUT | FLAG_SID;
|
|
|
|
h->res_SID = sid;
|
|
|
|
BuildResp_upnphttp(h, 0, 0);
|
|
|
|
}
|
|
|
|
#ifdef UPNP_STRICT
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2016-02-09 09:17:58 +00:00
|
|
|
#endif /* UPNP_STRICT */
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ProcessHTTPUnSubscribe_upnphttp(struct upnphttp * h, const char * path)
|
|
|
|
{
|
|
|
|
syslog(LOG_DEBUG, "ProcessHTTPUnSubscribe %s", path);
|
2012-04-25 22:30:32 +00:00
|
|
|
syslog(LOG_DEBUG, "SID '%.*s'", h->req_SIDLen, h->req_buf + h->req_SIDOff);
|
2011-09-28 19:13:20 +00:00
|
|
|
/* Remove from the list */
|
2012-10-04 22:25:08 +00:00
|
|
|
#ifdef UPNP_STRICT
|
|
|
|
if(h->req_SIDOff <= 0 || h->req_SIDLen == 0) {
|
|
|
|
/* SID: header missing or empty */
|
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
|
|
|
} else if(h->req_CallbackOff > 0 || h->req_NTOff > 0) {
|
|
|
|
/* no NT: or Callback: header must be present */
|
|
|
|
BuildResp2_upnphttp(h, 400, "Incompatible header fields", 0, 0);
|
|
|
|
} else
|
|
|
|
#endif
|
2012-04-25 22:30:32 +00:00
|
|
|
if(upnpevents_removeSubscriber(h->req_buf + h->req_SIDOff, h->req_SIDLen) < 0) {
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildResp2_upnphttp(h, 412, "Precondition Failed", 0, 0);
|
|
|
|
} else {
|
|
|
|
BuildResp_upnphttp(h, 0, 0);
|
|
|
|
}
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
/* Parse and process Http Query
|
2013-01-29 21:56:27 +00:00
|
|
|
* called once all the HTTP headers have been received,
|
|
|
|
* so it is guaranteed that h->req_buf contains the \r\n\r\n
|
|
|
|
* character sequence */
|
2011-09-28 19:13:20 +00:00
|
|
|
static void
|
|
|
|
ProcessHttpQuery_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
2012-05-31 13:29:18 +00:00
|
|
|
static const struct {
|
|
|
|
const char * path;
|
|
|
|
char * (* f)(int *);
|
|
|
|
} path_desc[] = {
|
|
|
|
{ ROOTDESC_PATH, genRootDesc},
|
|
|
|
{ WANIPC_PATH, genWANIPCn},
|
|
|
|
{ WANCFG_PATH, genWANCfg},
|
|
|
|
#ifdef HAS_DUMMY_SERVICE
|
|
|
|
{ DUMMY_PATH, NULL},
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_L3F_SERVICE
|
|
|
|
{ L3F_PATH, genL3F},
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_6FC_SERVICE
|
|
|
|
{ WANIP6FC_PATH, gen6FC},
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_DP_SERVICE
|
|
|
|
{ DP_PATH, genDP},
|
|
|
|
#endif
|
|
|
|
{ NULL, NULL}
|
|
|
|
};
|
2011-09-28 19:13:20 +00:00
|
|
|
char HttpCommand[16];
|
|
|
|
char HttpUrl[128];
|
|
|
|
char * HttpVer;
|
|
|
|
char * p;
|
|
|
|
int i;
|
2015-12-16 10:26:03 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
p = h->req_buf;
|
|
|
|
if(!p)
|
|
|
|
return;
|
2013-01-29 21:56:27 +00:00
|
|
|
/* note : checking (*p != '\r') is enough to avoid runing off the
|
|
|
|
* end of the buffer, because h->req_buf is guaranteed to contain
|
|
|
|
* the \r\n\r\n character sequence */
|
2011-09-28 19:13:20 +00:00
|
|
|
for(i = 0; i<15 && *p != ' ' && *p != '\r'; i++)
|
|
|
|
HttpCommand[i] = *(p++);
|
|
|
|
HttpCommand[i] = '\0';
|
|
|
|
while(*p==' ')
|
|
|
|
p++;
|
|
|
|
for(i = 0; i<127 && *p != ' ' && *p != '\r'; i++)
|
|
|
|
HttpUrl[i] = *(p++);
|
|
|
|
HttpUrl[i] = '\0';
|
|
|
|
while(*p==' ')
|
|
|
|
p++;
|
|
|
|
HttpVer = h->HttpVer;
|
|
|
|
for(i = 0; i<15 && *p != '\r'; i++)
|
|
|
|
HttpVer[i] = *(p++);
|
|
|
|
HttpVer[i] = '\0';
|
2015-12-16 10:26:03 +00:00
|
|
|
syslog(LOG_INFO, "HTTP REQUEST from %s : %s %s (%s)",
|
|
|
|
h->clientaddr_str, HttpCommand, HttpUrl, HttpVer);
|
2011-09-28 19:13:20 +00:00
|
|
|
ParseHttpHeaders(h);
|
2014-12-09 16:49:02 +00:00
|
|
|
if(h->req_HostOff > 0 && h->req_HostLen > 0) {
|
|
|
|
syslog(LOG_DEBUG, "Host: %.*s", h->req_HostLen, h->req_buf + h->req_HostOff);
|
|
|
|
p = h->req_buf + h->req_HostOff;
|
|
|
|
if(*p == '[') {
|
|
|
|
/* IPv6 */
|
|
|
|
p++;
|
|
|
|
while(p < h->req_buf + h->req_HostOff + h->req_HostLen) {
|
|
|
|
if(*p == ']') break;
|
|
|
|
/* TODO check *p in [0-9a-f:.] */
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if(*p != ']') {
|
|
|
|
syslog(LOG_NOTICE, "DNS rebinding attack suspected (Host: %.*s)", h->req_HostLen, h->req_buf + h->req_HostOff);
|
|
|
|
Send404(h);/* 403 */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
/* TODO : Check port */
|
|
|
|
} else {
|
|
|
|
for(i = 0; i < h->req_HostLen; i++) {
|
|
|
|
if(*p != ':' && *p != '.' && (*p > '9' || *p < '0')) {
|
|
|
|
syslog(LOG_NOTICE, "DNS rebinding attack suspected (Host: %.*s)", h->req_HostLen, h->req_buf + h->req_HostOff);
|
|
|
|
Send404(h);/* 403 */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
if(strcmp("POST", HttpCommand) == 0)
|
|
|
|
{
|
|
|
|
h->req_command = EPost;
|
|
|
|
ProcessHTTPPOST_upnphttp(h);
|
|
|
|
}
|
|
|
|
else if(strcmp("GET", HttpCommand) == 0)
|
|
|
|
{
|
|
|
|
h->req_command = EGet;
|
2012-05-31 13:29:18 +00:00
|
|
|
for(i=0; path_desc[i].path; i++) {
|
|
|
|
if(strcasecmp(path_desc[i].path, HttpUrl) == 0) {
|
|
|
|
if(path_desc[i].f)
|
|
|
|
sendXMLdesc(h, path_desc[i].f);
|
|
|
|
else
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef HAS_DUMMY_SERVICE
|
2012-05-31 13:29:18 +00:00
|
|
|
sendDummyDesc(h);
|
|
|
|
#else
|
|
|
|
continue;
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
2012-05-31 13:29:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-05-31 13:29:18 +00:00
|
|
|
if(0 == memcmp(HttpUrl, "/ctl/", 5)) {
|
|
|
|
/* 405 Method Not Allowed
|
|
|
|
* Allow: POST */
|
|
|
|
h->respflags = FLAG_ALLOW_POST;
|
|
|
|
Send405(h);
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2012-05-31 13:29:18 +00:00
|
|
|
#ifdef ENABLE_EVENTS
|
|
|
|
if(0 == memcmp(HttpUrl, "/evt/", 5)) {
|
|
|
|
/* 405 Method Not Allowed
|
|
|
|
* Allow: SUBSCRIBE, UNSUBSCRIBE */
|
|
|
|
h->respflags = FLAG_ALLOW_SUB_UNSUB;
|
|
|
|
Send405(h);
|
|
|
|
return;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-05-31 13:29:18 +00:00
|
|
|
syslog(LOG_NOTICE, "%s not found, responding ERROR 404", HttpUrl);
|
|
|
|
Send404(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#ifdef ENABLE_EVENTS
|
|
|
|
else if(strcmp("SUBSCRIBE", HttpCommand) == 0)
|
|
|
|
{
|
|
|
|
h->req_command = ESubscribe;
|
|
|
|
ProcessHTTPSubscribe_upnphttp(h, HttpUrl);
|
|
|
|
}
|
|
|
|
else if(strcmp("UNSUBSCRIBE", HttpCommand) == 0)
|
|
|
|
{
|
|
|
|
h->req_command = EUnSubscribe;
|
|
|
|
ProcessHTTPUnSubscribe_upnphttp(h, HttpUrl);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
else if(strcmp("SUBSCRIBE", HttpCommand) == 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "SUBSCRIBE not implemented. ENABLE_EVENTS compile option disabled");
|
|
|
|
Send501(h);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_NOTICE, "Unsupported HTTP Command %s", HttpCommand);
|
|
|
|
Send501(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Process_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
2012-12-11 21:10:57 +00:00
|
|
|
char * h_tmp;
|
2011-09-28 19:13:20 +00:00
|
|
|
char buf[2048];
|
|
|
|
int n;
|
2012-02-07 00:26:15 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
if(!h)
|
|
|
|
return;
|
|
|
|
switch(h->state)
|
|
|
|
{
|
2012-02-07 00:26:15 +00:00
|
|
|
case EWaitingForHttpRequest:
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
n = SSL_read(h->ssl, buf, sizeof(buf));
|
|
|
|
} else {
|
|
|
|
n = recv(h->socket, buf, sizeof(buf), 0);
|
|
|
|
}
|
|
|
|
#else
|
2012-02-07 00:26:15 +00:00
|
|
|
n = recv(h->socket, buf, sizeof(buf), 0);
|
2014-04-09 13:35:06 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(n<0)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
int err;
|
|
|
|
err = SSL_get_error(h->ssl, n);
|
|
|
|
if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "SSL_read() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
h->state = EToDelete;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
2012-02-02 17:45:43 +00:00
|
|
|
if(errno != EAGAIN &&
|
|
|
|
errno != EWOULDBLOCK &&
|
|
|
|
errno != EINTR)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "recv (state0): %m");
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EToDelete;
|
2012-02-02 17:45:43 +00:00
|
|
|
}
|
|
|
|
/* if errno is EAGAIN, EWOULDBLOCK or EINTR, try again later */
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else if(n==0)
|
|
|
|
{
|
2015-06-06 02:27:20 +00:00
|
|
|
syslog(LOG_WARNING, "HTTP Connection from %s closed unexpectedly", inet_ntoa(h->clientaddr));
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EToDelete;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * endheaders;
|
|
|
|
/* if 1st arg of realloc() is null,
|
|
|
|
* realloc behaves the same as malloc() */
|
2012-12-11 21:10:57 +00:00
|
|
|
h_tmp = (char *)realloc(h->req_buf, n + h->req_buflen + 1);
|
|
|
|
if (h_tmp == NULL)
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "Unable to allocate new memory for h->req_buf)");
|
|
|
|
h->state = EToDelete;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h->req_buf = h_tmp;
|
|
|
|
memcpy(h->req_buf + h->req_buflen, buf, n);
|
|
|
|
h->req_buflen += n;
|
|
|
|
h->req_buf[h->req_buflen] = '\0';
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
/* search for the string "\r\n\r\n" */
|
|
|
|
endheaders = findendheaders(h->req_buf, h->req_buflen);
|
|
|
|
if(endheaders)
|
|
|
|
{
|
2013-01-29 21:56:27 +00:00
|
|
|
/* at this point, the request buffer (h->req_buf)
|
|
|
|
* is guaranteed to contain the \r\n\r\n character sequence */
|
2011-09-28 19:13:20 +00:00
|
|
|
h->req_contentoff = endheaders - h->req_buf + 4;
|
|
|
|
ProcessHttpQuery_upnphttp(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-02-07 00:26:15 +00:00
|
|
|
case EWaitingForHttpContent:
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
n = SSL_read(h->ssl, buf, sizeof(buf));
|
|
|
|
} else {
|
|
|
|
n = recv(h->socket, buf, sizeof(buf), 0);
|
|
|
|
}
|
|
|
|
#else
|
2012-02-07 00:26:15 +00:00
|
|
|
n = recv(h->socket, buf, sizeof(buf), 0);
|
2014-04-09 13:35:06 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(n<0)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
int err;
|
|
|
|
err = SSL_get_error(h->ssl, n);
|
|
|
|
if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "SSL_read() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
h->state = EToDelete;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
2012-02-02 17:45:43 +00:00
|
|
|
if(errno != EAGAIN &&
|
|
|
|
errno != EWOULDBLOCK &&
|
|
|
|
errno != EINTR)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "recv (state1): %m");
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EToDelete;
|
2012-02-02 17:45:43 +00:00
|
|
|
}
|
|
|
|
/* if errno is EAGAIN, EWOULDBLOCK or EINTR, try again later */
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else if(n==0)
|
|
|
|
{
|
2015-06-06 02:27:20 +00:00
|
|
|
syslog(LOG_WARNING, "HTTP Connection from %s closed unexpectedly", inet_ntoa(h->clientaddr));
|
2012-02-07 00:26:15 +00:00
|
|
|
h->state = EToDelete;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-07 00:26:15 +00:00
|
|
|
void * tmp = realloc(h->req_buf, n + h->req_buflen);
|
|
|
|
if(!tmp)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2012-02-07 00:26:15 +00:00
|
|
|
syslog(LOG_ERR, "memory allocation error %m");
|
|
|
|
h->state = EToDelete;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h->req_buf = tmp;
|
|
|
|
memcpy(h->req_buf + h->req_buflen, buf, n);
|
|
|
|
h->req_buflen += n;
|
|
|
|
if((h->req_buflen - h->req_contentoff) >= h->req_contentlen)
|
|
|
|
{
|
|
|
|
ProcessHTTPPOST_upnphttp(h);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-09-28 09:12:50 +00:00
|
|
|
case ESendingContinue:
|
|
|
|
if(SendResp_upnphttp(h))
|
|
|
|
h->state = EWaitingForHttpContent;
|
|
|
|
break;
|
2012-02-07 00:26:15 +00:00
|
|
|
case ESendingAndClosing:
|
|
|
|
SendRespAndClose_upnphttp(h);
|
|
|
|
break;
|
2011-09-28 19:13:20 +00:00
|
|
|
default:
|
|
|
|
syslog(LOG_WARNING, "Unexpected state: %d", h->state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char httpresphead[] =
|
|
|
|
"%s %d %s\r\n"
|
|
|
|
"Content-Type: %s\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Length: %d\r\n"
|
|
|
|
"Server: " MINIUPNPD_SERVER_STRING "\r\n"
|
2013-02-07 10:26:48 +00:00
|
|
|
"Ext:\r\n"
|
2011-09-28 19:13:20 +00:00
|
|
|
; /*"\r\n";*/
|
|
|
|
/*
|
|
|
|
"<?xml version=\"1.0\"?>\n"
|
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body>"
|
|
|
|
|
|
|
|
"</s:Body>"
|
|
|
|
"</s:Envelope>";
|
|
|
|
*/
|
|
|
|
/* with response code and response message
|
|
|
|
* also allocate enough memory */
|
|
|
|
|
2014-12-09 10:04:15 +00:00
|
|
|
int
|
2011-09-28 19:13:20 +00:00
|
|
|
BuildHeader_upnphttp(struct upnphttp * h, int respcode,
|
|
|
|
const char * respmsg,
|
|
|
|
int bodylen)
|
|
|
|
{
|
|
|
|
int templen;
|
2012-10-04 22:28:06 +00:00
|
|
|
if(!h->res_buf ||
|
|
|
|
h->res_buf_alloclen < ((int)sizeof(httpresphead) + 256 + bodylen)) {
|
|
|
|
if(h->res_buf)
|
|
|
|
free(h->res_buf);
|
|
|
|
templen = sizeof(httpresphead) + 256 + bodylen;
|
2011-09-28 19:13:20 +00:00
|
|
|
h->res_buf = (char *)malloc(templen);
|
2012-10-04 22:28:06 +00:00
|
|
|
if(!h->res_buf) {
|
2011-09-28 19:13:20 +00:00
|
|
|
syslog(LOG_ERR, "malloc error in BuildHeader_upnphttp()");
|
2014-12-09 10:04:15 +00:00
|
|
|
return -1;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
h->res_buf_alloclen = templen;
|
|
|
|
}
|
2012-09-28 09:12:01 +00:00
|
|
|
h->res_sent = 0;
|
2011-09-28 19:13:20 +00:00
|
|
|
h->res_buflen = snprintf(h->res_buf, h->res_buf_alloclen,
|
|
|
|
httpresphead, h->HttpVer,
|
|
|
|
respcode, respmsg,
|
2012-09-28 09:12:01 +00:00
|
|
|
(h->respflags&FLAG_HTML)?"text/html":"text/xml; charset=\"utf-8\"",
|
2011-09-28 19:13:20 +00:00
|
|
|
bodylen);
|
2012-09-28 09:12:01 +00:00
|
|
|
/* Content-Type MUST be 'text/xml; charset="utf-8"' according to UDA v1.1 */
|
|
|
|
/* Content-Type MUST be 'text/xml' according to UDA v1.0 */
|
2011-09-28 19:13:20 +00:00
|
|
|
/* Additional headers */
|
2012-10-04 22:17:40 +00:00
|
|
|
#ifdef ENABLE_HTTP_DATE
|
|
|
|
{
|
|
|
|
char http_date[64];
|
|
|
|
time_t t;
|
|
|
|
struct tm tm;
|
|
|
|
time(&t);
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
/* %a and %b depend on locale */
|
|
|
|
strftime(http_date, sizeof(http_date),
|
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"Date: %s\r\n", http_date);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_EVENTS
|
|
|
|
if(h->respflags & FLAG_TIMEOUT) {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"Timeout: Second-");
|
|
|
|
if(h->req_Timeout) {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"%d\r\n", h->req_Timeout);
|
|
|
|
} else {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"infinite\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(h->respflags & FLAG_SID) {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
2012-04-25 22:30:32 +00:00
|
|
|
"SID: %s\r\n", h->res_SID);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-05-31 13:29:18 +00:00
|
|
|
if(h->respflags & FLAG_ALLOW_POST) {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"Allow: %s\r\n", "POST");
|
|
|
|
} else if(h->respflags & FLAG_ALLOW_SUB_UNSUB) {
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"Allow: %s\r\n", "SUBSCRIBE, UNSUBSCRIBE");
|
|
|
|
}
|
2012-09-28 09:10:20 +00:00
|
|
|
if(h->accept_language[0] != '\0') {
|
|
|
|
/* defaulting to "en" */
|
|
|
|
h->res_buflen += snprintf(h->res_buf + h->res_buflen,
|
|
|
|
h->res_buf_alloclen - h->res_buflen,
|
|
|
|
"Content-Language: %s\r\n",
|
|
|
|
h->accept_language[0] == '*' ? "en" : h->accept_language);
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
h->res_buf[h->res_buflen++] = '\r';
|
|
|
|
h->res_buf[h->res_buflen++] = '\n';
|
|
|
|
if(h->res_buf_alloclen < (h->res_buflen + bodylen))
|
|
|
|
{
|
|
|
|
char * tmp;
|
|
|
|
tmp = (char *)realloc(h->res_buf, (h->res_buflen + bodylen));
|
|
|
|
if(tmp)
|
|
|
|
{
|
|
|
|
h->res_buf = tmp;
|
|
|
|
h->res_buf_alloclen = h->res_buflen + bodylen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "realloc error in BuildHeader_upnphttp()");
|
2014-12-09 10:04:15 +00:00
|
|
|
return -1;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 10:04:15 +00:00
|
|
|
return 0;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BuildResp2_upnphttp(struct upnphttp * h, int respcode,
|
|
|
|
const char * respmsg,
|
|
|
|
const char * body, int bodylen)
|
|
|
|
{
|
2014-12-09 10:04:15 +00:00
|
|
|
int r = BuildHeader_upnphttp(h, respcode, respmsg, bodylen);
|
2014-12-09 10:44:28 +00:00
|
|
|
if(body && (r >= 0)) {
|
2011-09-28 19:13:20 +00:00
|
|
|
memcpy(h->res_buf + h->res_buflen, body, bodylen);
|
2014-12-09 10:44:28 +00:00
|
|
|
h->res_buflen += bodylen;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* responding 200 OK ! */
|
|
|
|
void
|
|
|
|
BuildResp_upnphttp(struct upnphttp * h,
|
2014-12-09 10:04:38 +00:00
|
|
|
const char * body, int bodylen)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
BuildResp2_upnphttp(h, 200, "OK", body, bodylen);
|
|
|
|
}
|
|
|
|
|
2012-09-28 09:12:50 +00:00
|
|
|
int
|
|
|
|
SendResp_upnphttp(struct upnphttp * h)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
while (h->res_sent < h->res_buflen)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
n = SSL_write(h->ssl, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent);
|
|
|
|
} else {
|
|
|
|
n = send(h->socket, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent, 0);
|
|
|
|
}
|
|
|
|
#else
|
2012-09-28 09:12:50 +00:00
|
|
|
n = send(h->socket, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent, 0);
|
2014-04-09 13:35:06 +00:00
|
|
|
#endif
|
2012-09-28 09:12:50 +00:00
|
|
|
if(n<0)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
int err;
|
|
|
|
err = SSL_get_error(h->ssl, n);
|
|
|
|
if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
|
|
|
/* try again later */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "SSL_write() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
#endif
|
2012-09-28 09:12:50 +00:00
|
|
|
if(errno == EINTR)
|
|
|
|
continue; /* try again immediatly */
|
|
|
|
if(errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
/* try again later */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "send(res_buf): %m");
|
|
|
|
break; /* avoid infinite loop */
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-28 09:12:50 +00:00
|
|
|
}
|
|
|
|
else if(n == 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "send(res_buf): %d bytes sent (out of %d)",
|
|
|
|
h->res_sent, h->res_buflen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h->res_sent += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1; /* finished */
|
|
|
|
}
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
void
|
2011-11-18 11:23:16 +00:00
|
|
|
SendRespAndClose_upnphttp(struct upnphttp * h)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
|
|
|
ssize_t n;
|
2012-02-07 00:26:15 +00:00
|
|
|
|
|
|
|
while (h->res_sent < h->res_buflen)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
n = SSL_write(h->ssl, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent);
|
|
|
|
} else {
|
|
|
|
n = send(h->socket, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent, 0);
|
|
|
|
}
|
|
|
|
#else
|
2012-02-07 00:26:15 +00:00
|
|
|
n = send(h->socket, h->res_buf + h->res_sent,
|
|
|
|
h->res_buflen - h->res_sent, 0);
|
2014-04-09 13:35:06 +00:00
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(n<0)
|
|
|
|
{
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
if(h->ssl) {
|
|
|
|
int err;
|
|
|
|
err = SSL_get_error(h->ssl, n);
|
|
|
|
if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
|
|
|
/* try again later */
|
|
|
|
h->state = ESendingAndClosing;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "SSL_write() failed");
|
|
|
|
syslogsslerr();
|
|
|
|
break; /* avoid infinite loop */
|
|
|
|
} else {
|
|
|
|
#endif
|
2012-02-07 00:26:15 +00:00
|
|
|
if(errno == EINTR)
|
|
|
|
continue; /* try again immediatly */
|
|
|
|
if(errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
/* try again later */
|
|
|
|
h->state = ESendingAndClosing;
|
|
|
|
return;
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
syslog(LOG_ERR, "send(res_buf): %m");
|
2012-02-07 00:26:15 +00:00
|
|
|
break; /* avoid infinite loop */
|
2014-04-09 13:35:06 +00:00
|
|
|
#ifdef ENABLE_HTTPS
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
else if(n == 0)
|
|
|
|
{
|
2012-02-07 00:26:15 +00:00
|
|
|
syslog(LOG_ERR, "send(res_buf): %d bytes sent (out of %d)",
|
|
|
|
h->res_sent, h->res_buflen);
|
2011-09-28 19:13:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-07 00:26:15 +00:00
|
|
|
h->res_sent += n;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-18 11:23:16 +00:00
|
|
|
CloseSocket_upnphttp(h);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|