Changeset 1767


Ignore:
Timestamp:
Feb 2, 2008 8:22:20 AM (16 years ago)
Author:
bennylp
Message:

Added trace to PCAP reader

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/src/pjlib-util/pcap.c

    r1766 r1767  
    2121#include <pj/errno.h> 
    2222#include <pj/file_io.h> 
     23#include <pj/log.h> 
    2324#include <pj/pool.h> 
    2425#include <pj/sock.h> 
    2526#include <pj/string.h> 
    2627 
     28#if 0 
     29#   define TRACE_(x)    PJ_LOG(5,x) 
     30#else 
     31#   define TRACE_(x) 
     32#endif 
     33 
     34 
     35#pragma pack(1) 
    2736 
    2837typedef struct pj_pcap_hdr  
     
    4554} pj_pcap_rec_hdr; 
    4655 
     56#if 0 
    4757typedef struct pj_pcap_eth_hdr  
    4858{ 
    4959    pj_uint8_t  dest[6]; 
    5060    pj_uint8_t  src[6]; 
    51     pj_uint16_t len; 
     61    pj_uint8_t  len[2]; /* problem with struct size if pj_uint16_t */ 
    5262} pj_pcap_eth_hdr; 
     63#else 
     64typedef pj_uint8_t pj_pcap_eth_hdr[14]; 
     65#endif 
    5366 
    5467typedef struct pj_pcap_ip_hdr  
     
    7487} pj_pcap_udp_hdr; 
    7588 
     89#pragma pack() 
    7690 
    7791/* Implementation of pcap file */ 
    7892struct pj_pcap_file 
    7993{ 
     94    char            obj_name[PJ_MAX_OBJ_NAME]; 
    8095    pj_oshandle_t   fd; 
    8196    pj_bool_t       swap; 
     
    101116    PJ_ASSERT_RETURN(pool && path && p_file, PJ_EINVAL); 
    102117 
     118    /* More sanity checks */ 
     119    TRACE_(("pcap", "sizeof(pj_pcap_eth_hdr)=%d", 
     120            sizeof(pj_pcap_eth_hdr))); 
     121    PJ_ASSERT_RETURN(sizeof(pj_pcap_eth_hdr)==14, PJ_EBUG); 
     122    TRACE_(("pcap", "sizeof(pj_pcap_ip_hdr)=%d", 
     123            sizeof(pj_pcap_ip_hdr))); 
     124    PJ_ASSERT_RETURN(sizeof(pj_pcap_ip_hdr)==20, PJ_EBUG); 
     125    TRACE_(("pcap", "sizeof(pj_pcap_udp_hdr)=%d", 
     126            sizeof(pj_pcap_udp_hdr))); 
     127    PJ_ASSERT_RETURN(sizeof(pj_pcap_udp_hdr)==8, PJ_EBUG); 
     128     
    103129    file = PJ_POOL_ZALLOC_T(pool, pj_pcap_file); 
     130 
     131    pj_ansi_strcpy(file->obj_name, "pcap"); 
    104132 
    105133    status = pj_file_open(pool, path, PJ_O_RDONLY, &file->fd); 
     
    127155    } 
    128156 
     157    TRACE_((file->obj_name, "PCAP file %s opened", path)); 
     158     
    129159    *p_file = file; 
    130160    return PJ_SUCCESS; 
     
    135165{ 
    136166    PJ_ASSERT_RETURN(file, PJ_EINVAL); 
     167    TRACE_((file->obj_name, "PCAP file closed")); 
    137168    return pj_file_close(file->fd); 
    138169} 
     
    208239        pj_status_t status; 
    209240 
     241        TRACE_((file->obj_name, "Reading packet..")); 
     242 
    210243        /* Read PCAP packet header */ 
    211244        sz = sizeof(tmp.rec); 
    212245        status = read_file(file, &tmp.rec, &sz); 
    213         if (status != PJ_SUCCESS)  
     246        if (status != PJ_SUCCESS) { 
     247            TRACE_((file->obj_name, "read_file() error: %d", status)); 
    214248            return status; 
     249        } 
    215250 
    216251        rec_incl = tmp.rec.incl_len; 
     
    231266            break; 
    232267        default: 
     268            TRACE_((file->obj_name, "Error: link layer not Ethernet")); 
    233269            return PJ_ENOTSUP; 
    234270        } 
    235271 
    236         if (status != PJ_SUCCESS) 
     272        if (status != PJ_SUCCESS) { 
     273            TRACE_((file->obj_name, "Error reading Eth header: %d", status)); 
    237274            return status; 
     275        } 
    238276 
    239277        sz_read += sz; 
     
    242280        sz = sizeof(tmp.ip); 
    243281        status = read_file(file, &tmp.ip, &sz); 
    244         if (status != PJ_SUCCESS) 
     282        if (status != PJ_SUCCESS) { 
     283            TRACE_((file->obj_name, "Error reading IP header: %d", status)); 
    245284            return status; 
     285        } 
    246286 
    247287        sz_read += sz; 
     
    249289        /* Skip if IP source mismatch */ 
    250290        if (file->filter.ip_src && tmp.ip.ip_src != file->filter.ip_src) { 
     291            TRACE_((file->obj_name, "IP source %s mismatch, skipping",  
     292                    pj_inet_ntoa(*(pj_in_addr*)&tmp.ip.ip_src))); 
    251293            SKIP_PKT(); 
    252294            continue; 
     
    255297        /* Skip if IP destination mismatch */ 
    256298        if (file->filter.ip_dst && tmp.ip.ip_dst != file->filter.ip_dst) { 
     299            TRACE_((file->obj_name, "IP detination %s mismatch, skipping",  
     300                    pj_inet_ntoa(*(pj_in_addr*)&tmp.ip.ip_dst))); 
    257301            SKIP_PKT(); 
    258302            continue; 
     
    261305        /* Skip if proto mismatch */ 
    262306        if (file->filter.proto && tmp.ip.proto != file->filter.proto) { 
     307            TRACE_((file->obj_name, "IP proto %d mismatch, skipping",  
     308                    tmp.ip.proto)); 
    263309            SKIP_PKT(); 
    264310            continue; 
     
    270316            sz = sizeof(tmp.udp); 
    271317            status = read_file(file, &tmp.udp, &sz); 
    272             if (status != PJ_SUCCESS) 
     318            if (status != PJ_SUCCESS) { 
     319                TRACE_((file->obj_name, "Error reading UDP header: %d",status)); 
    273320                return status; 
     321            } 
    274322 
    275323            sz_read += sz; 
     
    279327                tmp.udp.src_port != file->filter.src_port)  
    280328            { 
     329                TRACE_((file->obj_name, "UDP src port %d mismatch, skipping",  
     330                        pj_ntohs(tmp.udp.src_port))); 
    281331                SKIP_PKT(); 
    282332                continue; 
     
    287337                tmp.udp.dst_port != file->filter.dst_port)  
    288338            { 
     339                TRACE_((file->obj_name, "UDP dst port %d mismatch, skipping",  
     340                        pj_ntohs(tmp.udp.dst_port))); 
    289341                SKIP_PKT(); 
    290342                continue; 
     
    295347            break; 
    296348        default: 
     349            TRACE_((file->obj_name, "Not UDP, skipping")); 
    297350            SKIP_PKT(); 
    298351            continue; 
     
    301354        /* Check if payload fits the buffer */ 
    302355        if (sz > (pj_ssize_t)*udp_payload_size) { 
     356            TRACE_((file->obj_name,  
     357                    "Error: packet too large (%d bytes required)", sz)); 
    303358            SKIP_PKT(); 
    304359            return PJ_ETOOSMALL; 
     
    307362        /* Read the payload */ 
    308363        status = read_file(file, udp_payload, &sz); 
    309         if (status != PJ_SUCCESS) 
     364        if (status != PJ_SUCCESS) { 
     365            TRACE_((file->obj_name, "Error reading payload: %d", status)); 
    310366            return status; 
     367        } 
    311368 
    312369        sz_read += sz; 
Note: See TracChangeset for help on using the changeset viewer.