]>
git.saurik.com Git - apple/libinfo.git/blob - netinfo.subproj/ni_pwdomain.c
9d9c541550091f713b782b1f1e398b8c8cc17242
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 * Copyright (C) 1990 by NeXT, Inc. All rights reserved.
29 * ni_pwdomain function: present working domain for a netinfo handle
32 * ni_status ni_pwdomain(void *ni, ni_name *buf)
34 * pwd is returned in buf, which can be freed with ni_name_free
38 #include <sys/types.h>
39 #include <sys/param.h>
42 #include <netinfo/ni.h>
44 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include "sys_interfaces.h"
49 extern char *inet_ntoa();
51 static const char NAME_NAME
[] = "name";
52 static const char NAME_MACHINES
[] = "machines";
53 static const char NAME_IP_ADDRESS
[] = "ip_address";
54 static const char NAME_SERVES
[] = "serves";
55 static const char NAME_UNKNOWN
[] = "###UNKNOWN###";
58 escape_domain(ni_name name
)
66 for (p
= name
; *p
; p
++)
68 if ((*p
== '/') || (*p
== '\\')) extra
++;
71 newname
= malloc(strlen(name
) + extra
+ 1);
73 for (p
= name
; *p
; p
++)
75 if ((*p
== '/') || (*p
== '\\')) *s
++ = '\\';
85 finddomain(void *ni
, struct in_addr addr
, ni_name tag
)
95 status
= ni_root(ni
, &nid
);
96 if (status
!= NI_OK
) return NULL
;
98 status
= ni_lookup(ni
, &nid
, NAME_NAME
, NAME_MACHINES
, &idl
);
99 if (status
!= NI_OK
) return NULL
;
101 nid
.nii_object
= idl
.niil_val
[0];
102 ni_idlist_free(&idl
);
104 status
= ni_lookup(ni
, &nid
, NAME_IP_ADDRESS
, inet_ntoa(addr
), &idl
);
105 if (status
!= NI_OK
) return NULL
;
107 nid
.nii_object
= idl
.niil_val
[0];
108 ni_idlist_free(&idl
);
110 status
= ni_lookupprop(ni
, &nid
, NAME_SERVES
, &nl
);
111 if (status
!= NI_OK
) return NULL
;
113 for (i
= 0; i
< nl
.ninl_len
; i
++)
115 slash
= rindex(nl
.ninl_val
[i
], '/');
116 if (slash
== NULL
) continue;
118 if (ni_name_match(slash
+ 1, tag
))
121 domain
= escape_domain(nl
.ninl_val
[i
]);
122 ni_namelist_free(&nl
);
127 ni_namelist_free(&nl
);
133 ni_domainof(void *ni
, void *parent
)
135 struct sockaddr_in addr
;
139 interface_list_t
*ilist
;
142 status
= ni_addrtag(ni
, &addr
, &tag
);
143 if (status
!= NI_OK
) return ni_name_dup(NAME_UNKNOWN
);
145 dom
= finddomain(parent
, addr
.sin_addr
, tag
);
152 ilist
= sys_interfaces();
153 if (ilist
== NULL
) return ni_name_dup(NAME_UNKNOWN
);
154 if (sys_is_my_address(ilist
, &(addr
.sin_addr
)))
156 /* Try all my non-loopback interfaces */
157 for (i
= 0; i
< ilist
->count
; i
++)
159 if (ilist
->interface
[i
].addr
.s_addr
== htonl(INADDR_LOOPBACK
)) continue;
161 addr
.sin_addr
.s_addr
= ilist
->interface
[i
].addr
.s_addr
;
162 dom
= finddomain(parent
, addr
.sin_addr
, tag
);
166 sys_interfaces_release(ilist
);
171 sys_interfaces_release(ilist
);
173 dom
= malloc(strlen(tag
) + 256);
174 sprintf(dom
, "%s@%s", tag
, inet_ntoa(addr
.sin_addr
.s_addr
));
180 _ni_pwdomain(void *ni
, ni_name
*buf
)
187 /* Open domain name */
188 nip
= ni_new(ni
, "..");
196 /* Get parent's name */
197 status
= _ni_pwdomain(nip
, buf
);
198 if (status
!= NI_OK
) return status
;
200 /* Get my name relative to my parent */
201 dom
= ni_domainof(ni
, nip
);
203 /* Append my relative name to my parent's name */
205 *buf
= realloc(*buf
, len
+ 1 + strlen(dom
) + 1);
207 strcpy(&(*buf
)[len
+ 1], dom
);
215 * Just call the recursive ni_pwdomain above, and then fix for case of root
219 ni_pwdomain(void *ni
, ni_name
*buf
)
224 status
= _ni_pwdomain(ni
, buf
);
227 if (*buf
!= NULL
) ni_name_free(buf
);