* @def CA_PORT
* @brief Port to listen for incoming data
*/
-#define CA_PORT 6298
+#define CA_PORT 0
/**
* @def CA_SECURE_PORT
if (CA_INTERFACE_UP == status)
{
uint16_t port = CA_PORT;
- CAResult_t ret = CAIPStartUnicastServer(ipAddress, &port, false, false);
+ CAResult_t ret = CAIPStartUnicastServer(ipAddress, &port, false);
if (CA_STATUS_OK == ret)
{
OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Unicast server started on %d port", port);
#ifdef __WITH_DTLS__
port = CA_SECURE_PORT;
- ret = CAIPStartUnicastServer(ipAddress, &port, false, true);
+ ret = CAIPStartUnicastServer(ipAddress, &port, true);
if (CA_STATUS_OK == ret)
{
OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Secure Unicast server started on %d", port);
continue;
}
uint16_t unicastPort = CA_PORT;
- ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false, false);
+ ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false);
if (CA_STATUS_OK == ret)
{
OIC_LOG_V(DEBUG, IP_ADAPTER_TAG, "Unicast server started on %d port",
#ifdef __WITH_DTLS__
unicastPort = CA_SECURE_PORT;
- ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, false, true);
+ ret = CAIPStartUnicastServer(netInfo->ipAddress, &unicastPort, true);
if (CA_STATUS_OK == ret)
{
OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT");
}
-static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port,
- bool forceBindStart)
+static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *port)
{
VERIFY_NON_NULL(socketFD, IP_SERVER_TAG, "socketFD is NULL");
VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
return CA_STATUS_FAILED;
}
- if (true == forceBindStart)
+ if (0 != *port)
{
int setOptionOn = SOCKETOPTION;
if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &setOptionOn,
sockAddr.sin_addr.s_addr = inet_addr(localIp);
}
- int16_t i = 0;
- bool isBound = false;
- for (i = 0; i < CA_UDP_BIND_RETRY_COUNT; i++)
+ if (-1 != bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
{
- if (-1 == bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)))
- {
- if (false == forceBindStart)
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]. Trying again..",
- strerror(errno));
+ struct sockaddr_in sin;
+ socklen_t len = sizeof(sin);
- //Set the port to next one
- serverPort += 1;
- sockAddr.sin_port = htons(serverPort);
- continue;
- }
- else
- {
- OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
- strerror(errno));
- break;
- }
+ if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
+ {
+ OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to get socket[%s]!",
+ strerror(errno));
+ close(sock);
+ return CA_STATUS_FAILED;
+ }
+ else
+ {
+ serverPort = (uint16_t) ntohs(sin.sin_port);
}
-
- isBound = true;
- break;
}
-
- if (false == isBound)
+ else
{
+ OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to bind socket[%s]!",
+ strerror(errno));
close(sock);
return CA_STATUS_FAILED;
}
}
static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port,
- bool forceBindStart, bool isSecured, int *serverFD)
+ bool isSecured, int *serverFD)
{
OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
- CAResult_t ret = CACreateSocket(serverFD, localAddress, port, forceBindStart);
+ CAResult_t ret = CACreateSocket(serverFD, localAddress, port);
if (CA_STATUS_OK != ret)
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket");
}
-CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port,
- bool forceBindStart, bool isSecured)
+CAResult_t CAIPStartUnicastServer(const char *localAddress, uint16_t *port, bool isSecured)
{
OIC_LOG(DEBUG, IP_SERVER_TAG, "IN");
VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress");
VERIFY_NON_NULL(port, IP_SERVER_TAG, "port");
- if (0 >= *port)
+ if (0 > *port)
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Invalid input: port is invalid!");
return CA_STATUS_INVALID_PARAM;
if (!isUnicastServerStarted)
{
int unicastServerFd = -1;
- if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, forceBindStart, isSecured,
+ if (CA_STATUS_OK != CAStartUnicastServer(localAddress, port, isSecured,
&unicastServerFd))
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to start unicast server!");
if (!isMulticastServerStarted)
{
int mulicastServerFd = -1;
- CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port, true);
+ CAResult_t ret = CACreateSocket(&mulicastServerFd, multicastAddress, &port);
if (ret != CA_STATUS_OK)
{
OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create multicast socket");