aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--networking/nslookup.c88
-rw-r--r--nslookup.c88
2 files changed, 108 insertions, 68 deletions
diff --git a/networking/nslookup.c b/networking/nslookup.c
index 0a2a57437..82d3b3690 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -32,14 +32,24 @@
32#include <sys/types.h> 32#include <sys/types.h>
33#include <netinet/in.h> 33#include <netinet/in.h>
34 34
35/*
36 | I'm only implementing non-interactive mode;
37 | I totally forgot nslookup even had an interactive mode.
38 |
39 | [ TODO ]
40 | + find out how to use non-default name servers
41 | + find out how the real nslookup gets the default name server
42 */
35 43
36static const char nslookup_usage[] = 44static const char nslookup_usage[] =
37"only implementing non-interactive mode\n" 45 "nslookup [HOST]\n\n"
38"I totally forgot nslookup even had an interactive mode\n"
39; 46;
40 47
41 48
42/* */ 49/* I have to see how the real nslookup does this.
50 * I could dig through /etc/resolv.conf, but is there a
51 * better (programatic) way?
52 */
43static void 53static void
44server_fprint(FILE *dst) 54server_fprint(FILE *dst)
45{ 55{
@@ -67,7 +77,9 @@ addr_fprint(char *addr, FILE *dst)
67 return 0; 77 return 0;
68} 78}
69 79
70/* */ 80/* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
81 * into a uint32_t
82 */
71static uint32_t 83static uint32_t
72str_to_addr(const char *addr) 84str_to_addr(const char *addr)
73{ 85{
@@ -86,59 +98,63 @@ str_to_addr(const char *addr)
86 return htonl(ip); 98 return htonl(ip);
87} 99}
88 100
89/* */ 101/* takes the NULL-terminated array h_addr_list, and
102 * prints its contents appropriately
103 */
90static int 104static int
91addr_list_fprint(char **h_addr_list, FILE *dst) 105addr_list_fprint(char **h_addr_list, FILE *dst)
92{ 106{
93 int i; 107 int i, j;
94 char *addr_string = (h_addr_list[1]) 108 char *addr_string = (h_addr_list[1])
95 ? "Addresses" 109 ? "Addresses"
96 : "Address"; 110 : "Address";
97 111
98 fprintf(dst, "%s: ", addr_string); 112 fprintf(dst, "%s: ", addr_string);
99 for (i = 0; h_addr_list[i]; i++) { 113 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
100 addr_fprint(h_addr_list[i], dst); 114 addr_fprint(h_addr_list[i], dst);
101 if (h_addr_list[i+1]) { 115
102 fprintf(dst, ", "); 116 /* real nslookup does this */
117 if (j == 4) {
118 if (h_addr_list[i+1]) {
119 fprintf(dst, "\n ");
120 }
121 j = 0;
122 } else {
123 if (h_addr_list[i+1]) {
124 fprintf(dst, ", ");
125 }
103 } 126 }
127
104 } 128 }
105 fprintf(dst,"\n"); 129 fprintf(dst,"\n");
106 return 0; 130 return 0;
107} 131}
108 132
109/* */ 133/* gethostbyaddr wrapper */
110static struct hostent * 134static struct hostent *
111lookup_by_name(const char *hostname) 135gethostbyaddr_wrapper(const char *address)
112{ 136{
113 struct hostent *host; 137 struct in_addr addr;
114 138
115 host = gethostbyname(hostname); 139 addr.s_addr = str_to_addr(address);
116 if (host) { 140 return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
117 fprintf(stdout, "Name: %s\n", host->h_name);
118 addr_list_fprint(host->h_addr_list, stdout);
119 } else {
120 herror("crap");
121 }
122 return host;
123} 141}
124 142
125/* */ 143/* print the results as nslookup would */
126static struct hostent * 144static struct hostent *
127lookup_by_addr(const char *addr) 145hostent_fprint(struct hostent *host, FILE *dst)
128{ 146{
129 struct hostent *host;
130
131 host = gethostbyaddr(addr, 4, AF_INET); /* IPv4 only for now */
132 if (host) { 147 if (host) {
133 fprintf(stdout, "Name: %s\n", host->h_name); 148 fprintf(dst, "Name: %s\n", host->h_name);
134 addr_list_fprint(host->h_addr_list, stdout); 149 addr_list_fprint(host->h_addr_list, dst);
135 } else { 150 } else {
136 herror("crap"); 151 fprintf(dst, "*** %s\n", hstrerror(h_errno));
137 } 152 }
138 return host; 153 return host;
139} 154}
140 155
141/* */ 156
157/* naive function to check whether char *s is an ip address */
142static int 158static int
143is_ip_address(const char *s) 159is_ip_address(const char *s)
144{ 160{
@@ -153,16 +169,20 @@ is_ip_address(const char *s)
153int 169int
154nslookup_main(int argc, char **argv) 170nslookup_main(int argc, char **argv)
155{ 171{
156 struct in_addr addr; 172 struct hostent *host;
173
174 if (argc < 2) {
175 usage(nslookup_usage);
176 }
157 177
158 server_fprint(stdout); 178 server_fprint(stdout);
159 if (is_ip_address(argv[1])) { 179 if (is_ip_address(argv[1])) {
160 addr.s_addr = str_to_addr(argv[1]); 180 host = gethostbyaddr_wrapper(argv[1]);
161 lookup_by_addr((char *) &addr);
162 } else { 181 } else {
163 lookup_by_name(argv[1]); 182 host = gethostbyname(argv[1]);
164 } 183 }
184 hostent_fprint(host, stdout);
165 return 0; 185 return 0;
166} 186}
167 187
168/* $Id: nslookup.c,v 1.1 2000/01/29 12:59:01 beppu Exp $ */ 188/* $Id: nslookup.c,v 1.2 2000/01/30 09:47:16 beppu Exp $ */
diff --git a/nslookup.c b/nslookup.c
index 0a2a57437..82d3b3690 100644
--- a/nslookup.c
+++ b/nslookup.c
@@ -32,14 +32,24 @@
32#include <sys/types.h> 32#include <sys/types.h>
33#include <netinet/in.h> 33#include <netinet/in.h>
34 34
35/*
36 | I'm only implementing non-interactive mode;
37 | I totally forgot nslookup even had an interactive mode.
38 |
39 | [ TODO ]
40 | + find out how to use non-default name servers
41 | + find out how the real nslookup gets the default name server
42 */
35 43
36static const char nslookup_usage[] = 44static const char nslookup_usage[] =
37"only implementing non-interactive mode\n" 45 "nslookup [HOST]\n\n"
38"I totally forgot nslookup even had an interactive mode\n"
39; 46;
40 47
41 48
42/* */ 49/* I have to see how the real nslookup does this.
50 * I could dig through /etc/resolv.conf, but is there a
51 * better (programatic) way?
52 */
43static void 53static void
44server_fprint(FILE *dst) 54server_fprint(FILE *dst)
45{ 55{
@@ -67,7 +77,9 @@ addr_fprint(char *addr, FILE *dst)
67 return 0; 77 return 0;
68} 78}
69 79
70/* */ 80/* changes a c-string matching the perl regex \d+\.\d+\.\d+\.\d+
81 * into a uint32_t
82 */
71static uint32_t 83static uint32_t
72str_to_addr(const char *addr) 84str_to_addr(const char *addr)
73{ 85{
@@ -86,59 +98,63 @@ str_to_addr(const char *addr)
86 return htonl(ip); 98 return htonl(ip);
87} 99}
88 100
89/* */ 101/* takes the NULL-terminated array h_addr_list, and
102 * prints its contents appropriately
103 */
90static int 104static int
91addr_list_fprint(char **h_addr_list, FILE *dst) 105addr_list_fprint(char **h_addr_list, FILE *dst)
92{ 106{
93 int i; 107 int i, j;
94 char *addr_string = (h_addr_list[1]) 108 char *addr_string = (h_addr_list[1])
95 ? "Addresses" 109 ? "Addresses"
96 : "Address"; 110 : "Address";
97 111
98 fprintf(dst, "%s: ", addr_string); 112 fprintf(dst, "%s: ", addr_string);
99 for (i = 0; h_addr_list[i]; i++) { 113 for (i = 0, j = 0; h_addr_list[i]; i++, j++) {
100 addr_fprint(h_addr_list[i], dst); 114 addr_fprint(h_addr_list[i], dst);
101 if (h_addr_list[i+1]) { 115
102 fprintf(dst, ", "); 116 /* real nslookup does this */
117 if (j == 4) {
118 if (h_addr_list[i+1]) {
119 fprintf(dst, "\n ");
120 }
121 j = 0;
122 } else {
123 if (h_addr_list[i+1]) {
124 fprintf(dst, ", ");
125 }
103 } 126 }
127
104 } 128 }
105 fprintf(dst,"\n"); 129 fprintf(dst,"\n");
106 return 0; 130 return 0;
107} 131}
108 132
109/* */ 133/* gethostbyaddr wrapper */
110static struct hostent * 134static struct hostent *
111lookup_by_name(const char *hostname) 135gethostbyaddr_wrapper(const char *address)
112{ 136{
113 struct hostent *host; 137 struct in_addr addr;
114 138
115 host = gethostbyname(hostname); 139 addr.s_addr = str_to_addr(address);
116 if (host) { 140 return gethostbyaddr((char *) &addr, 4, AF_INET); /* IPv4 only for now */
117 fprintf(stdout, "Name: %s\n", host->h_name);
118 addr_list_fprint(host->h_addr_list, stdout);
119 } else {
120 herror("crap");
121 }
122 return host;
123} 141}
124 142
125/* */ 143/* print the results as nslookup would */
126static struct hostent * 144static struct hostent *
127lookup_by_addr(const char *addr) 145hostent_fprint(struct hostent *host, FILE *dst)
128{ 146{
129 struct hostent *host;
130
131 host = gethostbyaddr(addr, 4, AF_INET); /* IPv4 only for now */
132 if (host) { 147 if (host) {
133 fprintf(stdout, "Name: %s\n", host->h_name); 148 fprintf(dst, "Name: %s\n", host->h_name);
134 addr_list_fprint(host->h_addr_list, stdout); 149 addr_list_fprint(host->h_addr_list, dst);
135 } else { 150 } else {
136 herror("crap"); 151 fprintf(dst, "*** %s\n", hstrerror(h_errno));
137 } 152 }
138 return host; 153 return host;
139} 154}
140 155
141/* */ 156
157/* naive function to check whether char *s is an ip address */
142static int 158static int
143is_ip_address(const char *s) 159is_ip_address(const char *s)
144{ 160{
@@ -153,16 +169,20 @@ is_ip_address(const char *s)
153int 169int
154nslookup_main(int argc, char **argv) 170nslookup_main(int argc, char **argv)
155{ 171{
156 struct in_addr addr; 172 struct hostent *host;
173
174 if (argc < 2) {
175 usage(nslookup_usage);
176 }
157 177
158 server_fprint(stdout); 178 server_fprint(stdout);
159 if (is_ip_address(argv[1])) { 179 if (is_ip_address(argv[1])) {
160 addr.s_addr = str_to_addr(argv[1]); 180 host = gethostbyaddr_wrapper(argv[1]);
161 lookup_by_addr((char *) &addr);
162 } else { 181 } else {
163 lookup_by_name(argv[1]); 182 host = gethostbyname(argv[1]);
164 } 183 }
184 hostent_fprint(host, stdout);
165 return 0; 185 return 0;
166} 186}
167 187
168/* $Id: nslookup.c,v 1.1 2000/01/29 12:59:01 beppu Exp $ */ 188/* $Id: nslookup.c,v 1.2 2000/01/30 09:47:16 beppu Exp $ */