void p2p_main (int a1) {
  some_initialization();
  int intf_used[32], intf_listening[32], intf_active[32], intf_ips[32],
      intf_pvt_subnet[32], intf_netmasks;
  int var_tcp_server_tds[32], var_udp_server_tds[32], fix_tcp_server_tds[32],
      fix_udp_server_tds[32];

  /* start Internet Time Thread and P2P client threads */
  t1 = Global_alloc_Then_Create_Thread(InternetTime_thread, 0, 0);
  t2 = Global_alloc_Then_Create_Thread(TCP_client_scan_thread, 1, 0);
  t3 = Global_alloc_Then_Create_Thread(UDP_client_scan_thread, 1, 0);
  t4 = Global_alloc_Then_Create_Thread(TCP_client_scan_thread, 0, 0);
  t5 = Global_alloc_Then_Create_Thread(UDP_client_scan_thread, 0, 0);
  epoch = get_epoch_week();

  while ( 1 ) {
    if ( call_WaitForSingleObject(v4, 5000,  r) )
        goto CLOSE_HANDLES_AND_EXIT;
    build_interface_list();
   
CHECK_TIME:      
    temp = get_epoch_week();
    if ( temp != epoch ) {
      epoch = temp;

      for (i=0; i < 32; i++) {
        if (intf_used[i] && intf_active[i] && intf_listening[i]) {
           ipaddr = intf_ips[i];
           
           /* close old server threads, recompute variable ports and restart threads */
           call_WaitForSingleObject_and_CloseHandle(&var_tcp_server_tds[i]);
           call_WaitForSingleObject_and_CloseHandle(&var_udp_server_tds[i]);
           portgen(ipaddr, (int)&ports, epoch);
           var_tcp_server_tds[i] =
             (int)Global_alloc_Then_Create_Thread(TCP_server_thread, ipaddr, ports[3]);
           var_udp_server_tds[i] =
             (int)Global_alloc_Then_Create_Thread(UDP_server_thread, ipaddr, ports[4]);
        }
      }
    }

    EnterCriticalSection(&CriticalSection);
    for (i = 0; i < 32; i++) {
       if (intf_used[i] && !intf_active [i]  && !intf_listening[i]) {
          intf_used[i] = intf_ips[i] = intf_netmasks[i]= 0;
          intf_is_private_subnet[i] = 0;
       }     
    }
    LeaveCriticalSection(lpCriticalSection);
  }

  for(i=0; i < 32 ; i++){
    if (intf_used[i] ) {
      if (!intf_active[i]) {
         if (!intf_listening[i]) continue;
           intf_listening[i] = 0;
           call_WaitForSingleObject_and_CloseHandle(&fix_tcp_server_tds[i]);
           call_WaitForSingleObject_and_CloseHandle(&fix_udp_server_tds[i]);
           call_WaitForSingleObject_and_CloseHandle(&var_tcp_server_tds[i]);
           call_WaitForSingleObject_and_CloseHandle(&var_udp_server_tds[i]);     
         }
         else if (!intf_listening[i]) {   
           intf_listening[i] = 1;
           ipaddr = intf_ips[i];
           portgen(ipaddr, (int)&ports, epoch);
     
           /* start servers on fixed ports */
           fix_tcp_server_tds[i] = Global_alloc_Then_Create_Thread(TCP_server_thread
                                 ipaddr, ports[1]); 
           fix_udp_server_tds[i] = Global_alloc_Then_Create_Thread(UDP_server_thread,
                                 ipaddr, ports[2]);
         
           /* start servers on variable ports */                      
           var_tcp_server_tds[i] = Global_alloc_Then_Create_Thread(TCP_server_thread,
                                 ipaddr, ports[3]);
           var_udp_server_tds[i] = Global_alloc_Then_Create_Thread(UDP_server_thread,
                                 ipaddr, ports[4]);
         }
      }
  }
  goto CHECK_TIME;       

CLOSE_HANDLES_AND_EXIT:
  /* close client threads t1...t5 */
  call_WaitForSingleObject_and_CloseHandle(&t1 ... &t5); 
  /* close active server threads on all interfaces*/
  for(i=0; i < 32; i++ ) {
    if (intf_used[i] && intf_listening[i]) {
         call_WaitForSingleObject_and_CloseHandle(&fix_tcp_server_tds[i]);
         call_WaitForSingleObject_and_CloseHandle(&fix_udp_server_tds[i]);
         call_WaitForSingleObject_and_CloseHandle(&var_tcp_server_tds[i]);
         call_WaitForSingleObject_and_CloseHandle(&var_udp_server_tds[i]);
    }
  }
  ExitThread(0);
}

SOURCE LISTING 2:  P2P_Main starts the P2P service



 


 







Acknowledements

  This material is based upon work supported through the U.S. Army Research Office under the Cyber-TA Research Grant No. W911NF-06-1- 0316 and by the National Science Foundation, Grant No. CNS-07-16 612. The views expressed in this document are those of the authors and do not necessarily represent the official position of the sponsors.