diff --git a/ChangeLog b/ChangeLog index b0b1bdf..7daf4e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -318,6 +318,17 @@ Version 1.8.0 Version 1.6.16 ******************************************************************************* +2012-03-16 Fabrice Fontaine + + Add more explicit casts and remove dead code + + Comment unused SERVER from DeviceShutdown. + Comment unused max from parse_hostport. + Comment unused nodeptr from ixmlNode_cloneDoc. + Comment unused newNode from Parser_hasDefaultNamespace. + Comment unused Parser_parseReference function + Check return code of shutdown and display an error if needed. + 2012-03-15 Fabrice Fontaine Fix previous commit diff --git a/ixml/src/ixmlparser.c b/ixml/src/ixmlparser.c index 0006a9d..8f4d981 100644 --- a/ixml/src/ixmlparser.c +++ b/ixml/src/ixmlparser.c @@ -1096,7 +1096,7 @@ static char *safe_strdup( assert(s != NULL); if (s == NULL) { - return strdup(""); + return strdup((const char*)""); } return strdup(s); } @@ -1215,7 +1215,7 @@ static int Parser_processCDSect( IXML_Node *node) { char *pEnd; - size_t tokenLength = 0; + size_t tokenLength = (size_t)0; char *pCDataStart; if( *pSrc == NULL ) { @@ -1451,6 +1451,7 @@ ExitFunction: * * \return IXML_SUCCESS. */ +#if 0 static int Parser_parseReference( /*! [in] Currently unused. */ char *pStr) @@ -1459,6 +1460,7 @@ static int Parser_parseReference( return IXML_SUCCESS; pStr = pStr; } +#endif /*! @@ -1746,9 +1748,9 @@ static int Parser_processAttribute( line = __LINE__; goto ExitFunction; } - if (*pCur == '&') { + /*if (*pCur == '&') { Parser_parseReference(++pCur); - } + }*/ pCur++; } /* clear token buffer */ @@ -1861,7 +1863,7 @@ static int Parser_getNextNode( line = __LINE__; ret = IXML_SUCCESS; goto ExitFunction; - } else if ((xmlParser->tokenBuf).length == 0) { + } else if ((xmlParser->tokenBuf).length == (size_t)0) { line = __LINE__; ret = IXML_SYNTAX_ERR; goto ExitFunction; @@ -1901,8 +1903,16 @@ static int Parser_getNextNode( line = __LINE__; ret = IXML_SUCCESS; goto ExitFunction; - } else if (xmlParser->state == eATTRIBUTE && xmlParser->pCurElement != NULL) { - if (Parser_processAttribute(xmlParser, node) != IXML_SUCCESS) { + } else if (xmlParser->pCurElement != NULL) { + switch (xmlParser->state) { + case eATTRIBUTE: + if (Parser_processAttribute(xmlParser, node) != IXML_SUCCESS) { + line = __LINE__; + ret = IXML_SYNTAX_ERR; + goto ExitFunction; + } + break; + default: line = __LINE__; ret = IXML_SYNTAX_ERR; goto ExitFunction; @@ -2139,7 +2149,7 @@ static BOOL Parser_hasDefaultNamespace( /*! [in] The XML parser. */ Parser *xmlParser, /*! [in] The Node to process. */ - IXML_Node *newNode, + /*IXML_Node *newNode,*/ /*! [in,out] The name space URI. */ char **nsURI ) { @@ -2155,7 +2165,7 @@ static BOOL Parser_hasDefaultNamespace( } return FALSE; - newNode = newNode; + //newNode = newNode; } @@ -2208,7 +2218,7 @@ static int Parser_processElementName( } else { /* does element has default namespace */ /* the node may have default namespace definition */ - if (Parser_hasDefaultNamespace(xmlParser, newNode, &nsURI)) { + if (Parser_hasDefaultNamespace(xmlParser, /*newNode,*/ &nsURI)) { Parser_setElementNamespace(newElement, nsURI); } else { switch (xmlParser->state) { @@ -2528,7 +2538,7 @@ static int Parser_readFileOrBuffer( BOOL file) { long fileSize = 0; - size_t bytesRead = 0; + size_t bytesRead = (size_t)0; FILE *xmlFilePtr = NULL; if( file ) { @@ -2543,7 +2553,7 @@ static int Parser_readFileOrBuffer( return IXML_SYNTAX_ERR; } - xmlParser->dataBuffer = (char *)malloc((size_t)fileSize + 1); + xmlParser->dataBuffer = (char *)malloc((size_t)fileSize + (size_t)1); if( xmlParser->dataBuffer == NULL ) { fclose( xmlFilePtr ); return IXML_INSUFFICIENT_MEMORY; diff --git a/ixml/src/namedNodeMap.c b/ixml/src/namedNodeMap.c index ad8071a..9a56a78 100644 --- a/ixml/src/namedNodeMap.c +++ b/ixml/src/namedNodeMap.c @@ -128,7 +128,7 @@ IXML_Node *ixmlNamedNodeMap_item( unsigned long ixmlNamedNodeMap_getLength(IXML_NamedNodeMap *nnMap) { IXML_Node *tempNode; - unsigned long length = 0; + unsigned long length = 0lu; if (nnMap != NULL) { tempNode = nnMap->nodeItem; diff --git a/ixml/src/node.c b/ixml/src/node.c index 12e3ea9..b267185 100644 --- a/ixml/src/node.c +++ b/ixml/src/node.c @@ -776,13 +776,13 @@ static IXML_Element *ixmlNode_cloneElement( */ static IXML_Document *ixmlNode_cloneDoc( /*! [in] The \b Node to clone. */ - IXML_Document *nodeptr) + /*IXML_Document *nodeptr*/) { IXML_Document *newDoc; IXML_Node *docNode; int rc; - assert(nodeptr != NULL); + //assert(nodeptr != NULL); newDoc = (IXML_Document *)malloc(sizeof (IXML_Document)); if (newDoc == NULL) { @@ -801,7 +801,7 @@ static IXML_Document *ixmlNode_cloneDoc( newDoc->n.nodeType = eDOCUMENT_NODE; return newDoc; - nodeptr = nodeptr; + //nodeptr = nodeptr; } /*! @@ -968,7 +968,7 @@ static IXML_Node *ixmlNode_cloneNodeTreeRecursive( break; case eDOCUMENT_NODE: - newDoc = ixmlNode_cloneDoc((IXML_Document *)nodeptr); + newDoc = ixmlNode_cloneDoc(/*(IXML_Document *)nodeptr*/); if (newDoc == NULL) return NULL; newNode = (IXML_Node *)newDoc; diff --git a/threadutil/src/ThreadPool.c b/threadutil/src/ThreadPool.c index cc03063..e18d6ae 100644 --- a/threadutil/src/ThreadPool.c +++ b/threadutil/src/ThreadPool.c @@ -83,18 +83,18 @@ static void StatsInit( /*! Must be valid non null stats structure. */ ThreadPoolStats *stats) { - stats->totalIdleTime = 0; + stats->totalIdleTime = 0.0; stats->totalJobsHQ = 0; stats->totalJobsLQ = 0; stats->totalJobsMQ = 0; - stats->totalTimeHQ = 0; - stats->totalTimeMQ = 0; - stats->totalTimeLQ = 0; - stats->totalWorkTime = 0; - stats->totalIdleTime = 0; - stats->avgWaitHQ = 0; - stats->avgWaitMQ = 0; - stats->avgWaitLQ = 0; + stats->totalTimeHQ = 0.0; + stats->totalTimeMQ = 0.0; + stats->totalTimeLQ = 0.0; + stats->totalWorkTime = 0.0; + stats->totalIdleTime = 0.0; + stats->avgWaitHQ = 0.0; + stats->avgWaitMQ = 0.0; + stats->avgWaitLQ = 0.0; stats->workerThreads = 0; stats->idleThreads = 0; stats->persistentThreads = 0; @@ -1205,7 +1205,7 @@ int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats) if (stats->totalJobsHQ > 0) stats->avgWaitHQ = stats->totalTimeHQ / (double)stats->totalJobsHQ; else - stats->avgWaitHQ = 0; + stats->avgWaitHQ = 0.0; if (stats->totalJobsMQ > 0) stats->avgWaitMQ = stats->totalTimeMQ / (double)stats->totalJobsMQ; else diff --git a/upnp/src/genlib/net/http/httpparser.c b/upnp/src/genlib/net/http/httpparser.c index 8a2681e..ff23b55 100644 --- a/upnp/src/genlib/net/http/httpparser.c +++ b/upnp/src/genlib/net/http/httpparser.c @@ -251,7 +251,7 @@ static parse_status_t scanner_get_token( /* possibly more valid chars */ return PARSE_INCOMPLETE; /* calc token length */ - token->length = (size_t)(cursor - token->buf); + token->length = (size_t)cursor - (size_t)token->buf; } else if (c == ' ' || c == '\t') { token->buf = cursor++; token_type = TT_WHITESPACE; @@ -260,7 +260,7 @@ static parse_status_t scanner_get_token( if (!scanner->entire_msg_loaded && cursor == null_terminator) /* possibly more chars */ return PARSE_INCOMPLETE; - token->length = (size_t)(cursor - token->buf); + token->length = (size_t)cursor - (size_t)token->buf; } else if (c == TOKCHAR_CR) { /* scan CRLF */ token->buf = cursor++; @@ -306,7 +306,7 @@ static parse_status_t scanner_get_token( return PARSE_FAILURE; } if (got_end_quote) - token->length = (size_t)(cursor - token->buf); + token->length = (size_t)cursor - (size_t)token->buf; else { /* incomplete */ assert(cursor == null_terminator); @@ -1416,7 +1416,7 @@ parse_status_t parser_parse_responseline(INOUT http_parser_t *parser) while (*p == ' ' || *p == '\t') p++; /* now, p is at start of status msg */ - n = line.length - (size_t)(p - line.buf); + n = line.length - ((size_t)p - (size_t)line.buf); if (membuffer_assign(&hmsg->status_msg, p, n) != 0) { /* out of mem */ parser->http_error_code = HTTP_INTERNAL_SERVER_ERROR; @@ -1737,7 +1737,7 @@ parser_parse_chunky_entity( INOUT http_parser_t * parser ) ( scanner->cursor - save_pos ) ); scanner->cursor = save_pos; /* adjust scanner too */ - if( parser->chunk_size == 0 ) { + if( parser->chunk_size == (size_t)0 ) { /* done reading entity; determine length of entity */ parser->msg.entity.length = parser->scanner.cursor - parser->entity_start_position + parser->msg.amount_discarded; diff --git a/upnp/src/genlib/net/http/httpreadwrite.c b/upnp/src/genlib/net/http/httpreadwrite.c index a0da821..4c208b9 100644 --- a/upnp/src/genlib/net/http/httpreadwrite.c +++ b/upnp/src/genlib/net/http/httpreadwrite.c @@ -280,6 +280,7 @@ SOCKET http_Connect( SOCKET connfd; socklen_t sockaddr_len; int ret_connect; + char errorBuffer[ERROR_BUFFER_LEN]; http_FixUrl(destination_url, url); @@ -297,7 +298,11 @@ SOCKET http_Connect( UpnpPrintf(UPNP_CRITICAL, HTTP, __FILE__, __LINE__, "connect error: %d\n", WSAGetLastError()); #endif - shutdown(connfd, SD_BOTH); + if (shutdown(connfd, SD_BOTH) == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, HTTP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } UpnpCloseSocket(connfd); return (SOCKET)(UPNP_E_SOCKET_CONNECT); } @@ -1501,7 +1506,7 @@ int http_MakeMessage(membuffer *buf, int http_major_version, /* mem buffer */ s = (char *)va_arg(argp, char *); UpnpPrintf(UPNP_ALL, HTTP, __FILE__, __LINE__, - "Adding a char Buffer starting with: %c\n", s[0]); + "Adding a char Buffer starting with: %c\n", (int)s[0]); assert(s); length = (size_t) va_arg(argp, size_t); if (membuffer_append(buf, s, length)) diff --git a/upnp/src/genlib/net/sock.c b/upnp/src/genlib/net/sock.c index 00415d0..afea4f8 100644 --- a/upnp/src/genlib/net/sock.c +++ b/upnp/src/genlib/net/sock.c @@ -48,6 +48,9 @@ #include "upnp.h" #include "UpnpStdInt.h" /* for ssize_t */ +#include "upnpdebug.h" +#include "upnputil.h" + #include #include #include /* for F_GETFL, F_SETFL, O_NONBLOCK */ @@ -115,16 +118,21 @@ int sock_ssl_connect(SOCKINFO *info) int sock_destroy(SOCKINFO *info, int ShutdownMethod) { int ret = UPNP_E_SUCCESS; + char errorBuffer[ERROR_BUFFER_LEN]; if (info->socket != INVALID_SOCKET) { #ifdef UPNP_ENABLE_OPEN_SSL - if (info->ssl) { - SSL_shutdown(info->ssl); - SSL_free(info->ssl); - info->ssl = NULL; - } + if (info->ssl) { + SSL_shutdown(info->ssl); + SSL_free(info->ssl); + info->ssl = NULL; + } #endif - shutdown(info->socket, ShutdownMethod); + if (shutdown(info->socket, ShutdownMethod) == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, HTTP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } if (sock_close(info->socket) == -1) { ret = UPNP_E_SOCKET_ERROR; } @@ -163,7 +171,7 @@ static int sock_read_write( time_t start_time = time(NULL); SOCKET sockfd = info->socket; long bytes_sent = 0; - size_t byte_left = 0; + size_t byte_left = (size_t)0; ssize_t num_written; if (*timeoutSecs < 0) @@ -217,7 +225,7 @@ static int sock_read_write( } else { byte_left = bufsize; bytes_sent = 0; - while (byte_left != 0) { + while (byte_left != (size_t)0) { #ifdef UPNP_ENABLE_OPEN_SSL if (info->ssl) { num_written = SSL_write(info->ssl, diff --git a/upnp/src/genlib/net/uri/uri.c b/upnp/src/genlib/net/uri/uri.c index 16d5596..9bb2b6a 100644 --- a/upnp/src/genlib/net/uri/uri.c +++ b/upnp/src/genlib/net/uri/uri.c @@ -315,7 +315,7 @@ int token_cmp(token *in1, token *in2) int parse_hostport( const char *in, - size_t max, + /*size_t max,*/ unsigned short int defaultPort, hostport_type *out) { @@ -447,7 +447,7 @@ int parse_hostport( out->text.buff = in; return (int)hostport_size; - max = max; + //max=max; } /*! @@ -711,7 +711,7 @@ int parse_uri(const char *in, size_t max, uri_type *out) defaultPort = 443; } begin_path = parse_hostport(&in[begin_hostport], - max - begin_hostport, + /*max - begin_hostport,*/ defaultPort, &out->hostport); if (begin_path >= 0) { diff --git a/upnp/src/inc/ssdplib.h b/upnp/src/inc/ssdplib.h index 40e5b28..77e7664 100644 --- a/upnp/src/inc/ssdplib.h +++ b/upnp/src/inc/ssdplib.h @@ -502,7 +502,7 @@ int DeviceShutdown( /* [in] Device UDN. */ char *Udn, /* [in] . */ - char *_Server, + /*char *_Server,*/ /* [in] Location URL. */ char *Location, /* [in] Device duration in sec. */ diff --git a/upnp/src/inc/uri.h b/upnp/src/inc/uri.h index 5018e5e..6f8ee71 100644 --- a/upnp/src/inc/uri.h +++ b/upnp/src/inc/uri.h @@ -287,7 +287,7 @@ int parse_hostport( /*! [in] String of characters representing host and port. */ const char *in, /*! [in] Sets a maximum limit. */ - size_t max, + /*size_t max,*/ /*! [in] The default port if the port is not specified. */ unsigned short int defaultPort, /*! [out] Output parameter where the host and port are represented as diff --git a/upnp/src/ssdp/ssdp_device.c b/upnp/src/ssdp/ssdp_device.c index 9560af3..87580db 100644 --- a/upnp/src/ssdp/ssdp_device.c +++ b/upnp/src/ssdp/ssdp_device.c @@ -119,8 +119,11 @@ void ssdp_handle_device_request(http_message_t *hmsg, struct sockaddr_storage *d HandleLock(); /* device info. */ - if (GetDeviceHandleInfo(dest_addr->ss_family, - &handle, &dev_info) != HND_DEVICE) { + switch (GetDeviceHandleInfo((int)dest_addr->ss_family, + &handle, &dev_info)) { + case HND_DEVICE: + break; + default: HandleUnlock(); /* no info found. */ return; @@ -241,7 +244,12 @@ static int NewRequestHandler( } end_NewRequestHandler: - shutdown(ReplySock, SD_BOTH); + ret = shutdown(ReplySock, SD_BOTH); + if (ret == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } UpnpCloseSocket(ReplySock); return ret; @@ -823,7 +831,7 @@ error_handler: return RetVal; } -int DeviceShutdown(char *DevType, int RootDev, char *Udn, char *_Server, +int DeviceShutdown(char *DevType, int RootDev, char *Udn, /*char *_Server,*/ char *Location, int Duration, int AddressFamily, int PowerState, int SleepPeriod, int RegistrationState) { @@ -903,7 +911,7 @@ error_handler: free(msgs[2]); return ret_code; - _Server = _Server; + //_Server = _Server; } #endif /* EXCLUDE_SSDP */ #endif /* INCLUDE_DEVICE_APIS */ diff --git a/upnp/src/ssdp/ssdp_server.c b/upnp/src/ssdp/ssdp_server.c index 427f231..dfd5125 100644 --- a/upnp/src/ssdp/ssdp_server.c +++ b/upnp/src/ssdp/ssdp_server.c @@ -108,7 +108,7 @@ int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, IXML_Node *tmpNode2 = NULL; IXML_Node *textNode = NULL; const DOMString tmpStr; - char SERVER[200]; + //char SERVER[200]; const DOMString dbgStr; int NumCopy = 0; @@ -127,7 +127,7 @@ int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, } defaultExp = SInfo->MaxAge; /* get server info */ - get_sdk_info(SERVER); + //get_sdk_info(SERVER); /* parse the device list and send advertisements/replies */ while (NumCopy == 0 || (AdFlag && NumCopy < NUM_SSDP_COPY)) { if (NumCopy != 0) @@ -218,7 +218,7 @@ int AdvertiseAndReply(int AdFlag, UpnpDevice_Handle Hnd, } else { /* AdFlag == -1 */ DeviceShutdown(devType, i == 0lu, UDNstr, - SERVER, SInfo->DescURL, + /*SERVER,*/ SInfo->DescURL, Exp, SInfo->DeviceAf, SInfo->PowerState, SInfo->SleepPeriod, @@ -812,10 +812,8 @@ static int create_ssdp_sock_v4( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEADDR: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #if defined(BSD) || defined(__OSX__) || defined(__APPLE__) onOff = 1; @@ -826,10 +824,8 @@ static int create_ssdp_sock_v4( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEPORT: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #endif /* BSD, __OSX__, __APPLE__ */ memset(&__ss, 0, sizeof(__ss)); @@ -842,10 +838,8 @@ static int create_ssdp_sock_v4( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in bind(), addr=0x%08X, port=%d: %s\n", INADDR_ANY, SSDP_PORT, errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_BIND; + ret = UPNP_E_SOCKET_BIND; + goto error_handler; } memset((void *)&ssdpMcastAddr, 0, sizeof(struct ip_mreq)); ssdpMcastAddr.imr_interface.s_addr = inet_addr(gIF_IPV4); @@ -857,10 +851,8 @@ static int create_ssdp_sock_v4( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() IP_ADD_MEMBERSHIP (join multicast group): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } /* Set multicast interface. */ memset((void *)&addr, 0, sizeof(struct in_addr)); @@ -885,13 +877,22 @@ static int create_ssdp_sock_v4( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_BROADCAST (set broadcast): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); + ret = UPNP_E_NETWORK_ERROR; + goto error_handler; + } + ret = UPNP_E_SUCCESS; - return UPNP_E_NETWORK_ERROR; +error_handler: + if (ret != UPNP_E_SUCCESS) { + if (shutdown(*ssdpSock, SD_BOTH) == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } + UpnpCloseSocket(*ssdpSock); } - return UPNP_E_SUCCESS; + return ret; } #ifdef INCLUDE_CLIENT_APIS @@ -953,10 +954,8 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEADDR: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #if defined(BSD) || defined(__OSX__) || defined(__APPLE__) onOff = 1; @@ -967,10 +966,8 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEPORT: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #endif /* BSD, __OSX__, __APPLE__ */ onOff = 1; @@ -981,10 +978,8 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() IPV6_V6ONLY: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } memset(&__ss, 0, sizeof(__ss)); ssdpAddr6->sin6_family = (sa_family_t)AF_INET6; @@ -997,10 +992,8 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in bind(), addr=0x%032lX, port=%d: %s\n", 0lu, SSDP_PORT, errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_BIND; + ret = UPNP_E_SOCKET_BIND; + goto error_handler; } memset((void *)&ssdpMcastAddr, 0, sizeof(ssdpMcastAddr)); ssdpMcastAddr.ipv6mr_interface = gIF_INDEX; @@ -1013,10 +1006,8 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() IPV6_JOIN_GROUP (join multicast group): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } onOff = 1; ret = setsockopt(*ssdpSock, SOL_SOCKET, SO_BROADCAST, @@ -1026,13 +1017,22 @@ static int create_ssdp_sock_v6( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_BROADCAST (set broadcast): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); + ret = UPNP_E_NETWORK_ERROR; + goto error_handler; + } + ret = UPNP_E_SUCCESS; - return UPNP_E_NETWORK_ERROR; +error_handler: + if (ret != UPNP_E_SUCCESS) { + if (shutdown(*ssdpSock, SD_BOTH) == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } + UpnpCloseSocket(*ssdpSock); } - return UPNP_E_SUCCESS; + return ret; } #endif /* IPv6 */ @@ -1067,10 +1067,8 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEADDR: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #if defined(BSD) || defined(__OSX__) || defined(__APPLE__) onOff = 1; @@ -1081,10 +1079,8 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_REUSEPORT: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } #endif /* BSD, __OSX__, __APPLE__ */ onOff = 1; @@ -1095,10 +1091,8 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() IPV6_V6ONLY: %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } memset(&__ss, 0, sizeof(__ss)); ssdpAddr6->sin6_family = (sa_family_t)AF_INET6; @@ -1111,10 +1105,8 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in bind(), addr=0x%032lX, port=%d: %s\n", 0lu, SSDP_PORT, errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_BIND; + ret = UPNP_E_SOCKET_BIND; + goto error_handler; } memset((void *)&ssdpMcastAddr, 0, sizeof(ssdpMcastAddr)); ssdpMcastAddr.ipv6mr_interface = gIF_INDEX; @@ -1128,10 +1120,8 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() IPV6_JOIN_GROUP (join multicast group): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); - - return UPNP_E_SOCKET_ERROR; + ret = UPNP_E_SOCKET_ERROR; + goto error_handler; } onOff = 1; ret = setsockopt(*ssdpSock, SOL_SOCKET, SO_BROADCAST, @@ -1141,13 +1131,22 @@ static int create_ssdp_sock_v6_ula_gua( UpnpPrintf(UPNP_CRITICAL, SSDP, __FILE__, __LINE__, "Error in setsockopt() SO_BROADCAST (set broadcast): %s\n", errorBuffer); - shutdown(*ssdpSock, SD_BOTH); - UpnpCloseSocket(*ssdpSock); + ret = UPNP_E_NETWORK_ERROR; + goto error_handler; + } + ret = UPNP_E_SUCCESS; - return UPNP_E_NETWORK_ERROR; +error_handler: + if (ret != UPNP_E_SUCCESS) { + if (shutdown(*ssdpSock, SD_BOTH) == -1) { + strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); + UpnpPrintf(UPNP_INFO, SSDP, __FILE__, __LINE__, + "Error in shutdown: %s\n", errorBuffer); + } + UpnpCloseSocket(*ssdpSock); } - return UPNP_E_SUCCESS; + return ret; } #endif /* IPv6 */