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 netent
*_res_getnetbyaddr();
42 extern struct netent
*_res_getnetbyname();
43 extern struct netent
*_old_getnetbyaddr();
44 extern struct netent
*_old_getnetbyname();
45 extern struct netent
*_old_getnetent();
46 extern void _old_setnetent();
47 extern void _old_endnetent();
49 static lookup_state n_state
= LOOKUP_CACHE
;
50 static struct netent global_n
;
51 static int global_free
= 1;
52 static char *n_data
= NULL
;
53 static unsigned n_datalen
;
54 static int n_nentries
;
55 static int n_start
= 1;
63 if (global_free
== 1) return;
65 free(global_n
.n_name
);
67 aliases
= global_n
.n_aliases
;
70 while (*aliases
!= NULL
) free(*aliases
++);
71 free(global_n
.n_aliases
);
78 convert_n(_lu_netent
*lu_n
)
84 global_n
.n_name
= strdup(lu_n
->n_names
.n_names_val
[0]);
86 len
= lu_n
->n_names
.n_names_len
- 1;
87 global_n
.n_aliases
= malloc((len
+ 1) * sizeof(char *));
89 for (i
= 0; i
< len
; i
++)
91 global_n
.n_aliases
[i
] = strdup(lu_n
->n_names
.n_names_val
[i
+ 1]);
94 global_n
.n_aliases
[len
] = NULL
;
96 global_n
.n_addrtype
= AF_INET
;
97 global_n
.n_net
= lu_n
->n_net
;
102 static struct netent
*
103 lu_getnetbyaddr(long addr
, int type
)
108 static int proc
= -1;
109 unit lookup_buf
[MAX_INLINE_UNITS
];
118 if (_lookup_link(_lu_port
, "getnetbyaddr", &proc
) != KERN_SUCCESS
)
125 datalen
= MAX_INLINE_UNITS
;
126 if (_lookup_one(_lu_port
, proc
, (unit
*)&addr
, 1, lookup_buf
, &datalen
)
132 datalen
*= BYTES_PER_XDR_UNIT
;
133 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
135 if (!xdr__lu_netent_ptr(&xdr
, &lu_n
) || (lu_n
== NULL
))
144 xdr_free(xdr__lu_netent_ptr
, &lu_n
);
148 static struct netent
*
149 lu_getnetbyname(const char *name
)
152 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
156 static int proc
= -1;
157 unit lookup_buf
[MAX_INLINE_UNITS
];
161 if (_lookup_link(_lu_port
, "getnetbyname", &proc
) != KERN_SUCCESS
)
167 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
168 if (!xdr__lu_string(&outxdr
, &name
))
170 xdr_destroy(&outxdr
);
174 datalen
= MAX_INLINE_UNITS
;
175 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
176 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
179 xdr_destroy(&outxdr
);
183 xdr_destroy(&outxdr
);
185 datalen
*= BYTES_PER_XDR_UNIT
;
186 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
189 if (!xdr__lu_netent_ptr(&inxdr
, &lu_n
) || (lu_n
== NULL
))
198 xdr_free(xdr__lu_netent_ptr
, &lu_n
);
209 vm_deallocate(mach_task_self(), (vm_address_t
)n_data
, n_datalen
);
221 static struct netent
*
224 static int proc
= -1;
233 if (_lookup_link(_lu_port
, "getnetent", &proc
) != KERN_SUCCESS
)
240 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &n_data
, &n_datalen
)
248 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
249 n_datalen
*= BYTES_PER_XDR_UNIT
;
251 xdrmem_create(&n_xdr
, n_data
, n_datalen
,
253 if (!xdr_int(&n_xdr
, &n_nentries
))
268 bzero(&lu_n
, sizeof(lu_n
));
269 if (!xdr__lu_netent(&n_xdr
, &lu_n
))
278 xdr_free(xdr__lu_netent
, &lu_n
);
283 getnetbyaddr(long addr
, int type
)
289 res
= lu_getnetbyaddr(addr
, type
);
293 res
= _res_getnetbyaddr(addr
, type
);
294 if (res
== NULL
) res
= _old_getnetbyaddr(addr
, type
);
301 getnetbyname(const char *name
)
307 res
= lu_getnetbyname(name
);
311 res
= _res_getnetbyname(name
);
312 if (res
== NULL
) res
= _old_getnetbyname(name
);
321 GETENT(lu_getnetent
, _old_getnetent
, &n_state
, struct netent
);
325 setnetent(int stayopen
)
327 SETSTATE(lu_setnetent
, _old_setnetent
, &n_state
, stayopen
);
333 UNSETSTATE(lu_endnetent
, _old_endnetent
, &n_state
);