コード例 #1
0
ファイル: ippusbxd.c プロジェクト: jhcloos/ippusbxd-gpl3
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
ファイル: ippusbxd.c プロジェクト: jhcloos/ippusbxd-gpl3
static void *service_connection(void *arg_void)
{
	struct service_thread_param *arg =
		(struct service_thread_param *)arg_void;

	// clasify priority
	while (!arg->tcp->is_closed) {
		struct usb_conn_t *usb = NULL;
		struct http_message_t *server_msg = NULL;
		struct http_message_t *client_msg = NULL;

		// Client's request
		NOTE("Client msg starting");
		client_msg = http_message_new();
		if (client_msg == NULL) {
			ERR("Failed to create message");
			break;
		}

		while (!client_msg->is_completed) {
			struct http_packet_t *pkt;
			pkt = tcp_packet_get(arg->tcp, client_msg);
			if (pkt == NULL) {
				if (arg->tcp->is_closed) {
					NOTE("Client closed connection\n");
					goto cleanup_subconn;
				}
				ERR_AND_EXIT("Got null packet from tcp");
			}
			if (usb == NULL) {
				usb = usb_conn_aquire(arg->usb_sock, 1);
				if (usb == NULL) {
					ERR("Failed to aquire usb interface");
					packet_free(pkt);
					goto cleanup_subconn;
				}
				NOTE("Interface #%d: aquired usb conn",
						usb->interface_index);
			}

			//NOTE("%.*s", (int)pkt->filled_size, pkt->buffer);
			usb_conn_packet_send(usb, pkt);
			packet_free(pkt);
		}
		message_free(client_msg);
		client_msg = NULL;
		NOTE("Interface #%d: Client msg completed\n",
				usb->interface_index);


		// Server's responce
		NOTE("Interface #%d: Server msg starting",
				usb->interface_index);
		server_msg = http_message_new();
		if (server_msg == NULL) {
			ERR("Failed to create message");
			goto cleanup_subconn;
		}
		while (!server_msg->is_completed) {
			struct http_packet_t *pkt;
			pkt = usb_conn_packet_get(usb, server_msg);
			if (pkt == NULL)
				break;

			NOTE("Pkt from usb: \n%.*s",
					(int)pkt->filled_size, pkt->buffer);
			tcp_packet_send(arg->tcp, pkt);
			packet_free(pkt);
			NOTE("Interface #%d: Server pkt done",
					usb->interface_index);
		}
		NOTE("Interface #%d: Server msg completed\n",
				usb->interface_index);

cleanup_subconn:
		if (client_msg != NULL)
			message_free(client_msg);
		if (server_msg != NULL)
			message_free(server_msg);
		if (usb != NULL)
			usb_conn_release(usb);
	}



	tcp_conn_close(arg->tcp);
	free(arg);
	return NULL;
}
コード例 #3
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;
  }
}