Changeset 5699389 for lib


Ignore:
Timestamp:
03/05/13 13:43:48 (9 years ago)
Author:
Shane Alcock <salcock@…>
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:
af27241
Parents:
c5ff469
Message:
  • Added new API functions for getting the source and destination IPs as strings. Dealing with sockaddrs is hard, especially for n00bs.
Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/libtrace.h.in

    r05f2718 r5699389  
    24082408                struct sockaddr *addr);
    24092409
     2410/** Get the source IP address for a packet and convert it into a string
     2411 * @param packet        The packet to extract the source IP address from
     2412 * @param space         A pointer to a character buffer to store the address
     2413 *                      in. If NULL, static storage is used instead.
     2414 * @param spacelen      The size of the buffer passed in via 'space'. Set this
     2415 *                      to zero if you are going to pass in a NULL buffer.
     2416 * @return A pointer to a character buffer containing the string representation
     2417 * of the source IP address. For packets where there is no suitable IP address,
     2418 * the source MAC will be returned instead. Returns NULL if no valid address
     2419 * is available.
     2420 *
     2421 * @note Be wary of the possibility of the address being an IPv6 address - make
     2422 * sure your buffer is large enough!
     2423 *
     2424 * New in libtrace 3.0.17.
     2425 */
     2426DLLEXPORT SIMPLE_FUNCTION
     2427char *trace_get_source_address_string(const libtrace_packet_t *packet,
     2428                char *space, int spacelen);
     2429
    24102430/** Get the destination IP address for a given packet
    24112431 * @param packet        The packet to extract the destination IP address from
     
    24252445                struct sockaddr *addr);
    24262446
     2447/** Get the destination IP address for a packet and convert it into a string
     2448 * @param packet        The packet to extract the destination IP address from
     2449 * @param space         A pointer to a character buffer to store the address
     2450 *                      in. If NULL, static storage is used instead.
     2451 * @param spacelen      The size of the buffer passed in via 'space'. Set this
     2452 *                      to zero if you are going to pass in a NULL buffer.
     2453 * @return A pointer to a character buffer containing the string representation
     2454 * of the destination IP address. For packets where there is no suitable IP
     2455 * address, the destination MAC will be returned instead. Returns NULL if no
     2456 * valid address is available.
     2457 *
     2458 * @note Be wary of the possibility of the address being an IPv6 address - make
     2459 * sure your buffer is large enough!
     2460 *
     2461 * New in libtrace 3.0.17.
     2462 */
     2463DLLEXPORT SIMPLE_FUNCTION
     2464char *trace_get_destination_address_string(const libtrace_packet_t *packet,
     2465                char *space, int spacelen);
    24272466
    24282467/** Parses an IP or TCP option
  • lib/protocols_l3.c

    r48d72f8 r5699389  
    3838#include <assert.h>
    3939#include <stdlib.h>
     40#include <arpa/inet.h>
    4041
    4142#ifdef HAVE_NETPACKET_PACKET_H
     
    345346}
    346347
     348static char *sockaddr_to_string(struct sockaddr *addrptr, char *space,
     349                int spacelen) {
     350
     351        assert(addrptr && space);
     352        assert(spacelen > 0);
     353       
     354        if (addrptr->sa_family == AF_INET) {
     355                struct sockaddr_in *v4 = (struct sockaddr_in *)addrptr;
     356                inet_ntop(AF_INET, &(v4->sin_addr), space, spacelen);
     357        }
     358
     359        else if (addrptr->sa_family == AF_INET6) {
     360                struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addrptr;
     361                inet_ntop(AF_INET6, &(v6->sin6_addr), space, spacelen);
     362        }
     363#ifdef HAVE_NETPACKET_PACKET_H
     364        else if (addrptr->sa_family == AF_PACKET) {
     365                struct sockaddr_ll *l2addr = (struct sockaddr_ll *)addrptr;
     366                uint8_t *macbytes = (uint8_t *)l2addr->sll_addr;
     367
     368                snprintf(space, spacelen, "%02x:%02x:%02x:%02x:%02x:%02x",
     369                                macbytes[0], macbytes[1], macbytes[2],
     370                                macbytes[3], macbytes[4], macbytes[5]);
     371
     372        }
     373#else
     374        else if (addrptr->sa_family == AF_LINK) {
     375                struct sockaddr_dl *l2addr = (struct sockaddr_dl *)addrptr;
     376                uint8_t *macbytes = (uint8_t *)l2addr->sdl_data;
     377
     378                snprintf(space, spacelen, "%02x:%02x:%02x:%02x:%02x:%02x",
     379                                macbytes[0], macbytes[1], macbytes[2],
     380                                macbytes[3], macbytes[4], macbytes[5]);
     381       
     382        }
     383#endif
     384        else {
     385                space[0] = '\0';
     386                return NULL;
     387        }
     388
     389        return space;
     390
     391}
     392
    347393/* Extract the source mac address from a frame and bundle it up into a sockaddr */
    348394static struct sockaddr *get_source_ethernet_address(
     
    469515
    470516
     517DLLEXPORT char *trace_get_source_address_string(
     518                const libtrace_packet_t *packet, char *space, int spacelen) {
     519
     520        static char staticspace[INET6_ADDRSTRLEN];
     521        struct sockaddr_storage addr;
     522        struct sockaddr *addrptr;
     523       
     524
     525        if (space == NULL || spacelen == 0) {
     526                space = staticspace;
     527                spacelen = INET6_ADDRSTRLEN;
     528        }
     529
     530        addrptr = trace_get_source_address(packet, (struct sockaddr *)&addr);
     531
     532        if (addrptr == NULL)
     533                return NULL;
     534       
     535        return sockaddr_to_string(addrptr, space, spacelen);
     536}
     537
    471538static struct sockaddr *get_destination_ethernet_address(
    472539        libtrace_ether_t *ethernet, struct sockaddr *addr)
     
    587654}
    588655
    589 
     656DLLEXPORT char *trace_get_destination_address_string(
     657                const libtrace_packet_t *packet, char *space, int spacelen) {
     658
     659        struct sockaddr_storage addr;
     660        struct sockaddr *addrptr;
     661       
     662        static char staticspace[INET6_ADDRSTRLEN];
     663
     664        if (space == NULL || spacelen == 0) {
     665                space = staticspace;
     666                spacelen = INET6_ADDRSTRLEN;
     667        }
     668
     669        addrptr = trace_get_destination_address(packet,
     670                        (struct sockaddr *)&addr);
     671
     672        if (addrptr == NULL)
     673                return NULL;
     674       
     675        return sockaddr_to_string(addrptr, space, spacelen);
     676}
     677
Note: See TracChangeset for help on using the changeset viewer.