1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h>
#define MAXLIMIT 64 #define IPCOUNT 165158 #define MOBILECOUNT 49664
struct row { in_addr_t start_ip; in_addr_t end_ip; char code[16]; };
void resolve_file(FILE *fp, struct row all_data[]); in_addr_t addr_str_to_int(char *cp); void int_to_addr_str(in_addr_t start, in_addr_t end, char *base_code, char *mobile_code);
int main(int argc, char *argv[]) { FILE *fp; char *base_ip = "../base_ip.csv"; char *mobile_ip = "../mobile_ip.csv"; struct row struct_base_ip[IPCOUNT]; struct row struct_mobile_ip[MOBILECOUNT];
if ((fp = fopen(base_ip, "r")) == NULL) { printf("cat: can't open %s\n", base_ip); exit(1); } else { resolve_file(fp, struct_base_ip); } if ((fp = fopen(mobile_ip, "r")) == NULL) { printf("cat: can't open %s\n", mobile_ip); exit(1); } else { resolve_file(fp, struct_mobile_ip); }
char *line = "start_ip,end_ip,base_code,mobile_code"; printf("%s\n", line); in_addr_t start = 0, end = 0; int i = 0, j = 0, is_mobile_out = 0;; while (i < IPCOUNT) { if (j < MOBILECOUNT) { if (struct_mobile_ip[j].start_ip > struct_base_ip[i].start_ip) { start = struct_base_ip[i].start_ip; if (struct_mobile_ip[j].start_ip > struct_base_ip[i].end_ip) { end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, "NONE"); ++i; } else if (struct_mobile_ip[j].end_ip > struct_base_ip[i].end_ip) { end = struct_mobile_ip[j].start_ip - 1; int_to_addr_str(start, end, struct_base_ip[i].code, "NONE"); start = struct_mobile_ip[j].start_ip; end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); ++i; } else if (struct_mobile_ip[j].end_ip < struct_base_ip[i].end_ip){ end = struct_mobile_ip[j].start_ip - 1; int_to_addr_str(start, end, struct_base_ip[i].code, "NONE"); start = struct_mobile_ip[j].start_ip; end = struct_mobile_ip[j].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); struct_base_ip[i].start_ip = struct_mobile_ip[j].end_ip + 1; ++j; } else if (struct_mobile_ip[j].end_ip > struct_base_ip[i].end_ip) { end = struct_mobile_ip[j].start_ip - 1; int_to_addr_str(start, end, struct_base_ip[i].code, "NONE"); start = struct_mobile_ip[j].start_ip; end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); ++i; ++j; } } else if (struct_mobile_ip[j].start_ip < struct_base_ip[i].start_ip) { if (struct_base_ip[i].start_ip > struct_mobile_ip[j].end_ip) { start = struct_mobile_ip[j].start_ip; end = struct_mobile_ip[j].end_ip; int_to_addr_str(start, end, "XXX", struct_mobile_ip[j].code); ++j; } else if (struct_base_ip[i].end_ip > struct_mobile_ip[j].end_ip) { start = struct_base_ip[i].start_ip; end = struct_mobile_ip[j].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); struct_base_ip[i].start_ip = struct_mobile_ip[j].end_ip + 1; ++j; } else if (struct_base_ip[i].end_ip < struct_mobile_ip[j].end_ip) { start = struct_base_ip[i].start_ip; end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); ++i; } else if (struct_base_ip[i].end_ip == struct_mobile_ip[j].end_ip) { start = struct_base_ip[i].start_ip; end = struct_mobile_ip[j].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); struct_base_ip[i].start_ip = struct_mobile_ip[j].end_ip + 1; ++j; ++i; } } else { start = struct_base_ip[i].start_ip; if (struct_base_ip[i].end_ip > struct_mobile_ip[j].end_ip) { end = struct_mobile_ip[j].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); struct_base_ip[i].start_ip = struct_mobile_ip[j].end_ip + 1; ++j; } else if (struct_base_ip[i].end_ip < struct_mobile_ip[j].end_ip) { end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); ++i; } else { end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, struct_mobile_ip[j].code); ++i; ++j; } } } else { if (!is_mobile_out) { start = struct_mobile_ip[MOBILECOUNT - 1].end_ip + 1; is_mobile_out = 1; } else { start = struct_base_ip[i].start_ip; } end = struct_base_ip[i].end_ip; int_to_addr_str(start, end, struct_base_ip[i].code, "NONE"); ++i; } }
exit(0); }
void resolve_file(FILE *fp, struct row all_data[]) { char line[MAXLIMIT], s_start_ip[16], s_end_ip[16]; char *lp; int i = 0; while ((lp = fgets(line, MAXLIMIT, fp)) != NULL) { sscanf(lp, "%[^,],%[^,],%s", s_start_ip, s_end_ip, (&all_data[i])->code); (&all_data[i])->start_ip = addr_str_to_int(s_start_ip); (&all_data[i])->end_ip = addr_str_to_int(s_end_ip); i++; } fclose(fp); }
in_addr_t addr_str_to_int(char *cp) { return ntohl(inet_addr(cp)); }
void int_to_addr_str(in_addr_t start, in_addr_t end, char *base_code, char *mobile_code) { start = htonl(start); end = htonl(end); char start_str[16], end_str[16]; unsigned char *start_bytes = (unsigned char *) &start; unsigned char *end_bytes = (unsigned char *) &end; snprintf (start_str, sizeof (start_str), "%d.%d.%d.%d", start_bytes[0], start_bytes[1], start_bytes[2], start_bytes[3]); snprintf (end_str, sizeof (start_str), "%d.%d.%d.%d", end_bytes[0], end_bytes[1], end_bytes[2], end_bytes[3]); printf("%s,%s,%s,%s\n", start_str, end_str, base_code, mobile_code); }
|