raynstard #2074(2007/08/12 05:08 GMT) [ C ] Rating1/1=1.00
Cの投稿がなかったので作ってみました。 夏休み万歳! ばかでかくなってしまってごめんなさい。 半分くらいはリスト管理です。 並列化はしていませんorz 入力が1つしかないので シリアルでも十分な性能は出ていると 思いますけど、動作テストしただけだし 大きなログを持っていないのでそのへんは適当です。 マルチスレッドにするのならどこをやるんだろう? BTREEにして分割検索? #名前解決ができないと3秒くらい止まってしまうのはうちの環境だけ? gcc -std=c99 -Wall doukaku039.c -o a
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <memory.h> #include <string.h> #include <sys/types.h> #include <netdb.h> #include <arpa/inet.h> #include <netinet/in.h> #ifdef TEST #define DPRINTF(fmt, ...) { printf("#debug(%4d)@%-10s: ", __LINE__, __func__); \ printf(fmt, ## __VA_ARGS__); fflush(stdout); } #else #define DPRINTF(fmt, ...) { ; } #endif typedef struct tagHOSTINFO { struct in_addr addr; char IPv4[16+1]; char hostname[63]; struct tagHOSTINFO *next; } HOSTINFO; typedef struct tagROOTNODE { int count; struct tagHOSTINFO *begin; struct tagHOSTINFO *end; } ROOTNODE; int compare_IPv4(const HOSTINFO a, const HOSTINFO b ) { if( a.addr.s_addr < b.addr.s_addr ) return -1; if( a.addr.s_addr > b.addr.s_addr ) return 1; return 0; } int clear_list(const ROOTNODE root) { int n=1; HOSTINFO *cursor; HOSTINFO *store; for( cursor=root.begin; n<=root.count; n ++) { #ifdef TEST printf("%2d:[%s] %s\n", n, cursor->IPv4, cursor->hostname); #endif store = cursor; cursor = cursor->next; free(store); } free(store); return 0; } int addList(ROOTNODE *root, HOSTINFO *before, const HOSTINFO info) { HOSTINFO *item = NULL; int result; /* 追加用のキッシュアイテム 作成 */ DPRINTF("登録(%d):[%s]\n", root->count+1,info.IPv4); item = (HOSTINFO *)calloc( 1, sizeof(HOSTINFO) ); if( item == NULL ) { return -1; } memcpy(item, &info, sizeof(HOSTINFO)); item->next = item; /* IPアドレスの変換 */ result = inet_aton( item->IPv4, &item->addr ); if( result == 0 ) { DPRINTF("IPアドレスの書式不正:[%s]\n", info.IPv4); free(item); return -1; } #ifdef TEST strncpy(item->hostname, "HOSTNAME", sizeof(item->hostname)); #else /* ホスト名取得 */ struct hostent *ent; ent = gethostbyaddr((const char *)&item->addr.s_addr, sizeof(item->addr.s_addr), AF_INET); if( ent != NULL ) { strncpy(item->hostname, ent->h_name, sizeof(item->hostname)); } #endif root->count ++; /* キッシュした数*/ if( root->count == 1 ) { root->begin = item; root->end = item; return 0; } /* 挿入 before item cursor */ if( before == NULL ) { /* 先頭へ挿入 */ item->next = root->begin; root->begin = item; } else { if( root->end == before ) { /* 最後尾へ追加 */ before->next = item; root->end = item; } else { /* 中間へ挿入 */ item->next = before->next; before->next = item; } } return 0; } int find(ROOTNODE *root, HOSTINFO *in) { static HOSTINFO *cache = NULL; HOSTINFO *cursor = cache; HOSTINFO *before = root->begin; HOSTINFO *term = root->end; int compare_status = 0; int result ; int n ; memset(in->hostname, '\0', sizeof(in->hostname)); result = inet_aton( in->IPv4, &in->addr ); if( result == 0 ) { DPRINTF("IPアドレスの書式不正:[%s]\n", in->IPv4); return 1; } /* 検索範囲設定 */ if( root->begin == NULL ) { /* 初めてなので強制的に追加 */ result = addList(root, NULL, *in); if( result == 0 ) { cache = root->begin; return find(root, in); } return 1; } if( cursor != NULL ) { compare_status = compare_IPv4(*cursor, *in); //DPRINTF("a:[%s] / b:[%s] result=[%d]\n",cache->IPv4, in->IPv4, compare_status ); if( compare_status == 0 ) { /* 前回と同じIPアドレス */ if( *cursor->hostname == '\0' ) { /* 不明なホスト */ return 1; } strncpy(in->hostname, cursor->hostname, sizeof(in->hostname)); return 0; /* 検索の必要なし */ } else if( compare_status < 0 ) { /* 前回ヒットした位置から終点まで */ cursor = cursor; term = root->end; DPRINTF("前回からの続きで検索:[%s] ~ [%s]\n", cursor->IPv4, term->IPv4); } else { /* 先頭から前回ヒットした位置まで */ cursor = root->begin; term = cursor; DPRINTF("先頭から検索:[%s] ~ [%s]\n", cursor->IPv4, term->IPv4); } } else { cursor = root->begin; } /* キャッシュ内検索開始 */ /* 最初に最後尾と比較 */ compare_status = compare_IPv4(*term, *in); if( compare_status == 0 ) { if( *term->hostname == '\0' ) { /* 不明なホスト */ return 1; } strncpy(in->hostname, term->hostname, sizeof(in->hostname)); return 0; /* 検索の必要なし */ } if( compare_status < 0 ) { /* 最後尾よりも大きいので検索する必要なし */ result = addList(root, term, *in); if( result == 0 ) { cache = term; return find(root, in); } return 1; } /* おおざっぱに検索 */ before = cursor; while( cursor != term ) { compare_status = compare_IPv4(*cursor, *in); if( compare_status == 0 ) { /* キャッシュヒット */ if( *cursor->hostname == '\0' ) { /* 不明なホスト */ return 1; } strncpy(in->hostname, cursor->hostname, sizeof(in->hostname)); cache = cursor; return 0; } if( compare_status > 0 ) { /* SKIPした範囲内のIPアドレス */ break; } before = cursor; for(n=root->count/10; n>=0 ; n-- ) { cursor = cursor->next; if( cursor->next == term ) break; } } /* SKIPした部分を一つずつ検索 */ for( cursor = before; before != term ; cursor = cursor->next ) { compare_status = compare_IPv4(*cursor, *in); if( compare_status == 0 ) { /* キャッシュヒット */ if( *cursor->hostname == '\0' ) { /* 不明なホスト */ return 1; } strncpy(in->hostname, cursor->hostname, sizeof(in->hostname)); cache = cursor; return 0; } if( compare_status > 0 ) { /* 初めてのIPアドレス */ break; } before = cursor; } /* 初めてのIPアドレスをキャッシュに登録 */ if( cursor == before ) { /* 一番小さいので先頭に挿入 */ before = NULL; } result = addList(root, before, *in); if( result == 0 ) { cache = before; return find(root, in); } return result; } int convertLog(const char *log_filename) { ROOTNODE root; HOSTINFO ip; FILE *fp; char line[2048]; char *data; size_t length; int result ; long line_count = 0; root.begin = NULL; root.end = NULL; fp = fopen(log_filename, "rb"); if( fp == NULL ) { return -1; } while( fgets(line, sizeof(line), fp) != NULL ) { line_count ++; data = strchr(line, ' '); if( data == NULL ) { /* 空白無しはおかしい */ fprintf(stderr, "FILENAME:[%s] LINE:[%ld]\n", log_filename, line_count); fclose(fp); return -1; } length = data - line; length = (sizeof(ip.IPv4)-1 < length )?sizeof(ip.IPv4)-1:length; memset(&ip, 0, sizeof(ip)); strncpy(ip.IPv4, line, length); result = find(&root, &ip); if( result == 0 ) { printf("%s%s", ip.hostname, data); } else { printf("%s", line); } } fclose(fp); clear_list(root); return 0; } int main(int argc, char *argv[]) { #ifdef TEST static ROOTNODE root; int result ; HOSTINFO ip; root.begin = NULL; root.end = NULL; int a,b,c,d; srand( time(NULL) ); for( size_t wkA=0; wkA<20; wkA++) { a = ( rand() * 100 ) & 0xff; b = ( rand() * 100 ) & 0xff; c = ( rand() * 100 ) & 0xff; d = ( rand() * 100 ) & 0xff; sprintf(ip.IPv4, "%d.%d.%d.%d", a,b,c,d); result = find(&root, &ip); printf("result=[%d] / ip=[ %16s ] hostname=[%s]\n", result, ip.IPv4, ip.hostname); } clear_list(root); #else convertLog(argv[1]); #endif return 0; }
Rating1/1=1.00-0+
1 reply [ reply ]
raynstard #2075(2007/08/12 05:14 GMT) Rating0/0=0.00
しまったよく考えたらアクセスログなのだから 昇順固定でなく個数制限付きのラウンドロビンにすれば良かったorz
[ reply ]
raynstard
#2074()
[
C
]
Rating1/1=1.00
Rating1/1=1.00-0+
1 reply [ reply ]