Beispiel #1
0
CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool isSecured)
{
    OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");

    // Input validation
    VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
    VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");

    ca_mutex_lock(g_mutexServerInfoList);
    bool isUnicastServerStarted = CAIsUnicastServerStarted(g_serverInfoList, localAddress, *port);
    if (!isUnicastServerStarted)
    {
        int unicastServerFd = -1;
        if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, isSecured,
                                                 &unicastServerFd))
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
            ca_mutex_unlock(g_mutexServerInfoList);
            return CA_STATUS_FAILED;
        }

        CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
        if (!info)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
            close(unicastServerFd);
            ca_mutex_unlock(g_mutexServerInfoList);
            return CA_MEMORY_ALLOC_FAILED;
        }

        char *netMask = NULL;
        if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
        }
        if (netMask)
        {
            OICStrcpy(info->subNetMask, sizeof(info->subNetMask), netMask);
            OICFree(netMask);
        }
        OICStrcpy(info->endpoint.addr, sizeof(info->endpoint.addr), localAddress);
        info->endpoint.port = *port;
        info->endpoint.flags = isSecured ? CA_SECURE : 0;
        info->endpoint.adapter = CA_ADAPTER_IP;
        info->socketFd = unicastServerFd;
        info->isServerStarted = true;
        info->isMulticastServer = false;
        OICStrcpy(info->ifAddr, sizeof(info->ifAddr), localAddress);

        CAResult_t res = CAAddServerInfo(g_serverInfoList, info);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
            close(unicastServerFd);
            ca_mutex_unlock(g_mutexServerInfoList);
            return res;
        }
        ca_mutex_unlock(g_mutexServerInfoList);

        res = CAIPStartPacketReceiverHandler();
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
            close(unicastServerFd);
            return res;
        }
    }
    else
    {
        OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Already Unicast Server Started ip [%s] port [%d]",
                  localAddress, *port);
        ca_mutex_unlock(g_mutexServerInfoList);
    }

    OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
    return CA_STATUS_OK;
}
Beispiel #2
0
CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
                                        bool forceBindStart, bool isSecured)
{
    OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");

    // Input validation
    VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
    VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");

    if (0 >= *port)
    {
        OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: port is invalid!");
        return CA_STATUS_INVALID_PARAM;
    }

    ca_mutex_lock(g_mutexServerInfoList);
    bool isUnicastServerStarted = CAIsUnicastServerStarted(g_serverInfoList, localAddress, *port);
    if (!isUnicastServerStarted)
    {
        int unicastServerFd = -1;
        if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceBindStart, isSecured,
                                                 &unicastServerFd))
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
            ca_mutex_unlock(g_mutexServerInfoList);
            return CA_STATUS_FAILED;
        }

        CAServerInfo_t *info = (CAServerInfo_t *) OICCalloc(1, sizeof(CAServerInfo_t));
        if (!info)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Malloc failed");
            close(unicastServerFd);
            ca_mutex_unlock(g_mutexServerInfoList);
            return CA_MEMORY_ALLOC_FAILED;
        }

        char *netMask = NULL;
        if (CA_STATUS_OK != CAIPGetInterfaceSubnetMask(localAddress, &netMask))
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to get IP subnet");
        }
        if (netMask)
        {
            strncpy(info->subNetMask, netMask, sizeof(info->subNetMask) - 1);
            info->subNetMask[sizeof(info->subNetMask)-1] = '\0';
            OICFree(netMask);
        }
        strncpy(info->ipAddress, localAddress, sizeof(info->ipAddress) - 1);
        info->ipAddress[sizeof(info->ipAddress) - 1] = '\0';
        info->port = *port;
        info->socketFd = unicastServerFd;
        info->isSecured = isSecured;
        info->isServerStarted = true;
        info->isMulticastServer = false;
        strncpy(info->ifAddr, localAddress, sizeof(info->ifAddr) - 1);
        info->ifAddr[sizeof(info->ifAddr) - 1] = '\0';

        CAResult_t res = CAAddServerInfo(g_serverInfoList, info);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "CAAddServerInfo failed!");
            close(unicastServerFd);
            ca_mutex_unlock(g_mutexServerInfoList);
            return res;
        }
        ca_mutex_unlock(g_mutexServerInfoList);

        res = CAIPStartPacketReceiverHandler();
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, IP_SERVER_TAG, "CAIPStartPacketReceiverHandler failed!");
            close(unicastServerFd);
            return res;
        }
    }
    else
    {
        OIC_LOG_V(DEBUG, IP_SERVER_TAG, "Already Unicast Server Started ip [%s] port [%d]",
                  localAddress, *port);
        ca_mutex_unlock(g_mutexServerInfoList);
    }

    OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
    return CA_STATUS_OK;
}