1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
|
/**************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (C) 2012 France Telecom All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************/
#include "config.h"
#if EXCLUDE_MINISERVER == 0
/*!
* \file
*
* \brief Implements the functionality and utility functions
* used by the Miniserver module.
*
* The miniserver is a central point for processing all network requests.
* It is made of:
* - The SSDP sockets for discovery.
* - The HTTP listeners for description / control / eventing.
*
*/
#include "miniserver.h"
#include "httpreadwrite.h"
#include "ithread.h"
#include "ssdplib.h"
#include "statcodes.h"
#include "ThreadPool.h"
#include "unixutil.h" /* for socklen_t, EAFNOSUPPORT */
#include "upnpapi.h"
#include "upnputil.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
/*! . */
#define APPLICATION_LISTENING_PORT 49152
struct mserv_request_t {
/*! Connection handle. */
SOCKET connfd;
/*! . */
struct sockaddr_storage foreign_sockaddr;
};
/*! . */
typedef enum {
/*! . */
MSERV_IDLE,
/*! . */
MSERV_RUNNING,
/*! . */
MSERV_STOPPING
} MiniServerState;
/*! . */
uint16_t miniStopSockPort;
/*!
* module vars
*/
static MiniServerState gMServState = MSERV_IDLE;
#ifdef INTERNAL_WEB_SERVER
static MiniServerCallback gGetCallback = NULL;
static MiniServerCallback gSoapCallback = NULL;
static MiniServerCallback gGenaCallback = NULL;
void SetHTTPGetCallback(MiniServerCallback callback)
{
gGetCallback = callback;
}
#ifdef INCLUDE_DEVICE_APIS
void SetSoapCallback(MiniServerCallback callback)
{
gSoapCallback = callback;
}
#endif /* INCLUDE_DEVICE_APIS */
void SetGenaCallback(MiniServerCallback callback)
{
gGenaCallback = callback;
}
/*!
* \brief Based on the type pf message, appropriate callback is issued.
*
* \return 0 on Success or HTTP_INTERNAL_SERVER_ERROR if Callback is NULL.
*/
static int dispatch_request(
/*! [in] Socket Information object. */
IN SOCKINFO *info,
/*! [in] HTTP parser object. */
http_parser_t *hparser)
{
MiniServerCallback callback;
switch (hparser->msg.method) {
/* Soap Call */
case SOAPMETHOD_POST:
case HTTPMETHOD_MPOST:
callback = gSoapCallback;
break;
/* Gena Call */
case HTTPMETHOD_NOTIFY:
case HTTPMETHOD_SUBSCRIBE:
case HTTPMETHOD_UNSUBSCRIBE:
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"miniserver %d: got GENA msg\n", info->socket);
callback = gGenaCallback;
break;
/* HTTP server call */
case HTTPMETHOD_GET:
case HTTPMETHOD_POST:
case HTTPMETHOD_HEAD:
case HTTPMETHOD_SIMPLEGET:
callback = gGetCallback;
break;
default:
callback = NULL;
}
if (callback == NULL) {
return HTTP_INTERNAL_SERVER_ERROR;
}
callback(hparser, &hparser->msg, info);
return 0;
}
/*!
* \brief Send Error Message.
*/
static UPNP_INLINE void handle_error(
/*! [in] Socket Information object. */
SOCKINFO *info,
/*! [in] HTTP Error Code. */
int http_error_code,
/*! [in] Major Version Number. */
int major,
/*! [in] Minor Version Number. */
int minor)
{
http_SendStatusResponse(info, http_error_code, major, minor);
}
/*!
* \brief Free memory assigned for handling request and unitialize socket
* functionality.
*/
static void free_handle_request_arg(
/*! [in] Request Message to be freed. */
void *args)
{
struct mserv_request_t *request = (struct mserv_request_t *)args;
sock_close(request->connfd);
free(request);
}
/*!
* \brief Receive the request and dispatch it for handling.
*/
static void handle_request(
/*! [in] Request Message to be handled. */
void *args)
{
SOCKINFO info;
int http_error_code;
int ret_code;
int major = 1;
int minor = 1;
http_parser_t parser;
http_message_t *hmsg = NULL;
int timeout = HTTP_DEFAULT_TIMEOUT;
struct mserv_request_t *request = (struct mserv_request_t *)args;
SOCKET connfd = request->connfd;
UpnpPrintf( UPNP_INFO, MSERV, __FILE__, __LINE__,
"miniserver %d: READING\n", connfd );
/* parser_request_init( &parser ); */ /* LEAK_FIX_MK */
hmsg = &parser.msg;
ret_code = sock_init_with_ip(
&info, connfd, (struct sockaddr *)&request->foreign_sockaddr);
if (ret_code != UPNP_E_SUCCESS) {
free(request);
httpmsg_destroy(hmsg);
return;
}
/* read */
ret_code = http_RecvMessage(
&info, &parser, HTTPMETHOD_UNKNOWN, &timeout, &http_error_code);
if (ret_code != 0) {
goto error_handler;
}
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"miniserver %d: PROCESSING...\n", connfd);
/* dispatch */
http_error_code = dispatch_request(&info, &parser);
if (http_error_code != 0) {
goto error_handler;
}
http_error_code = 0;
error_handler:
if (http_error_code > 0) {
if (hmsg) {
major = hmsg->major_version;
minor = hmsg->minor_version;
}
handle_error(&info, http_error_code, major, minor);
}
sock_destroy(&info, SD_BOTH);
httpmsg_destroy(hmsg);
free(request);
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"miniserver %d: COMPLETE\n", connfd);
}
/*!
* \brief Initilize the thread pool to handle a request, sets priority for the
* job and adds the job to the thread pool.
*/
static UPNP_INLINE void schedule_request_job(
/*! [in] Socket Descriptor on which connection is accepted. */
SOCKET connfd,
/*! [in] Clients Address information. */
struct sockaddr *clientAddr)
{
struct mserv_request_t *request;
ThreadPoolJob job;
memset(&job, 0, sizeof(job));
request = (struct mserv_request_t *)malloc(
sizeof (struct mserv_request_t));
if (request == NULL) {
UpnpPrintf( UPNP_INFO, MSERV, __FILE__, __LINE__,
"mserv %d: out of memory\n", connfd);
sock_close(connfd);
return;
}
request->connfd = connfd;
memcpy(&request->foreign_sockaddr, clientAddr,
sizeof(request->foreign_sockaddr));
TPJobInit(&job, (start_routine)handle_request, (void *)request);
TPJobSetFreeFunction(&job, free_handle_request_arg);
TPJobSetPriority(&job, MED_PRIORITY);
if (ThreadPoolAdd(&gMiniServerThreadPool, &job, NULL) != 0) {
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"mserv %d: cannot schedule request\n", connfd);
free(request);
sock_close(connfd);
return;
}
}
#endif
static UPNP_INLINE void fdset_if_valid(SOCKET sock, fd_set *set)
{
if (sock != INVALID_SOCKET) {
FD_SET(sock, set);
}
}
static void web_server_accept(SOCKET lsock, fd_set *set)
{
#ifdef INTERNAL_WEB_SERVER
SOCKET asock;
socklen_t clientLen;
struct sockaddr_storage clientAddr;
char errorBuffer[ERROR_BUFFER_LEN];
if (lsock != INVALID_SOCKET && FD_ISSET(lsock, set)) {
clientLen = sizeof(clientAddr);
asock = accept(lsock, (struct sockaddr *)&clientAddr,
&clientLen);
if (asock == INVALID_SOCKET) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"miniserver: Error in accept(): %s\n",
errorBuffer);
} else {
schedule_request_job(asock,
(struct sockaddr *)&clientAddr);
}
}
#endif /* INTERNAL_WEB_SERVER */
}
static void ssdp_read(SOCKET rsock, fd_set *set)
{
if (rsock != INVALID_SOCKET && FD_ISSET(rsock, set)) {
readFromSSDPSocket(rsock);
}
}
static int receive_from_stopSock(SOCKET ssock, fd_set *set)
{
ssize_t byteReceived;
socklen_t clientLen;
struct sockaddr_storage clientAddr;
char requestBuf[256];
char buf_ntop[INET6_ADDRSTRLEN];
if (FD_ISSET(ssock, set)) {
clientLen = sizeof(clientAddr);
memset((char *)&clientAddr, 0, sizeof(clientAddr));
byteReceived = recvfrom(ssock, requestBuf,
(size_t)25, 0, (struct sockaddr *)&clientAddr, &clientLen);
if (byteReceived > 0) {
requestBuf[byteReceived] = '\0';
inet_ntop(AF_INET,
&((struct sockaddr_in*)&clientAddr)->sin_addr,
buf_ntop, sizeof(buf_ntop));
UpnpPrintf( UPNP_INFO, MSERV, __FILE__, __LINE__,
"Received response: %s From host %s \n",
requestBuf, buf_ntop );
UpnpPrintf( UPNP_PACKET, MSERV, __FILE__, __LINE__,
"Received multicast packet: \n %s\n",
requestBuf);
if (NULL != strstr(requestBuf, "ShutDown")) {
return 1;
}
}
}
return 0;
}
/*!
* \brief Run the miniserver.
*
* The MiniServer accepts a new request and schedules a thread to handle the
* new request. Checks for socket state and invokes appropriate read and
* shutdown actions for the Miniserver and SSDP sockets.
*/
static void RunMiniServer(
/*! [in] Socket Array. */
MiniServerSockArray *miniSock)
{
char errorBuffer[ERROR_BUFFER_LEN];
fd_set expSet;
fd_set rdSet;
SOCKET maxMiniSock;
int ret = 0;
SOCKET stopSock = 0;
maxMiniSock = 0;
maxMiniSock = max(maxMiniSock, miniSock->miniServerSock4);
maxMiniSock = max(maxMiniSock, miniSock->miniServerSock6);
maxMiniSock = max(maxMiniSock, miniSock->miniServerStopSock);
maxMiniSock = max(maxMiniSock, miniSock->ssdpSock4);
maxMiniSock = max(maxMiniSock, miniSock->ssdpSock6);
maxMiniSock = max(maxMiniSock, miniSock->ssdpSock6UlaGua);
#ifdef INCLUDE_CLIENT_APIS
maxMiniSock = max(maxMiniSock, miniSock->ssdpReqSock4);
maxMiniSock = max(maxMiniSock, miniSock->ssdpReqSock6);
#endif /* INCLUDE_CLIENT_APIS */
++maxMiniSock;
gMServState = MSERV_RUNNING;
while (!stopSock) {
FD_ZERO(&rdSet);
FD_ZERO(&expSet);
/* FD_SET()'s */
FD_SET(miniSock->miniServerStopSock, &expSet);
FD_SET(miniSock->miniServerStopSock, &rdSet);
fdset_if_valid(miniSock->miniServerSock4, &rdSet);
fdset_if_valid(miniSock->miniServerSock6, &rdSet);
fdset_if_valid(miniSock->ssdpSock4, &rdSet);
fdset_if_valid(miniSock->ssdpSock6, &rdSet);
fdset_if_valid(miniSock->ssdpSock6UlaGua, &rdSet);
#ifdef INCLUDE_CLIENT_APIS
fdset_if_valid(miniSock->ssdpReqSock4, &rdSet);
fdset_if_valid(miniSock->ssdpReqSock6, &rdSet);
#endif /* INCLUDE_CLIENT_APIS */
/* select() */
ret = select((int) maxMiniSock, &rdSet, NULL, &expSet, NULL);
if (ret == SOCKET_ERROR && errno == EINTR) {
continue;
}
if (ret == SOCKET_ERROR) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__,
"Error in select(): %s\n", errorBuffer);
continue;
} else {
web_server_accept(miniSock->miniServerSock4, &rdSet);
web_server_accept(miniSock->miniServerSock6, &rdSet);
#ifdef INCLUDE_CLIENT_APIS
ssdp_read(miniSock->ssdpReqSock4, &rdSet);
ssdp_read(miniSock->ssdpReqSock6, &rdSet);
#endif /* INCLUDE_CLIENT_APIS */
ssdp_read(miniSock->ssdpSock4, &rdSet);
ssdp_read(miniSock->ssdpSock6, &rdSet);
ssdp_read(miniSock->ssdpSock6UlaGua, &rdSet);
stopSock = receive_from_stopSock(
miniSock->miniServerStopSock, &rdSet);
}
}
/* Close all sockets. */
sock_close(miniSock->miniServerSock4);
sock_close(miniSock->miniServerSock6);
sock_close(miniSock->miniServerStopSock);
sock_close(miniSock->ssdpSock4);
sock_close(miniSock->ssdpSock6);
sock_close(miniSock->ssdpSock6UlaGua);
#ifdef INCLUDE_CLIENT_APIS
sock_close(miniSock->ssdpReqSock4);
sock_close(miniSock->ssdpReqSock6);
#endif /* INCLUDE_CLIENT_APIS */
/* Free minisock. */
free(miniSock);
gMServState = MSERV_IDLE;
return;
}
/*!
* \brief Returns port to which socket, sockfd, is bound.
*
* \return -1 on error; check errno. 0 if successfull.
*/
static int get_port(
/*! [in] Socket descriptor. */
SOCKET sockfd,
/*! [out] The port value if successful, otherwise, untouched. */
uint16_t *port)
{
struct sockaddr_storage sockinfo;
socklen_t len;
int code;
len = sizeof(sockinfo);
code = getsockname(sockfd, (struct sockaddr *)&sockinfo, &len);
if (code == -1) {
return -1;
}
if (sockinfo.ss_family == AF_INET) {
*port = ntohs(((struct sockaddr_in*)&sockinfo)->sin_port);
} else if(sockinfo.ss_family == AF_INET6) {
*port = ntohs(((struct sockaddr_in6*)&sockinfo)->sin6_port);
}
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"sockfd = %d, .... port = %d\n", sockfd, (int)*port);
return 0;
}
#ifdef INTERNAL_WEB_SERVER
/*!
* \brief Creates a STREAM socket, binds to INADDR_ANY and listens for
* incoming connecttions. Returns the actual port which the sockets
* sub-system returned.
*
* Also creates a DGRAM socket, binds to the loop back address and
* returns the port allocated by the socket sub-system.
*
* \return
* \li UPNP_E_OUTOF_SOCKET: Failed to create a socket.
* \li UPNP_E_SOCKET_BIND: Bind() failed.
* \li UPNP_E_LISTEN: Listen() failed.
* \li UPNP_E_INTERNAL_ERROR: Port returned by the socket layer is < 0.
* \li UPNP_E_SUCCESS: Success.
*/
static int get_miniserver_sockets(
/*! [in] Socket Array. */
MiniServerSockArray *out,
/*! [in] port on which the server is listening for incoming IPv4
* connections. */
uint16_t listen_port4,
/*! [in] port on which the server is listening for incoming IPv6
* connections. */
uint16_t listen_port6)
{
char errorBuffer[ERROR_BUFFER_LEN];
struct sockaddr_storage __ss_v4;
struct sockaddr_in* serverAddr4 = (struct sockaddr_in*)&__ss_v4;
SOCKET listenfd4;
uint16_t actual_port4 = 0u;
#ifdef UPNP_ENABLE_IPV6
struct sockaddr_storage __ss_v6;
struct sockaddr_in6* serverAddr6 = (struct sockaddr_in6*)&__ss_v6;
SOCKET listenfd6;
uint16_t actual_port6 = 0u;
int onOff;
#endif
int ret_code;
int reuseaddr_on = 0;
int sockError = UPNP_E_SUCCESS;
int errCode = 0;
/* Create listen socket for IPv4/IPv6. An error here may indicate
* that we don't have an IPv4/IPv6 stack. */
listenfd4 = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd4 == INVALID_SOCKET) {
return UPNP_E_OUTOF_SOCKET;
}
#ifdef UPNP_ENABLE_IPV6
listenfd6 = socket(AF_INET6, SOCK_STREAM, 0);
if (listenfd6 == INVALID_SOCKET) {
sock_close(listenfd4);
return UPNP_E_OUTOF_SOCKET;
}
onOff = 1;
sockError = setsockopt(listenfd6, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&onOff, sizeof(onOff));
if (sockError == SOCKET_ERROR) {
sock_close(listenfd4);
sock_close(listenfd6);
return UPNP_E_SOCKET_BIND;
}
#endif
/* As per the IANA specifications for the use of ports by applications
* override the listen port passed in with the first available. */
if (listen_port4 < APPLICATION_LISTENING_PORT) {
listen_port4 = (uint16_t)APPLICATION_LISTENING_PORT;
}
#ifdef UPNP_ENABLE_IPV6
if (listen_port6 < APPLICATION_LISTENING_PORT) {
listen_port6 = (uint16_t)APPLICATION_LISTENING_PORT;
}
#endif
memset(&__ss_v4, 0, sizeof (__ss_v4));
serverAddr4->sin_family = (sa_family_t)AF_INET;
serverAddr4->sin_addr.s_addr = htonl(INADDR_ANY);
#ifdef UPNP_ENABLE_IPV6
memset(&__ss_v6, 0, sizeof (__ss_v6));
serverAddr6->sin6_family = (sa_family_t)AF_INET6;
serverAddr6->sin6_addr = in6addr_any;
#endif
/* Getting away with implementation of re-using address:port and
* instead choosing to increment port numbers.
* Keeping the re-use address code as an optional behaviour that
* can be turned on if necessary.
* TURN ON the reuseaddr_on option to use the option. */
if (reuseaddr_on) {
/* THIS IS ALLOWS US TO BIND AGAIN IMMEDIATELY
* AFTER OUR SERVER HAS BEEN CLOSED
* THIS MAY CAUSE TCP TO BECOME LESS RELIABLE
* HOWEVER IT HAS BEEN SUGESTED FOR TCP SERVERS. */
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"get_miniserver_sockets: resuseaddr is set.\n");
if (listenfd4 != INVALID_SOCKET) {
sockError = setsockopt(listenfd4, SOL_SOCKET,
SO_REUSEADDR,
(const char *)&reuseaddr_on, sizeof (int));
if (sockError == SOCKET_ERROR) {
sock_close(listenfd4);
#ifdef UPNP_ENABLE_IPV6
sock_close(listenfd6);
#endif
return UPNP_E_SOCKET_BIND;
}
serverAddr4->sin_port = htons(listen_port4);
sockError = bind(listenfd4,
(struct sockaddr *)&__ss_v4,
sizeof (__ss_v4));
if (sockError == SOCKET_ERROR) {
strerror_r(errno, errorBuffer,
ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV,
__FILE__, __LINE__,
"get_miniserver_sockets: "
"Error in IPv4 bind(): %s\n",
errorBuffer);
sock_close(listenfd4);
#ifdef UPNP_ENABLE_IPV6
sock_close(listenfd6);
#endif
/* Bind failed */
return UPNP_E_SOCKET_BIND;
}
}
#ifdef UPNP_ENABLE_IPV6
if (listenfd6 != INVALID_SOCKET) {
sockError = setsockopt(listenfd6, SOL_SOCKET,
SO_REUSEADDR,
(const char *)&reuseaddr_on, sizeof (int));
if (sockError == SOCKET_ERROR) {
sock_close(listenfd4);
sock_close(listenfd6);
return UPNP_E_SOCKET_BIND;
}
serverAddr6->sin6_port = htons(listen_port6);
sockError = bind(listenfd6,
(struct sockaddr *)&__ss_v6,
sizeof (__ss_v6));
if (sockError == SOCKET_ERROR) {
strerror_r(errno, errorBuffer,
ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV,
__FILE__, __LINE__,
"get_miniserver_sockets: "
"Error in IPv6 bind(): %s\n",
errorBuffer);
sock_close(listenfd4);
sock_close(listenfd6);
/* Bind failed */
return UPNP_E_SOCKET_BIND;
}
}
#endif /* IPv6 */
} else {
if (listenfd4 != INVALID_SOCKET) {
uint16_t orig_listen_port4 = listen_port4;
do {
serverAddr4->sin_port = htons(listen_port4++);
sockError = bind(listenfd4,
(struct sockaddr *)serverAddr4,
sizeof(*serverAddr4));
if (sockError == SOCKET_ERROR) {
#ifdef WIN32
errCode = WSAGetLastError();
#else
errCode = errno;
#endif
if (errno == EADDRINUSE) {
errCode = 1;
}
} else {
errCode = 0;
}
} while (errCode != 0 &&
listen_port4 >= orig_listen_port4);
if (sockError == SOCKET_ERROR) {
strerror_r(errno, errorBuffer,
ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV,
__FILE__, __LINE__,
"get_miniserver_sockets: "
"Error in IPv4 bind(): %s\n",
errorBuffer);
sock_close(listenfd4);
#ifdef UPNP_ENABLE_IPV6
sock_close(listenfd6);
#endif
/* Bind failied. */
return UPNP_E_SOCKET_BIND;
}
}
#ifdef UPNP_ENABLE_IPV6
if (listenfd6 != INVALID_SOCKET) {
uint16_t orig_listen_port6 = listen_port6;
do {
serverAddr6->sin6_port = htons(listen_port6++);
sockError = bind(listenfd6,
(struct sockaddr *)serverAddr6,
sizeof(*serverAddr6));
if (sockError == SOCKET_ERROR) {
#ifdef WIN32
errCode = WSAGetLastError();
#else
errCode = errno;
#endif
if (errno == EADDRINUSE) {
errCode = 1;
}
} else {
errCode = 0;
}
} while (errCode != 0 &&
listen_port6 >= orig_listen_port6);
if (sockError == SOCKET_ERROR) {
strerror_r(errno, errorBuffer,
ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV,
__FILE__, __LINE__,
"get_miniserver_sockets: "
"Error in IPv6 bind(): %s\n",
errorBuffer);
sock_close(listenfd4);
sock_close(listenfd6);
/* Bind failied. */
return UPNP_E_SOCKET_BIND;
}
}
#endif
}
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"get_miniserver_sockets: bind successful\n");
if (listenfd4 != INVALID_SOCKET) {
ret_code = listen(listenfd4, SOMAXCONN);
if (ret_code == SOCKET_ERROR) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"mserv start: Error in IPv4 listen(): %s\n",
errorBuffer);
sock_close(listenfd4);
#ifdef UPNP_ENABLE_IPV6
sock_close(listenfd6);
#endif
return UPNP_E_LISTEN;
}
ret_code = get_port(listenfd4, &actual_port4);
if (ret_code < 0) {
sock_close(listenfd4);
#ifdef UPNP_ENABLE_IPV6
sock_close(listenfd6);
#endif
return UPNP_E_INTERNAL_ERROR;
}
out->miniServerPort4 = actual_port4;
}
#ifdef UPNP_ENABLE_IPV6
if (listenfd6 != INVALID_SOCKET) {
ret_code = listen(listenfd6, SOMAXCONN);
if (ret_code == SOCKET_ERROR) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, MSERV, __FILE__, __LINE__,
"mserv start: Error in IPv6 listen(): %s\n",
errorBuffer);
sock_close(listenfd4);
sock_close(listenfd6);
return UPNP_E_LISTEN;
}
ret_code = get_port(listenfd6, &actual_port6);
if (ret_code < 0) {
sock_close(listenfd4);
sock_close(listenfd6);
return UPNP_E_INTERNAL_ERROR;
}
out->miniServerPort6 = actual_port6;
}
#endif
out->miniServerSock4 = listenfd4;
#ifdef UPNP_ENABLE_IPV6
out->miniServerSock6 = listenfd6;
#else
/* Silence compiler warning message:
* warning: unused parameter ‘listen_port6’ */
listen_port6 = 0u;
#endif
return UPNP_E_SUCCESS;
}
#endif /* INTERNAL_WEB_SERVER */
/*!
* \brief Creates the miniserver STOP socket. This socket is created and
* listened on to know when it is time to stop the Miniserver.
*
* \return
* \li \c UPNP_E_OUTOF_SOCKET: Failed to create a socket.
* \li \c UPNP_E_SOCKET_BIND: Bind() failed.
* \li \c UPNP_E_INTERNAL_ERROR: Port returned by the socket layer is < 0.
* \li \c UPNP_E_SUCCESS: Success.
*/
static int get_miniserver_stopsock(
/*! [in] Miniserver Socket Array. */
MiniServerSockArray *out)
{
char errorBuffer[ERROR_BUFFER_LEN];
struct sockaddr_in stop_sockaddr;
SOCKET miniServerStopSock = 0;
int ret = 0;
miniServerStopSock = socket(AF_INET, SOCK_DGRAM, 0);
if (miniServerStopSock == INVALID_SOCKET) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_CRITICAL, MSERV, __FILE__, __LINE__,
"Error in socket(): %s\n", errorBuffer);
return UPNP_E_OUTOF_SOCKET;
}
/* Bind to local socket. */
memset(&stop_sockaddr, 0, sizeof (stop_sockaddr));
stop_sockaddr.sin_family = (sa_family_t)AF_INET;
stop_sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = bind(miniServerStopSock, (struct sockaddr *)&stop_sockaddr,
sizeof(stop_sockaddr));
if (ret == SOCKET_ERROR) {
UpnpPrintf(UPNP_CRITICAL,
MSERV, __FILE__, __LINE__,
"Error in binding localhost!!!\n");
sock_close(miniServerStopSock);
return UPNP_E_SOCKET_BIND;
}
ret = get_port(miniServerStopSock, &miniStopSockPort);
if (ret < 0) {
sock_close(miniServerStopSock);
return UPNP_E_INTERNAL_ERROR;
}
out->miniServerStopSock = miniServerStopSock;
out->stopPort = miniStopSockPort;
return UPNP_E_SUCCESS;
}
static UPNP_INLINE void InitMiniServerSockArray(MiniServerSockArray *miniSocket)
{
miniSocket->miniServerSock4 = INVALID_SOCKET;
miniSocket->miniServerSock6 = INVALID_SOCKET;
miniSocket->miniServerStopSock = INVALID_SOCKET;
miniSocket->ssdpSock4 = INVALID_SOCKET;
miniSocket->ssdpSock6 = INVALID_SOCKET;
miniSocket->ssdpSock6UlaGua = INVALID_SOCKET;
miniSocket->stopPort = 0u;
miniSocket->miniServerPort4 = 0u;
miniSocket->miniServerPort6 = 0u;
#ifdef INCLUDE_CLIENT_APIS
miniSocket->ssdpReqSock4 = INVALID_SOCKET;
miniSocket->ssdpReqSock6 = INVALID_SOCKET;
#endif /* INCLUDE_CLIENT_APIS */
}
int StartMiniServer(
/*! [in,out] Port on which the server listens for incoming IPv4
* connections. */
uint16_t *listen_port4,
/*! [in,out] Port on which the server listens for incoming IPv6
* connections. */
uint16_t *listen_port6)
{
int ret_code;
int count;
int max_count = 10000;
MiniServerSockArray *miniSocket;
ThreadPoolJob job;
memset(&job, 0, sizeof(job));
switch (gMServState) {
case MSERV_IDLE:
break;
default:
/* miniserver running. */
return UPNP_E_INTERNAL_ERROR;
}
miniSocket = (MiniServerSockArray *)malloc(
sizeof (MiniServerSockArray));
if (!miniSocket) {
return UPNP_E_OUTOF_MEMORY;
}
InitMiniServerSockArray(miniSocket);
#ifdef INTERNAL_WEB_SERVER
/* V4 and V6 http listeners. */
ret_code = get_miniserver_sockets(
miniSocket, *listen_port4, *listen_port6);
if (ret_code != UPNP_E_SUCCESS) {
free(miniSocket);
return ret_code;
}
#endif
/* Stop socket (To end miniserver processing). */
ret_code = get_miniserver_stopsock(miniSocket);
if (ret_code != UPNP_E_SUCCESS) {
sock_close(miniSocket->miniServerSock4);
sock_close(miniSocket->miniServerSock6);
free(miniSocket);
return ret_code;
}
/* SSDP socket for discovery/advertising. */
ret_code = get_ssdp_sockets(miniSocket);
if (ret_code != UPNP_E_SUCCESS) {
sock_close(miniSocket->miniServerSock4);
sock_close(miniSocket->miniServerSock6);
sock_close(miniSocket->miniServerStopSock);
free(miniSocket);
return ret_code;
}
TPJobInit(&job, (start_routine)RunMiniServer, (void *)miniSocket);
TPJobSetPriority(&job, MED_PRIORITY);
TPJobSetFreeFunction(&job, (free_routine)free);
ret_code = ThreadPoolAddPersistent(&gMiniServerThreadPool, &job, NULL);
if (ret_code < 0) {
sock_close(miniSocket->miniServerSock4);
sock_close(miniSocket->miniServerSock6);
sock_close(miniSocket->miniServerStopSock);
sock_close(miniSocket->ssdpSock4);
sock_close(miniSocket->ssdpSock6);
sock_close(miniSocket->ssdpSock6UlaGua);
#ifdef INCLUDE_CLIENT_APIS
sock_close(miniSocket->ssdpReqSock4);
sock_close(miniSocket->ssdpReqSock6);
#endif /* INCLUDE_CLIENT_APIS */
return UPNP_E_OUTOF_MEMORY;
}
/* Wait for miniserver to start. */
count = 0;
while (gMServState != (MiniServerState)MSERV_RUNNING && count < max_count) {
/* 0.05s */
usleep(50u * 1000u);
count++;
}
if (count >= max_count) {
/* Took it too long to start that thread. */
sock_close(miniSocket->miniServerSock4);
sock_close(miniSocket->miniServerSock6);
sock_close(miniSocket->miniServerStopSock);
sock_close(miniSocket->ssdpSock4);
sock_close(miniSocket->ssdpSock6);
sock_close(miniSocket->ssdpSock6UlaGua);
#ifdef INCLUDE_CLIENT_APIS
sock_close(miniSocket->ssdpReqSock4);
sock_close(miniSocket->ssdpReqSock6);
#endif /* INCLUDE_CLIENT_APIS */
return UPNP_E_INTERNAL_ERROR;
}
#ifdef INTERNAL_WEB_SERVER
*listen_port4 = miniSocket->miniServerPort4;
*listen_port6 = miniSocket->miniServerPort6;
#endif
return UPNP_E_SUCCESS;
}
int StopMiniServer()
{
char errorBuffer[ERROR_BUFFER_LEN];
socklen_t socklen = sizeof (struct sockaddr_in);
SOCKET sock;
struct sockaddr_in ssdpAddr;
char buf[256] = "ShutDown";
size_t bufLen = strlen(buf);
switch(gMServState) {
case MSERV_RUNNING:
gMServState = MSERV_STOPPING;
break;
default:
return 0;
}
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == INVALID_SOCKET) {
strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN);
UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__,
"SSDP_SERVER: StopSSDPServer: Error in socket() %s\n",
errorBuffer);
return 0;
}
while(gMServState != (MiniServerState)MSERV_IDLE) {
ssdpAddr.sin_family = (sa_family_t)AF_INET;
ssdpAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ssdpAddr.sin_port = htons(miniStopSockPort);
sendto(sock, buf, bufLen, 0,
(struct sockaddr *)&ssdpAddr, socklen);
usleep(1000u);
if (gMServState == (MiniServerState)MSERV_IDLE) {
break;
}
isleep(1u);
}
sock_close(sock);
return 0;
}
#endif /* EXCLUDE_MINISERVER */
|