4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $DragonFly: src/sbin/rconfig/client.c,v 1.4 2005/04/02 22:15:20 dillon Exp $
41 #define LONG_ALIGN(n) (((n) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
43 static void load_client_broadcast_tags(tag_t tag
, const char *tagName
);
52 * The server list is in the form host[:tag]
55 for (tag
= AddrBase
; tag
&& !done
; tag
= tag
->next
) {
56 struct sockaddr_in sain
;
57 struct sockaddr_in rsin
;
68 bzero(&sain
, sizeof(sain
));
69 if (tag
->name
== NULL
) {
70 load_client_broadcast_tags(tag
, "auto");
73 if (tag
->name
[0] == ':') {
74 load_client_broadcast_tags(tag
, tag
->name
+ 1);
77 host
= strdup(tag
->name
);
78 if ((tagName
= strchr(host
, ':')) != NULL
) {
80 tagName
= strdup(tagName
);
82 tagName
= strdup("auto");
84 if (inet_aton(host
, &sain
.sin_addr
) == 0) {
86 if ((hp
= gethostbyname2(host
, AF_INET
)) == NULL
) {
87 fprintf(stderr
, "Unable to resolve %s\n", host
);
90 bcopy(hp
->h_addr_list
[0], &sain
.sin_addr
, hp
->h_length
);
92 host
= strdup(hp
->h_name
);
95 sain
.sin_port
= htons(257);
96 sain
.sin_len
= sizeof(sain
);
97 sain
.sin_family
= AF_INET
;
100 * Do a couple of UDP transactions to locate the tag on the server.
102 printf("%s:%s - ", host
, tagName
);
104 rc
= udp_transact(&sain
, &rsin
, &ufd
, &res
, &len
, "TAG %s\r\n", tagName
);
105 if (rc
!= 101 || res
== NULL
) {
106 printf("NO LUCK %s\n", (res
? res
: ""));
110 rc
= tcp_transact(&rsin
, &fi
, &fo
, &buf
, &len
, "TAG %s\r\n", tagName
);
111 if (rc
== 201 && buf
) {
115 asprintf(&path
, "%s/%s.sh", WorkDir
, tagName
);
116 ffd
= open(path
, O_CREAT
|O_TRUNC
|O_RDWR
, 0755);
117 if (ffd
>= 0 && write(ffd
, buf
, len
) == len
) {
118 printf("running %s [%d] in", path
, len
);
121 for (rc
= 5; rc
> 0; --rc
) {
130 printf("rconfig script exit code %d\n", rc
);
138 printf(" unable to create %s [%d] - DOWNLOAD FAILED\n",
142 printf(" DOWNLOAD FAILED\n");
168 load_client_broadcast_tags(tag_t tag
, const char *tagName
)
170 struct sockaddr_dl
*sdl
;
171 struct if_msghdr
*ifm
;
181 mib
[4] = NET_RT_IFLIST
;
184 if (sysctl(mib
, 6, NULL
, &bytes
, NULL
, 0) < 0) {
185 printf("no interfaces!\n");
189 if (sysctl(mib
, 6, buf
, &bytes
, NULL
, 0) < 0) {
190 printf("no interfaces!\n");
195 while ((char *)ifm
< buf
+ bytes
&& ifm
->ifm_msglen
) {
196 switch(ifm
->ifm_type
) {
198 if (ifm
->ifm_flags
& IFF_UP
) {
199 sdl
= (void *)(ifm
+ 1);
206 struct sockaddr_in
*sain
;
207 struct ifa_msghdr
*ifam
;
213 scan
= (char *)(ifam
+ 1);
214 for (i
= 0; i
< RTAX_MAX
; ++i
) {
215 if ((1 << i
) & ifam
->ifam_addrs
) {
218 asprintf(&name
, "%s:%s",
219 inet_ntoa(sain
->sin_addr
), tagName
);
220 ntag
= calloc(sizeof(struct tag
), 1);
223 ntag
->next
= tag
->next
;
227 printf("add: %s (%s)\n", sdl
->sdl_data
, tag
->name
);
229 scan
= scan
+ LONG_ALIGN(sain
->sin_len
);
235 ifm
= (void *)((char *)ifm
+ ifm
->ifm_msglen
);