Exemplo n.º 1
0
/** 
 * @brief Parse one line of TCP stats.
 * This is a callback function which is called for each line parsed by
 * parse_file_per_line() when parsing the <code>/proc/net/tcp6</code>.
 * A line of information from /proc/net/tcp6 is parsed and interested components
 * (src and dst addresses and ports, connection state and inode number) are
 * extracted. The connection information is then inserted to system with
 * insert_connection(). 
 * 
 * @param line Line read from file. 
 * @param ctx Pointer to the main context.
 */
static void parse_connection6_data( char *line, void *ctx ) 
{
        struct line_token *tokens_p;
        struct sockaddr_storage local_addr, remote_addr;
        int state;
#ifdef ENABLE_FOLLOW_PID
        int wanted[NROF_WANTED_TOKENS] = { 2,3,4,10 };
#else
        int wanted[NROF_WANTED_TOKENS] = { 2,3,4};
#endif /* ENABLE_FOLLOW_PID */
        struct line_token tokens[NROF_WANTED_TOKENS];
        struct parser_req req = {
                .interested_tokens = wanted,
                .interested_size = NROF_WANTED_TOKENS,
                .tokens = tokens,
                .token_count = NROF_WANTED_TOKENS
        };
#ifdef ENABLE_FOLLOW_PID 
        ino_t inode; 
#endif /* ENABLE_FOLLOW_PID */
        char addrbuf[INET6_ADDRSTRLEN];

        TRACE("Tokenizing\n" );
        tokens_p = tokenize( &req, line );
        TRACE("Done\n");

        TRACE( "Building connection \n" );

        if ( tokens_p == NULL ) {
                WARN( "Error in generating interesting tokens \n" );
                return;
        } 
                
        memset( &local_addr, 0, sizeof( local_addr ) );
        memset( &remote_addr, 0, sizeof( local_addr ) );

        TRACE( "token 1:(%d)*%s*\n", tokens_p->token_len, tokens_p->token );
        TRACE( "Decoding the local address \n" );
        if ( token_to_addr6( tokens_p, (struct sockaddr_storage *)&local_addr ) != 0 ) {
               WARN( "Error while parsing data, discarding connection! \n" );
               return;
        } 

        tokens_p = tokens_p->next;
        TRACE( "token 2:(%d)*%s*\n", tokens_p->token_len, tokens_p->token );
        TRACE( "Decoding the remote address \n" );
        if ( token_to_addr6( tokens_p, (struct sockaddr_storage *)&remote_addr ) != 0 ) {
               WARN( "Error while parsing data, discarding connection! \n" );
               return;
        } 

        tokens_p = tokens_p->next;
        TRACE( "token 3:(%d)*%s*\n", tokens_p->token_len, tokens_p->token );
        state = strtol( tokens_p->token, NULL, 16 );
        TRACE( "State %d \n", state ); 

#ifdef ENABLE_FOLLOW_PID
        tokens_p = tokens_p->next;
        TRACE( "token 4:(%d)*%s*\n", tokens_p->token_len, tokens_p->token );
        inode = strtol( tokens_p->token, NULL, 10 );
        TRACE( "Inode %d \n", inode );
#endif /* ENABLE_FOLLOW_PID */
        

        TRACE("Done\n" );
        if ( tokens_p->next != NULL ) {
                WARN( "Eccess elements in token structure \n" );
        }

        inet_ntop( local_addr.ss_family,
                        ss_get_addr6( &local_addr ),addrbuf, INET6_ADDRSTRLEN );
        DBG( "Read local IPV6 address %s \n", addrbuf );
        inet_ntop( remote_addr.ss_family,
                        ss_get_addr6( &remote_addr ), addrbuf, INET6_ADDRSTRLEN );
        DBG( "Read remote IPV6 address %s \n", addrbuf );


#ifdef ENABLE_FOLLOW_PID
        insert_connection( &local_addr, &remote_addr, state, inode, (struct stat_context *)ctx );
#else
        insert_connection( &local_addr, &remote_addr, state, (struct stat_context *)ctx );
#endif /* ENABLE_FOLLOW_PID */

}
STATUS insert_dispatcher_connection(int socket)
{
  return insert_connection(_connections, socket);
}