]>
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 if (global_s
.s_name
!= NULL
) free(global_s
.s_name
);
64 global_s
.s_name
= NULL
;
66 if (global_s
.s_proto
!= NULL
) free(global_s
.s_proto
);
67 global_s
.s_proto
= NULL
;
69 aliases
= global_s
.s_aliases
;
72 while (*aliases
!= NULL
) free(*aliases
++);
73 free(global_s
.s_aliases
);
74 global_s
.s_aliases
= NULL
;
81 convert_s(_lu_servent
*lu_s
)
87 global_s
.s_name
= strdup(lu_s
->s_names
.s_names_val
[0]);
89 len
= lu_s
->s_names
.s_names_len
- 1;
90 global_s
.s_aliases
= (char **)malloc((len
+ 1) * sizeof(char *));
92 for (i
= 0; i
< len
; i
++)
94 global_s
.s_aliases
[i
] = strdup(lu_s
->s_names
.s_names_val
[i
+1]);
97 global_s
.s_aliases
[len
] = NULL
;
99 if (lu_s
->s_proto
!= NULL
) global_s
.s_proto
= strdup(lu_s
->s_proto
);
100 global_s
.s_port
= lu_s
->s_port
;
105 static struct servent
*
106 lu_getservbyport(int port
, const char *proto
)
109 _lu_servent_ptr lu_s
;
111 static int proc
= -1;
112 char output_buf
[_LU_MAXLUSTRLEN
+ 3 * BYTES_PER_XDR_UNIT
];
113 unit lookup_buf
[MAX_INLINE_UNITS
];
118 if (_lookup_link(_lu_port
, "getservbyport", &proc
) != KERN_SUCCESS
)
124 /* Encode NULL for xmission to lookupd. */
125 if (!proto
) proto
= "";
127 xdrmem_create(&outxdr
, output_buf
, sizeof(output_buf
), XDR_ENCODE
);
128 if (!xdr_int(&outxdr
, &port
) || !xdr__lu_string(&outxdr
, &proto
))
130 xdr_destroy(&outxdr
);
134 datalen
= MAX_INLINE_UNITS
;
135 if (_lookup_one(_lu_port
, proc
, (unit
*)output_buf
,
136 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
139 xdr_destroy(&outxdr
);
143 xdr_destroy(&outxdr
);
145 datalen
*= BYTES_PER_XDR_UNIT
;
146 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
148 if (!xdr__lu_servent_ptr(&xdr
, &lu_s
) || (lu_s
== NULL
))
157 xdr_free(xdr__lu_servent_ptr
, &lu_s
);
161 static struct servent
*
162 lu_getservbyname(const char *name
, const char *proto
)
165 unit lookup_buf
[MAX_INLINE_UNITS
];
166 char output_buf
[2 * (_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
)];
169 _lu_servent_ptr lu_s
;
170 static int proc
= -1;
174 if (_lookup_link(_lu_port
, "getservbyname", &proc
) != KERN_SUCCESS
)
180 /* Encode NULL for xmission to lookupd. */
181 if (!proto
) proto
= "";
183 xdrmem_create(&outxdr
, output_buf
, sizeof(output_buf
), XDR_ENCODE
);
184 if (!xdr__lu_string(&outxdr
, &name
) || !xdr__lu_string(&outxdr
, &proto
))
186 xdr_destroy(&outxdr
);
190 datalen
= MAX_INLINE_UNITS
;
191 if (_lookup_one(_lu_port
, proc
, (unit
*)output_buf
,
192 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
195 xdr_destroy(&outxdr
);
199 xdr_destroy(&outxdr
);
201 datalen
*= BYTES_PER_XDR_UNIT
;
202 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
205 if (!xdr__lu_servent_ptr(&inxdr
, &lu_s
) || (lu_s
== NULL
))
214 xdr_free(xdr__lu_servent_ptr
, &lu_s
);
225 vm_deallocate(mach_task_self(), (vm_address_t
)s_data
, s_datalen
);
237 static struct servent
*
240 static int proc
= -1;
249 if (_lookup_link(_lu_port
, "getservent", &proc
) != KERN_SUCCESS
)
256 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &s_data
, &s_datalen
)
264 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
265 s_datalen
*= BYTES_PER_XDR_UNIT
;
268 xdrmem_create(&s_xdr
, s_data
, s_datalen
,
270 if (!xdr_int(&s_xdr
, &s_nentries
))
285 bzero(&lu_s
, sizeof(lu_s
));
286 if (!xdr__lu_servent(&s_xdr
, &lu_s
))
295 xdr_free(xdr__lu_servent
, &lu_s
);
300 getservbyport(int port
, const char *proto
)
302 LOOKUP2(lu_getservbyport
, _old_getservbyport
, port
, proto
, struct servent
);
306 getservbyname(const char *name
, const char *proto
)
308 LOOKUP2(lu_getservbyname
, _old_getservbyname
, name
, proto
,
315 GETENT(lu_getservent
, _old_getservent
, &s_state
, struct servent
);
319 setservent(int stayopen
)
321 SETSTATE(lu_setservent
, _old_setservent
, &s_state
, stayopen
);
327 UNSETSTATE(lu_endservent
, _old_endservent
, &s_state
);