示例#1
0
static void start_daemon()
{
	// Capture USB device
	struct usb_sock_t *usb_sock = usb_open();
	if (usb_sock == NULL)
		goto cleanup_usb;

	// Capture a socket
	uint32_t desired_port = g_options.desired_port;
	struct tcp_sock_t *tcp_socket = tcp_open(desired_port);
	if (tcp_socket == NULL)
		goto cleanup_tcp;

	uint32_t real_port = tcp_port_number_get(tcp_socket);
	if (desired_port != 0 && desired_port != real_port) {
		ERR("Received port number did not match requested port number."
		    " The requested port number may be too high.");
		goto cleanup_tcp;
	}
	printf("%u\n", real_port);

	// Lose connection to caller
	if (!g_options.nofork_mode && fork() > 0)
		exit(0);

	for (;;) {
		struct service_thread_param *args = calloc(1, sizeof(*args));
		if (args == NULL) {
			ERR("Failed to alloc space for thread args");
			goto cleanup_thread;
		}

		args->usb_sock = usb_sock;
		args->tcp = tcp_conn_accept(tcp_socket);
		if (args->tcp == NULL) {
			ERR("Failed to open tcp connection");
			goto cleanup_thread;
		}

		int status = pthread_create(&args->thread_handle, NULL,
		                            &service_connection, args);
		if (status) {
			ERR("Failed to spawn thread, error %d", status);
			goto cleanup_thread;
		}

		continue;

	cleanup_thread:
		if (args != NULL) {
			if (args->tcp != NULL)
				tcp_conn_close(args->tcp);
			free(args);
		}
		break;
	}

cleanup_tcp:
	if (tcp_socket!= NULL)
		tcp_close(tcp_socket);
cleanup_usb:
	if (usb_sock != NULL)
		usb_close(usb_sock);
	return;
}
示例#2
0
文件: http.c 项目: lijie169/stm32-tcp
static void http_control_process(int soc_handler,char event,char *srcipaddr,unsigned short srcport,
                                     unsigned short data_index, unsigned short data_len) {
  int resp;

  switch(event) {
    case TCP_EVENT_CONN_REQ:
      if (debug_http)
        printf("Connection Request from IP: %d.%d.%d.%d - Port: %d\n",
	       *srcipaddr,*(srcipaddr+1),*(srcipaddr+2),*(srcipaddr+3),srcport);
      resp = tcp_conn_accept(soc_handler);
      if (resp == TCP_SOCKET_ERROR)
	  	printf("Accept Error: no free socket available\n");

      else 
	  {		
	   
        if (debug_http)
	  		printf("Socket %d created\n", resp);
	  
      }
      break;
    case TCP_EVENT_ESTABLISHED:
	
      if (debug_http)
	  	
        printf("Connection Established with IP: %d.%d.%d.%d - Port: %d\n",
	       *srcipaddr,*(srcipaddr+1),*(srcipaddr+2),*(srcipaddr+3),srcport);
		
      break;
    case TCP_EVENT_DATA:
      if (debug_http)
        printf("Event: Data Available from IP: %d.%d.%d.%d - Port: %d\n",
	       *srcipaddr,*(srcipaddr+1),*(srcipaddr+2),*(srcipaddr+3),srcport);
      resp = HTTP_process(data_index, data_len);	
      file_index[soc_handler-1] = resp;		
      if (debug_http_file) {
	if (resp == -2)
	  printf("Serving: 501 Command Not Implemented!\n");
    else if (resp == -1)
	  printf("Serving: 404 Page Not Found!\n");	
	else 
	  printf("Serving file %s\n", filesystem[resp].filename);
	  printf("Serving size %d\r\n", filesystem[resp].filesize);

	  
      }
      	if (tcp_send_data(soc_handler, create_content) == TCP_SOCKET_ERROR)
	 		 	printf("Error sending data: TCP already have data to send!\r\n");

      break;
    case TCP_EVENT_SEND_COMPLETED:
      tcp_conn_close(soc_handler);		
      break;
    case TCP_EVENT_CONN_CLOSED:
      tcp_socket_close(soc_handler);
      if (debug_http)
	  printf("Socket %d closed\n", soc_handler);

      break;
    case TCP_EVENT_RESET:
      if (debug_http)     
	  printf("Error: Socket %d was Reset\r\n", soc_handler);
		tcp_conn_close(soc_handler);
      break;
    case TCP_EVENT_rTOUT:
		tcp_conn_close(soc_handler);
	printf("Error: Socket %d Timed Out\r\n", soc_handler);
      break;
    case TCP_EVENT_cTOUT:
	  printf("Error: Connection in socket %d Timed Out\r\n", soc_handler);

      break;
    case TCP_EVENT_CLOSE_REQ:
		tcp_conn_close(soc_handler);
      break;  	  	    	
    default:
	  printf("Unknown Event: %d\n",event);
      break;
  }
}