Changeset 2f07199


Ignore:
Timestamp:
11/09/18 09:43:58 (2 years ago)
Author:
Jacob Van Walraven <jcv9@…>
Branches:
develop
Children:
acffeb8
Parents:
3687ca2
Message:

Cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • examples/tutorial/ipdist-parallel.c

    r3687ca2 r2f07199  
    4141};
    4242
    43 /* interval between outputs */
     43/* interval between outputs in seconds */
    4444uint64_t tickrate;
     45/* The octet which to plot results for */
     46int octet = 0;
    4547
    4648static void per_tick(libtrace_t *trace, libtrace_thread_t *thread, void *global, void *tls, uint64_t tick) {
     
    119121
    120122                        /* Split the IPv4 address into each octet */
    121                         uint8_t octet[4];
    122                         octet[0] = (address & 0xff000000) >> 24;
    123                         octet[1] = (address & 0x00ff0000) >> 16;
    124                         octet[2] = (address & 0x0000ff00) >> 8;
    125                         octet[3] = (address & 0x000000ff);
     123                        uint8_t octets[4];
     124                        octets[0] = (address & 0xff000000) >> 24;
     125                        octets[1] = (address & 0x00ff0000) >> 16;
     126                        octets[2] = (address & 0x0000ff00) >> 8;
     127                        octets[3] = (address & 0x000000ff);
    126128
    127129                        /* check if the supplied address was a source or destination,
    128130                           increment the correct one */
    129131                        if(srcaddr) {
    130                                 local->src[octet[0]]++;
     132                                local->src[octets[octet]]++;
    131133                        } else {
    132                                 local->dst[octet[0]]++;
     134                                local->dst[octets[octet]]++;
    133135                        }
    134136                }
     
    177179
    178180                /* We only want to call per_tick if we are due to output something
    179                  * Right shifting these converts them to seconds */
    180                 if((timestamp >> 32) >= (local->lastkey >> 32) + (tickrate/1000)) {
     181                 * Right shifting these converts them to seconds, tickrate is in seconds */
     182                if((timestamp >> 32) >= (local->lastkey >> 32) + tickrate) {
    181183                        per_tick(trace, thread,global, local, timestamp);
    182184                        local->lastkey = timestamp;
     
    295297
    296298        /* If the current timestamp is greater than the last printed plus the interval, output a result */
    297         if((key >> 32) >= (tally->lastkey >> 32) + (tickrate/1000)) {
     299        if((key >> 32) >= (tally->lastkey >> 32) + tickrate) {
    298300
    299301                /* update last key */
     
    329331}
    330332
     333static void libtrace_cleanup(libtrace_t *trace, libtrace_callback_set_t *processing,
     334        libtrace_callback_set_t *reporting) {
     335        /* Only destroy if the structure exists */
     336        if(trace) {
     337                trace_destroy(trace);
     338        }
     339        if(processing) {
     340                trace_destroy_callback_set(processing);
     341        }
     342        if(reporting) {
     343                trace_destroy_callback_set(reporting);
     344        }
     345}
     346
     347/* Converts a string representation eg 1.2.3.4/24 into a network structure */
     348static int get_network(char *network_string, struct network *network) {
     349
     350        char delim[] = "/";
     351        /* Split the address and mask portion of the string */
     352        char *address = strtok(network_string, delim);
     353        char *mask = strtok(NULL, delim);
     354
     355        /* Check the subnet mask is valid */
     356        if(atoi(mask) == 0 || atoi(mask) > 32 || atoi(mask) < 0) {
     357                return 1;
     358        }
     359        /* right shift so netmask is in network byte order */
     360        network->mask = 0xffffffff << (32 - atoi(mask));
     361
     362        struct in_addr addr;
     363        /* Convert address string into uint32_t and check its valid */
     364        if(inet_aton(address, &addr) == 0) {
     365                return 2;
     366        }
     367        /* Ensure its saved in network byte order */
     368        network->address = htonl(addr.s_addr);
     369
     370        /* Calculate the network address */
     371        network->network = network->address & network->mask;
     372
     373        return 0;
     374}
     375
    331376int main(int argc, char *argv[]) {
    332377
     
    337382        /* Ensure the input URI was supplied */
    338383        if(argc < 3) {
    339                 fprintf(stderr, "Usage: %s inputURI outputInterval [excluded networks]\n", argv[0]);
    340                 fprintf(stderr, "       eg. ./ipdist input.erf 10000 210.10.3.0/24 70.5.0.0/16\n");
     384                fprintf(stderr, "Usage: %s inputURI [outputInterval (Seconds)] [excluded networks]\n", argv[0]);
     385                fprintf(stderr, "       eg. ./ipdist input.erf 60 210.10.3.0/24 70.5.0.0/16\n");
    341386                return 1;
    342387        }
    343         /* Convert tick into correct format */
     388        /* Convert tick into an int */
    344389        tickrate = atoi(argv[2]);
    345390
     
    373418        /* Set the tick interval only if this is a live capture */
    374419        if(trace_get_information(trace)->live) {
    375                 trace_set_tick_interval(trace, tickrate);
     420                /* tickrate is in seconds but tick_interval expects milliseconds */
     421                trace_set_tick_interval(trace, tickrate*1000);
    376422        }
    377423        /* Do not buffer the reports */
     
    384430        if(exclude == NULL || exclude->networks == NULL) {
    385431                fprintf(stderr, "Unable to allocate memory");
     432                libtrace_cleanup(trace, processing, reporter);
    386433                return 1;
    387434        }
    388435        exclude->count = 0;
    389436
    390         char delim[] = "/";
    391437        int i;
    392438        for(i=0;i<argc-3;i++) {
    393                 char *address = strtok(argv[i+3], delim);
    394                 char *mask = strtok(NULL, delim);
    395 
    396                 /* Check the subnet mask is valid */
    397                 if(atoi(mask) == 0 || atoi(mask) > 32 || atoi(mask) < 0) {
    398                         fprintf(stderr, "Invalid subnet mask: %s\n", mask);
    399                         return 1;
    400                 }
    401                 /* right shift so netmask is in network byte order */
    402                 exclude->networks[i].mask = 0xffffffff << (32 - atoi(mask));
    403 
    404                 struct in_addr addr;
    405                 /* Convert address string into uint32_t and check its valid */
    406                 if(inet_aton(address, &addr) == 0) {
    407                         fprintf(stderr, "Invalid exclude address: %s\n", address);
    408                         return 1;
    409                 }
    410                 /* Ensure its saved in network byte order */
    411                 exclude->networks[i].address = htonl(addr.s_addr);
    412 
    413                 /* Calculate the network address */
    414                 exclude->networks[i].network = exclude->networks[i].address & exclude->networks[i].mask;
    415 
    416                 /* Increment counter of excluded networks */
     439                /* convert the network string into a network structure */
     440                get_network(argv[i+3], &exclude->networks[i]);
     441                /* increment the count of excluded networks */
    417442                exclude->count += 1;
    418443        }
     
    421446        if(trace_pstart(trace, exclude, processing, reporter)) {
    422447                trace_perror(trace, "Starting parallel trace");
     448                libtrace_cleanup(trace, processing, reporter);
    423449                return 1;
    424450        }
     
    430456        free(exclude->networks);
    431457        free(exclude);
    432         trace_destroy(trace);
    433         trace_destroy_callback_set(processing);
    434         trace_destroy_callback_set(reporter);
     458        libtrace_cleanup(trace, processing, reporter);
    435459
    436460        return 0;
Note: See TracChangeset for help on using the changeset viewer.