コード例 #1
0
ファイル: stk500v2.c プロジェクト: GBert/openwrt-files
/*******************************************************************************
 *
 * Open interface
 */
int16_t
stk500v2_open(const char *interface, int port, int flag)
{
	if (interface[0] == '/') {
		stk_fsock = serial_open(interface, serial_speed(p.baudrate));
	} else {
		if (flag == STK_CONNECT) {
			stk_fsock = ip_connect(interface, port);
		} else { /* STK _LISTEN */
			stk_fsock = ip_listen(interface, port);
		}
	}
	return stk_fsock;
}
コード例 #2
0
ファイル: hypervisor.c プロジェクト: andrewshadura/dynamips
/* Hypervisor TCP server */
int hypervisor_tcp_server(char *ip_addr,int tcp_port)
{
   int fd_array[HYPERVISOR_MAX_FD];
   struct sockaddr_storage remote_addr;
   socklen_t remote_len;
   int i,res,clnt,fd_count,fd_max;
   struct timeval tv;
   fd_set fds;

   /* Initialize all hypervisor modules */
   hypervisor_init();
   hypervisor_nio_init();
   hypervisor_nio_bridge_init();
   hypervisor_frsw_init();
   hypervisor_atmsw_init();
   hypervisor_atm_bridge_init();
   hypervisor_ethsw_init();
   hypervisor_vm_init();
   hypervisor_vm_debug_init();
   hypervisor_store_init();

   signal(SIGPIPE,sigpipe_handler);

   if (!tcp_port)
      tcp_port = HYPERVISOR_TCP_PORT;

   fd_count = ip_listen(ip_addr,tcp_port,SOCK_STREAM,
                        HYPERVISOR_MAX_FD,fd_array);

   if (fd_count <= 0) {
      fprintf(stderr,"Hypervisor: unable to create TCP sockets.\n");
      return(-1);
   }

   /* Start accepting connections */
   m_log("HYPERVISOR","Release %s/%s (tag %s)\n",
         sw_version,os_name,sw_version_tag);
         
    if (ip_addr != NULL) {
        binding_addr = ip_addr;
        m_log("HYPERVISOR","Started on IP = %s, TCP port = %d.\n", ip_addr, tcp_port);
        printf("Hypervisor TCP control server started (IP %s port %d).\n", ip_addr, tcp_port);
    }
    else {
        m_log("HYPERVISOR","Started on TCP port = %d.\n",tcp_port);
        printf("Hypervisor TCP control server started (port %d).\n",tcp_port);
   }
   hypervisor_running = TRUE;

   while(hypervisor_running) {
      FD_ZERO(&fds);
      fd_max = -1;

      for(i=0;i<fd_count;i++)
         if (fd_array[i] != -1) {
            FD_SET(fd_array[i],&fds);
            if (fd_array[i] > fd_max)
               fd_max = fd_array[i];
         }

      /* Wait for incoming connections */
      tv.tv_sec  = 0;
      tv.tv_usec = 500 * 1000;  /* 500 ms */
      res = select(fd_max+1,&fds,NULL,NULL,&tv);

      if (res == -1) {
         if (errno == EINTR)
            continue;
         else
            perror("hypervisor_tcp_server: select");
      }

      /* Accept connections on signaled sockets */
      for(i=0;i<fd_count;i++) {
         if (fd_array[i] == -1)
            continue;
         
         if (!FD_ISSET(fd_array[i],&fds))
            continue;

         remote_len = sizeof(remote_addr);
         clnt = accept(fd_array[i],(struct sockaddr *)&remote_addr,
                       &remote_len);

         if (clnt < 0) {
            perror("hypervisor_tcp_server: accept");
            continue;
         }
            
         /* create a new connection and start a thread to handle it */
         if (!hypervisor_create_conn(clnt)) {
            fprintf(stderr,"hypervisor_tcp_server: unable to create new "
                    "connection for FD %d\n",clnt);
            close(clnt);
         }
      }

      /* Walk through the connection list to eliminate dead connections */
      hypervisor_close_conn_list(TRUE);
   }   

   /* Close all control sockets */
   printf("Hypervisor: closing control sockets.\n");
   for(i=0;i<fd_count;i++) {
      if (fd_array[i] != -1) {
         shutdown(fd_array[i],2);
         close(fd_array[i]);
      }
   }

   /* Close all remote client connections */
   printf("Hypervisor: closing remote client connections.\n");
   hypervisor_close_conn_list(FALSE);

   m_log("HYPERVISOR","Stopped.\n");
   return(0);
}