4 * $DragonFly: src/sbin/rconfig/subs.c,v 1.2 2004/06/19 16:03:01 dillon Exp $
9 static void udp_alarm(int signo
);
15 return(c
== ' ' || c
== '\t' || c
== '\r' || c
== '\n');
19 parse_str(char **scanp
, int flags
)
24 for (base
= *scanp
; *base
&& iswhite(*base
); ++base
)
26 for (ptr
= base
; *ptr
&& !iswhite(*ptr
); ++ptr
) {
27 if (flags
& PAS_ALPHA
) {
28 if ((*ptr
>= 'a' && *ptr
<= 'z') ||
29 (*ptr
>= 'A' && *ptr
<= 'Z') ||
35 if (flags
& PAS_NUMERIC
) {
36 if (*ptr
>= '0' && *ptr
<= '9')
39 if ((flags
& PAS_ANY
) == 0)
49 udp_transact(struct sockaddr_in
*sain
, struct sockaddr_in
*rsin
, int *pfd
,
50 char **bufp
, int *lenp
, const char *ctl
, ...)
55 int rsin_len
= sizeof(*rsin
);
67 if ((fd
= *pfd
) < 0) {
68 struct sockaddr_in lsin
;
70 lsin
.sin_addr
.s_addr
= INADDR_ANY
;
72 lsin
.sin_family
= AF_INET
;
73 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, PF_UNSPEC
)) < 0) {
74 asprintf(bufp
, "udp_transaction: socket: %s", strerror(errno
));
75 *lenp
= strlen(*bufp
);
78 setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof(on
));
79 if (bind(fd
, (void *)&lsin
, sizeof(lsin
)) < 0) {
80 asprintf(bufp
, "udp_transaction: bind: %s", strerror(errno
));
81 *lenp
= strlen(*bufp
);
89 vsnprintf(buf
, sizeof(buf
), ctl
, va
);
91 if (sendto(fd
, buf
, strlen(buf
), 0, (void *)sain
, sizeof(*sain
)) >= 0) {
92 struct sigaction nact
;
93 struct sigaction oact
;
95 bzero(&nact
, sizeof(nact
));
96 nact
.sa_handler
= udp_alarm
;
98 sigaction(SIGALRM
, &nact
, &oact
);
100 n
= recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, (void *)rsin
, &rsin_len
);
102 sigaction(SIGALRM
, &oact
, NULL
);
104 if (errno
== EINTR
&& --nretry
> 0)
106 asprintf(bufp
, "udp_transaction: recvfrom: timeout");
107 *lenp
= strlen(*bufp
);
110 while (n
> 0 && (buf
[n
- 1] == '\r' || buf
[n
- 1] == '\n'))
113 rc
= strtol(buf
, NULL
, 10);
118 asprintf(bufp
, "udp_transaction: sendto: %s", strerror(errno
));
119 *lenp
= strlen(*bufp
);
125 tcp_transact(struct sockaddr_in
*sain
, FILE **pfi
, FILE **pfo
, char **bufp
,
126 int *lenp
, const char *ctl
, ...)
140 if ((fd
= socket(AF_INET
, SOCK_STREAM
, PF_UNSPEC
)) < 0) {
141 asprintf(bufp
, "tcp_transaction: socket: %s", strerror(errno
));
142 *lenp
= strlen(*bufp
);
145 if (connect(fd
, (void *)sain
, sizeof(*sain
)) < 0) {
146 asprintf(bufp
, "tcp_transaction: connect: %s", strerror(errno
));
147 *lenp
= strlen(*bufp
);
151 *pfi
= fdopen(fd
, "r");
152 *pfo
= fdopen(dup(fd
), "w");
153 if (tcp_transact(sain
, pfi
, pfo
, bufp
, lenp
, NULL
) != 108) {
159 asprintf(bufp
, "tcp_transaction: did not get HELLO from server\n");
160 *lenp
= strlen(*bufp
);
164 printf("%s\n", *bufp
);
171 vfprintf(*pfo
, ctl
, va
);
175 if (fgets(buf
, sizeof(buf
), *pfi
) != NULL
) {
176 rc
= strtol(buf
, NULL
, 10);
178 if (rc
== 201 && strstr(buf
, "SIZE=") != NULL
) {
179 *lenp
= strtol(strstr(buf
, "SIZE=") + 5, NULL
, 0);
181 *bufp
= malloc(*lenp
);
182 for (rc
= 0; *bufp
&& rc
< *lenp
; rc
+= n
) {
183 if ((n
= *lenp
- rc
) > sizeof(buf
))
185 n
= fread(*bufp
+ rc
, 1, n
, *pfi
);
189 if (rc
== *lenp
&& fgets(buf
, sizeof(buf
), *pfi
)) {
190 if (strstr(buf
, "ERROR=")) {
191 rc
= strtol(strstr(buf
, "ERROR=") + 6, NULL
, 0);
204 asprintf(bufp
, "tcp_transaction: download failed\n");
205 *lenp
= strlen(*bufp
);
208 while (n
> 0 && (buf
[n
-1] == '\r' || buf
[n
-1] == '\n'))
215 asprintf(bufp
, "tcp_transaction: read: %s", strerror(errno
));
216 *lenp
= strlen(*bufp
);