Part of bug 382647 (move xpfe bookmarks to suite) - do the xpfe removals. r=KaiRo...
[mozilla-central.git] / netwerk / test / TestDNSDaemon.cpp
blobf024d77de299ed6be6860b042d9ccff1f96df90d
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43 #include <errno.h>
45 #if defined(AIX) || defined(__linux)
46 #include <sys/select.h> // for fd_set
47 #endif
49 #if defined(__linux)
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.
59 #endif
62 #include "nspr.h"
63 #include "nsCRT.h"
64 #include "unix_dns.h"
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");
74 return -1;
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));
81 if (err == -1) {
82 fprintf(stderr, "connect failed (errno = %d).\n",errno);
83 close(socket_fd);
84 return -1;
87 char buf[256];
88 strcpy(buf, "lookup: ");
89 strcpy(&buf[8], hostName);
91 err = send(socket_fd, buf, strlen(buf)+1, 0);
92 if (err < 0)
93 fprintf(stderr, "send(%s) returned error (errno=%d).\n",buf, errno);
95 // receive 4 byte ID
96 err = recv(socket_fd, buf, 256, 0);
97 if (err < 0)
98 fprintf(stderr, "recv() returned error (errno=%d).\n", errno);
99 else
101 // printf("recv() returned %d bytes.");
102 int id = *(int *)buf;
103 fprintf(stderr, "id: %d\n", id);
106 return socket_fd;
109 static char *
110 string_trim(char *s)
112 char *s2;
113 if (!s) return 0;
114 s2 = s + strlen(s) - 1;
115 while (s2 > s && (*s2 == '\n' || *s2 == '\r' || *s2 == ' ' || *s2 == '\t'))
116 *s2-- = 0;
117 while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
118 s++;
119 return s;
122 hostent *
123 bytesToHostent(char *buf)
125 int i;
126 // int size = 0;
127 int len, aliasCount, addressCount;
128 int addrtype, addrlength;
129 char* p = buf;
130 char s[1024];
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);
156 p += sizeof(int);
157 addrlength = *(int *)p;
159 fprintf(stderr, "addrlength: %d\n", addrlength);
161 p += sizeof(int);
162 addressCount = *(int *)p;
163 p += sizeof(int);
165 for (i=0; i<addressCount; i++) {
166 len = *(int *)p;
167 p += sizeof(int);
169 fprintf(stderr, "addr len: %d\n", len);
170 fprintf(stderr, "addr : %x\n", *(int *)p);
172 p += len;
175 // size = p - buf;
176 // size += 1 + aliasCount;
177 return 0;
181 main(int argc, char* argv[])
183 PRStatus status;
185 // launch daemon
186 printf("### launch daemon...\n");
188 PRProcessAttr *attributes = PR_NewProcessAttr();
189 if (attributes == nsnull) {
190 printf("PR_NewProcessAttr() failed.\n");
191 return -1;
194 PRProcess *daemon = PR_CreateProcess("nsDnsAsyncLookup", nsnull, nsnull, attributes);
195 if (daemon == nsnull) {
196 printf("PR_CreateProcess failed.\n");
197 } else {
198 // status = PR_DetachProcess(daemon);
199 //if (status != 0)
200 // printf("PR_DetachProcess returned %d\n", status);
201 //daemon = nsnull;
204 PR_DestroyProcessAttr(attributes);
206 // create socket and connect to daemon
207 int socket_fd = 0;
210 PRBool notDone = PR_TRUE;
211 char buf[1024];
213 while(notDone) {
214 int status = 0;
215 fd_set fdset;
217 FD_ZERO(&fdset);
218 FD_SET(fileno(stdin), &fdset);
219 if (socket_fd > 0)
220 FD_SET(socket_fd, &fdset);
222 status = select(getdtablehi(), &fdset, 0, 0, 0);
223 if (status <= 0)
225 fprintf(stderr, "%s: select() returned %d\n", argv[0], status);
226 exit(-1);
229 // which fd is set?
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");
239 notDone = PR_FALSE;
241 else if (!strncmp(line, "abort ", 6))
243 // abort id
245 else if (strchr(line, ' ') || strchr(line, '\t'))
247 fprintf(stderr, "%s: unrecognized command %s.\n", argv[0], line);
249 else
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);
261 if (size > 0)
263 // parse buffer into hostent
264 char *p = buf;
265 fprintf(stderr, "bytes read: %d\n", size);
266 fprintf(stderr, "response code: %d\n", *(int *)p);
267 p += sizeof(int);
269 for (int i=0; i < size; i++) {
270 if (!(i%8))
271 fprintf(stderr, "\n");
272 fprintf(stderr, "%2.2x ",(unsigned char)buf[i]);
274 fprintf(stderr, "\n");
275 hostent *h;
276 h = bytesToHostent(p);
278 close(socket_fd);
279 socket_fd = 0;
283 return 0;
287 buffer
289 int nameLen;
291 if (nameLen > 0)
292 char [nameLen+1] name
294 int aliasCount
295 for each alias
296 int aliasNameLen
297 char [aliasNameLen+1] aliasName
299 int h_addrtype
300 int h_length
301 int addrCount
302 for each addr
303 char[h_length] addr