]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_service.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@
25 * Services file lookup
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 servent
*_old_getservbyport();
41 extern struct servent
*_old_getservbyname();
42 extern struct servent
*_old_getservent();
43 extern void _old_setservent();
44 extern void _old_endservent();
45 extern void _old_setservfile();
47 static lookup_state s_state
= LOOKUP_CACHE
;
48 static struct servent global_s
;
49 static int global_free
= 1;
50 static char *s_data
= NULL
;
51 static unsigned s_datalen
;
52 static int s_nentries
;
53 static int s_start
= 1;
61 if (global_free
== 1) return;
63 free(global_s
.s_name
);
65 aliases
= global_s
.s_aliases
;
68 while (*aliases
!= NULL
) free(*aliases
++);
69 free(global_s
.s_aliases
);
76 convert_s(_lu_servent
*lu_s
)
82 global_s
.s_name
= strdup(lu_s
->s_names
.s_names_val
[0]);
84 len
= lu_s
->s_names
.s_names_len
- 1;
85 global_s
.s_aliases
= (char **)malloc((len
+ 1) * sizeof(char *));
87 for (i
= 0; i
< len
; i
++)
89 global_s
.s_aliases
[i
] = strdup(lu_s
->s_names
.s_names_val
[i
+1]);
92 global_s
.s_aliases
[len
] = NULL
;
94 global_s
.s_proto
= lu_s
->s_proto
;
95 global_s
.s_port
= lu_s
->s_port
;
100 static struct servent
*
101 lu_getservbyport(int port
, const char *proto
)
104 _lu_servent_ptr lu_s
;
106 static int proc
= -1;
107 char output_buf
[_LU_MAXLUSTRLEN
+ 3 * BYTES_PER_XDR_UNIT
];
108 unit lookup_buf
[MAX_INLINE_UNITS
];
113 if (_lookup_link(_lu_port
, "getservbyport", &proc
) != KERN_SUCCESS
)
119 /* Encode NULL for xmission to lookupd. */
120 if (!proto
) proto
= "";
122 xdrmem_create(&outxdr
, output_buf
, sizeof(output_buf
), XDR_ENCODE
);
123 if (!xdr_int(&outxdr
, &port
) || !xdr__lu_string(&outxdr
, &proto
))
125 xdr_destroy(&outxdr
);
129 datalen
= MAX_INLINE_UNITS
;
130 if (_lookup_one(_lu_port
, proc
, (unit
*)output_buf
,
131 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
134 xdr_destroy(&outxdr
);
138 xdr_destroy(&outxdr
);
140 datalen
*= BYTES_PER_XDR_UNIT
;
141 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
143 if (!xdr__lu_servent_ptr(&xdr
, &lu_s
) || (lu_s
== NULL
))
152 xdr_free(xdr__lu_servent_ptr
, &lu_s
);
156 static struct servent
*
157 lu_getservbyname(const char *name
, const char *proto
)
160 unit lookup_buf
[MAX_INLINE_UNITS
];
161 char output_buf
[2 * (_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
)];
164 _lu_servent_ptr lu_s
;
165 static int proc
= -1;
169 if (_lookup_link(_lu_port
, "getservbyname", &proc
) != KERN_SUCCESS
)
175 /* Encode NULL for xmission to lookupd. */
176 if (!proto
) proto
= "";
178 xdrmem_create(&outxdr
, output_buf
, sizeof(output_buf
), XDR_ENCODE
);
179 if (!xdr__lu_string(&outxdr
, &name
) || !xdr__lu_string(&outxdr
, &proto
))
181 xdr_destroy(&outxdr
);
185 datalen
= MAX_INLINE_UNITS
;
186 if (_lookup_one(_lu_port
, proc
, (unit
*)output_buf
,
187 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
190 xdr_destroy(&outxdr
);
194 xdr_destroy(&outxdr
);
196 datalen
*= BYTES_PER_XDR_UNIT
;
197 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
200 if (!xdr__lu_servent_ptr(&inxdr
, &lu_s
) || (lu_s
== NULL
))
209 xdr_free(xdr__lu_servent_ptr
, &lu_s
);
220 vm_deallocate(mach_task_self(), (vm_address_t
)s_data
, s_datalen
);
232 static struct servent
*
235 static int proc
= -1;
244 if (_lookup_link(_lu_port
, "getservent", &proc
) != KERN_SUCCESS
)
251 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &s_data
, &s_datalen
)
259 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
260 s_datalen
*= BYTES_PER_XDR_UNIT
;
263 xdrmem_create(&s_xdr
, s_data
, s_datalen
,
265 if (!xdr_int(&s_xdr
, &s_nentries
))
280 bzero(&lu_s
, sizeof(lu_s
));
281 if (!xdr__lu_servent(&s_xdr
, &lu_s
))
290 xdr_free(xdr__lu_servent
, &lu_s
);
295 getservbyport(int port
, const char *proto
)
297 LOOKUP2(lu_getservbyport
, _old_getservbyport
, port
, proto
, struct servent
);
301 getservbyname(const char *name
, const char *proto
)
303 LOOKUP2(lu_getservbyname
, _old_getservbyname
, name
, proto
,
310 GETENT(lu_getservent
, _old_getservent
, &s_state
, struct servent
);
314 setservent(int stayopen
)
316 SETSTATE(lu_setservent
, _old_setservent
, &s_state
, stayopen
);
322 UNSETSTATE(lu_endservent
, _old_endservent
, &s_state
);