示例#1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udp_server_process, ev, data)
{
  PROCESS_BEGIN();

  print_local_addresses();

  dtls_init();
  init_dtls();

  if (!dtls_context) {
    dsrv_log(LOG_EMERG, "cannot create context\n");
    PROCESS_EXIT();
  }

  while(1) {
    PROCESS_WAIT_EVENT();
    if(ev == tcpip_event) {
      dtls_handle_read(dtls_context);
    }
#if 0
    if (bytes_read > 0) {
      /* dtls_handle_message(dtls_context, &the_session, readbuf, bytes_read); */
      read_from_peer(dtls_context, &the_session, readbuf, bytes_read);
    }
    dtls_handle_message(ctx, &session, uip_appdata, bytes_read);
#endif
  }

  PROCESS_END();
}
示例#2
0
文件: bt_connect.c 项目: sdini1/bt
//send the handshake to all our peers
void handshake_all(bt_args_t *args){
  int i;
  int sockfd;
  struct sockaddr_in sockaddr, handshake_addr;
  bt_msg_t msg; //message structure
  char *fname = args->bt_info->name;
  char *ip;
  int port;
  char init_stats[80];
  for (i=0;i<MAX_CONNECTIONS;i++){
    if (args->peers[i]){
      //initiate socket and send out handshake
      //keep track of the sockfd in the args struct
      if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket");
      }

      sockaddr = args->peers[i]->sockaddr;
      handshake_addr = args->peers[i]->sockaddr;

      if (connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr)) < 0){
        perror("connect");
        continue;
      }
      
      args->peers[i]->sockfd = sockfd; //backup the socket 
      ip = inet_ntoa(handshake_addr.sin_addr);
      port = ntohs(handshake_addr.sin_port);
      sprintf(init_stats, "HANDSHAKE INIT to peer: %s on port: %d\n", ip, port);
      LOGGER(args->log_file, 1, init_stats);
      if (seeder_handshake(sockfd, fname, args->id, handshake_addr)){ 
        sprintf(init_stats, "HANDSHAKE SUCCESS from peer: %s on port: %d\n", ip, port);
        LOGGER(args->log_file, 1, init_stats);
        args->sockets[i] = sockfd;
        args->poll_sockets[i].fd = sockfd;
        args->poll_sockets[i].events = POLLIN;
        
        //send our bitfield here
        msg.length = sizeof(bt_bitfield_t);
        msg.bt_type = BT_BITFIELD;
        msg.payload.bitfield = args->bitfield;
        send_to_peer(args->peers[i], &msg); //send out the message
        read_from_peer(args->peers[i], &msg, args); //listen for bitfield
        unchoke_message(&msg);
        send_to_peer(args->peers[i], &msg); //send out the unchoke message
        
      }
      else{
        sprintf(init_stats, "HANDSHAKE DECLINED from peer: %s on port: %d\n", ip, port);
        LOGGER(args->log_file, 1, init_stats);

      }
    }
  }
}