Changeset 8753bb8


Ignore:
Timestamp:
01/10/12 14:23:53 (10 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:
66e6d16
Parents:
a3041a4
Message:
  • Moved OSPF code into a separate source file (hope you're happy, Perry!) :)
Location:
lib
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • lib/Makefile.am

    r9589941 r8753bb8  
    6262                linktypes.c link_wireless.c \
    6363                protocols_pktmeta.c protocols_l2.c protocols_l3.c \
    64                 protocols_transport.c protocols.h \
     64                protocols_transport.c protocols.h protocols_ospf.c \
    6565                $(DAGSOURCE) format_erf.h \
    6666                $(BPFJITSOURCE) \
  • lib/protocols_transport.c

    rcc6fcee r8753bb8  
    194194}
    195195
    196 DLLEXPORT void *trace_get_ospf_header(libtrace_packet_t *packet,
    197                 uint8_t *version, uint32_t *remaining) {
    198         uint8_t proto;
    199         void *ospf;
    200         uint32_t dummy_rem = 0;
    201 
    202 
    203         if (!remaining)
    204                 remaining = &dummy_rem;
    205 
    206         assert(version != NULL && "version may not be NULL when calling trace_get_ospf_header!");
    207 
    208         ospf = trace_get_transport(packet, &proto, remaining);
    209 
    210         if (!ospf || proto != TRACE_IPPROTO_OSPF || *remaining == 0)
    211                 return NULL;
    212 
    213         *version = *((uint8_t *)ospf);
    214        
    215         if (*version == 2 && *remaining < sizeof(libtrace_ospf_v2_t))
    216                 return NULL;
    217 
    218         return ospf;
    219 }
    220 
    221 DLLEXPORT void *trace_get_ospf_contents_v2(libtrace_ospf_v2_t *header,
    222                 uint8_t *ospf_type, uint32_t *remaining) {
    223 
    224         uint8_t dummy_type;
    225         char *ptr;
    226        
    227         assert(remaining != NULL && "remaining may not be NULL when calling trace_get_ospf_contents!");
    228 
    229         if (!ospf_type)
    230                 ospf_type = &dummy_type;
    231 
    232         if (!header || *remaining < sizeof(libtrace_ospf_v2_t)) {
    233                 *ospf_type = 0;
    234                 *remaining = 0;
    235                 return NULL;
    236         }
    237        
    238         *ospf_type = header->type;
    239 
    240         ptr = ((char *)header) + sizeof(libtrace_ospf_v2_t);
    241         *remaining -= sizeof(libtrace_ospf_v2_t);
    242 
    243         return (void *)ptr;
    244 
    245 }
    246 
    247 DLLEXPORT unsigned char *trace_get_first_ospf_link_from_router_lsa_v2(
    248                 libtrace_ospf_router_lsa_v2_t *lsa,
    249                 uint32_t *remaining) {
    250 
    251         unsigned char *link_ptr = NULL;
    252         assert(remaining != NULL && "remaining may not be NULL when calling trace_get_first_link_from_router_lsa_v2!");
    253 
    254         if (!lsa || *remaining < sizeof(libtrace_ospf_router_lsa_v2_t)) {
    255                 *remaining = 0;
    256                 return NULL;
    257         }
    258 
    259         link_ptr = ((unsigned char *)lsa) + sizeof(libtrace_ospf_router_lsa_v2_t);
    260         *remaining -= sizeof(libtrace_ospf_router_lsa_v2_t);
    261         return link_ptr;
    262 
    263 }
    264 
    265 DLLEXPORT unsigned char *trace_get_first_ospf_lsa_from_db_desc_v2(
    266                 libtrace_ospf_db_desc_v2_t *db_desc,
    267                 uint32_t *remaining) {
    268 
    269         unsigned char *lsa_ptr = NULL;
    270 
    271         assert(remaining != NULL && "remaining may not be NULL when calling trace_get_first_ospf_v2_lsa!");
    272 
    273         if (!db_desc || *remaining < sizeof(libtrace_ospf_db_desc_v2_t)) {
    274                 *remaining = 0;
    275                 return NULL;
    276         }
    277        
    278         lsa_ptr = ((unsigned char *)db_desc) + sizeof(libtrace_ospf_db_desc_v2_t);
    279         *remaining -= sizeof(libtrace_ospf_db_desc_v2_t);
    280 
    281         return lsa_ptr;
    282 }
    283 
    284 DLLEXPORT unsigned char *trace_get_first_ospf_lsa_from_update_v2(
    285                 libtrace_ospf_ls_update_t *ls_update,
    286                 uint32_t *remaining) {
    287 
    288         unsigned char *lsa_ptr = NULL;
    289 
    290         assert(remaining != NULL && "remaining may not be NULL when calling trace_get_first_ospf_v2_lsa!");
    291 
    292         if (!ls_update || *remaining < sizeof(libtrace_ospf_ls_update_t)) {
    293                 *remaining = 0;
    294                 return NULL;
    295         }
    296        
    297         lsa_ptr = ((unsigned char *)ls_update) + sizeof(libtrace_ospf_ls_update_t);
    298         *remaining -= sizeof(libtrace_ospf_ls_update_t);
    299 
    300         return lsa_ptr;
    301 }
    302 
    303 DLLEXPORT uint32_t trace_get_ospf_metric_from_as_external_lsa_v2(
    304                 libtrace_ospf_as_external_lsa_v2_t *as_lsa) {
    305 
    306         uint32_t metric = 0;
    307 
    308         assert(as_lsa);
    309 
    310         metric = as_lsa->metric_a << 16;
    311         metric |= (as_lsa->metric_b << 8);
    312         metric |= as_lsa->metric_c;
    313 
    314         return metric;
    315 }
    316 
    317 DLLEXPORT uint32_t trace_get_ospf_metric_from_summary_lsa_v2(
    318                 libtrace_ospf_summary_lsa_v2_t *sum_lsa) {
    319 
    320         uint32_t metric = 0;
    321 
    322         assert(sum_lsa);
    323 
    324         metric = sum_lsa->metric_a << 16;
    325         metric |= (sum_lsa->metric_b << 8);
    326         metric |= sum_lsa->metric_c;
    327 
    328         return metric;
    329 }
    330 
    331 DLLEXPORT int trace_get_next_ospf_link_v2(unsigned char **current,
    332                 libtrace_ospf_link_v2_t **link,
    333                 uint32_t *remaining,
    334                 uint32_t *link_len) {
    335 
    336         if (*current == NULL || *remaining < sizeof(libtrace_ospf_link_v2_t)) {
    337                 *remaining = 0;
    338                 *link = NULL;
    339                 return 0;
    340         }
    341 
    342         *link = (libtrace_ospf_link_v2_t *)*current;
    343 
    344         /* XXX The spec allows for multiple metrics for a single link. This
    345          * approach won't support this, so we may need to be more intelligent
    346          * about this in future */
    347         *remaining -= sizeof(libtrace_ospf_link_v2_t);
    348         *link_len = sizeof(libtrace_ospf_link_v2_t);
    349         *current += sizeof(libtrace_ospf_link_v2_t);
    350        
    351         return 1;
    352 
    353 }
    354 
    355 DLLEXPORT int trace_get_next_ospf_lsa_header_v2(unsigned char **current,
    356                 libtrace_ospf_lsa_v2_t **lsa_hdr,
    357                 uint32_t *remaining,
    358                 uint8_t *lsa_type,
    359                 uint16_t *lsa_length) {
    360        
    361         int valid_lsa = 0;
    362 
    363         if (*current == NULL || *remaining < sizeof(libtrace_ospf_lsa_v2_t)) {
    364                 *lsa_hdr = NULL;
    365                 *remaining = 0;
    366                 return 0;
    367 
    368         }
    369 
    370         *lsa_hdr = (libtrace_ospf_lsa_v2_t *)(*current);
    371        
    372         /* Check that the LSA type is valid */
    373         switch ((*lsa_hdr)->lsa_type) {
    374                 case TRACE_OSPF_LS_ROUTER:
    375                 case TRACE_OSPF_LS_NETWORK:
    376                 case TRACE_OSPF_LS_SUMMARY:
    377                 case TRACE_OSPF_LS_ASBR_SUMMARY:
    378                 case TRACE_OSPF_LS_EXTERNAL:
    379                         valid_lsa = 1;
    380                         break;
    381         }
    382        
    383         /* This function is for reading LSA headers only, e.g. those in DB desc
    384          * or LS Ack packets. As such, I'm going to set the type and length to
    385          * values that should prevent anyone from trying to treat subsequent
    386          * payload as an LSA body */
    387         *lsa_type = 0;
    388         *lsa_length = sizeof(libtrace_ospf_lsa_v2_t);
    389 
    390         if (!valid_lsa) {
    391                 *remaining = 0;
    392                 return -1;
    393         }
    394        
    395         *remaining -= *lsa_length;
    396         *current += *lsa_length;
    397 
    398         if (remaining == 0) {
    399                 /* No more LSAs */
    400                 return 0;
    401         }
    402 
    403         return 1;
    404 }
    405 
    406 DLLEXPORT int trace_get_next_ospf_lsa_v2(unsigned char **current,
    407                 libtrace_ospf_lsa_v2_t **lsa_hdr,
    408                 unsigned char **lsa_body,
    409                 uint32_t *remaining,
    410                 uint8_t *lsa_type,
    411                 uint16_t *lsa_length) {
    412 
    413         int valid_lsa = 0;
    414 
    415         if (*current == NULL || *remaining < sizeof(libtrace_ospf_lsa_v2_t)) {
    416                 *lsa_hdr = NULL;
    417                 *lsa_body = NULL;
    418                 *remaining = 0;
    419 
    420                 return 0;
    421 
    422         }
    423 
    424         *lsa_hdr = (libtrace_ospf_lsa_v2_t *)(*current);
    425         *lsa_type = (*lsa_hdr)->lsa_type;
    426         *lsa_length = ntohs((*lsa_hdr)->length);
    427 
    428         /* Check that the LSA type is valid */
    429         switch (*lsa_type) {
    430                 case TRACE_OSPF_LS_ROUTER:
    431                 case TRACE_OSPF_LS_NETWORK:
    432                 case TRACE_OSPF_LS_SUMMARY:
    433                 case TRACE_OSPF_LS_ASBR_SUMMARY:
    434                 case TRACE_OSPF_LS_EXTERNAL:
    435                         valid_lsa = 1;
    436                         break;
    437         }
    438                
    439         if (*lsa_length > *remaining || !valid_lsa) {
    440                 /* LSA is incomplete or an invalid type.
    441                  *
    442                  * If this occurs, you've probably managed to read something
    443                  * that is NOT a legit LSA */
    444                 *remaining = 0;
    445                 *lsa_body = NULL;
    446                 return -1;
    447         }
    448        
    449         /* Some OSPF packets, e.g. LS ACKs, only contain LSA headers. If this
    450          * is the case, we'll set the body pointer to NULL so the caller
    451          * can't read invalid data */
    452         if (*lsa_length == sizeof(libtrace_ospf_lsa_v2_t))
    453                 *lsa_body = NULL;
    454         else
    455                 *lsa_body = (*current + sizeof(libtrace_ospf_lsa_v2_t));
    456 
    457         *remaining -= *lsa_length;
    458         *current += *lsa_length;
    459 
    460         if (remaining == 0) {
    461                 /* No more LSAs */
    462                 return 0;
    463         }
    464 
    465         return 1;
    466 
    467 }
    468 
    469196DLLEXPORT libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet) {
    470197        uint8_t proto;
Note: See TracChangeset for help on using the changeset viewer.