Changeset 7b4f5e2


Ignore:
Timestamp:
02/05/19 14:41:55 (20 months ago)
Author:
Jacob Van Walraven <jcv9@…>
Branches:
develop
Children:
58c226e
Parents:
a0e5b65
Message:

cleanup trace_get_interface ipv4 and ipv6 functions

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/format_pktmeta.c

    ra0e5b65 r7b4f5e2  
    322322 *
    323323 * @params libtrace_packet_t meta packet to extract the ipv4 address from.
    324  * @params A pointer to a character buffer to store the ipv4 address string in.
    325  * @params The size of the buffer passed in.
    326324 * @params The interface index within the meta packet.
    327325 * @returns Pointer to the character buffer containing the ipv4 address string or NULL.
    328326 */
    329327/* UNTESTED */
    330 char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
    331         int index) {
    332 
    333         uint32_t addr = htonl(trace_get_interface_ipv4(packet, index));
     328char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, int index) {
     329        struct in_addr ip;
     330        uint32_t addr;
     331
     332        addr = trace_get_interface_ipv4(packet, index);
    334333        if (addr == 0) { return NULL; }
    335334
    336         char *addrstr = inet_ntoa(*(struct in_addr *)&addr);
    337         memcpy(space, addrstr, spacelen);
    338         space[spacelen] = '\0';
    339         return space;
     335        ip.s_addr = addr;
     336
     337        return inet_ntoa(ip);
    340338}
    341339
     
    404402        int index) {
    405403
     404        /* Ensure we have enough space */
    406405        if (spacelen < INET6_ADDRSTRLEN) {
    407406                return NULL;
    408407        }
    409408
    410         void *addr = calloc(1, 16);
    411         void *r = trace_get_interface_ipv6(packet, addr, 16, index);
    412 
     409        struct sockaddr_in6 sa;
     410
     411        void *r = trace_get_interface_ipv6(packet, &(sa.sin6_addr), 16, index);
    413412        if (r == NULL) {
    414413                return NULL;
    415414        }
    416415
    417         inet_ntop(AF_INET6, addr, space, INET6_ADDRSTRLEN);
    418         free(addr);
     416        /* get the string representation */
     417        inet_ntop(AF_INET6, &(sa.sin6_addr), space, INET6_ADDRSTRLEN);
    419418
    420419        return space;
  • lib/libtrace.h.in

    re0f2dca r7b4f5e2  
    37493749DLLEXPORT uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet, int index);
    37503750
    3751 DLLEXPORT char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
    3752         int index);
     3751DLLEXPORT char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, int index);
    37533752
    37543753DLLEXPORT libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet);
Note: See TracChangeset for help on using the changeset viewer.