Changeset 7d86ee2
- Timestamp:
- 11/07/18 11:34:25 (4 years ago)
- Branches:
- develop
- Children:
- 93f4c64
- Parents:
- 774b237
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
examples/tutorial/ipdist.c
r774b237 r7d86ee2 33 33 /* Commands that need to be sent to gnuplot */ 34 34 char *commands[] = {"set term png size 1280,960", 35 "set title 'IP Distrubtion'", 35 36 "set xrange [0:255]", 37 "set xlabel 'Prefix'", 38 "set ylabel 'Hits'", 36 39 "set xtics 0,10,255", 37 40 "set output 'ipdist.png'", … … 41 44 FILE *gnuplot = popen("gnuplot -persistent", "w"); 42 45 /* send all commands to gnuplot */ 43 for(i=0;i< 6;i++) {46 for(i=0;i<9;i++) { 44 47 fprintf(gnuplot, "%s \n", commands[i]); 45 48 } … … 78 81 uint32_t address = htonl(ip4.s_addr); 79 82 80 /* Check if the address is part of an excluded network. 81 This is performed before splitting the into octets so if performed over entire address. */ 83 /* Check if the address is part of an excluded network. */ 82 84 if(network_excluded(address) == 0) { 83 85 … … 89 91 octet[3] = (address & 0x000000ff); 90 92 91 /* check if the supplied address was a source or destination, increment92 the correct one */93 /* check if the supplied address was a source or destination, 94 increment the correct one */ 93 95 if(srcaddr) { 94 96 srcaddrcount[octet[0]]++; … … 97 99 } 98 100 } 99 100 101 } 101 102 } … … 118 119 process_ip(address, 0); 119 120 } 120 121 } 122 123 static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { 124 /* Only destroy trace and packet if they are not NULL */ 125 if(trace) { 126 trace_destroy(trace); 127 } 128 if(packet) { 129 trace_destroy_packet(packet); 130 } 121 131 } 122 132 … … 128 138 /* Ensure the input URI was supplied */ 129 139 if(argc < 2) { 130 fprintf(stderr, "Usage: %s inputURI\n", argv[0]); 140 fprintf(stderr, "Usage: %s inputURI [excluded networks]\n", argv[0]); 141 fprintf(stderr, " eg. ./ipdist input.erf 210.10.3.0/24 70.5.0.0/16\n"); 131 142 return 1; 132 143 } … … 149 160 char delim[] = "/"; 150 161 // Convert supplied address and mask to a exclude_network structure 151 for(i= 2;i<argc;i++) {152 char *address = strtok(argv[i ], delim);162 for(i=0;i<argc-2;i++) { 163 char *address = strtok(argv[i+2], delim); 153 164 char *mask = strtok(NULL, delim); 154 165 166 /* Check the subnet mask is valid */ 167 if(atoi(mask) == 0 || atoi(mask) > 32 || atoi(mask) < 0) { 168 fprintf(stderr, "Invalid subnet mask: %s\n", mask); 169 return 1; 170 } 155 171 /* right shift so netmask is in network byte order */ 156 exclude[i -2].mask = 0xffffffff << (32 - atoi(mask));172 exclude[i].mask = 0xffffffff << (32 - atoi(mask)); 157 173 158 174 struct in_addr addr; 159 /* Convert address string into uint32_t */ 160 inet_aton(address, &addr); 175 /* Convert address string into uint32_t and check its valid*/ 176 if(inet_aton(address, &addr) == 0) { 177 fprintf(stderr, "Invalid exclude address: %s\n", address); 178 return 1; 179 } 161 180 /* Ensure its saved in network byte order */ 162 exclude[i -2].address = htonl(addr.s_addr);181 exclude[i].address = htonl(addr.s_addr); 163 182 164 183 /* Calculate the network address */ 165 exclude[i -2].network = exclude[i-2].address & exclude[i-2].mask;184 exclude[i].network = exclude[i].address & exclude[i].mask; 166 185 } 167 186 168 187 /* Create the packet structure */ 169 188 packet = trace_create_packet(); 189 /* Ensure no error has occured creating the packet */ 190 if(packet == NULL) { 191 perror("Creating libtrace packet"); 192 libtrace_cleanup(trace, packet); 193 return 1; 194 } 170 195 171 196 /* Create the trace */ 172 197 trace = trace_create(argv[1]); 173 174 198 /* Ensure no error has occured creating the trace */ 175 199 if(trace_is_err(trace)) { 176 200 trace_perror(trace, "Opening trace file"); 201 libtrace_cleanup(trace, packet); 177 202 return 1; 178 203 } … … 181 206 if(trace_start(trace) == -1) { 182 207 trace_perror(trace, "Starting trace"); 183 trace_destroy(trace);208 libtrace_cleanup(trace, packet); 184 209 return 1; 185 210 } … … 194 219 if(trace_is_err(trace)) { 195 220 trace_perror(trace, packet); 221 libtrace_cleanup(trace, packet); 196 222 return 1; 197 223 } … … 203 229 plot_results(); 204 230 205 trace_destroy(trace);206 trace_destroy_packet(packet);207 //libtrace_cleanup(trace, packet); 231 /* cleanup */ 232 libtrace_cleanup(trace, packet); 233 208 234 return 0; 209 235 }
Note: See TracChangeset
for help on using the changeset viewer.