Added a new flag to OCInitUDP to indicated whether to reuse the address or not.
Modified create_new_context API. It now takes the ipAddr[4] as input. This needs
to be passed further down to dtls_init.
signed-off-by: Sashi Penta <sashi.kumar.penta@intel.com>
Change-Id: I536f215bf2a1ff4c90685e7a19e32fcb76f1e912
#endif
coap_context_t *
-coap_new_context(const coap_address_t *listen_addr) {
+coap_new_context(uint8_t ipAddr[], uint16_t port) {
+
+ OCDevAddr devAddr;
+ coap_address_t* listen_addr;
+
#if defined(WITH_POSIX) || defined(WITH_ARDUINO)
coap_context_t *c = (coap_context_t*)coap_malloc( sizeof( coap_context_t ) );
//int reuse = 1;
return NULL;
#endif /* WITH_CONTIKI */
+ OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
+ &devAddr);
+
+ listen_addr = (coap_address_t*) &devAddr;
+
if (!listen_addr) {
coap_free(c);
coap_log(LOG_EMERG, "no listen address specified\n");
coap_register_option(c, COAP_OPTION_BLOCK1);
#if defined(WITH_POSIX) || defined(WITH_ARDUINO)
- if (OCInitUDP((OCDevAddr *)listen_addr, (int32_t *)&(c->sockfd)) != ERR_SUCCESS) {
+ if (OCInitUDP((OCDevAddr *)listen_addr,
+ (int32_t *)&(c->sockfd), OC_SOCKET_REUSEADDR) != ERR_SUCCESS) {
coap_free( c);
return NULL;
}
#if defined(WITH_DTLS)
- if (coap_dtls_init(c) != 0) {
+ if (coap_dtls_init(c, ipAddr) != 0) {
coap_free( c);
return NULL;
}
coap_queue_t *coap_pop_next( coap_context_t *context );
/** Creates a new coap_context_t object that will hold the CoAP stack status. */
-coap_context_t *coap_new_context(const coap_address_t *listen_addr);
+coap_context_t *coap_new_context(uint8_t ipAddr[], uint16_t port);
/** Joins the CoAP stack to well-known multicast address. */
int coap_join_wellknown_group(coap_context_t *context,
*
* @param ctx - handle to global coap_context_t.
*
+ * @param ipAddr - ip address.
+ *
* @return A value less than zero on error, greater or
* equal otherwise.
*/
-int coap_dtls_init(coap_context_t *ctx) {
+int coap_dtls_init(coap_context_t *ctx, uint8_t ipAddr[]) {
int ret = -1;
coap_dtls_context_t *coap_dtls_ctx = NULL;
memset(coap_dtls_ctx, 0, sizeof(coap_dtls_ctx));
ctx->sockfd_dtls = -1;
- //TODO : Initialize secure socket descriptor
- OCBuildIPv4Address(0, 0, 0, 0, COAP_DTLS_DEFAULT_PORT, &dev_addr);
- if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls)) != ERR_SUCCESS) {
- OCBuildIPv4Address(0, 0, 0, 0, 5685, &dev_addr);
- if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls)) != ERR_SUCCESS) {
+ OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3],
+ COAP_DTLS_DEFAULT_PORT, &dev_addr);
+ if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls), 0) != ERR_SUCCESS) {
+ OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3],
+ COAP_DTLS_RANDOM_PORT, &dev_addr);
+ if (OCInitUDP((OCDevAddr *)&dev_addr, (int32_t *)&(ctx->sockfd_dtls), 0) != ERR_SUCCESS) {
goto exit;
}
}
#include "dtls.h"
#define COAP_DTLS_DEFAULT_PORT 5684
+#define COAP_DTLS_RANDOM_PORT 0
/**
* Data structure for holding the tinyDTLS interface
*
* @param ctx - handle to global coap_context_t.
*
+ * @param ipAddr - ip address.
+ *
* @return A value less than zero on error, greater or
* equal otherwise.
*/
-int coap_dtls_init(coap_context_t *ctx);
+int coap_dtls_init(coap_context_t *ctx, uint8_t ipAddr[]);
/**
* Closes secure port and de-inits tinyDTLS library.
TODO ("Below should go away and be replaced by OC_LOG");
coap_log_t log_level = (coap_log_t)(LOG_DEBUG + 1);
- OCDevAddr devAddr;
OCDevAddr mcastAddr;
uint8_t ipAddr[4] = { 0 };
uint16_t parsedPort = 0;
ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);
}
- OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
- &devAddr);
-
- gCoAPCtx = coap_new_context((coap_address_t*) &devAddr);
+ gCoAPCtx = coap_new_context(ipAddr, port);
VERIFY_NON_NULL(gCoAPCtx);
// To allow presence notification work we need to init socket gCoAPCtx->sockfd_wellknown
//------------------------------------------------------------------------
int32_t OCInitNetworkStack();
+typedef enum
+{
+ OC_SOCKET_NOOPTION = 0,
+ OC_SOCKET_REUSEADDR
+} OC_SOCKET_OPTION;
//-- OCInitUDP -----------------------------------------------------------
/** @ingroup ocsocket
* device address with which the new socket will be bind.
* @param[out] sockfd
* reference to the new socket.
+ * @param[in] sockoption
+ * specifies which socket option to be used.
*
* @retval 0 for Success, otherwise some error value
*/
//------------------------------------------------------------------------
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd);
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption);
#endif //__ANDROID__
/// Creates a BSD socket and binds it specified port for UDP
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t *sockfd, OC_SOCKET_OPTION sockoption)
{
int32_t ret = ERR_UNKNOWN;
int32_t sfd = 0xFFFFFFFF;
goto exit;
}
- if ((ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
- sizeof(set_option_on))) < 0) {
- OC_LOG_V(FATAL, MOD_NAME, "setsockopt API failed with errno %s",
- strerror(errno));
- goto exit;
+ if(OC_SOCKET_REUSEADDR == sockoption)
+ {
+ if ((ret = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
+ sizeof(set_option_on))) < 0) {
+ OC_LOG_V(FATAL, MOD_NAME, "setsockopt API failed with errno %s",
+ strerror(errno));
+ goto exit;
+ }
}
if ((ret = bind(sfd, (struct sockaddr*)ipAddr->addr, ipAddr->size)) < 0) {
}
/// Retrieves a empty socket and bind it for UDP with the input port
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
{
uint8_t state;
ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr;
}
/// Retrieves a empty socket and bind it for UDP with the input port
-int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd)
+int32_t OCInitUDP(OCDevAddr* ipAddr, int32_t* sockfd, OC_SOCKET_OPTION sockoption)
{
ArduinoAddr* ardAddr = (ArduinoAddr*)ipAddr;
uint8_t sock;
/// Currently WiFi shield does NOT support multicast.
int32_t OCInitUDPMulticast(OCDevAddr* ipMcastMacAddr, int32_t* sockfd)
{
- return OCInitUDP(ipMcastMacAddr, sockfd);
+ return OCInitUDP(ipMcastMacAddr, sockfd, OC_SOCKET_REUSEADDR);
}