dlzbdb.c
来自「非常好的dns解析软件」· C语言 代码 · 共 1,274 行 · 第 1/3 页
C
1,274 行
/* get a cursor, make sure it worked. */ bdbres = database->cursor(database, NULL, &dbcursor, 0); if (bdbres != 0) { fprintf(stderr, "Unexpected error. BDB Error: %s\n",db_strerror(bdbres)); free(bdbdata->data); return ISC_R_FAILURE; } /* loop and dump all data */ for (;;) { /* loop through data until DB_NOTFOUND is returned */ bdbres = dbcursor->c_get(dbcursor, bdbkey, bdbdata, DB_MULTIPLE_KEY | DB_NEXT); /* if not successful did we encounter DB_NOTFOUND, or */ /* have a different problem. */ if (bdbres != 0) { if (bdbres != DB_NOTFOUND) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); free(bdbdata->data); return ISC_R_FAILURE; } /* Hit DB_NOTFOUND which means end of data. */ break; } /* end of if (bdbres !=0) */ for (DB_MULTIPLE_INIT(p, bdbdata);;) { if (type == 'c') DB_MULTIPLE_KEY_NEXT(p, bdbdata, retkey, retklen, retdata, retdlen); else DB_MULTIPLE_RECNO_NEXT(p, bdbdata, recNum, retdata, retdlen); if (p == NULL) break; if (type == 'c') printf("c %.*s %.*s\n",(int)retklen, retkey,(int)retdlen, retdata); else printf("d %.*s\n", (int)retdlen, retdata); } /* end of for (DB_MULTIPLE_INIT....) */ } /* end of for (;;) */ /* free the buffer we created earlier */ free(bdbdata->data); return ISC_R_SUCCESS;}/*% * Perform listOrDelete operation * if dlt == true, delete data * else list data */voidoperation_listOrDelete(isc_boolean_t dlt) { int bdbres = 0; DBC *curList[3]; DBT bdbkey, bdbdata; db_recno_t recno; int curIndex = 0; /* verify that only allowed parameters were passed. */ if (dlt == isc_boolean_true) { checkInvalidParam(zone, "z", "for delete operation"); checkInvalidParam(host, "h", "for delete operation"); checkInvalidOption(list_everything, isc_boolean_true, "e", "for delete operation"); checkInvalidOption(create_allowed, isc_boolean_true, "n", "for delete operation"); } else if (key != NULL || zone != NULL || host != NULL) { checkInvalidParam(c_zone, "c", "for list when k, z or h are specified"); checkInvalidParam(c_ip, "i", "for list when k, z, or h are specified"); checkInvalidOption(list_everything, isc_boolean_true, "e", "for list when k, z, or h are specified"); checkInvalidOption(create_allowed, isc_boolean_true, "n", "for list operation"); } else if (c_ip != NULL || c_zone != NULL) { checkInvalidOption(list_everything, isc_boolean_true, "e", "for list when c or i are specified"); checkInvalidOption(create_allowed, isc_boolean_true, "n", "for list operation"); } memset(&bdbkey, 0, sizeof(bdbkey)); memset(&bdbdata, 0, sizeof(bdbdata)); /* Dump database in "dlzbdb" bulk format */ if (list_everything == isc_boolean_true) { if (bulk_write('c', db.client, db.cursor, &bdbkey, &bdbdata) != ISC_R_SUCCESS) return; memset(&bdbkey, 0, sizeof(bdbkey)); memset(&bdbdata, 0, sizeof(bdbdata)); bulk_write('d', db.data, db.cursor2, &bdbkey, &bdbdata); return; } /* end if (list_everything) */ /* set NULL the 2nd and 3rd positions in curList. */ /* that way later when add cursors to the join list */ /* it is already null terminated. */ curList[1] = curList[2] = NULL; if (key != NULL) { /* make sure other parameters weren't */ checkInvalidParam(zone, "z", "when k is specified"); checkInvalidParam(host, "h", "when k is specified"); recno = key_val; bdbkey.data = &recno; bdbkey.size = sizeof(recno); if (dlt == isc_boolean_true) { bdbres = db.data->del(db.data, NULL, &bdbkey, 0); } else { bdbdata.flags = DB_DBT_REALLOC; bdbres = db.data->get(db.data, NULL, &bdbkey, &bdbdata, 0); if (bdbres == 0) { printf("KEY | DATA\n"); printf("%lu | %.*s\n", *(u_long *) bdbkey.data, (int)bdbdata.size, (char *)bdbdata.data); } } /* closes else of if (dlt == isc_boolean_true) */ if (bdbres == DB_NOTFOUND) { printf("Key not found in database"); } } /* closes if (key != NULL) */ /* if zone is passed */ if (zone != NULL) { /* create a cursor and make sure it worked */ bdbres = db.zone->cursor(db.zone, NULL, &db.cursor2, 0); if (bdbres != 0) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); return; } bdbkey.data = zone; bdbkey.size = strlen(zone); bdbres = db.cursor2->c_get(db.cursor2, &bdbkey, &bdbdata, DB_SET); if (bdbres != 0) { if (bdbres != DB_NOTFOUND) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); } else { printf("Zone not found in database"); } return; } /* add cursor to cursor list for later use in join */ curList[curIndex++] = db.cursor2; } /* if host is passed */ if (host != NULL) { /* create a cursor and make sure it worked. */ bdbres = db.host->cursor(db.host, NULL, &db.cursor3, 0); if (bdbres != 0) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); return; } bdbkey.data = host; bdbkey.size = strlen(host); bdbres = db.cursor3->c_get(db.cursor3, &bdbkey, &bdbdata, DB_SET); if (bdbres != 0) { if (bdbres != DB_NOTFOUND) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); } else { printf("Host not found in database"); } return; } /* add cursor to cursor list for later use in join */ curList[curIndex++] = db.cursor3; } if (zone != NULL || host != NULL) { /* join any cursors */ bdbres = db.data->join(db.data, curList, &db.cursor4, 0); if (bdbres != 0) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); return; } memset(&bdbkey, 0, sizeof(bdbkey)); bdbkey.flags = DB_DBT_REALLOC; memset(&bdbdata, 0, sizeof(bdbdata)); bdbdata.flags = DB_DBT_REALLOC; /* print a header to explain the output */ printf("KEY | DATA\n"); /* loop and list all results. */ while (bdbres == 0) { /* get data */ bdbres = db.cursor4->c_get(db.cursor4, &bdbkey, &bdbdata, 0); /* verify call had no errors */ if (bdbres != 0) { break; } printf("%lu | %.*s\n", *(u_long *) bdbkey.data, (int)bdbdata.size, (char *)bdbdata.data); } /* closes while loop */ } if (c_ip != NULL && c_zone == NULL) { fprintf(stderr, "i may only be specified when c is also specified\n"); quit(2); } /* if client_zone was passed */ if (c_zone != NULL) { /* create a cursor and make sure it worked. */ if (dlt == isc_boolean_true) { /* open read-write cursor */ bdbres = db.client->cursor(db.client, NULL, &db.cursor, DB_WRITECURSOR); } else { /* open read only cursor */ bdbres = db.client->cursor(db.client, NULL, &db.cursor, 0); /* print a header to explain the output */ printf("CLIENT_ZONE | CLIENT_IP\n"); } bdbkey.data = c_zone; bdbkey.size = strlen(c_zone); if (c_ip != NULL) { bdbdata.data = c_ip; bdbdata.size = strlen(c_ip); bdbres = db.cursor->c_get(db.cursor, &bdbkey, &bdbdata, DB_GET_BOTH); if (bdbres == DB_NOTFOUND) { printf("Client zone & IP not found in database"); } } else { bdbdata.flags = DB_DBT_REALLOC; bdbres = db.cursor->c_get(db.cursor, &bdbkey, &bdbdata, DB_SET); if (bdbres == DB_NOTFOUND) { printf("Client zone not found in database"); } } while (bdbres == 0) { if (dlt == isc_boolean_false) { printf("%.*s | %.*s\n", (int)bdbkey.size, (char *) bdbkey.data, (int)bdbdata.size, (char *) bdbdata.data); } else { /* delete record. */ bdbres = db.cursor->c_del(db.cursor, 0); if (bdbres != 0) { fprintf(stderr, "Unexpected error. BDB Error: %s\n", db_strerror(bdbres)); break; } } if (c_ip != NULL) { break; } bdbres = db.cursor->c_get(db.cursor, &bdbkey, &bdbdata, DB_NEXT_DUP); if (bdbres != 0) { break; } } /* end while loop */ } if (bdbres != 0 && bdbres != DB_NOTFOUND) { fprintf(stderr, "Unexpected error during list operation " \ "BDB error: %s", db_strerror(bdbres)); } if (bdbkey.flags == DB_DBT_REALLOC && bdbkey.data != NULL) { free(bdbkey.data); } if (bdbdata.flags == DB_DBT_REALLOC && bdbdata.data != NULL) { free(bdbdata.data); }}intmain(int argc, char **argv) { int ch; char *endp; /* there has to be at least 2 args, some operations require more */ if (argc < 2) show_usage(); /* use the ISC commandline parser to get all the program arguments */ while ((ch= isc_commandline_parse(argc, argv, "ldesna:f:k:z:h:c:i:")) != -1) { switch (ch) { case 'n': create_allowed = isc_boolean_true; break; case 'l': checkOp(operation); operation = list; break; case 'd': checkOp(operation); operation = dele; break; case 'a': checkOp(operation); operation = add; a_data = isc_commandline_argument; break; case 'f': checkOp(operation); operation = bulk; bulk_file = isc_commandline_argument; break; case 's': checkOp(operation); operation = bulk; break; case 'k': checkParam(key, "k"); key = isc_commandline_argument; key_val = strtoul(key, &endp, 10); if (*endp != '\0' || key_val < 1) { fprintf(stderr, "Error converting key to integer"); } break; case 'z': checkParam(zone, "z"); zone = isc_commandline_argument; break; case 'h': checkParam(host, "h"); host = isc_commandline_argument; break; case 'c': checkParam(c_zone, "c"); c_zone = isc_commandline_argument; break; case 'i': checkParam(c_ip, "i"); c_ip = isc_commandline_argument; break; case 'e': checkOp(operation); operation = list; list_everything = isc_boolean_true; break; case '?': show_usage(); break; default: /* should never reach this point */ fprintf(stderr, "unexpected error parsing command arguments\n"); quit(1); break; } } argc -= isc_commandline_index; argv += isc_commandline_index; /* argc & argv have been modified, so now only "extra" parameters are */ /* left in argc & argv. "Extra" parameters are any parameters that were */ /* not passed using a command line flag. Exactly 2 args should be left. */ /* The first should be the BDB environment path, the second should be the */ /* BDB database. The BDB database path can be either relative to the */ /* BDB environment path, or absolute. */ if (argc < 2) { fprintf(stderr, "Both a Berkeley DB environment and file "\ "must be specified"); quit(2); } else if (argc > 2) { fprintf(stderr, "Too many parameters. Check command line for errors."); quit(2); } /* get db_file to operate on */ db_envdir = argv[0]; db_file = argv[1]; if (openBDB() != ISC_R_SUCCESS) { /* openBDB already prints error messages, don't do it here. */ bdb_cleanup(); quit(3); } switch(operation) { case list: operation_listOrDelete(isc_boolean_false); break; case dele: operation_listOrDelete(isc_boolean_true); break; case add: operation_add(); break; case bulk: operation_bulk(); break; default: fprintf(stderr, "\nNo operation was selected. "\ "Select an operation (l d a f)"); quit(2); break; } quit(0);}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?