diff --git a/ChangeLog b/ChangeLog index d0405da..6e8a27b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,11 @@ Version 1.8.0 ******************************************************************************* +2010-03-20 Marcelo Jimenez + * SF Patch Tracker [ 2969188 ] 1.8.0: patch for FreeBSD compilation + Submitted By: Nick Leverton (leveret) + Fix the order of header inclusion for FreeBSD. + 2010-03-20 Marcelo Jimenez * Forward port of svn revision 502: SF Patch Tracker [ 2836704 ] Search for nested serviceList (not diff --git a/threadutil/inc/ThreadPool.h b/threadutil/inc/ThreadPool.h index 945a802..bbca16a 100644 --- a/threadutil/inc/ThreadPool.h +++ b/threadutil/inc/ThreadPool.h @@ -58,7 +58,12 @@ }; int gettimeofday(struct timeval *tv, struct timezone *tz); #else /* WIN32 */ + #include #include /* for gettimeofday() */ + #if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__) + #include /* for setpriority() */ + #endif + #endif diff --git a/threadutil/inc/ithread.h b/threadutil/inc/ithread.h index 4324587..7d2aa28 100644 --- a/threadutil/inc/ithread.h +++ b/threadutil/inc/ithread.h @@ -38,6 +38,9 @@ * \file */ +#if ! defined(WIN32) + #include +#endif #include "UpnpGlobal.h" /* For EXPORT_SPEC */ @@ -55,7 +58,7 @@ extern "C" { #endif -#ifdef __FreeBSD__ +#if defined(BSD) #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE #endif diff --git a/threadutil/src/ThreadPool.c b/threadutil/src/ThreadPool.c index d925da7..0b004b3 100644 --- a/threadutil/src/ThreadPool.c +++ b/threadutil/src/ThreadPool.c @@ -35,6 +35,10 @@ */ +#if ! defined(WIN32) + #include +#endif + #include "ThreadPool.h" @@ -214,11 +218,11 @@ static int CmpThreadPoolJob( void *jobA, void *jobB ) * Parameters: * ThreadPoolJob *tpj - must be allocated with CreateThreadPoolJob *****************************************************************************/ -static void FreeThreadPoolJob( ThreadPool *tp, ThreadPoolJob *tpj ) +static void FreeThreadPoolJob(ThreadPool *tp, ThreadPoolJob *tpj) { - assert( tp != NULL ); + assert(tp != NULL); - FreeListFree( &tp->jobFreeList, tpj ); + FreeListFree(&tp->jobFreeList, tpj); } /**************************************************************************** @@ -228,28 +232,30 @@ static void FreeThreadPoolJob( ThreadPool *tp, ThreadPoolJob *tpj ) * Sets the scheduling policy of the current process. * Internal only. * Parameters: - * PolocyType in + * PolicyType in * Returns: * 0 on success, nonzero on failure * Returns result of GetLastError() on failure. * *****************************************************************************/ -static int SetPolicyType( PolicyType in ) +static int SetPolicyType(PolicyType in) { #ifdef __CYGWIN__ /* TODO not currently working... */ return 0; -#elif defined(__OSX__) || defined(__APPLE__) - setpriority( PRIO_PROCESS, 0, 0 ); +#elif defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__) + setpriority(PRIO_PROCESS, 0, 0); return 0; #elif defined(WIN32) - return sched_setscheduler( 0, in ); + return sched_setscheduler(0, in); #elif defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0 struct sched_param current; + int sched_result; - sched_getparam( 0, ¤t ); + sched_getparam(0, ¤t); current.sched_priority = DEFAULT_SCHED_PARAM; - return sched_setscheduler( 0, in, ¤t ); + sched_result = sched_setscheduler(0, in, ¤t); + return (-1 != sched_result || EPERM == errno) ? 0 : -1; #else return 0; #endif @@ -269,7 +275,7 @@ static int SetPolicyType( PolicyType in ) * Returns result of GerLastError on failure. * *****************************************************************************/ -static int SetPriority( ThreadPriority priority ) +static int SetPriority(ThreadPriority priority) { #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0 int currentPolicy; @@ -278,6 +284,7 @@ static int SetPriority( ThreadPriority priority ) int actPriority = 0; int midPriority = 0; struct sched_param newPriority; + int sched_result; pthread_getschedparam( ithread_self(), ¤tPolicy, &newPriority ); minPriority = sched_get_priority_min( currentPolicy ); @@ -299,7 +306,8 @@ static int SetPriority( ThreadPriority priority ) newPriority.sched_priority = actPriority; - return pthread_setschedparam(ithread_self(), currentPolicy, &newPriority ); + sched_result = pthread_setschedparam(ithread_self(), currentPolicy, &newPriority); + return (0 == sched_result || EPERM == errno) ? 0 : -1; #else return 0; #endif @@ -399,7 +407,7 @@ static void SetSeed() gettimeofday(&t, NULL); #if defined(WIN32) srand( ( unsigned int )t.tv_usec + (unsigned int)ithread_get_current_thread_id().p ); -#elif defined(__FreeBSD__) || defined(__OSX__) || defined(__APPLE__) +#elif defined(BSD) || defined(__OSX__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) srand( ( unsigned int )t.tv_usec + (unsigned int)ithread_get_current_thread_id() ); #elif defined(__linux__) || defined(__sun) || defined(__CYGWIN__) || defined(__GLIBC__) srand( ( unsigned int )t.tv_usec + ithread_get_current_thread_id() ); @@ -1525,11 +1533,8 @@ void ThreadPoolPrintStats(ThreadPoolStats *stats) return; } -#ifdef __FreeBSD__ - printf("ThreadPoolStats at Time: %d\n", StatsTime(NULL)); -#else /* __FreeBSD__ */ - printf("ThreadPoolStats at Time: %ld\n", StatsTime(NULL)); -#endif /* __FreeBSD__ */ + /* some OSses time_t length may depending on platform, promote it to long for safety */ + printf("ThreadPoolStats at Time: %ld\n", (long)StatsTime(NULL)); printf("High Jobs pending: %d\n", stats->currentJobsHQ); printf("Med Jobs Pending: %d\n", stats->currentJobsMQ); printf("Low Jobs Pending: %d\n", stats->currentJobsLQ); diff --git a/upnp/inc/UpnpInet.h b/upnp/inc/UpnpInet.h index 3d593c5..ad632a8 100644 --- a/upnp/inc/UpnpInet.h +++ b/upnp/inc/UpnpInet.h @@ -15,6 +15,13 @@ #include #include #else + #include + #if (defined(BSD) && BSD >= 199306) || defined (__FreeBSD_kernel__) + #include + /* Do not move or remove the include below for "sys/socket"! + * Will break FreeBSD builds. */ + #include + #endif #include #endif diff --git a/upnp/src/api/upnpapi.c b/upnp/src/api/upnpapi.c index ee18b4d..b89ebfb 100644 --- a/upnp/src/api/upnpapi.c +++ b/upnp/src/api/upnpapi.c @@ -50,6 +50,17 @@ #ifdef WIN32 /* Do not include these files */ #else + #include + #if defined(_sun) + #include + #include + #elif defined(BSD) && BSD >= 199306 + #include + /* Do not move or remove the include below for "sys/socket"! + * Will break FreeBSD builds. */ + #include + #endif + #include #include #include @@ -58,17 +69,7 @@ #include #include #include - - #include - - - #if defined(_sun) - #include - #include - #elif defined(BSD) && BSD >= 199306 - #include - #endif #endif @@ -2961,7 +2962,7 @@ int UpnpGetIfInfo(const char *IfName) inet_ntop(AF_INET, &v4_addr, gIF_IPV4, sizeof(gIF_IPV4)); inet_ntop(AF_INET6, &v6_addr, gIF_IPV6, sizeof(gIF_IPV6)); -#elif (defined(BSD) && BSD >= 199306) +#elif (defined(BSD) && BSD >= 199306) || defined(__FreeBSD_kernel__) struct ifaddrs *ifap, *ifa; struct in_addr v4_addr; struct in6_addr v6_addr; @@ -3478,7 +3479,7 @@ int getlocalhostname(char *out, const int out_len) h = gethostbyname(out); if (h != NULL) { memcpy(&LocalAddr.sin_addr, h->h_addr_list[0], 4); - p = inet_ntop(AF_INET, &LocalAddr.sin_addr, tempstr, 16); + p = inet_ntop(AF_INET, &LocalAddr.sin_addr, tempstr, sizeof(tempstr)); if (p) { strncpy(out, p, out_len); } else { @@ -3492,9 +3493,8 @@ int getlocalhostname(char *out, const int out_len) ret = UPNP_E_INIT; } -#elif (defined(BSD) && BSD >= 199306) +#elif (defined(BSD) && BSD >= 199306) || defined(__FreeBSD_kernel__) struct ifaddrs *ifap, *ifa; - char tempstr[16]; if (getifaddrs(&ifap) != 0) { UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, @@ -3517,12 +3517,14 @@ int getlocalhostname(char *out, const int out_len) htonl(INADDR_LOOPBACK)) { continue; } - p = inet_ntop(AF_INET, &LocalAddr.sin_addr, tempstr, 16); + p = inet_ntop(AF_INET, + &((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr, + tempstr, sizeof(tempstr)); if (p) { strncpy(out, p, out_len); } else { - UpnpPrintf( UPNP_ALL, API, __FILE__, __LINE__, - "getlocalhostname: inet_ntop returned error\n" ); + UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, + "getlocalhostname: inet_ntop returned error\n"); ret = UPNP_E_INIT; } UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, @@ -3596,7 +3598,7 @@ int getlocalhostname(char *out, const int out_len) } close(LocalSock); - p = inet_ntop(AF_INET, &LocalAddr.sin_addr, tempstr, 16); + p = inet_ntop(AF_INET, &LocalAddr.sin_addr, tempstr, sizeof(tempstr)); if (p) { strncpy(out, p, out_len); } else { diff --git a/upnp/src/genlib/miniserver/miniserver.c b/upnp/src/genlib/miniserver/miniserver.c index 7eb58f7..870b9ab 100644 --- a/upnp/src/genlib/miniserver/miniserver.c +++ b/upnp/src/genlib/miniserver/miniserver.c @@ -668,9 +668,10 @@ static int get_miniserver_sockets( } } else { if (listenfd4 != INVALID_SOCKET) { + unsigned short orig_listen_port4 = listen_port4; do { serverAddr4->sin_port = htons(listen_port4++); - sockError = bind(listenfd4, (struct sockaddr *)&__ss_v4, sizeof(__ss_v4)); + sockError = bind(listenfd4, (struct sockaddr *)serverAddr4, sizeof(*serverAddr4)); if (sockError == -1) { #ifdef WIN32 errCode = WSAGetLastError(); @@ -683,7 +684,7 @@ static int get_miniserver_sockets( } else { errCode = 0; } - } while ( errCode != 0 ); + } while ( errCode != 0 && (listen_port4 >= orig_listen_port4) ); if (sockError == -1) { strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); @@ -699,9 +700,10 @@ static int get_miniserver_sockets( } if (listenfd6 != INVALID_SOCKET) { + unsigned short orig_listen_port6 = listen_port6; do { serverAddr6->sin6_port = htons(listen_port6++); - sockError = bind(listenfd6, (struct sockaddr *)&__ss_v6, sizeof(__ss_v6)); + sockError = bind(listenfd6, (struct sockaddr *)serverAddr6, sizeof(*serverAddr6)); if (sockError == -1) { #ifdef WIN32 errCode = WSAGetLastError(); @@ -714,7 +716,7 @@ static int get_miniserver_sockets( } else { errCode = 0; } - } while (errCode != 0); + } while (errCode != 0 && (listen_port6 >= orig_listen_port6)); if (sockError == -1) { strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); diff --git a/upnp/src/inc/uri.h b/upnp/src/inc/uri.h index b3f1024..65a557f 100644 --- a/upnp/src/inc/uri.h +++ b/upnp/src/inc/uri.h @@ -38,6 +38,10 @@ * \file */ +#if !defined(WIN32) + #include +#endif + #include "UpnpGlobal.h" /* for */ #include "UpnpInet.h" @@ -48,6 +52,9 @@ #include #include #include +#if defined(BSD) + #include +#endif #include #include diff --git a/upnp/src/ssdp/ssdp_server.c b/upnp/src/ssdp/ssdp_server.c index ff27693..e513dad 100644 --- a/upnp/src/ssdp/ssdp_server.c +++ b/upnp/src/ssdp/ssdp_server.c @@ -29,6 +29,9 @@ * **************************************************************************/ +#if !defined(WIN32) + #include +#endif #include "config.h" @@ -1034,7 +1037,7 @@ int create_ssdp_sock_v4( SOCKET* ssdpSock ) u_char ttl = 4; struct ip_mreq ssdpMcastAddr; struct sockaddr_storage __ss; - struct sockaddr_in* ssdpAddr4 = (struct sockaddr_in*)&__ss; + struct sockaddr_in *ssdpAddr4 = (struct sockaddr_in *)&__ss; int ret = 0; struct in_addr addr; @@ -1061,7 +1064,7 @@ int create_ssdp_sock_v4( SOCKET* ssdpSock ) return UPNP_E_SOCKET_ERROR; } -#if defined(__FreeBSD__) || defined(__OSX__) || defined(__APPLE__) +#if defined(BSD) || defined(__OSX__) || defined(__APPLE__) onOff = 1; ret = setsockopt( *ssdpSock, SOL_SOCKET, SO_REUSEPORT, (char *)&onOff, sizeof(onOff) ); @@ -1074,14 +1077,13 @@ int create_ssdp_sock_v4( SOCKET* ssdpSock ) return UPNP_E_SOCKET_ERROR; } -#endif /* __FreeBSD__ */ +#endif /* BSD */ memset( &__ss, 0, sizeof( __ss ) ); ssdpAddr4->sin_family = AF_INET; - // ssdpAddr.sin_addr.s_addr = inet_addr(gIF_IPV4); ssdpAddr4->sin_addr.s_addr = htonl( INADDR_ANY ); ssdpAddr4->sin_port = htons( SSDP_PORT ); - ret = bind( *ssdpSock, (struct sockaddr *)&__ss, sizeof(__ss) ); + ret = bind( *ssdpSock, (struct sockaddr *)ssdpAddr4, sizeof(*ssdpAddr4) ); if ( ret == -1 ) { strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); UpnpPrintf( UPNP_CRITICAL, SSDP, __FILE__, __LINE__, @@ -1161,7 +1163,7 @@ int create_ssdp_sock_v6( SOCKET* ssdpSock ) char errorBuffer[ERROR_BUFFER_LEN]; struct ipv6_mreq ssdpMcastAddr; struct sockaddr_storage __ss; - struct sockaddr_in6* ssdpAddr6 = (struct sockaddr_in6*)&__ss; + struct sockaddr_in6 *ssdpAddr6 = (struct sockaddr_in6 *)&__ss; int onOff; int ret = 0; @@ -1187,7 +1189,7 @@ int create_ssdp_sock_v6( SOCKET* ssdpSock ) return UPNP_E_SOCKET_ERROR; } -#if defined(__FreeBSD__) || defined(__OSX__) || defined(__APPLE__) +#if defined(BSD) || defined(__OSX__) || defined(__APPLE__) onOff = 1; ret = setsockopt( *ssdpSock, SOL_SOCKET, SO_REUSEPORT, (char*)&onOff, sizeof (onOff) ); @@ -1200,15 +1202,14 @@ int create_ssdp_sock_v6( SOCKET* ssdpSock ) return UPNP_E_SOCKET_ERROR; } -#endif /* __FreeBSD__ */ +#endif /* BSD */ memset( &__ss, 0, sizeof( __ss ) ); ssdpAddr6->sin6_family = AF_INET6; ssdpAddr6->sin6_addr = in6addr_any; - //inet_pton( AF_INET6, gIF_IPV6, &ssdpAddr6->sin6_addr ); ssdpAddr6->sin6_scope_id = gIF_INDEX; ssdpAddr6->sin6_port = htons( SSDP_PORT ); - ret = bind( *ssdpSock, (struct sockaddr *)&__ss, sizeof(__ss) ); + ret = bind( *ssdpSock, (struct sockaddr *)ssdpAddr6, sizeof(*ssdpAddr6) ); if ( ret == -1 ) { strerror_r(errno, errorBuffer, ERROR_BUFFER_LEN); UpnpPrintf( UPNP_CRITICAL, SSDP, __FILE__, __LINE__,