示例#1
0
static void
timeout_handler(void)
{
  static int seq_id;
  char buf[MAX_PAYLOAD_LEN];
  int i;
  uip_ipaddr_t *globaladdr = NULL;
  uip_ipaddr_t newdest_addr;
  uint16_t dest_port = use_user_dest_addr ? user_dest_port : 3000;
  int has_dest=0;

  if ( use_user_dest_addr ) {
	uip_ipaddr_copy(&newdest_addr, &user_dest_addr);
	has_dest=1;
  } else if((globaladdr = &uip_ds6_get_global(-1)->ipaddr) != NULL) {
#if UIP_CONF_IPV6_RPL
    rpl_dag_t *dag = rpl_get_any_dag();
    uip_ipaddr_copy(&newdest_addr, globaladdr);
    memcpy(&newdest_addr.u8[8], &dag->dag_id.u8[8], sizeof(uip_ipaddr_t) / 2);
    has_dest = dag == NULL ? 0 : 1;
#else
    uip_ipaddr_t * defrt = uip_ds6_defrt_choose();
    if ( defrt != NULL ) {
      uip_ipaddr_copy(&newdest_addr, defrt);
      has_dest=1;
    }
#endif
  }

  if (client_conn == NULL) {
    if (has_dest) {
        /* At least one prefix announced : building of the global address to reach using prefixes */
        memcpy(dest_addr, &newdest_addr, sizeof(uip_ipaddr_t));
        PRINTF("UDP-CLIENT: address destination: ");
        PRINT6ADDR(dest_addr);
        PRINTF("\n");
        client_conn = udp_new(dest_addr, UIP_HTONS(dest_port), NULL);

        udp_bind(client_conn, UIP_HTONS(3001));
		PRINTF("Created a connection with the server ");
		PRINT6ADDR(&client_conn->ripaddr);
		PRINTF(" local/remote port %u/%u\n",
		UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
		print_local_addresses();
    } else {
      PRINTF("No address configured yet\n");
    }
  }
  if (client_conn != NULL) {
    if(memcmp(&client_conn->ripaddr, &newdest_addr, sizeof(uip_ipaddr_t)) != 0) {
      PRINTF("UPD-CLIENT : new address, connection changed\n");
      memcpy(dest_addr, &newdest_addr, sizeof(uip_ipaddr_t));
      client_conn = udp_new(dest_addr, UIP_HTONS(3000), NULL);
      udp_bind(client_conn, UIP_HTONS(3001));
      PRINTF("Created a connection with the server ");
      PRINT6ADDR(&client_conn->ripaddr);
      PRINTF(" local/remote port %u/%u\n",
      UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
      print_local_addresses();
    }
    if ( udp_client_run ) {
      PRINTF("Client sending to: ");
      PRINT6ADDR(&client_conn->ripaddr);
      i = sprintf(buf, "%d | ", ++seq_id);
#if UIP_CONF_IPV6_RPL
      rpl_dag_t *dag = rpl_get_any_dag();
      add_ipaddr(buf + i, &dag->instance->def_route->ipaddr);
#endif
      PRINTF(" (msg: %s)\n", buf);
      #if SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION
      uip_udp_packet_send(client_conn, buf, UIP_APPDATA_SIZE);
      #else /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
      uip_udp_packet_send(client_conn, buf, strlen(buf));
      #endif /* SEND_TOO_LARGE_PACKET_TO_TEST_FRAGMENTATION */
    }
  }
}
示例#2
0
/*---------------------------------------------------------------------------*/
static void
timeout_handler(void)
{
  static int seq_id;
  char buf[MAX_PAYLOAD_LEN];
  int i;
  uip_ip6addr_t *globaladdr = NULL;
  uint16_t dest_port = CETIC_6LBR_NODE_INFO_PORT;
  int has_dest = 0;
  rpl_dag_t *dag;

  uip_ds6_addr_t *addr_desc = uip_ds6_get_global(ADDR_PREFERRED);
  if(addr_desc != NULL) {
    globaladdr = &addr_desc->ipaddr;
    dag = rpl_get_any_dag();
    if(dag) {
      uip_ipaddr_copy(&dest_addr, globaladdr);
      memcpy(&dest_addr.u8[8], &dag->dag_id.u8[8], sizeof(uip_ipaddr_t) / 2);
      has_dest = 1;
    }
  }

  if(has_dest) {
    if(client_conn == NULL) {
      PRINTF("UDP-CLIENT: address destination: ");
      PRINT6ADDR(&dest_addr);
      PRINTF("\n");
      client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);

      if(client_conn != NULL) {
        PRINTF("Created a connection with the server ");
        PRINT6ADDR(&client_conn->ripaddr);
        PRINTF(" local/remote port %u/%u\n",
               UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
      } else {
        PRINTF("Could not open connection\n");
      }
    } else {
      if(memcmp(&client_conn->ripaddr, &dest_addr, sizeof(uip_ipaddr_t)) != 0) {
        PRINTF("UDP-CLIENT: new address destination: ");
        PRINT6ADDR(&dest_addr);
        PRINTF("\n");
        uip_udp_remove(client_conn);
        client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);
        if(client_conn != NULL) {
          PRINTF("Created a connection with the server ");
          PRINT6ADDR(&client_conn->ripaddr);
          PRINTF(" local/remote port %u/%u\n",
                 UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
        } else {
          PRINTF("Could not open connection\n");
        }
      }
    }
    if(client_conn != NULL) {
      PRINTF("Client sending to: ");
      PRINT6ADDR(&client_conn->ripaddr);
      i = sprintf(buf, "%d | ", ++seq_id);
      dag = rpl_get_any_dag();
      if(dag && dag->instance->def_route) {
        add_ipaddr(buf + i, &dag->instance->def_route->ipaddr);
      } else {
        sprintf(buf + i, "(null)");
      }
      PRINTF(" (msg: %s)\n", buf);
      uip_udp_packet_send(client_conn, buf, strlen(buf));
    } else {
      PRINTF("No connection created\n");
    }
  } else {
    PRINTF("No address configured\n");
  }
}