void do_query(const SOCKET sock, const char *query) { char buf[2000]; int i = 0, hide = hide_discl; if (send(sock, query, (int) strlen(query), 0) == SOCKET_ERROR) err_exit("send"); /* Using shutdown breaks the buggy RIPE server. if (shutdown(sock, 1) == SOCKET_ERROR) err_exit("shutdown"); */ for (;;) { if (LineRecv((HANDLE)sock, buf, sizeof(buf), 60000) == 0) if (win_errno != ERROR_SUCCESS) if (win_errno == ERROR_HANDLE_EOF) break; else err_exit("Receive error"); if (hide == 1) { if (strncmp(buf, hide_strings[i+1], strlen(hide_strings[i+1]))==0) hide = 2; /* stop hiding */ continue; /* hide this line */ } if (hide == 0) { for (i = 0; hide_strings[i] != NULL; i += 2) { if (strncmp(buf, hide_strings[i], strlen(hide_strings[i]))==0){ hide = 1; /* start hiding */ break; } } if (hide == 1) continue; /* hide the first line */ } #ifdef EXT_6BONE /* % referto: whois -h whois.arin.net -p 43 as 1 */ if (strncmp(buf, "% referto:", 10) == 0) { char nh[256], np[16], nq[1024]; if (sscanf(buf, REFERTO_FORMAT, nh, np, nq) == 3) { SOCKET fd; if (verb) printf(_("Detected referral to %s on %s.\n"), nq, nh); strcat(nq, "\r\n"); fd = openconn(nh, np); do_query(fd, nq); continue; } } #endif CharToOem(buf, buf); puts(buf); } if (hide == 1) err_quit(_("Catastrophic error: disclaimer text has been changed.\r\n" "Please upgrade this program.\r\n")); }
void do_query(const int sock, const char *query) { char buf[2000], *p; int i = 0, hide = hide_discl; if (send(sock, query, strlen(query), 0) == SOCKET_ERROR) h_perror("send"); /* Using shutdown breaks the buggy RIPE server. if (shutdown(sock, 1) == SOCKET_ERROR) h_perror("shutdown"); */ while (BufRecv((HANDLE)sock, buf, 1, 60000) == 1) { LineRecv((HANDLE)sock, buf+1, sizeof(buf)-1, 60000); if (hide == 1) { if (strncmp(buf, hide_strings[i+1], strlen(hide_strings[i+1]))==0) hide = 2; /* stop hiding */ continue; /* hide this line */ } if (hide == 0) { for (i = 0; hide_strings[i] != NULL; i += 2) { if (strncmp(buf, hide_strings[i], strlen(hide_strings[i]))==0){ hide = 1; /* start hiding */ break; } } if (hide == 1) continue; /* hide the first line */ } #ifdef EXT_6BONE /* % referto: whois -h whois.arin.net -p 43 as 1 */ if (strncmp(buf, "% referto:", 10) == 0) { char nh[256], np[16], nq[1024]; if (sscanf(buf, REFERTO_FORMAT, nh, np, nq) == 3) { int fd; if (verb) printf(_("Detected referral to %s on %s.\n"), nq, nh); strcat(nq, "\r\n"); fd = openconn(nh, np); do_query(fd, nq); continue; } } #endif for (p = buf; *p && *p != '\r' && *p != '\n'; p++); *p = '\0'; fprintf(stdout, "%s\n", buf); } if (hide == 1) err_quit(_("Catastrophic error: disclaimer text has been changed.\n" "Please upgrade this program.\n")); }
int main(int argc, char *argv[]) { int ch, nopar = 0; const char *server = NULL, *port = NULL; char *p, *q, *qstring, fstring[64] = "\0"; #ifdef _WIN32 WSADATA wsa; if (WSAStartup(0x0101, &wsa)) { fputs("Winsock initalization error.", stderr); return -1; } #endif #ifdef ENABLE_NLS setlocale(LC_ALL, ""); bindtextdomain(NLS_CAT_NAME, LOCALEDIR); textdomain(NLS_CAT_NAME); #endif while ((ch = GETOPT_LONGISH(argc, argv, "acdFg:h:Hi:KlLmMp:q:rRs:St:T:v:V:x", longopts, 0)) > 0) { /* RIPE flags */ if (strchr(ripeflags, ch)) { for (p = fstring; *p; p++); sprintf(p--, "-%c ", ch); continue; } if (strchr(ripeflagsp, ch)) { for (p = fstring; *p; p++); snprintf(p--, sizeof(fstring), "-%c %s ", ch, optarg); if (ch == 't' || ch == 'v' || ch == 'q') nopar = 1; continue; } /* program flags */ switch (ch) { case 'h': server = q = malloc(strlen(optarg) + 1); for (p = optarg; *p && *p != ':'; *q++ = tolower(*p++)); if (*p == ':') port = p + 1; *q = '\0'; break; case 'V': client_tag = optarg; case 'H': hide_discl = 0; /* enable disclaimers hiding */ break; case 'p': port = optarg; break; case 2: verb = 1; break; case 1: #ifdef VERSION fprintf(stderr, _("Version %s.\n\nReport bugs to %s.\n"), VERSION, "<*****@*****.**>"); #else fprintf(stderr, "%s %s\n", inetutils_package, inetutils_version); #endif exit(0); default: usage(); } } argc -= optind; argv += optind; if (argc == 0 && !nopar) /* there is no parameter */ usage(); /* On some systems realloc only works on non-NULL buffers */ qstring = malloc(64); *qstring = '\0'; /* parse other parameters, if any */ if (!nopar) { int qslen = 0; while (1) { qslen += strlen(*argv) + 1 + 1; qstring = realloc(qstring, qslen); strcat(qstring, *argv++); if (argc == 1) break; strcat(qstring, " "); argc--; } } /* -v or -t has been used */ if (!server && !*qstring) server = "whois.ripe.net"; #ifdef CONFIG_FILE if (!server) { server = match_config_file(qstring); if (verb && server) printf(_("Using server %s.\n"), server); } #endif signal(SIGTERM, sighandler); signal(SIGINT, sighandler); #ifndef _WIN32 signal(SIGALRM, alarm_handler); #endif alarm(60); if (!server) { char *tmp; tmp = normalize_domain(qstring); server = whichwhois(tmp); free(tmp); switch (server[0]) { case 0: if (!(server = getenv("WHOIS_SERVER"))) server = DEFAULTSERVER; if (verb) printf(_("Using default server %s.\n"), server); break; case 1: puts(_("This TLD has no whois server, but you can access the " "whois database at")); case 2: puts(server + 1); exit(0); case 3: puts(_("This TLD has no whois server.")); exit(0); case 4: if (verb) puts(_("Connecting to whois.crsnic.net.")); sockfd = openconn("whois.crsnic.net", NULL); server = query_crsnic(sockfd, qstring); closesocket(sockfd); if (!server) exit(0); printf(_("\nFound a referral to %s.\n\n"), server); alarm(60); break; case 9: if (verb) puts(_("Connecting to whois.nic.cc.")); sockfd = openconn("whois.nic.cc", NULL); server = query_crsnic(sockfd, qstring); closesocket(sockfd); if (!server) exit(0); printf(_("\nFound a referral to %s.\n\n"), server); alarm(60); break; case 7: if (verb) puts(_("Connecting to whois.publicinterestregistry.net.")); sockfd = openconn("whois.publicinterestregistry.net", NULL); server = query_pir(sockfd, qstring); closesocket(sockfd); if (!server) exit(0); printf(_("\nFound referral to %s.\n\n"), server); alarm(60); break; case 5: puts(_("No whois server is known for this kind of object.")); exit(0); case 6: puts(_("Unknown AS number or IP network. Please upgrade this program.")); exit(0); default: if (verb) printf(_("Using server %s.\n"), server); } } if (getenv("WHOIS_HIDE")) hide_discl = 0; p = queryformat(server, fstring, qstring); if (verb) printf(_("Query string: \"%s\"\n\n"), p); strcat(p, "\r\n"); sockfd = openconn(server, port); /* * Now we are connected and the query is supposed to complete quickly. * This will help people who run whois ... | less */ alarm(0); do_query(sockfd, p); exit(0); }
void do_query (const int sock, const char *query) { char buf[200], *p; FILE *fi; int i = 0, hide = hide_discl; fi = fdopen (sock, "r"); if (write (sock, query, strlen (query)) < 0) err_sys ("write"); if (shutdown (sock, 1) < 0) err_sys ("shutdown"); while (fgets (buf, 200, fi)) { /* XXX errors? */ if (hide == 1) { if (strncmp (buf, hide_strings[i + 1], strlen (hide_strings[i + 1])) == 0) hide = 2; /* stop hiding */ continue; /* hide this line */ } if (hide == 0) { for (i = 0; hide_strings[i] != NULL; i += 2) { if (strncmp (buf, hide_strings[i], strlen (hide_strings[i])) == 0) { hide = 1; /* start hiding */ break; } } if (hide == 1) continue; /* hide the first line */ } #ifdef EXT_6BONE /* % referto: whois -h whois.arin.net -p 43 as 1 */ if (strncmp (buf, "% referto:", 10) == 0) { char nh[256], np[16], nq[1024]; if (sscanf (buf, REFERTO_FORMAT, nh, np, nq) == 3) { int fd; if (verb) printf (_("Detected referral to %s on %s.\n"), nq, nh); strcat (nq, "\r\n"); fd = openconn (nh, np); do_query (fd, nq); closeconn (fd); continue; } } #endif for (p = buf; *p && *p != '\r' && *p != '\n'; p++); *p = '\0'; fprintf (stdout, "%s\n", buf); } if (ferror (fi)) err_sys ("fgets"); if (hide == 1) err_quit (_("Catastrophic error: disclaimer text has been changed.\n" "Please upgrade this program.\n")); }
int main (int argc, char *argv[]) { int index; char *fstring; char *qstring; char *p; set_program_name (argv[0]); #ifdef ENABLE_NLS setlocale (LC_MESSAGES, ""); bindtextdomain (NLS_CAT_NAME, LOCALEDIR); textdomain (NLS_CAT_NAME); #endif obstack_init (&query_stk); iu_argp_init ("whois", program_authors); argp_parse (&gwhois_argp, argc, argv, ARGP_IN_ORDER, &index, NULL); obstack_1grow (&query_stk, 0); fstring = obstack_finish (&query_stk); argc -= index; argv += index; if (argc == 0 && !nopar) /* there is no parameter */ error (EXIT_FAILURE, 0, "not enough arguments"); /* parse other parameters, if any */ if (!nopar) { while (argc--) { const char *arg = *argv++; obstack_grow (&query_stk, arg, strlen (arg)); if (argc) obstack_1grow (&query_stk, ' '); } } obstack_1grow (&query_stk, 0); qstring = obstack_finish (&query_stk); if (!server && domfind (qstring, gtlds)) { if (verb) puts (_("Connecting to whois.internic.net.")); sockfd = openconn ("whois.internic.net", NULL); server = query_crsnic (sockfd, qstring); closeconn (sockfd); if (!server) exit (EXIT_SUCCESS); printf (_("\nFound InterNIC referral to %s.\n\n"), server); } if (!server) { server = whichwhois (qstring); switch (server[0]) { case 0: if (!(server = getenv ("WHOIS_SERVER"))) server = DEFAULTSERVER; if (verb) printf (_("Using default server %s.\n"), server); break; case 1: puts (_("This TLD has no whois server, but you can access the " "whois database at")); case 2: puts (server + 1); exit (EXIT_SUCCESS); case 3: puts (_("This TLD has no whois server.")); exit (EXIT_SUCCESS); default: if (verb) printf (_("Using server %s.\n"), server); } } if (getenv ("WHOIS_HIDE")) hide_discl = 0; p = queryformat (server, fstring, qstring); if (verb) printf (_("Query string: \"%s\"\n\n"), p); strcat (p, "\r\n"); signal (SIGTERM, sighandler); signal (SIGINT, sighandler); sockfd = openconn (server, port); do_query (sockfd, p); closeconn (sockfd); exit (EXIT_SUCCESS); }