Changeset 1767
- Timestamp:
- Feb 2, 2008 8:22:20 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib-util/src/pjlib-util/pcap.c
r1766 r1767 21 21 #include <pj/errno.h> 22 22 #include <pj/file_io.h> 23 #include <pj/log.h> 23 24 #include <pj/pool.h> 24 25 #include <pj/sock.h> 25 26 #include <pj/string.h> 26 27 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) 27 36 28 37 typedef struct pj_pcap_hdr … … 45 54 } pj_pcap_rec_hdr; 46 55 56 #if 0 47 57 typedef struct pj_pcap_eth_hdr 48 58 { 49 59 pj_uint8_t dest[6]; 50 60 pj_uint8_t src[6]; 51 pj_uint 16_t len;61 pj_uint8_t len[2]; /* problem with struct size if pj_uint16_t */ 52 62 } pj_pcap_eth_hdr; 63 #else 64 typedef pj_uint8_t pj_pcap_eth_hdr[14]; 65 #endif 53 66 54 67 typedef struct pj_pcap_ip_hdr … … 74 87 } pj_pcap_udp_hdr; 75 88 89 #pragma pack() 76 90 77 91 /* Implementation of pcap file */ 78 92 struct pj_pcap_file 79 93 { 94 char obj_name[PJ_MAX_OBJ_NAME]; 80 95 pj_oshandle_t fd; 81 96 pj_bool_t swap; … … 101 116 PJ_ASSERT_RETURN(pool && path && p_file, PJ_EINVAL); 102 117 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 103 129 file = PJ_POOL_ZALLOC_T(pool, pj_pcap_file); 130 131 pj_ansi_strcpy(file->obj_name, "pcap"); 104 132 105 133 status = pj_file_open(pool, path, PJ_O_RDONLY, &file->fd); … … 127 155 } 128 156 157 TRACE_((file->obj_name, "PCAP file %s opened", path)); 158 129 159 *p_file = file; 130 160 return PJ_SUCCESS; … … 135 165 { 136 166 PJ_ASSERT_RETURN(file, PJ_EINVAL); 167 TRACE_((file->obj_name, "PCAP file closed")); 137 168 return pj_file_close(file->fd); 138 169 } … … 208 239 pj_status_t status; 209 240 241 TRACE_((file->obj_name, "Reading packet..")); 242 210 243 /* Read PCAP packet header */ 211 244 sz = sizeof(tmp.rec); 212 245 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)); 214 248 return status; 249 } 215 250 216 251 rec_incl = tmp.rec.incl_len; … … 231 266 break; 232 267 default: 268 TRACE_((file->obj_name, "Error: link layer not Ethernet")); 233 269 return PJ_ENOTSUP; 234 270 } 235 271 236 if (status != PJ_SUCCESS) 272 if (status != PJ_SUCCESS) { 273 TRACE_((file->obj_name, "Error reading Eth header: %d", status)); 237 274 return status; 275 } 238 276 239 277 sz_read += sz; … … 242 280 sz = sizeof(tmp.ip); 243 281 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)); 245 284 return status; 285 } 246 286 247 287 sz_read += sz; … … 249 289 /* Skip if IP source mismatch */ 250 290 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))); 251 293 SKIP_PKT(); 252 294 continue; … … 255 297 /* Skip if IP destination mismatch */ 256 298 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))); 257 301 SKIP_PKT(); 258 302 continue; … … 261 305 /* Skip if proto mismatch */ 262 306 if (file->filter.proto && tmp.ip.proto != file->filter.proto) { 307 TRACE_((file->obj_name, "IP proto %d mismatch, skipping", 308 tmp.ip.proto)); 263 309 SKIP_PKT(); 264 310 continue; … … 270 316 sz = sizeof(tmp.udp); 271 317 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)); 273 320 return status; 321 } 274 322 275 323 sz_read += sz; … … 279 327 tmp.udp.src_port != file->filter.src_port) 280 328 { 329 TRACE_((file->obj_name, "UDP src port %d mismatch, skipping", 330 pj_ntohs(tmp.udp.src_port))); 281 331 SKIP_PKT(); 282 332 continue; … … 287 337 tmp.udp.dst_port != file->filter.dst_port) 288 338 { 339 TRACE_((file->obj_name, "UDP dst port %d mismatch, skipping", 340 pj_ntohs(tmp.udp.dst_port))); 289 341 SKIP_PKT(); 290 342 continue; … … 295 347 break; 296 348 default: 349 TRACE_((file->obj_name, "Not UDP, skipping")); 297 350 SKIP_PKT(); 298 351 continue; … … 301 354 /* Check if payload fits the buffer */ 302 355 if (sz > (pj_ssize_t)*udp_payload_size) { 356 TRACE_((file->obj_name, 357 "Error: packet too large (%d bytes required)", sz)); 303 358 SKIP_PKT(); 304 359 return PJ_ETOOSMALL; … … 307 362 /* Read the payload */ 308 363 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)); 310 366 return status; 367 } 311 368 312 369 sz_read += sz;
Note: See TracChangeset
for help on using the changeset viewer.