diff --git a/ChangeLog b/ChangeLog index 2f0fa57..8d7c267 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,16 @@ Version 1.6.16 ******************************************************************************* +2012-03-14 Fabrice Fontaine + + Use switch instead of if with enums in upnpapi.c + + Replace if statements with switch when using HND_DEVICE and HND_CLIENT + enum constants. + Correct also UpnpUnRegisterRootDeviceLowPower and UpnpUnRegisterClient + as those functions were wrongly awaiting an UPNP_E_INVALID_HANDLE + instead of HND_INVALID from GetHandleInfo. + 2012-03-14 Fabrice Fontaine Improve ssdp part diff --git a/upnp/src/api/upnpapi.c b/upnp/src/api/upnpapi.c index faff73e..b3282b0 100644 --- a/upnp/src/api/upnpapi.c +++ b/upnp/src/api/upnpapi.c @@ -608,14 +608,29 @@ int UpnpFinish(void) PrintThreadPoolStats(&gMiniServerThreadPool, __FILE__, __LINE__, "MiniServer Thread Pool"); #ifdef INCLUDE_DEVICE_APIS - if (GetDeviceHandleInfo(AF_INET, &device_handle, &temp) == HND_DEVICE) + switch (GetDeviceHandleInfo(AF_INET, &device_handle, &temp)) { + case HND_DEVICE: UpnpUnRegisterRootDevice(device_handle); - if (GetDeviceHandleInfo(AF_INET6, &device_handle, &temp) == HND_DEVICE) + break; + default: + break; + } + switch (GetDeviceHandleInfo(AF_INET6, &device_handle, &temp)) { + case HND_DEVICE: UpnpUnRegisterRootDevice(device_handle); + break; + default: + break; + } #endif #ifdef INCLUDE_CLIENT_APIS - if (GetClientHandleInfo(&client_handle, &temp) == HND_CLIENT) + switch (GetClientHandleInfo(&client_handle, &temp)) { + case HND_CLIENT: UpnpUnRegisterClient(client_handle); + break; + default: + break; + } #endif TimerThreadShutdown(&gTimerThread); #if EXCLUDE_MINISERVER == 0 @@ -752,7 +767,9 @@ int UpnpRegisterRootDevice( { struct Handle_Info *HInfo = NULL; int retVal = 0; +#if EXCLUDE_GENA == 0 int hasServiceTable = 0; +#endif /* EXCLUDE_GENA */ HandleLock(); @@ -918,7 +935,9 @@ int UpnpRegisterRootDevice2( { struct Handle_Info *HInfo = NULL; int retVal = 0; +#if EXCLUDE_GENA == 0 int hasServiceTable = 0; +#endif /* EXCLUDE_GENA */ char *description = (char *)description_const; HandleLock(); @@ -1077,7 +1096,9 @@ int UpnpRegisterRootDevice4( { struct Handle_Info *HInfo; int retVal = 0; +#if EXCLUDE_GENA == 0 int hasServiceTable = 0; +#endif /* EXCLUDE_GENA */ int handler_index = 0; HandleLock(); @@ -1206,10 +1227,13 @@ int UpnpRegisterRootDevice4( } #endif /* EXCLUDE_GENA */ - if (AddressFamily == AF_INET) + switch (AddressFamily) { + case AF_INET: UpnpSdkDeviceRegisteredV4 = 1; - else + break; + default: UpnpSdkDeviceregisteredV6 = 1; + } retVal = UPNP_E_SUCCESS; @@ -1247,9 +1271,12 @@ int UpnpUnRegisterRootDeviceLowPower(UpnpDevice_Handle Hnd, int PowerState, #endif HandleLock(); - if (GetHandleInfo(Hnd, &HInfo) == UPNP_E_INVALID_HANDLE) { + switch (GetHandleInfo(Hnd, &HInfo)) { + case HND_INVALID: HandleUnlock(); return UPNP_E_INVALID_HANDLE; + default: + break; } HInfo->PowerState = PowerState; if( SleepPeriod < 0 ) @@ -1265,9 +1292,12 @@ int UpnpUnRegisterRootDeviceLowPower(UpnpDevice_Handle Hnd, int PowerState, #endif HandleLock(); - if (GetHandleInfo(Hnd, &HInfo) == UPNP_E_INVALID_HANDLE) { + switch (GetHandleInfo(Hnd, &HInfo)) { + case HND_INVALID: HandleUnlock(); return UPNP_E_INVALID_HANDLE; + default: + break; } ixmlNodeList_free(HInfo->DeviceList); ixmlNodeList_free(HInfo->ServiceList); @@ -1279,10 +1309,16 @@ int UpnpUnRegisterRootDeviceLowPower(UpnpDevice_Handle Hnd, int PowerState, if (HInfo->aliasInstalled) web_server_set_alias(NULL, NULL, 0, 0); #endif /* INTERNAL_WEB_SERVER */ - if (HInfo->DeviceAf == (unsigned short)AF_INET) + switch (HInfo->DeviceAf) { + case AF_INET: UpnpSdkDeviceRegisteredV4 = 0; - else if (HInfo->DeviceAf == (unsigned short)AF_INET6) + break; + case AF_INET6: UpnpSdkDeviceregisteredV6 = 0; + break; + default: + break; + } FreeHandle(Hnd); HandleUnlock(); @@ -1365,9 +1401,12 @@ int UpnpUnRegisterClient(UpnpClient_Handle Hnd) return UPNP_E_INVALID_HANDLE; #endif HandleLock(); - if (GetHandleInfo(Hnd, &HInfo) == UPNP_E_INVALID_HANDLE) { + switch (GetHandleInfo(Hnd, &HInfo)) { + case HND_INVALID: HandleUnlock(); return UPNP_E_INVALID_HANDLE; + default: + break; } /* clean up search list */ node = ListHead(&HInfo->SsdpSearchList); @@ -1648,7 +1687,10 @@ int UpnpSendAdvertisementLowPower(UpnpDevice_Handle Hnd, int Exp, "Inside UpnpSendAdvertisementLowPower \n" ); HandleLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -1681,7 +1723,10 @@ int UpnpSendAdvertisementLowPower(UpnpDevice_Handle Hnd, int Exp, adEvent->Event = ptrMx; HandleLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: HandleUnlock(); free( adEvent ); free( ptrMx ); @@ -1751,7 +1796,10 @@ int UpnpSearchAsync( "Inside UpnpSearchAsync\n" ); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -1799,9 +1847,15 @@ int UpnpSetMaxSubscriptions(UpnpDevice_Handle Hnd, int MaxSubscriptions) "Inside UpnpSetMaxSubscriptions \n" ); HandleLock(); - if( ( ( MaxSubscriptions != UPNP_INFINITE ) - && ( MaxSubscriptions < 0 ) ) - || ( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: + HandleUnlock(); + return UPNP_E_INVALID_HANDLE; + } + if( ( MaxSubscriptions != UPNP_INFINITE ) + && ( MaxSubscriptions < 0 ) ) { HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -1831,9 +1885,15 @@ int UpnpSetMaxSubscriptionTimeOut(UpnpDevice_Handle Hnd, int MaxSubscriptionTime HandleLock(); - if( ( ( MaxSubscriptionTimeOut != UPNP_INFINITE ) - && ( MaxSubscriptionTimeOut < 0 ) ) - || ( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: + HandleUnlock(); + return UPNP_E_INVALID_HANDLE; + } + if( ( MaxSubscriptionTimeOut != UPNP_INFINITE ) + && ( MaxSubscriptionTimeOut < 0 ) ) { HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -1873,7 +1933,10 @@ int UpnpSubscribeAsync( "Inside UpnpSubscribeAsync\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -1964,7 +2027,10 @@ int UpnpSubscribe( } HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_CLIENT) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_CLIENT: + break; + default: HandleUnlock(); retVal = UPNP_E_INVALID_HANDLE; goto exit_function; @@ -2012,7 +2078,10 @@ int UpnpUnSubscribe(UpnpClient_Handle Hnd, const Upnp_SID SubsId) UpnpString_set_String(SubsIdTmp, SubsId); HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_CLIENT) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_CLIENT: + break; + default: HandleUnlock(); retVal = UPNP_E_INVALID_HANDLE; goto exit_function; @@ -2063,7 +2132,10 @@ int UpnpUnSubscribeAsync( } HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_CLIENT) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_CLIENT: + break; + default: HandleUnlock(); retVal = UPNP_E_INVALID_HANDLE; goto exit_function; @@ -2128,7 +2200,10 @@ int UpnpRenewSubscription( } HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_CLIENT) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_CLIENT: + break; + default: HandleUnlock(); retVal = UPNP_E_INVALID_HANDLE; goto exit_function; @@ -2169,7 +2244,10 @@ int UpnpRenewSubscriptionAsync( UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "Inside UpnpRenewSubscriptionAsync\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2239,7 +2317,10 @@ int UpnpNotify( "Inside UpnpNotify\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2286,7 +2367,10 @@ int UpnpNotifyExt( "Inside UpnpNotify \n" ); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_DEVICE ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_DEVICE: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2339,7 +2423,10 @@ int UpnpAcceptSubscription( HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_DEVICE) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_DEVICE: + break; + default: HandleUnlock(); line = __LINE__; ret = UPNP_E_INVALID_HANDLE; @@ -2411,7 +2498,10 @@ int UpnpAcceptSubscriptionExt( HandleReadLock(); - if (GetHandleInfo(Hnd, &SInfo) != HND_DEVICE) { + switch (GetHandleInfo(Hnd, &SInfo)) { + case HND_DEVICE: + break; + default: HandleUnlock(); line = __LINE__; ret = UPNP_E_INVALID_HANDLE; @@ -2497,7 +2587,10 @@ int UpnpSendAction( DevUDN_const = NULL; HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2552,7 +2645,10 @@ int UpnpSendActionEx( } HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2604,7 +2700,10 @@ int UpnpSendActionAsync( "Inside UpnpSendActionAsync\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2700,7 +2799,10 @@ int UpnpSendActionExAsync( } HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2801,7 +2903,10 @@ int UpnpGetServiceVarStatusAsync( "Inside UpnpGetServiceVarStatusAsync\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -2862,7 +2967,10 @@ int UpnpGetServiceVarStatus( "Inside UpnpGetServiceVarStatus\n"); HandleReadLock(); - if( GetHandleInfo( Hnd, &SInfo ) != HND_CLIENT ) { + switch( GetHandleInfo( Hnd, &SInfo ) ) { + case HND_CLIENT: + break; + default: HandleUnlock(); return UPNP_E_INVALID_HANDLE; } @@ -3590,13 +3698,19 @@ Upnp_Handle_Type GetClientHandleInfo( Upnp_Handle_Type ret = HND_CLIENT; UpnpClient_Handle client; - if (GetHandleInfo(1, HndInfo) == HND_CLIENT) { + switch (GetHandleInfo(1, HndInfo)) { + case HND_CLIENT: client = 1; - } else if (GetHandleInfo(2, HndInfo) == HND_CLIENT) { - client = 2; - } else { - client = -1; - ret = HND_INVALID; + break; + default: + switch (GetHandleInfo(2, HndInfo)) { + case HND_CLIENT: + client = 2; + break; + default: + client = -1; + ret = HND_INVALID; + } } *client_handle_out = client; @@ -3618,10 +3732,14 @@ Upnp_Handle_Type GetDeviceHandleInfo( /* Find it. */ for (*device_handle_out=1; *device_handle_out < NUM_HANDLE; (*device_handle_out)++) { - if (GetHandleInfo(*device_handle_out, HndInfo) == HND_DEVICE) { + switch (GetHandleInfo(*device_handle_out, HndInfo)) { + case HND_DEVICE: if ((*HndInfo)->DeviceAf == AddressFamily) { return HND_DEVICE; } + break; + default: + break; } } @@ -3667,9 +3785,13 @@ int PrintHandleInfo(UpnpClient_Handle Hnd) UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "HType_%d\n", HndInfo->HType); #ifdef INCLUDE_DEVICE_APIS - if(HndInfo->HType != HND_CLIENT) + switch(HndInfo->HType) { + case HND_CLIENT: + break; + default: UpnpPrintf(UPNP_ALL, API, __FILE__, __LINE__, "DescURL_%s\n", HndInfo->DescURL); + } #endif /* INCLUDE_DEVICE_APIS */ } else { return UPNP_E_INVALID_HANDLE; @@ -4157,10 +4279,12 @@ int UpnpSetContentLength(UpnpClient_Handle Hnd, size_t contentLength) HandleLock(); - errCode = GetHandleInfo(Hnd, &HInfo); - if (errCode != HND_DEVICE) { - errCode = UPNP_E_INVALID_HANDLE; + switch (GetHandleInfo(Hnd, &HInfo)) { + case HND_DEVICE: break; + default: + HandleUnlock(); + return UPNP_E_INVALID_HANDLE; } if (contentLength > MAX_SOAP_CONTENT_LENGTH) { errCode = UPNP_E_OUTOF_BOUNDS;