示例#1
0
static int
read_from_peer(struct dtls_context_t *ctx, 
	       session_t *session, uint8 *data, size_t len) {
  clock_time_t start, end;
  char resp[MAX_PAYLOAD_LEN] = {0};
  int i;
  unsigned int control;
  
#define CC2538_5_MS 2620
//#define APPEXETIME CC2538_5_MS   
#define APPEXETIME ((5*CC2538_5_MS))

  start = RTIMER_NOW();
  control = atoi((char*)data);
  for(i=0; i < APPEXETIME; i++)
	  random_rand();
  end = RTIMER_NOW();
  
  sprintf(resp, "%lu\n", end - start);
  
  //Response length controled by Request.
  dtls_write(ctx, session, (void*) &resp, control);
  
  return 0;
}
示例#2
0
/*---------------------------------------------------------------------------*/
int
coap_context_send_message(coap_context_t *coap_ctx,
                          uip_ipaddr_t *addr, uint16_t port,
                          const uint8_t *data, uint16_t length)
{
  int res;
  session_t sn;

  if(coap_ctx == NULL || coap_ctx->is_used == 0) {
    PRINTF("coap-context: can not send on non-used context\n");
    return 0;
  }

  uip_ipaddr_copy(&sn.addr.ipaddr, addr);
  sn.addr.port = port;
  sn.size = sizeof(sn.addr);
  sn.ifindex = 1;

  res = dtls_write(coap_ctx->dtls_context, &sn, (uint8 *)data, length);
  if(res < 0) {
    PRINTF("coap-context: Failed to send with dtls (%d)\n", res);
  } else if (res == 0) {
    PRINTF("coap-context: No data sent with DTLS but connection made.\n");
  }
  return res;
}
示例#3
0
/* This function is the "key store" for tinyDTLS. It is called to
 * retrieve a key for the given identiy within this particular
 * session. */
int
get_key(struct dtls_context_t *ctx, 
	const session_t *session, 
	const unsigned char *id, size_t id_len, 
	const dtls_key_t **result) {

  static const dtls_key_t psk = {
    .type = DTLS_KEY_PSK,
    .key.psk.id = (unsigned char *)"Client_identity", 
    .key.psk.id_length = 15,
    .key.psk.key = (unsigned char *)"secretPSK", 
    .key.psk.key_length = 9
  };
   
  *result = &psk;
  return 0;
}

