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 #import <netinet/in.h>
40 extern struct protoent
*_old_getprotobynumber();
41 extern struct protoent
*_old_getprotobyname();
42 extern struct protoent
*_old_getprotoent();
43 extern void _old_setprotoent();
44 extern void _old_endprotoent();
46 static lookup_state p_state
= LOOKUP_CACHE
;
47 static struct protoent global_p
;
48 static int global_free
= 1;
49 static char *p_data
= NULL
;
50 static unsigned p_datalen
;
51 static int p_nentries
;
60 if (global_free
== 1) return;
62 free(global_p
.p_name
);
63 aliases
= global_p
.p_aliases
;
66 while (*aliases
!= NULL
) free(*aliases
++);
67 free(global_p
.p_aliases
);
74 convert_p(_lu_protoent
*lu_p
)
80 global_p
.p_name
= strdup(lu_p
->p_names
.p_names_val
[0]);
82 len
= lu_p
->p_names
.p_names_len
- 1;
83 global_p
.p_aliases
= (char **)malloc((len
+ 1) * sizeof(char *));
85 for (i
= 0; i
< len
; i
++)
87 global_p
.p_aliases
[i
] = strdup(lu_p
->p_names
.p_names_val
[i
+1]);
90 global_p
.p_aliases
[len
] = NULL
;
92 global_p
.p_proto
= lu_p
->p_proto
;
97 static struct protoent
*
98 lu_getprotobynumber(long number
)
101 _lu_protoent_ptr lu_p
;
103 static int proc
= -1;
104 unit lookup_buf
[MAX_INLINE_UNITS
];
108 if (_lookup_link(_lu_port
, "getprotobynumber", &proc
) != KERN_SUCCESS
)
114 number
= htonl(number
);
115 datalen
= MAX_INLINE_UNITS
;
116 if (_lookup_one(_lu_port
, proc
, (unit
*)&number
, 1, lookup_buf
, &datalen
)
122 datalen
*= BYTES_PER_XDR_UNIT
;
123 xdrmem_create(&xdr
, lookup_buf
, datalen
,
126 if (!xdr__lu_protoent_ptr(&xdr
, &lu_p
) || (lu_p
== NULL
))
135 xdr_free(xdr__lu_protoent_ptr
, &lu_p
);
139 static struct protoent
*
140 lu_getprotobyname(const char *name
)
143 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
146 _lu_protoent_ptr lu_p
;
147 static int proc
= -1;
148 unit lookup_buf
[MAX_INLINE_UNITS
];
152 if (_lookup_link(_lu_port
, "getprotobyname", &proc
) != KERN_SUCCESS
)
158 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
159 if (!xdr__lu_string(&outxdr
, &name
))
161 xdr_destroy(&outxdr
);
165 datalen
= MAX_INLINE_UNITS
;
166 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
167 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
170 xdr_destroy(&outxdr
);
174 xdr_destroy(&outxdr
);
176 datalen
*= BYTES_PER_XDR_UNIT
;
177 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
180 if (!xdr__lu_protoent_ptr(&inxdr
, &lu_p
) || (lu_p
== NULL
))
189 xdr_free(xdr__lu_protoent_ptr
, &lu_p
);
200 vm_deallocate(mach_task_self(), (vm_address_t
)p_data
, p_datalen
);
212 static struct protoent
*
215 static int proc
= -1;
224 if (_lookup_link(_lu_port
, "getprotoent", &proc
) != KERN_SUCCESS
)
231 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &p_data
, &p_datalen
)
238 p_datalen
*= BYTES_PER_XDR_UNIT
;
239 xdrmem_create(&p_xdr
, p_data
, p_datalen
,
241 if (!xdr_int(&p_xdr
, &p_nentries
))
256 bzero(&lu_p
, sizeof(lu_p
));
257 if (!xdr__lu_protoent(&p_xdr
, &lu_p
))
266 xdr_free(xdr__lu_protoent
, &lu_p
);
271 getprotobynumber(int number
)
273 LOOKUP1(lu_getprotobynumber
, _old_getprotobynumber
, number
,
278 getprotobyname(const char *name
)
280 LOOKUP1(lu_getprotobyname
, _old_getprotobyname
, name
, struct protoent
);
286 GETENT(lu_getprotoent
, _old_getprotoent
, &p_state
, struct protoent
);
290 setprotoent(int stayopen
)
292 SETSTATE(lu_setprotoent
, _old_setprotoent
, &p_state
, stayopen
);
298 UNSETSTATE(lu_endprotoent
, _old_endprotoent
, &p_state
);