/** * Copies the received data and (if appropriate) parses it and sends back a * response to the client. This method will not block as long as client->fd has * data waiting to be read. Otherwise, it will block until one read can be * completed. * * @return non-zero if client is still alive */ int cli_client_handle_request( cli_client_t* client ) { int ret; int ret_grow; /* search for \n and block until it is found (or socket is closed) */ ret = read_search( client->fd, "\n", &client->state, 1 ); /* add terminating NUL so we can use strchr on the valid part of the buffer */ ret_grow = search_state_grow_if_full( &client->state ); if( ret_grow==2 || ret_grow==-1 ) ret = -2; else client->state.chbuf[ client->state.used ] = '\0'; /* handle the result of reading from the socket */ switch( ret ) { case -3: /* data received but no newline yet */ return 1; case -2: /* data received but ran out of buffer space */ return 0; case -1: /* read error */ return 0; case 1: true_or_die( strchr( client->state.chbuf, '\n' ) == NULL, "Error: Unexpected newline found after EOF received" ); /* EOF received */ return 0; case 0: /* tell the parser to execute the client's command(s) */ pthread_mutex_lock( &parser_lock ); cli_focus_set( client->fd, &client->verbose ); int b = cli_parser_handle_client( client ); pthread_mutex_unlock( &parser_lock ); return b; default: die( "Error: invalid return in cli_client_handle_request" ); return 0; /* to make the compiler happy */ } }
int sr_cpu_init_intf_socket( int interface_index ) { true_or_die( interface_index>=0 && interface_index<=3, "Error: unexpected interface_index: %u", interface_index ); char iface_name[32]; sprintf( iface_name, "nf%u", interface_index ); int s = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ALL) ); if( s == -1 ) { perror( "Socket creation failed" ); die( "Error: socket creation failed" ); } else debug_println( "got socket to intf %d", s ); struct ifreq ifr; bzero( &ifr, sizeof(struct ifreq) ); strncpy( ifr.ifr_ifrn.ifrn_name, iface_name, IFNAMSIZ ); //strncpy( ifr.ifr_ifrn.ifrn_name, "nf0", IFNAMSIZ ); if( ioctl(s, SIOCGIFINDEX, &ifr) < 0 ) { perror("ioctl SIOCGIFINDEX"); die( "Error: ioctl SIOCGIFINDEX failed" ); } struct sockaddr_ll saddr; bzero( &saddr, sizeof(struct sockaddr_ll) ); saddr.sll_family = AF_PACKET; saddr.sll_protocol = htons(ETH_P_ALL); saddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue; if (bind(s, (struct sockaddr*)(&saddr), sizeof(saddr)) < 0) { perror( "bind error" ); die( "Error: bind failed" ); } debug_println( "Connected to hardware interface %s", iface_name ); return s; }