]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_host.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
33 #include <rpc/types.h>
35 #include "_lu_types.h"
38 #include <sys/socket.h>
39 #import <netinet/in.h>
41 extern struct hostent
*_res_gethostbyaddr();
42 extern struct hostent
*_res_gethostbyname();
43 extern struct hostent
*_old_gethostbyaddr();
44 extern struct hostent
*_old_gethostbyname();
45 extern struct hostent
*_old_gethostent();
46 extern void _old_sethostent();
47 extern void _old_endhostent();
48 extern void _old_sethostfile();
52 static lookup_state h_state
= LOOKUP_CACHE
;
54 * The static return value from get*ent functions
56 static struct hostent global_h
;
57 static int global_free
= 1;
58 static char *h_data
= NULL
;
59 static unsigned h_datalen
;
60 static int h_nentries
;
61 static int h_start
= 1;
70 if (global_free
== 1) return;
72 free(global_h
.h_name
);
73 aliases
= global_h
.h_aliases
;
76 while (*aliases
!= NULL
) free(*aliases
++);
77 free(global_h
.h_aliases
);
80 for (i
= 0; global_h
.h_addr_list
[i
] != NULL
; i
++)
81 free(global_h
.h_addr_list
[i
]);
83 free(global_h
.h_addr_list
);
89 convert_h(_lu_hostent
*lu_h
)
95 global_h
.h_name
= strdup(lu_h
->h_names
.h_names_val
[0]);
97 len
= lu_h
->h_names
.h_names_len
- 1;
98 global_h
.h_aliases
= (char **)malloc((len
+ 1) * sizeof(char *));
100 for (i
= 0; i
< len
; i
++)
102 global_h
.h_aliases
[i
] = strdup(lu_h
->h_names
.h_names_val
[i
+ 1]);
105 global_h
.h_aliases
[len
] = NULL
;
107 global_h
.h_addrtype
= AF_INET
;
108 global_h
.h_length
= sizeof(long);
110 len
= lu_h
->h_addrs
.h_addrs_len
;
111 addr_len
= sizeof(u_long
*);
113 global_h
.h_addr_list
= (char **)malloc((len
+ 1) * addr_len
);
115 for (i
= 0; i
< len
; i
++)
117 global_h
.h_addr_list
[i
] = (char *)malloc(sizeof(long));
118 bcopy((const void *)&(lu_h
->h_addrs
.h_addrs_val
[i
]),
119 (void *)global_h
.h_addr_list
[i
], sizeof(long));
122 global_h
.h_addr_list
[len
] = NULL
;
127 static struct hostent
*
128 lu_gethostbyaddr(const char *addr
, int len
, int type
)
131 _lu_hostent_ptr lu_h
;
134 static int proc
= -1;
135 unit lookup_buf
[MAX_INLINE_UNITS
];
137 if (len
!= sizeof(long) || (type
!= AF_INET
))
139 h_errno
= HOST_NOT_FOUND
;
145 if (_lookup_link(_lu_port
, "gethostbyaddr", &proc
) != KERN_SUCCESS
)
147 h_errno
= HOST_NOT_FOUND
;
152 bcopy(addr
, &address
, sizeof(address
));
153 address
= htonl(address
);
154 datalen
= MAX_INLINE_UNITS
;
155 if (_lookup_one(_lu_port
, proc
, (unit
*)&address
, 1, lookup_buf
, &datalen
)
158 h_errno
= HOST_NOT_FOUND
;
162 datalen
*= BYTES_PER_XDR_UNIT
;
163 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
165 h_errno
= HOST_NOT_FOUND
;
166 if (!xdr__lu_hostent_ptr(&xdr
, &lu_h
) ||
167 !xdr_int(&xdr
, &h_errno
) || (lu_h
== NULL
))
176 xdr_free(xdr__lu_hostent_ptr
, &lu_h
);
180 static struct hostent
*
181 lu_gethostbyname(const char *name
)
184 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
187 _lu_hostent_ptr lu_h
;
188 static int proc
= -1;
189 unit lookup_buf
[MAX_INLINE_UNITS
];
193 if (_lookup_link(_lu_port
, "gethostbyname", &proc
) != KERN_SUCCESS
)
195 h_errno
= HOST_NOT_FOUND
;
200 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
201 if (!xdr__lu_string(&outxdr
, &name
))
203 xdr_destroy(&outxdr
);
204 h_errno
= HOST_NOT_FOUND
;
208 datalen
= MAX_INLINE_UNITS
;
209 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
210 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
213 xdr_destroy(&outxdr
);
214 h_errno
= HOST_NOT_FOUND
;
218 xdr_destroy(&outxdr
);
220 datalen
*= BYTES_PER_XDR_UNIT
;
221 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
224 h_errno
= HOST_NOT_FOUND
;
225 if (!xdr__lu_hostent_ptr(&inxdr
, &lu_h
) ||
226 !xdr_int(&inxdr
, &h_errno
) || (lu_h
== NULL
))
235 xdr_free(xdr__lu_hostent_ptr
, &lu_h
);
246 vm_deallocate(mach_task_self(), (vm_address_t
)h_data
, h_datalen
);
258 static struct hostent
*
261 static int proc
= -1;
270 if (_lookup_link(_lu_port
, "gethostent", &proc
) != KERN_SUCCESS
)
277 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &h_data
, &h_datalen
)
285 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
286 h_datalen
*= BYTES_PER_XDR_UNIT
;
288 xdrmem_create(&h_xdr
, h_data
, h_datalen
,
290 if (!xdr_int(&h_xdr
, &h_nentries
))
305 bzero(&lu_h
, sizeof(lu_h
));
306 if (!xdr__lu_hostent(&h_xdr
, &lu_h
))
315 xdr_free(xdr__lu_hostent
, &lu_h
);
320 gethostbyaddr(const char *addr
, int len
, int type
)
326 res
= lu_gethostbyaddr(addr
, len
, type
);
330 res
= _res_gethostbyaddr(addr
, len
, type
);
331 if (res
== NULL
) res
= _old_gethostbyaddr(addr
, len
, type
);
338 gethostbyname(const char *name
)
345 res
= lu_gethostbyname(name
);
349 res
= _res_gethostbyname(name
);
350 if (res
== NULL
) res
= _old_gethostbyname(name
);
355 if (inet_aton(name
, &addr
) == 0) return NULL
;
356 return gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET
);
365 GETENT(lu_gethostent
, _old_gethostent
, &h_state
, struct hostent
);
369 sethostent(int stayopen
)
371 SETSTATE(lu_sethostent
, _old_sethostent
, &h_state
, stayopen
);
377 UNSETSTATE(lu_endhostent
, _old_endhostent
, &h_state
);