void
try_send(struct dtls_context_t *ctx, session_t *dst) {
  int res;
  res = dtls_write(ctx, dst, (uint8 *)buf, len);
  if (res >= 0) {
    memmove(buf, buf + res, len - res);
    len -= res;
  }
}
示例#4
0
static void
try_send(struct dtls_context_t *ctx, session_t *dst) {
  int res;
  res = dtls_write(ctx, dst, (uint8 *)buf, buflen);
  if (res >= 0) {
    memmove(buf, buf + res, buflen - res);
    buflen -= res;
  }
}
示例#5
0
int
read_from_peer(struct dtls_context_t *ctx, 
	       session_t *session, uint8 *data, size_t len) {
  size_t i;
  for (i = 0; i < len; i++)
    printf("%c", data[i]);
  dtls_write(ctx, session, data, len);
  return 0;
}
示例#6
0
int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length){

    if (connP->dtlsSession == NULL) {
        // no security
        if ( 0 != send_data(connP, buffer, length)) {
            return -1 ;
        }
    } else {
        if (-1 == dtls_write(connP->dtlsContext, connP->dtlsSession, buffer, length)) {
            return -1;
        }
    }
    return 0;
}
示例#7
0
static int
read_from_peer(struct dtls_context_t *ctx, 
	       session_t *session, uint8 *data, size_t len) {
  //size_t i;
  //for (i = 0; i < len; i++)
  //  PRINTF("%c", data[i]);

  /* echo incoming application data */
  //dtls_write(ctx, session, data, len);
  
  //Reply with a constant value.
  dtls_write(ctx, session, dummydata, PAYLOAD_LEN);
  
  return 0;
}
示例#8
0
static int
read_from_peer(struct dtls_context_t *ctx, 
	       session_t *session, uint8 *data, size_t len) {
  size_t i;
  for (i = 0; i < len; i++)
    printf("%c", data[i]);
  if (len >= strlen(DTLS_SERVER_CMD_CLOSE) &&
      !memcmp(data, DTLS_SERVER_CMD_CLOSE, strlen(DTLS_SERVER_CMD_CLOSE))) {
    printf("server: closing connection\n");
    dtls_close(ctx, session);
    return len;
  } else if (len >= strlen(DTLS_SERVER_CMD_RENEGOTIATE) &&
      !memcmp(data, DTLS_SERVER_CMD_RENEGOTIATE, strlen(DTLS_SERVER_CMD_RENEGOTIATE))) {
    printf("server: renegotiate connection\n");
    dtls_renegotiate(ctx, session);
    return len;
  }

  return dtls_write(ctx, session, data, len);
}
示例#9
0
static eDtlsRet_t CAAdapterNetDtlsEncryptInternal(const stCADtlsAddrInfo_t *dstSession,
        uint8_t *data, uint32_t dataLen)
{
    OIC_LOG(DEBUG, NET_DTLS_TAG, "IN");

    VERIFY_NON_NULL_RET(dstSession, NET_DTLS_TAG, "Param dstSession is NULL" , DTLS_FAIL);
    VERIFY_NON_NULL_RET(data, NET_DTLS_TAG, "Param data is NULL" , DTLS_FAIL);

    if (0 == dataLen)
    {
        OIC_LOG(ERROR, NET_DTLS_TAG, "Given Packet length is equal to zero.");
        return DTLS_FAIL;
    }

    if (NULL == g_caDtlsContext)
    {
        OIC_LOG(ERROR, NET_DTLS_TAG, "Context is NULL");
        return DTLS_FAIL;
    }

    int retLen = dtls_write(g_caDtlsContext->dtlsContext, (session_t *)dstSession, data,
                                dataLen);
    OIC_LOG_V(DEBUG, NET_DTLS_TAG, "dtls_write retun len [%d]", retLen);
    if (retLen < 0)
    {
        OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
        return DTLS_FAIL;
    }
    if (0 == retLen)
    {
        // A new DTLS session was initiated by tinyDTLS library and wait for callback.
        return DTLS_SESSION_INITIATED;
    }
    else if (dataLen != (uint32_t)retLen)
    {
        OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT FAILURE");
        return DTLS_FAIL;
    }
    OIC_LOG(DEBUG, NET_DTLS_TAG, "OUT");
    return DTLS_OK;
}
示例#10
0
int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length, coap_protocol_t proto){
    if (connP->dtlsSession == NULL) {
        // no security
        if ( 0 != send_data(connP, buffer, length)) {
            return -1 ;
        }
    } else {
        if (DTLS_NAT_TIMEOUT > 0 && (lwm2m_gettime() - connP->lastSend) > DTLS_NAT_TIMEOUT)
        {
            // we need to rehandhake because our source IP/port probably changed for the server
            if ( connection_rehandshake(connP, false) != 0 )
            {
                printf("can't send due to rehandshake error\n");
                return -1;
            }
        }
        if (-1 == dtls_write(connP->dtlsContext, connP->dtlsSession, buffer, length)) {
            return -1;
        }
    }

    return 0;
}
示例#11
0
/* send a buffer to a session*/
static uint8_t prv_buffer_send(void * sessionH,
                               uint8_t * buffer,
                               size_t length,
                               void * userdata)
{
    printf("sending\n");
    connection_t * connP = (connection_t*) sessionH;

    if (connP == NULL)
    {
        printf("#> failed sending %lu bytes, missing connection\r\n", length);
        return COAP_500_INTERNAL_SERVER_ERROR;
    }

    printf("sending to %s\n",connP->ep.get_address());

    if (connP->dtlsSession == NULL)
	{
		printf("send NO_SEC datagram\n");
		if (-1 == udp.sendTo(connP->ep, (char*)buffer, length))
		{
			printf("send error\n");
			return COAP_500_INTERNAL_SERVER_ERROR;
		}
	} else {
		printf("send thru dtls\n");
		
		int res = dtls_write(dtls_context, connP->dtlsSession, (uint8 *)buffer, length);
		if (res != length)
		{
			printf("send dtls error: %d\n", res);
			return COAP_500_INTERNAL_SERVER_ERROR;
		}
	}
    return COAP_NO_ERROR;
}
示例#12
0
static int
get_key(struct dtls_context_t *ctx,
        const session_t *session,
        const unsigned char *id, size_t id_len,
        const dtls_key_t **result) {

  static const dtls_key_t psk = {
    .type = DTLS_KEY_PSK,
    .key.psk.id = (unsigned char *)DTLS_IDENTITY_HINT,
    .key.psk.id_length = DTLS_IDENTITY_HINT_LENGTH,
    .key.psk.key = (unsigned char *)DTLS_PSK_KEY,
    .key.psk.key_length = DTLS_PSK_KEY_LENGTH
  };

  *result = &psk;
  return 0;
}
#else
static int
get_psk_key(struct dtls_context_t *ctx,
            const session_t *session,
            const unsigned char *id, size_t id_len,
            const dtls_psk_key_t **result) {

  static const dtls_psk_key_t psk = {
    .id = (unsigned char *)DTLS_IDENTITY_HINT,
    .id_length = DTLS_IDENTITY_HINT_LENGTH,
    .key = (unsigned char *)DTLS_PSK_KEY,
    .key_length = DTLS_PSK_KEY_LENGTH
  };

  *result = &psk;
  return 0;
}
#endif
/*-----------------------------------------------------------------------------------*/
void
coap_init_communication_layer(uint16_t port)
{
  static dtls_handler_t cb = {
    .write = send_to_peer,
    .read  = read_from_peer,
    .event = NULL,
#if DTLS_VERSION_0_4_0
    .get_key = get_key
#else
    .get_psk_key = get_psk_key,
    .get_ecdsa_key = NULL,
    .verify_ecdsa_key = NULL
#endif
  };

  server_conn = udp_new(NULL, 0, NULL);
  udp_bind(server_conn, port);

  dtls_set_log_level(LOG_DEBUG);

  dtls_context = dtls_new_context(server_conn);
  if (dtls_context)
    dtls_set_handler(dtls_context, &cb);

  /* new connection with remote host */
  printf("COAP-DTLS listening on port %u\n", uip_ntohs(server_conn->lport));
}
/*-----------------------------------------------------------------------------------*/
static int
send_to_peer(struct dtls_context_t *ctx,
             session_t *session, uint8 *data, size_t len) {

  struct uip_udp_conn *conn = (struct uip_udp_conn *)dtls_get_app_data(ctx);

  uip_ipaddr_copy(&conn->ripaddr, &session->addr);
  conn->rport = session->port;

  uip_udp_packet_send(conn, data, len);

  /* Restore server connection to allow data from any node */
  memset(&conn->ripaddr, 0, sizeof(conn->ripaddr));
  memset(&conn->rport, 0, sizeof(conn->rport));

  return len;
}
/*-----------------------------------------------------------------------------------*/
void
coap_send_message(context_t * ctx, uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length)
{
  session_t session;

  dtls_session_init(&session);
  uip_ipaddr_copy(&session.addr, addr);
  session.port = port;

  dtls_write(ctx, &session, data, length);
}
/*-----------------------------------------------------------------------------------*/
static int
read_from_peer(struct dtls_context_t *ctx,
               session_t *session, uint8 *data, size_t len) {
  uip_len = len;
  memmove(uip_appdata, data, len);
  coap_receive(ctx);
  return 0;
}
/*-----------------------------------------------------------------------------------*/
void
coap_handle_receive()
{
  session_t session;

  if(uip_newdata()) {
    dtls_session_init(&session);
    uip_ipaddr_copy(&session.addr, &UIP_IP_BUF->srcipaddr);
    session.port = UIP_UDP_BUF->srcport;

    dtls_handle_message(dtls_context, &session, uip_appdata, uip_datalen());
  }
}