1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include <sys/types.h>
40 #include <sys/socket.h>
45 #if defined(AIX) || defined(__linux)
46 #include <sys/select.h> // for fd_set
50 // Didn't find gettdtablehi() or gettdtablesize() on linux. Using FD_SETSIZE
51 #define getdtablehi() FD_SETSIZE
52 #elif !defined(__irix)
53 // looks like Irix is the only one that has getdtablehi()?
54 #define getdtablehi() getdtablesize()
56 // If you find a system doesn't have getdtablesize try #define getdtablesize
57 // to FD_SETSIZE. And if you encounter a system that doesn't even have
58 // FD_SETSIZE, just grab your ankles and use 255.
66 struct sockaddr_un unix_addr
;
68 int async_dns_lookup(char* hostName
)
70 fprintf(stderr
, "start async_dns_lookup\n");
71 int socket_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
72 if (socket_fd
== -1) {
73 fprintf(stderr
, "socket returned error.\n");
77 unix_addr
.sun_family
= AF_UNIX
;
78 strcpy(unix_addr
.sun_path
, DNS_SOCK_NAME
);
80 int err
= connect(socket_fd
,(struct sockaddr
*)&unix_addr
, sizeof(unix_addr
));
82 fprintf(stderr
, "connect failed (errno = %d).\n",errno
);
88 strcpy(buf
, "lookup: ");
89 strcpy(&buf
[8], hostName
);
91 err
= send(socket_fd
, buf
, strlen(buf
)+1, 0);
93 fprintf(stderr
, "send(%s) returned error (errno=%d).\n",buf
, errno
);
96 err
= recv(socket_fd
, buf
, 256, 0);
98 fprintf(stderr
, "recv() returned error (errno=%d).\n", errno
);
101 // printf("recv() returned %d bytes.");
102 int id
= *(int *)buf
;
103 fprintf(stderr
, "id: %d\n", id
);
114 s2
= s
+ strlen(s
) - 1;
115 while (s2
> s
&& (*s2
== '\n' || *s2
== '\r' || *s2
== ' ' || *s2
== '\t'))
117 while (*s
== ' ' || *s
== '\t' || *s
== '\n' || *s
== '\r')
123 bytesToHostent(char *buf
)
127 int len
, aliasCount
, addressCount
;
128 int addrtype
, addrlength
;
132 len
= *(int *)p
; // length of name
133 p
+= sizeof(int); // advance past name length
135 memcpy(s
, p
, len
); s
[len
] = 0;
136 fprintf(stderr
, "hostname: %s\n", s
);
138 p
+= len
; // advance past name
139 aliasCount
= *(int *)p
; // number of aliases
140 p
+= sizeof(int); // advance past alias count
142 for (i
=0; i
<aliasCount
; i
++) {
143 len
= *(int *)p
; // length of alias name
144 p
+= sizeof(int); // advance past alias name length
146 memcpy(s
, p
, len
); s
[len
] = 0;
147 fprintf(stderr
, "alias: %s\n", s
);
149 p
+= len
; // advance past alias name
152 addrtype
= *(int *)p
;
154 fprintf(stderr
, "addrtype: %d\n", addrtype
);
157 addrlength
= *(int *)p
;
159 fprintf(stderr
, "addrlength: %d\n", addrlength
);
162 addressCount
= *(int *)p
;
165 for (i
=0; i
<addressCount
; i
++) {
169 fprintf(stderr
, "addr len: %d\n", len
);
170 fprintf(stderr
, "addr : %x\n", *(int *)p
);
176 // size += 1 + aliasCount;
181 main(int argc
, char* argv
[])
186 printf("### launch daemon...\n");
188 PRProcessAttr
*attributes
= PR_NewProcessAttr();
189 if (attributes
== nsnull
) {
190 printf("PR_NewProcessAttr() failed.\n");
194 PRProcess
*daemon
= PR_CreateProcess("nsDnsAsyncLookup", nsnull
, nsnull
, attributes
);
195 if (daemon
== nsnull
) {
196 printf("PR_CreateProcess failed.\n");
198 // status = PR_DetachProcess(daemon);
200 // printf("PR_DetachProcess returned %d\n", status);
204 PR_DestroyProcessAttr(attributes
);
206 // create socket and connect to daemon
210 PRBool notDone
= PR_TRUE
;
218 FD_SET(fileno(stdin
), &fdset
);
220 FD_SET(socket_fd
, &fdset
);
222 status
= select(getdtablehi(), &fdset
, 0, 0, 0);
225 fprintf(stderr
, "%s: select() returned %d\n", argv
[0], status
);
231 if (FD_ISSET(fileno(stdin
), &fdset
))
233 char *line
= fgets(buf
, sizeof(buf
)-1, stdin
);
234 line
= string_trim(line
);
236 if(!strcmp(line
, "quit") || !strcmp(line
, "exit"))
238 fprintf(stderr
, "bye now.\n");
241 else if (!strncmp(line
, "abort ", 6))
245 else if (strchr(line
, ' ') || strchr(line
, '\t'))
247 fprintf(stderr
, "%s: unrecognized command %s.\n", argv
[0], line
);
251 fprintf(stderr
, "%s: looking up %s...\n", argv
[0], line
);
252 // initiate dns lookup
253 socket_fd
= async_dns_lookup(line
);
257 if (socket_fd
&& FD_ISSET(socket_fd
, &fdset
))
259 // read from socket, parse results
260 int size
= read(socket_fd
, buf
, 1024);
263 // parse buffer into hostent
265 fprintf(stderr
, "bytes read: %d\n", size
);
266 fprintf(stderr
, "response code: %d\n", *(int *)p
);
269 for (int i
=0; i
< size
; i
++) {
271 fprintf(stderr
, "\n");
272 fprintf(stderr
, "%2.2x ",(unsigned char)buf
[i
]);
274 fprintf(stderr
, "\n");
276 h
= bytesToHostent(p
);
292 char [nameLen+1] name
297 char [aliasNameLen+1] aliasName