int main(int argc, char *argv[]) { int ch, i, r; uint16_t type = T_A; char buf[1024], *host; while((ch = getopt(argc, argv, "R:et:")) != -1) { switch(ch) { case 'R': parseresopt(optarg); break; case 'e': long_err += 1; break; case 't': if ((type = strtotype(optarg)) == 0) usage(); break; default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; for (i = 0; i < argc; i++) { if (i) printf("\n"); printf("===> \"%s\"\n", argv[i]); host = gethostarg(argv[i]); errno = 0; h_errno = 0; gai_errno = 0; rrset_errno = 0; r = res_mkquery(QUERY, host, C_IN, type, NULL, 0, NULL, buf, sizeof(buf)); if (r != -1) { dump_packet(buf, r); printf(";; MSG SIZE %i\n", r); } print_errors(); } return (0); }
/* sets global variables (options) based on command line options. * allocates 'input' and 'output'. */ static void parseopt(int argc, char* const argv[]) { int opt; while((opt = getopt(argc, argv, "i:t:x:y:z:o:vh")) != -1) { switch(opt) { case 'i': if(input != NULL) { free(input); input = NULL; } input = tjfstrdup(optarg); break; case 't': itype = strtotype(optarg); break; case 'o': if(output != NULL) { free(output); output = NULL; } output = tjfstrdup(optarg); break; case 'x': vol[0] = (uint64_t)atoll(optarg); break; case 'y': vol[1] = (uint64_t)atoll(optarg); break; case 'z': vol[2] = (uint64_t)atoll(optarg); break; case 'v': verbose++; break; case 'h': /* FALL-THROUGH */ default: usage(argv[0]); exit(EXIT_FAILURE); } } if(NULL == input) { fprintf(stderr, "No input file given!\n"); usage(argv[0]); exit(EXIT_FAILURE); } if(NULL == output) { fprintf(stderr, "No output file given!\n"); usage(argv[0]); exit(EXIT_FAILURE); } if(vol[0] == 0 || vol[1] == 0 || vol[2] == 0) { fprintf(stderr, "Volume size is 0.\n"); usage(argv[0]); exit(EXIT_FAILURE); } }
int main(int argc, char *argv[]) { struct timeval start, end; time_t when; int ch, i, qflag, dflag, r; uint16_t type = T_A; char buf[1024], *host; dflag = 0; qflag = 0; while((ch = getopt(argc, argv, "deqt:")) != -1) { switch(ch) { case 'd': dflag = 1; break; case 'e': long_err += 1; break; case 'q': qflag = 1; break; case 't': if ((type = strtotype(optarg)) == 0) usage(); break; default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; for (i = 0; i < argc; i++) { if (i) printf("\n"); printf("===> \"%s\"\n", argv[i]); host = gethostarg(argv[i]); errno = 0; h_errno = 0; gai_errno = 0; rrset_errno = 0; if (gettimeofday(&start, NULL) != 0) err(1, "gettimeofday"); if (qflag) r = res_query(host, C_IN, type, buf, sizeof(buf)); else r = res_search(host, C_IN, type, buf, sizeof(buf)); if (gettimeofday(&end, NULL) != 0) err(1, "gettimeofday"); if (r != -1) { dump_packet(buf, r); printf("\n"); if (dflag) { printf(";; Query time: %d msec\n", msec(start, end)); when = time(NULL); printf(";; WHEN: %s", ctime(&when)); } printf(";; MSG SIZE rcvd: %i\n", r); } print_errors(); } return (0); }
int main(int argc, char *argv[]) { if (argc == 1) { print_help(); exit(1); } opterr = 1; int option_index = 0; struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"base", required_argument, NULL, 'b'}, {"type", required_argument, NULL, 't'}, {"size", required_argument, NULL, 's'}, {"num", required_argument, NULL, 'n'}, {"keep", required_argument, NULL, 'k'}, {"port", required_argument, NULL, 'p'}, {"addr", no_argument, NULL, 'a'}, {"daemon", no_argument, NULL, 'd'}, {0, 0, 0, 0}, }; char *base_name = NULL; int shift_type = 0; size_t max_size = 0; int log_num = 0; int keep_time = 0; int port = 0; bool log_addr = 0; bool in_daemon = 0; int c; while ((c = getopt_long(argc, argv, "hb:t:s:n:k:p:ad", long_options, &option_index)) != -1) { switch (c) { case 'h': print_help(); exit(0); case 'b': base_name = strdup(optarg); break; case 't': if ((shift_type = strtotype(optarg)) < 0) error(1, 0, "invalid shift type"); break; case 's': max_size = strtoull(optarg, NULL, 0); break; case 'n': log_num = atoi(optarg); break; case 'k': keep_time = atoi(optarg); break; case 'p': port = atoi(optarg); break; case 'a': log_addr = true; break; case 'd': in_daemon = true; break; case '?': exit(EXIT_FAILURE); default: abort(); } } if (base_name == NULL) error(1, errno, "-b --base base_name is required"); if (port == 0) error(1, errno, "-p --port port is required"); if (shift_type == 0) shift_type = DLOG_SHIFT_BY_DAY; if (in_daemon) daemon(true, true); dlog_t *lp = dlog_init(base_name, shift_type | DLOG_USE_FORK, max_size, log_num, keep_time); if (lp == NULL) error(1, errno, "dlog_init fail"); int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) error(1, errno, "create socket fail"); struct sockaddr_in server; bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons((uint16_t)port); if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) error(1, errno, "bind fail"); running = 1; signal(SIGQUIT, sig_handle); signal(SIGCHLD, SIG_IGN); while (running) { fd_set r_set; FD_ZERO(&r_set); FD_SET(sockfd, &r_set); int ret = select(sockfd + 1, &r_set, NULL, NULL, NULL); if (ret > 0 && FD_ISSET(sockfd, &r_set)) { struct sockaddr_in client; socklen_t len = sizeof(client); char buf[UINT16_MAX]; ssize_t n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&client, &len); if (n < 0) continue; dlog_server(lp, buf, n, log_addr ? &client : NULL); } } return 0; }