- Timestamp:
- 03/23/09 17:01:09 (13 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- 5dd06d6
- Parents:
- d4242e4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/protocols_l3.c
rad36006 r59751a5 4 4 #include <assert.h> 5 5 #include <stdlib.h> 6 #include "config.h" 7 8 #ifdef HAVE_NETPACKET_PACKET_H 9 #include <sys/socket.h> 10 #include <netpacket/packet.h> 11 #include <net/ethernet.h> 12 #include <net/if_arp.h> 13 #include <string.h> 14 #endif 6 15 7 16 libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet) … … 248 257 } 249 258 259 /* Extract the source mac address from a frame and bundle it up into a sockaddr */ 260 static struct sockaddr *get_source_ethernet_address( 261 libtrace_ether_t *ethernet, struct sockaddr *addr) 262 { 263 #ifdef HAVE_NETPACKET_PACKET_H 264 /* Use linux's sockaddr_ll structure */ 265 static struct sockaddr_storage dummy; 266 struct sockaddr_ll *l2addr; 267 268 if (addr) 269 l2addr = (struct sockaddr_ll*)addr; 270 else 271 l2addr = (struct sockaddr_ll*)&dummy; 272 273 l2addr->sll_family = AF_PACKET; 274 l2addr->sll_protocol = ethernet->ether_type; 275 l2addr->sll_ifindex = 0; /* Irrelevant */ 276 l2addr->sll_hatype = ARPHRD_ETHER; 277 l2addr->sll_pkttype = PACKET_OTHERHOST; 278 l2addr->sll_halen = 6; 279 memcpy(l2addr->sll_addr,ethernet->ether_shost, 6); 280 281 return (struct sockaddr*)l2addr; 282 #else 283 /* TODO: implement BSD's sockaddr_dl structure, sigh. */ 284 return NULL; 285 #endif 286 } 287 288 static struct sockaddr *get_source_l2_address( 289 const libtrace_packet_t *packet, struct sockaddr *addr) 290 { 291 static struct sockaddr_storage dummy; 292 void *l2; 293 libtrace_linktype_t linktype; 294 uint32_t remaining; 295 296 if (!addr) 297 addr =(struct sockaddr*)&dummy; 298 299 l2=trace_get_layer2(packet, &linktype, &remaining); 300 if (!l2) { 301 return NULL; 302 } 303 304 switch (linktype) { 305 case TRACE_TYPE_ETH: 306 return get_source_ethernet_address((libtrace_ether_t*)l2, addr); 307 default: 308 return NULL; 309 } 310 } 311 250 312 DLLEXPORT struct sockaddr *trace_get_source_address( 251 313 const libtrace_packet_t *packet, struct sockaddr *addr) … … 263 325 264 326 if (!l3) 265 return NULL;327 return get_source_l2_address(packet,addr); 266 328 267 329 switch (ethertype) { … … 296 358 } 297 359 default: 360 return get_source_l2_address(packet, addr); 361 } 362 } 363 364 365 static struct sockaddr *get_destination_ethernet_address( 366 libtrace_ether_t *ethernet, struct sockaddr *addr) 367 { 368 #ifdef HAVE_NETPACKET_PACKET_H 369 /* Use linux's sockaddr_ll structure */ 370 static struct sockaddr_storage dummy; 371 struct sockaddr_ll *l2addr; 372 if (addr) 373 l2addr = (struct sockaddr_ll*)addr; 374 else 375 l2addr = (struct sockaddr_ll*)&dummy; 376 377 l2addr->sll_family = AF_PACKET; 378 l2addr->sll_protocol = ethernet->ether_type; 379 l2addr->sll_ifindex = 0; /* Irrelevant */ 380 l2addr->sll_hatype = ARPHRD_ETHER; 381 l2addr->sll_pkttype = PACKET_OTHERHOST; 382 l2addr->sll_halen = 6; 383 memcpy(l2addr->sll_addr,ethernet->ether_dhost, 6); 384 385 return (struct sockaddr*)l2addr; 386 #else 387 /* TODO: implement BSD's sockaddr_dl structure, sigh. */ 388 return NULL; 389 #endif 390 } 391 392 static struct sockaddr *get_destination_l2_address( 393 const libtrace_packet_t *packet, struct sockaddr *addr) 394 { 395 static struct sockaddr_storage dummy; 396 void *l2; 397 libtrace_linktype_t linktype; 398 uint32_t remaining; 399 if (!addr) 400 addr =(struct sockaddr*)&dummy; 401 l2=trace_get_layer2(packet, &linktype, &remaining); 402 if (!l2) 403 return NULL; 404 405 switch (linktype) { 406 case TRACE_TYPE_ETH: 407 return get_destination_ethernet_address((libtrace_ether_t*)l2, addr); 408 default: 298 409 return NULL; 299 410 } … … 315 426 316 427 if (!l3) 317 return NULL;428 return get_destination_l2_address(packet,addr); 318 429 319 430 switch (ethertype) { … … 348 459 } 349 460 default: 350 return NULL;351 } 352 } 353 354 461 return get_destination_l2_address(packet, addr); 462 } 463 } 464 465
Note: See TracChangeset
for help on using the changeset viewer.