Changeset 66ffac4


Ignore:
Timestamp:
01/17/19 14:33:40 (22 months ago)
Author:
Jacob Van Walraven <jcv9@…>
Branches:
develop
Children:
c8171e5
Parents:
d4eed70
Message:

Add more meta functions, cleanup function names, DLLEXPORT all meta-api prototypes

Location:
lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • lib/format_pcapng.c

    rd4eed70 r66ffac4  
    23422342
    23432343                result->items = malloc(sizeof(libtrace_meta_item_t));
    2344                 result->items->option = option;
    2345                 result->items->len = len;
    2346                 result->items->data_type = 0;
    2347                 result->items->data = calloc(1, len);
     2344                result->items[result->num-1].option = option;
     2345                result->items[result->num-1].len = len;
     2346                result->items[result->num-1].data_type = 0;
     2347                result->items[result->num-1].data = calloc(1, len);
    23482348
    23492349                memcpy(result->items->data, ptr+sizeof(struct pcapng_optheader), len);
  • lib/format_pktmeta.c

    ref5ba20 r66ffac4  
    1010
    1111/* Internal Meta functions */
    12 
    1312static int trace_meta_check_input(libtrace_packet_t *packet, char *input_func) {
    1413        if (packet == NULL) {
     
    4948}
    5049
    51 /* Get the interface name/s
     50/* Get the interface name/s for a meta packet.
     51 * Must be destroyed with trace_destroy_meta().
     52 *
     53 * @params libtrace_packet_t meta packet.
     54 * @returns Pointer to libtrace_meta_t structure containing all found interface names
     55 * or NULL.
     56 */
     57libtrace_meta_t *trace_get_interface_name_meta(libtrace_packet_t *packet) {
     58        if (trace_meta_check_input(packet, "trace_get_interface_name()")<0) {
     59                return NULL;
     60        }
     61
     62        /* get the result */
     63        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     64                return packet->trace->format->get_meta_section_option(packet,
     65                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_NAME);
     66        }
     67        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     68                return packet->trace->format->get_meta_section_option(packet,
     69                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_NAME);
     70        }
     71
     72        return NULL;
     73}
     74/* Get the interface name for a meta packet.
     75 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     76 * specify the interface index.
     77 *
     78 * @params libtrace_packet_t meta packet to extract the interface name from.
     79 * @params A pointer to a character buffer to store the interface name in.
     80 * @params The size of the buffer passed in.
     81 * @params The interface index within the meta packet.
     82 * @returns Pointer to the character buffer containing the interface name or NULL.
     83 */
     84char *trace_get_interface_name(libtrace_packet_t *packet, char *space, int spacelen,
     85        int index) {
     86
     87        libtrace_meta_t *r = trace_get_interface_name_meta(packet);
     88        if (r == NULL) { return NULL; }
     89        /* If there is not a result for the index return */
     90        if (r->num <= index) { return NULL; }
     91        /* Ensure the supplied memory allocation is enough, if not only fill
     92         * what we can */
     93        if (spacelen > r->items[index].len) {
     94                memcpy(space, r->items[index].data, r->items[index].len);
     95        } else {
     96                memcpy(space, r->items[index].data, spacelen);
     97        }
     98        trace_destroy_meta(r);
     99        return space;
     100}
     101/* Get the interface MAC address/s for a meta packet.
     102 * Must be destroyed with trace_destroy_meta().
     103 *
     104 * @params libtrace_packet_t meta packet.
     105 * @returns Pointer to libtrace_meta_t structure containing all found interface mac
     106 * addresses or NULL.
     107 */
     108libtrace_meta_t *trace_get_interface_mac_meta(libtrace_packet_t *packet) {
     109        if (trace_meta_check_input(packet, "trace_get_interface_mac()")<0) {
     110                return NULL;
     111        }
     112
     113        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     114                return packet->trace->format->get_meta_section_option(packet,
     115                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC);
     116        }
     117        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     118                return packet->trace->format->get_meta_section_option(packet,
     119                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC);
     120        }
     121
     122        return NULL;
     123}
     124/* Get the interface MAC address for a meta packet.
     125 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     126 * specify the interface index.
     127 *
     128 * @params libtrace_packet_t meta packet to extract the MAC address from.
     129 * @params A pointer to a character buffer to store the MAC address in.
     130 * @params The size of the buffer passed in.
     131 * @params The interface index within the meta packet.
     132 * @returns Pointer to the character buffer containing the MAC address or NULL.
     133 */
     134char *trace_get_interface_mac(libtrace_packet_t *packet, char *space, int spacelen,
     135        int index) {
     136
     137        libtrace_meta_t *r = trace_get_interface_mac_meta(packet);
     138        if (r == NULL) { return NULL; }
     139        if (index >= r->num) { return NULL; }
     140        if (r->items[index].len > spacelen) {
     141                memcpy(space, r->items[index].data, spacelen);
     142        } else {
     143                memcpy(space, r->items[index].data, r->items[index].len);
     144        }
     145        trace_destroy_meta(r);
     146        return space;
     147}
     148
     149/* Get the interface speed/s from a meta packet.
     150 * Must be destroyed with trace_destroy_meta().
     151 *
     152 * @params libtrace_packet_t packet.
     153 * @returns Pointer to libtrace_meta_t structure containing all found interface
     154 * speeds or NULL.
     155 */
     156libtrace_meta_t *trace_get_interface_speed_meta(libtrace_packet_t *packet) {
     157        if (trace_meta_check_input(packet, "trace_get_interface_speed()")<0) {
     158                return NULL;
     159        }
     160
     161        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     162                return packet->trace->format->get_meta_section_option(packet,
     163                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED);
     164        }
     165        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     166                return packet->trace->format->get_meta_section_option(packet,
     167                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_SPEED);
     168        }
     169
     170        return NULL;
     171}
     172/* Get the interface speed for a meta packet.
     173 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     174 * specify the interface index.
     175 *
     176 * @params libtrace_packet_t meta packet to extract the interface speed from.
     177 * @params The interface index within the meta packet.
     178 * @returns uint64_t interface speed or NULL.
     179 */
     180uint64_t trace_get_interface_speed(libtrace_packet_t *packet, int index) {
     181        libtrace_meta_t *r = trace_get_interface_speed_meta(packet);
     182        if (r == NULL) { return 0; }
     183        /* If the index wanted does not exist return 0 */
     184        if (index >= r->num) { return 0; }
     185        /* Need to check this more ERF reports this in network order */
     186        uint64_t data = *(uint64_t *)r->items[index].data;
     187        trace_destroy_meta(r);
     188        return data;
     189}
     190
     191/* Get the interface ipv4 address/s for a meta packet.
     192 * Must be destroyed with trace_destroy_meta().
     193 *
     194 * @params libtrace_packet_t meta packet.
     195 * @returns Pointer to libtrace_meta_t structure containing all found ipv4 addresses
     196 * or NULL
     197 */
     198libtrace_meta_t *trace_get_interface_ipv4_meta(libtrace_packet_t *packet) {
     199        if (trace_meta_check_input(packet, "trace_get_interface_ip4()")<0) {
     200                return NULL;
     201        }
     202
     203        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     204                return packet->trace->format->get_meta_section_option(packet,
     205                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
     206        }
     207        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     208                return packet->trace->format->get_meta_section_option(packet,
     209                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
     210        }
     211
     212        return NULL;
     213}
     214/* Get the interface ipv4 address for a meta packet.
     215 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     216 * specify the interface index.
     217 *
     218 * @params libtrace_packet_t meta packet to extract the ipv4 address from.
     219 * @params The interface index within the meta packet.
     220 * @returns uint32_t ipv4 address or 0.
     221 */
     222uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet, int index) {
     223        libtrace_meta_t *r = trace_get_interface_ipv4_meta(packet);
     224        if (r == NULL) { return 0; }
     225        if (index >= r->num) { return 0; }
     226        uint32_t data = *(uint32_t *)r->items[index].data;
     227        trace_destroy_meta(r);
     228        return data;
     229}
     230/* Get the interface ipv4 address string for a meta packet.
     231 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     232 * specify the interface index.
     233 *
     234 * @params libtrace_packet_t meta packet to extract the ipv4 address from.
     235 * @params A pointer to a character buffer to store the ipv4 address string in.
     236 * @params The size of the buffer passed in.
     237 * @params The interface index within the meta packet.
     238 * @returns Pointer to the character buffer containing the ipv4 address string or NULL.
     239 */
     240char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
     241        int index) {
     242
     243        uint32_t addr = htonl(trace_get_interface_ipv4(packet, index));
     244        if (addr == 0) { return NULL; }
     245
     246        char *addrstr = inet_ntoa(*(struct in_addr *)&addr);
     247        memcpy(space, addrstr, strlen(addrstr));
     248        return space;
     249}
     250
     251/* Get the interface ipv6 address/s for a meta packet.
     252 * Must be destroyed with trace_destroy_meta().
     253 *
     254 * @params libtrace_packet_t meta packet.
     255 * @returns Pointer to libtrace_meta_t structure containing all found ipv6 addresses
     256 * or NULL.
     257 */
     258libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet) {
     259        if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) {
     260                return NULL;
     261        }
     262
     263        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     264                return packet->trace->format->get_meta_section_option(packet,
     265                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
     266        }
     267        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     268                return packet->trace->format->get_meta_section_option(packet,
     269                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
     270        }
     271
     272        return NULL;
     273}
     274/* Get the interface ipv6 address for a meta packet.
     275 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     276 * specify the interface index.
     277 *
     278 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
     279 * @params A pointer to a character buffer to store the ipv6 address in.
     280 * @params The size of the buffer passed in.
     281 * @params The interface index within the meta packet.
     282 * @returns Pointer to the buffer containing the ipv6 address or NULL.
     283 */
     284void *trace_get_interface_ipv6(libtrace_packet_t *packet, void *space, int spacelen,
     285        int index) {
     286
     287        libtrace_meta_t *r = trace_get_interface_ipv6_meta(packet);
     288        if (r == NULL) { return NULL; }
     289        if (r->num <= index) { return NULL; }
     290        if (r->items[index].len > spacelen) {
     291                memcpy(space, r->items[index].data, spacelen);
     292        } else {
     293                memcpy(space, r->items[index].data, r->items[index].len);
     294        }
     295        trace_destroy_meta(r);
     296        return space;
     297}
     298/* Get the interface ipv6 address string for a meta packet.
     299 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     300 * specify the interface index.
     301 *
     302 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
     303 * @params A pointer to a character buffer to store the ipv6 address in.
     304 * @params The size of the buffer passed in.
     305 * @params The interface index within the meta packet.
     306 * @returns Pointer to the character buffer containing the ipv6 address string or NULL.
     307 */
     308char *trace_get_interface_ipv6_string(libtrace_packet_t *packet, char *space, int spacelen,
     309        int index) {
     310
     311        return NULL;
     312}
     313
     314
     315/* Get the interface description/s for a meta packet.
     316 * Must be destroyed with trace_destroy_meta().
     317 *
     318 * @params libtrace_packet_t meta packet.
     319 * @returns Pointer to libtrace_meta_t structure containing all found interface
     320 * descriptions or NULL.
     321 */
     322libtrace_meta_t *trace_get_interface_description_meta(libtrace_packet_t *packet) {
     323        if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) {
     324                return NULL;
     325        }
     326
     327        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     328                return packet->trace->format->get_meta_section_option(packet,
     329                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
     330        }
     331        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     332                return packet->trace->format->get_meta_section_option(packet,
     333                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR);
     334        }
     335
     336        return NULL;
     337}
     338/* Get the interface description for a meta packet.
     339 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     340 * specify the interface index.
     341 *
     342 * @params libtrace_packet_t meta packet to extract the interface description from.
     343 * @params A pointer to a character buffer to store the interface description in.
     344 * @params The size of the buffer passed in.
     345 * @params The interface index within the meta packet.
     346 * @returns Pointer to the character buffer containing the interface description or NULL.
     347 */
     348char *trace_get_interface_description(libtrace_packet_t *packet, char *space, int spacelen,
     349        int index) {
     350
     351        libtrace_meta_t *r = trace_get_interface_description_meta(packet);
     352        if (r == NULL) { return NULL; }
     353        if (r->num <= index) { return NULL; }
     354        if (r->items[index].len > spacelen) {
     355                memcpy(space, r->items[index].data, spacelen);
     356        } else {
     357                memcpy(space, r->items[index].data, r->items[index].len);
     358        }
     359        trace_destroy_meta(r);
     360        return space;
     361}
     362
     363
     364/* Get the host OS for a meta packet.
     365 * Must be destroyed with trace_destroy_meta().
     366 *
     367 * @params libtrace_packet_t meta packet.
     368 * @returns Pointer to libtrace_meta_t structure containing the host OS or NULL.
     369 */
     370libtrace_meta_t *trace_get_host_os_meta(libtrace_packet_t *packet) {
     371        if (trace_meta_check_input(packet, "trace_get_host_os()")<0) {
     372                return NULL;
     373        }
     374
     375        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     376                return packet->trace->format->get_meta_section_option(packet,
     377                        ERF_PROV_SECTION_HOST, ERF_PROV_OS);
     378        }
     379        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     380                return packet->trace->format->get_meta_section_option(packet,
     381                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS);
     382        }
     383
     384        return NULL;
     385}
     386/* Get the host OS for a meta packet.
     387 *
     388 * @params libtrace_packet_t meta packet to extract the host OS from.
     389 * @params A pointer to a character buffer to store the host OS in.
     390 * @params The size of the buffer passed in.
     391 * @returns Pointer to the character buffer containing the host OS or NULL.
     392 */
     393char *trace_get_host_os(libtrace_packet_t *packet, char *space, int spacelen) {
     394        libtrace_meta_t *r = trace_get_host_os_meta(packet);
     395        if (r == NULL) { return NULL; }
     396        if (r->items[0].len > spacelen) {
     397                memcpy(space, r->items[0].data, spacelen);
     398        } else {
     399                memcpy(space, r->items[0].data, r->items[0].len);
     400        }
     401        trace_destroy_meta(r);
     402        return space;
     403}
     404
     405/* Get the interface frame check sequence length for a meta packet.
     406 * Must be destroyed with trace_destroy_meta().
     407 *
     408 * @params libtrace_packet_t meta packet.
     409 * @returns Pointer to libtrace_meta_t structure containing all found frame check
     410 * sequence lengths or NULL.
     411 */
     412libtrace_meta_t *trace_get_interface_fcslen_meta(libtrace_packet_t *packet) {
     413        if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) {
     414                return NULL;
     415        }
     416
     417        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     418                return packet->trace->format->get_meta_section_option(packet,
     419                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN);
     420        }
     421        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     422                return packet->trace->format->get_meta_section_option(packet,
     423                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN);
     424        }
     425
     426        return NULL;
     427}
     428/* Get the interface frame check sequence length for a meta packet.
     429 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     430 * specify the interface index.
     431 *
     432 * @params libtrace_packet_t meta packet to extract the interface fcslen from.
     433 * @params The interface index within the meta packet.
     434 * @returns uint32_t frame check sequence length or 0.
     435 */
     436uint32_t trace_get_interface_fcslen(libtrace_packet_t *packet, int index) {
     437        libtrace_meta_t *r = trace_get_interface_fcslen_meta(packet);
     438        if (r == NULL) { return 0; }
     439        if (r->num <= index) { return 0; }
     440        uint32_t data = *(uint32_t *)r->items[index].data;
     441        trace_destroy_meta(r);
     442        return data;
     443}
     444
     445/* Get the hardware description
    52446 * Must be destroyed with trace_destroy_meta()
    53447 *
     
    55449 * @returns Pointer to libtrace_meta_t structure or NULL
    56450 */
    57 libtrace_meta_t *trace_get_interface_name(libtrace_packet_t *packet) {
    58         if (trace_meta_check_input(packet, "trace_get_interface_name()")<0) {
    59                 return NULL;
    60         }
    61 
    62         /* get the result */
    63         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    64                 return packet->trace->format->get_meta_section_item(packet,
    65                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_NAME);
    66         }
    67         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    68                 return packet->trace->format->get_meta_section_item(packet,
    69                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_NAME);
    70         }
    71 
    72         return NULL;
    73 }
    74 
    75 /* Get the interface MAC address/s
    76  * Must be destroyed with trace_destroy_meta()
    77  *
    78  * @params libtrace_packet_t meta packet
    79  * @returns Pointer to libtrace_meta_t structure or NULL
    80  */
    81 libtrace_meta_t *trace_get_interface_mac(libtrace_packet_t *packet) {
    82         if (trace_meta_check_input(packet, "trace_get_interface_mac()")<0) {
    83                 return NULL;
    84         }
    85 
    86         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    87                 return packet->trace->format->get_meta_section_item(packet,
    88                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC);
    89         }
    90         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    91                 return packet->trace->format->get_meta_section_item(packet,
    92                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC);
    93         }
    94 
    95         return NULL;
    96 }
    97 
    98 /* Get the interface speed/s from a meta packet
     451libtrace_meta_t *trace_get_interface_hardware_description_meta(libtrace_packet_t *packet) {
     452        if (trace_meta_check_input(packet, "trace_get_interface_hardware_description()")<0) {
     453                return NULL;
     454        }
     455
     456        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     457                return packet->trace->format->get_meta_section_option(packet,
     458                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_HARDWARE);
     459        }
     460        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     461                return packet->trace->format->get_meta_section_option(packet,
     462                        ERF_PROV_SECTION_MODULE, ERF_PROV_MODEL);
     463        }
     464
     465        return NULL;
     466}
     467/* Get the hardware description for a meta packet.
     468 *
     469 * @params libtrace_packet_t meta packet to extract the hardware description from.
     470 * @params A pointer to a character buffer to store the hardware description in.
     471 * @params The size of the buffer passed in.
     472 * @returns Pointer to the character buffer containing the hardware description or NULL.
     473 */
     474char *trace_get_interface_hardware_description(libtrace_packet_t *packet, char *space,
     475        int spacelen) {
     476
     477        libtrace_meta_t *r = trace_get_interface_hardware_description_meta(packet);
     478        if (r == NULL) { return NULL; }
     479        if (r->items[0].len > spacelen) {
     480                memcpy(space, r->items[0].data, spacelen);
     481        } else {
     482                memcpy(space, r->items[0].data, r->items[0].len);
     483        }
     484        trace_destroy_meta(r);
     485        return space;
     486}
     487
     488/* Get any interface comments for a meta packet
    99489 * Must be destroyed with trace_destroy_meta()
    100490 *
     
    102492 * @returns Pointer to libtrace_meta_t structure or NULL
    103493 */
    104 libtrace_meta_t *trace_get_interface_speed(libtrace_packet_t *packet) {
    105         if (trace_meta_check_input(packet, "trace_get_interface_speed()")<0) {
    106                 return NULL;
    107         }
    108 
    109         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    110                 return packet->trace->format->get_meta_section_item(packet,
    111                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED);
    112         }
    113         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    114                 return packet->trace->format->get_meta_section_item(packet,
    115                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_SPEED);
    116         }
    117 
    118         return NULL;
    119 }
    120 
    121 /* Get the interface ipv4 address/s
    122  * Must be destroyed with trace_destroy_meta()
    123  *
    124  * @params libtrace_packet_t meta packet
    125  * @returns Pointer to libtrace_meta_t structure or NULL
    126  */
    127 libtrace_meta_t *trace_get_interface_ip4(libtrace_packet_t *packet) {
    128         if (trace_meta_check_input(packet, "trace_get_interface_ip4()")<0) {
    129                 return NULL;
    130         }
    131 
    132         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    133                 return packet->trace->format->get_meta_section_item(packet,
    134                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
    135         }
    136         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    137                 return packet->trace->format->get_meta_section_item(packet,
    138                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
    139         }
    140 
    141         return NULL;
    142 }
    143 libtrace_meta_t *trace_get_interface_ipv4(libtrace_packet_t *packet) {
    144         return trace_get_interface_ip4(packet);
    145 }
    146 
    147 /* Get the interface ipv6 address/s
    148  * Must be destroyed with trace_destroy_meta()
    149  *
    150  * @params libtrace_packet_t meta packet
    151  * @returns Pointer to libtrace_meta_t structure or NULL
    152  */
    153 libtrace_meta_t *trace_get_interface_ip6(libtrace_packet_t *packet) {
    154         if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) {
    155                 return NULL;
    156         }
    157 
    158         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    159                 return packet->trace->format->get_meta_section_item(packet,
    160                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
    161         }
    162         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    163                 return packet->trace->format->get_meta_section_item(packet,
    164                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
    165         }
    166 
    167         return NULL;
    168 }
    169 libtrace_meta_t *trace_get_interface_ipv6(libtrace_packet_t *packet) {
    170         return trace_get_interface_ip6(packet);
    171 }
    172 
    173 /* Get the interface description/s
    174  * Must be destroyed with trace_destroy_meta()
    175  *
    176  * @params libtrace_packet_t meta packet
    177  * @returns Pointer to libtrace_meta_t structure or NULL
    178  */
    179 libtrace_meta_t *trace_get_interface_description(libtrace_packet_t *packet) {
    180         if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) {
    181                 return NULL;
    182         }
    183 
    184         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    185                 return packet->trace->format->get_meta_section_item(packet,
    186                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
    187         }
    188         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    189                 return packet->trace->format->get_meta_section_item(packet,
    190                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR);
    191         }
    192 
    193         return NULL;
    194 }
    195 
    196 /* Get the interface number/s
    197  * Must be destroyed with trace_destroy_meta()
    198  *
    199  * @params libtrace_packet_t meta packet
    200  * @returns Pointer to libtrace_meta_t structure or NULL
    201  */
    202 libtrace_meta_t *trace_get_interface_num(libtrace_packet_t *packet) {
    203         if (trace_meta_check_input(packet, "trace_get_interface_num()")<0) {
    204                 return NULL;
    205         }
    206 
    207         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    208                 return packet->trace->format->get_meta_section_item(packet,
    209                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_NUM);
    210         }
    211         /* Note: pcapng doesnt provide this */
    212 
    213         return NULL;
    214 }
    215 
    216 /* Get the host OS
    217  * Must be destroyed with trace_destroy_meta()
    218  *
    219  * @params libtrace_packet_t meta packet
    220  * @returns Pointer to libtrace_meta_t structure or NULL
    221  */
    222 libtrace_meta_t *trace_get_host_os(libtrace_packet_t *packet) {
    223         if (trace_meta_check_input(packet, "trace_get_host_os()")<0) {
    224                 return NULL;
    225         }
    226 
    227         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    228                 return packet->trace->format->get_meta_section_item(packet,
    229                         ERF_PROV_SECTION_HOST, ERF_PROV_OS);
    230         }
    231         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    232                 return packet->trace->format->get_meta_section_item(packet,
    233                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS);
    234         }
    235 
    236         return NULL;
    237 }
    238 
    239 /* Get the frame check sequence length
    240  * Must be destroyed with trace_destroy_meta()
    241  *
    242  * @params libtrace_packet_t meta packet
    243  * @returns Pointer to libtrace_meta_t structure or NULL
    244  */
    245 libtrace_meta_t *trace_get_interface_frame_check_sequence_length(libtrace_packet_t *packet) {
    246         if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) {
    247                 return NULL;
    248         }
    249 
    250         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    251                 return packet->trace->format->get_meta_section_item(packet,
    252                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN);
    253         }
    254         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    255                 return packet->trace->format->get_meta_section_item(packet,
    256                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN);
    257         }
    258 
    259         return NULL;
    260 }
    261 
    262 /* Get the hardware description
    263  * Must be destroyed with trace_destroy_meta()
    264  *
    265  * @params libtrace_packet_t meta packet
    266  * @returns Pointer to libtrace_meta_t structure or NULL
    267  */
    268 libtrace_meta_t *trace_get_interface_hardware_description(libtrace_packet_t *packet) {
    269         if (trace_meta_check_input(packet, "trace_get_interface_hardware_description()")<0) {
    270                 return NULL;
    271         }
    272 
    273         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    274                 return packet->trace->format->get_meta_section_item(packet,
    275                         PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_HARDWARE);
    276         }
    277         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    278                 return packet->trace->format->get_meta_section_item(packet,
    279                         ERF_PROV_SECTION_MODULE, ERF_PROV_MODEL);
    280         }
    281 
    282         return NULL;
    283 }
    284 
    285 /* Get any interface comments for a meta packet
     494libtrace_meta_t *trace_get_interface_comment_meta(libtrace_packet_t *packet) {
     495        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
     496                return NULL;
     497        }
     498
     499        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
     500                return packet->trace->format->get_meta_section_option(packet,
     501                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT);
     502        }
     503        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
     504                return packet->trace->format->get_meta_section_option(packet,
     505                        PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT);
     506        }
     507
     508        return NULL;
     509}
     510/* Get the interface comment for a meta packet.
     511 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
     512 * specify the interface ID.
     513 *
     514 * @params libtrace_packet_t meta packet to extract the interface comment from.
     515 * @params A pointer to a character buffer to store the interface description in.
     516 * @params The size of the buffer passed in.
     517 * @params The interface number within the meta packet.
     518 * @returns Pointer to the character buffer containing the hardware description or NULL.
     519 */
     520char *trace_get_interface_comment(libtrace_packet_t *packet, char *space, int spacelen,
     521        int index) {
     522
     523        libtrace_meta_t *r = trace_get_interface_comment_meta(packet);
     524        if (r == NULL) { return NULL; }
     525        if (index > r->num) { return NULL; }
     526        if (r->items[index].len > spacelen) {
     527                memcpy(space, r->items[index].data, spacelen);
     528        } else {
     529                memcpy(space, r->items[index].data, r->items[index].len);
     530        }
     531        trace_destroy_meta(r);
     532        return space;
     533}
     534
     535/* Get the capture application for a meta packet
    286536 * Must be destroyed with trace_destroy_meta()
    287537 *
     
    289539 * @returns Pointer to libtrace_meta_t structure or NULL
    290540 */
    291 libtrace_meta_t *trace_get_interface_comment(libtrace_packet_t *packet) {
     541libtrace_meta_t *trace_get_capture_application_meta(libtrace_packet_t *packet) {
    292542        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
    293543                return NULL;
     
    295545
    296546        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    297                 return packet->trace->format->get_meta_section_item(packet,
    298                         ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT);
    299         }
    300         if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    301                 return packet->trace->format->get_meta_section_item(packet,
    302                         PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT);
    303         }
    304 
    305         return NULL;
    306 }
    307 
    308 /* Get the capture application for a meta packet
    309  * Must be destroyed with trace_destroy_meta()
    310  *
    311  * @params libtrace_packet_t packet
    312  * @returns Pointer to libtrace_meta_t structure or NULL
    313  */
    314 libtrace_meta_t *trace_get_capture_application(libtrace_packet_t *packet) {
    315         if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
    316                 return NULL;
    317         }
    318 
    319         if (packet->trace->format->type == TRACE_FORMAT_ERF) {
    320                 return packet->trace->format->get_meta_section_item(packet,
     547                return packet->trace->format->get_meta_section_option(packet,
    321548                        ERF_PROV_SECTION_CAPTURE, ERF_PROV_APP_NAME);
    322549        }
    323550        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
    324                 return packet->trace->format->get_meta_section_item(packet,
     551                return packet->trace->format->get_meta_section_option(packet,
    325552                        PCAPNG_SECTION_TYPE, PCAPNG_META_SHB_USERAPPL);
    326553        }
     
    328555        return NULL;
    329556}
    330 
    331 /* Get meta section option from a meta packet
     557/* Get the capture application for a meta packet.
     558 *
     559 * @params libtrace_packet_t meta packet to extract the application name from.
     560 * @params A pointer to a character buffer to store the application name in.
     561 * @params The size of the buffer passed in.
     562 * @returns Pointer to the character buffer containing the application name or NULL.
     563 */
     564char *trace_get_capture_application(libtrace_packet_t *packet, char *space, int spacelen) {
     565        libtrace_meta_t *r = trace_get_capture_application_meta(packet);
     566        if (r == NULL) { return NULL; }
     567        if (r->items[0].len > spacelen) {
     568                memcpy(space, r->items[0].data, spacelen);
     569        } else {
     570                memcpy(space, r->items[0].data, r->items[0].len);
     571        }
     572        trace_destroy_meta(r);
     573        return space;
     574}
     575
     576/* Get a meta section option from a meta packet
    332577 * Must be destroyed with trace_destroy_meta()
    333578 *
     
    344589        }
    345590
    346         return packet->trace->format->get_meta_section_item(packet,
     591        return packet->trace->format->get_meta_section_option(packet,
    347592                section_code, option_code);
    348593}
  • lib/libtrace.h.in

    rd4eed70 r66ffac4  
    37323732/*@}*/
    37333733
     3734
     3735
     3736DLLEXPORT int trace_destroy_meta(libtrace_meta_t *result);
     3737
     3738DLLEXPORT libtrace_meta_t *trace_get_interface_name_meta(libtrace_packet_t *packet);
     3739
     3740DLLEXPORT char *trace_get_interface_name(libtrace_packet_t *packet, char *space, int spacelen,
     3741        int index);
     3742
     3743DLLEXPORT libtrace_meta_t *trace_get_interface_mac_meta(libtrace_packet_t *packet);
     3744
     3745DLLEXPORT char *trace_get_interface_mac(libtrace_packet_t *packet, char *space, int spacelen,
     3746        int index);
     3747
     3748DLLEXPORT libtrace_meta_t *trace_get_interface_speed_meta(libtrace_packet_t *packet);
     3749
     3750DLLEXPORT uint64_t trace_get_interface_speed(libtrace_packet_t *packet, int index);
     3751
     3752
     3753DLLEXPORT libtrace_meta_t *trace_get_interface_ipv4_meta(libtrace_packet_t *packet);
     3754
     3755DLLEXPORT uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet, int index);
     3756
     3757DLLEXPORT char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
     3758        int index);
     3759
     3760DLLEXPORT libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet);
     3761
     3762DLLEXPORT void *trace_get_interface_ipv6(libtrace_packet_t *packet, void *space, int spacelen,
     3763        int index);
     3764
     3765DLLEXPORT char *trace_get_interface_ipv6_string(libtrace_packet_t *packet, char *space, int spacelen,
     3766        int index);
     3767
     3768DLLEXPORT libtrace_meta_t *trace_get_interface_description_meta(libtrace_packet_t *packet);
     3769
     3770DLLEXPORT char *trace_get_interface_description(libtrace_packet_t *packet, char *space, int spacelen,
     3771
     3772        int index);
     3773
     3774DLLEXPORT libtrace_meta_t *trace_get_host_os_meta(libtrace_packet_t *packet);
     3775
     3776DLLEXPORT char *trace_get_host_os(libtrace_packet_t *packet, char *space, int spacelen);
     3777
     3778DLLEXPORT libtrace_meta_t *trace_get_interface_fcslen_meta(libtrace_packet_t *packet);
     3779
     3780DLLEXPORT uint32_t trace_get_interface_fcslen(libtrace_packet_t *packet, int index);
     3781
     3782DLLEXPORT libtrace_meta_t *trace_get_interface_hardware_description_meta(libtrace_packet_t *packet);
     3783
     3784DLLEXPORT char *trace_get_interface_hardware_description(libtrace_packet_t *packet, char *space,
     3785        int spacelen);
     3786
     3787DLLEXPORT libtrace_meta_t *trace_get_interface_comment_meta(libtrace_packet_t *packet);
     3788
     3789DLLEXPORT char *trace_get_interface_comment(libtrace_packet_t *packet, char *space, int spacelen,
     3790        int index);
     3791
     3792DLLEXPORT libtrace_meta_t *trace_get_capture_application_meta(libtrace_packet_t *packet);
     3793
     3794DLLEXPORT char *trace_get_capture_application(libtrace_packet_t *packet, char *space, int spacelen);
     3795
     3796DLLEXPORT libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code,
     3797        uint16_t option_code);
     3798
     3799DLLEXPORT libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code);
     3800
     3801
    37343802#ifdef __cplusplus
    37353803} /* extern "C" */
    37363804#endif /* #ifdef __cplusplus */
    37373805
    3738 int trace_destroy_meta(libtrace_meta_t *result);
    3739 libtrace_meta_t *trace_get_interface_name(libtrace_packet_t *packet);
    3740 libtrace_meta_t *trace_get_interface_mac(libtrace_packet_t *packet);
    3741 libtrace_meta_t *trace_get_interface_speed(libtrace_packet_t *packet);
    3742 libtrace_meta_t *trace_get_interface_ip4(libtrace_packet_t *packet);
    3743 libtrace_meta_t *trace_get_interface_ipv4(libtrace_packet_t *packet);
    3744 libtrace_meta_t *trace_get_interface_ip6(libtrace_packet_t *packet);
    3745 libtrace_meta_t *trace_get_interface_ipv6(libtrace_packet_t *packet);
    3746 libtrace_meta_t *trace_get_interface_description(libtrace_packet_t *packet);
    3747 libtrace_meta_t *trace_get_interface_num(libtrace_packet_t *packet);
    3748 libtrace_meta_t *trace_get_host_os(libtrace_packet_t *packet);
    3749 libtrace_meta_t *trace_get_interface_frame_check_sequence_length(libtrace_packet_t *packet);
    3750 libtrace_meta_t *trace_get_interface_hardware_description(libtrace_packet_t *packet);
    3751 libtrace_meta_t *trace_get_interface_comment(libtrace_packet_t *packet);
    3752 libtrace_meta_t *trace_get_capture_application(libtrace_packet_t *packet);
    3753 libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code,
    3754         uint16_t option_code);
    3755 libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code);
    3756 
    37573806#endif /* LIBTRACE_H_ */
  • lib/libtrace_int.h

    ref5ba20 r66ffac4  
    725725        /**
    726726         */
    727         void *(*get_meta_section)(libtrace_packet_t *packet, uint32_t section_type);
    728         void *(*get_meta_section_item)(libtrace_packet_t *packet, uint32_t section_type,
    729                 uint16_t section);
     727        void *(*get_meta_section)(libtrace_packet_t *packet, uint32_t section);
     728        void *(*get_meta_section_option)(libtrace_packet_t *packet, uint32_t section,
     729                uint16_t option);
    730730
    731731        /** Moves the read pointer to a certain ERF timestamp within an input
Note: See TracChangeset for help on using the changeset viewer.