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
39 #define LONG_ALIGN(n) roundup2(n, sizeof(long))
41 static void load_client_broadcast_tags(tag_t tag
, const char *tagName
);
50 * The server list is in the form host[:tag]
53 for (tag
= AddrBase
; tag
&& !done
; tag
= tag
->next
) {
54 struct sockaddr_in sain
;
55 struct sockaddr_in rsin
;
66 bzero(&sain
, sizeof(sain
));
67 if (tag
->name
== NULL
) {
68 load_client_broadcast_tags(tag
, "auto");
71 if (tag
->name
[0] == ':') {
72 load_client_broadcast_tags(tag
, tag
->name
+ 1);
75 host
= strdup(tag
->name
);
76 if ((tagName
= strchr(host
, ':')) != NULL
) {
78 tagName
= strdup(tagName
);
80 tagName
= strdup("auto");
82 if (inet_aton(host
, &sain
.sin_addr
) == 0) {
84 if ((hp
= gethostbyname2(host
, AF_INET
)) == NULL
) {
85 fprintf(stderr
, "Unable to resolve %s\n", host
);
88 bcopy(hp
->h_addr_list
[0], &sain
.sin_addr
, hp
->h_length
);
90 host
= strdup(hp
->h_name
);
93 sain
.sin_port
= htons(257);
94 sain
.sin_len
= sizeof(sain
);
95 sain
.sin_family
= AF_INET
;
98 * Do a couple of UDP transactions to locate the tag on the server.
100 printf("%s:%s - ", host
, tagName
);
102 rc
= udp_transact(&sain
, &rsin
, &ufd
, &res
, &len
, "TAG %s\r\n", tagName
);
103 if (rc
!= 101 || res
== NULL
) {
104 printf("NO LUCK %s\n", (res
? res
: ""));
108 rc
= tcp_transact(&rsin
, &fi
, &fo
, &buf
, &len
, "TAG %s\r\n", tagName
);
109 if (rc
== 201 && buf
) {
113 asprintf(&path
, "%s/%s.sh", WorkDir
, tagName
);
114 ffd
= open(path
, O_CREAT
|O_TRUNC
|O_RDWR
, 0755);
115 if (ffd
>= 0 && write(ffd
, buf
, len
) == len
) {
116 printf("running %s [%d] in", path
, len
);
119 for (rc
= 5; rc
> 0; --rc
) {
128 printf("rconfig script exit code %d\n", rc
);
136 printf(" unable to create %s [%d] - DOWNLOAD FAILED\n",
140 printf(" DOWNLOAD FAILED\n");
166 load_client_broadcast_tags(tag_t tag
, const char *tagName
)
168 struct sockaddr_dl
*sdl
;
169 struct if_msghdr
*ifm
;
179 mib
[4] = NET_RT_IFLIST
;
182 if (sysctl(mib
, 6, NULL
, &bytes
, NULL
, 0) < 0) {
183 printf("no interfaces!\n");
187 if (sysctl(mib
, 6, buf
, &bytes
, NULL
, 0) < 0) {
188 printf("no interfaces!\n");
193 while ((char *)ifm
< buf
+ bytes
&& ifm
->ifm_msglen
) {
194 switch(ifm
->ifm_type
) {
196 if (ifm
->ifm_flags
& IFF_UP
) {
197 sdl
= (void *)(ifm
+ 1);
204 struct sockaddr_in
*sain
;
205 struct ifa_msghdr
*ifam
;
211 scan
= (char *)(ifam
+ 1);
212 for (i
= 0; i
< RTAX_MAX
; ++i
) {
213 if ((1 << i
) & ifam
->ifam_addrs
) {
216 asprintf(&name
, "%s:%s",
217 inet_ntoa(sain
->sin_addr
), tagName
);
218 ntag
= calloc(sizeof(struct tag
), 1);
221 ntag
->next
= tag
->next
;
225 printf("add: %s (%s)\n", sdl
->sdl_data
, tag
->name
);
227 scan
= scan
+ LONG_ALIGN(sain
->sin_len
);
233 ifm
= (void *)((char *)ifm
+ ifm
->ifm_msglen
);