* Copyright 2013 Kyungwan Han <asura321@gmail.com>
*
* Not in SUSv4.
-USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0T#<0t#<0s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
+USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0=20T#<0=3t#<0=3s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
config DHCP
bool "dhcp"
char *vendor_cls;
)
-#define flag_get(f,v,d) ((toys.optflags & f) ? v : d)
-#define flag_chk(f) ((toys.optflags & f) ? 1 : 0)
-
#define STATE_INIT 0
#define STATE_REQUESTING 1
#define STATE_BOUND 2
pid_t pid;
char *argv[3];
struct stat sts;
- char *script = flag_get(FLAG_s, TT.script, "/usr/share/dhcp/default.script");
+ char *script = (toys.optflags & FLAG_s) ? TT.script
+ : "/usr/share/dhcp/default.script";
if (stat(script, &sts) == -1 && errno == ENOENT) return;
if (fill_envp(res)) {
*len = 0;
optptr++;
- if (!flag_chk(FLAG_o)) {
+ if (!(toys.optflags & FLAG_o)) {
*len = 4;
*optptr++ = DHCP_OPTION_SUBNET_MASK;
*optptr++ = DHCP_OPTION_ROUTER;
*optptr++ = DHCP_OPTION_DNS_SERVER;
*optptr++ = DHCP_OPTION_BROADCAST;
}
- if (flag_chk(FLAG_O)) {
+ if (toys.optflags & FLAG_O) {
memcpy(optptr++, raw_opt, raw_optcount);
*len += raw_optcount;
}
pend = state->pdhcp.options;
pend = dhcpc_addmsgtype(pend, msgtype);
- if (!flag_chk(FLAG_C)) pend = dhcpc_addclientid(pend);
+ if (!(toys.optflags & FLAG_C)) pend = dhcpc_addclientid(pend);
// Handle the message specific settings
switch (msgtype) {
case DHCPDISCOVER: // Broadcast DISCOVER message to all servers
state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
- if (flag_chk(FLAG_r)) {
+ if (toys.optflags & FLAG_r) {
inet_aton(TT.req_ip, &rqsd);
pend = dhcpc_addreqipaddr(&rqsd, pend);
}
pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t)));
- vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
+ vendor = (toys.optflags & FLAG_V) ? TT.vendor_cls : "toybox\0";
pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
- if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
- if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
- if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
- if (flag_chk(FLAG_x)) pend = set_xopt(pend);
+ if (toys.optflags & FLAG_H) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
+ if (toys.optflags & FLAG_F) pend = dhcpc_addfdnname(pend, TT.fdn_name);
+ if (!(toys.optflags & FLAG_o) || (toys.optflags & FLAG_O))
+ pend = dhcpc_addreqoptions(pend);
+ if (toys.optflags & FLAG_x) pend = set_xopt(pend);
break;
case DHCPREQUEST: // Send REQUEST message to the server that sent the *first* OFFER
state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit.
rqsd.s_addr = htonl(server);
pend = dhcpc_addserverid(&rqsd, pend);
pend = dhcpc_addreqipaddr(&state->ipaddr, pend);
- vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0");
+ vendor = (toys.optflags & FLAG_V) ? TT.vendor_cls : "toybox\0";
pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor));
- if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
- if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name);
- if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend);
- if (flag_chk(FLAG_x)) pend = set_xopt(pend);
+ if (toys.optflags & FLAG_H) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname));
+ if (toys.optflags & FLAG_F) pend = dhcpc_addfdnname(pend, TT.fdn_name);
+ if (!(toys.optflags & FLAG_o) || (toys.optflags & FLAG_O))
+ pend = dhcpc_addreqoptions(pend);
+ if (toys.optflags & FLAG_x) pend = set_xopt(pend);
break;
case DHCPRELEASE: // Send RELEASE message to the server.
memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4);
struct in_addr addr;
int count, optlen, size = ARRAY_LEN(options_list);
- if (flag_chk(FLAG_x)) {
+ if (toys.optflags & FLAG_x) {
if(msgopt_list){
for (count = 0; count < size; count++){
if(msgopt_list[count].val) free(msgopt_list[count].val);
int count, size = ARRAY_LEN(options_list);
for (count = 0; count < size; count++)
if (options_list[count].val) free(options_list[count].val);
- if(flag_chk(FLAG_x)){
+ if (toys.optflags & FLAG_x) {
for (count = 0; count < size; count++)
if (msgopt_list[count].val) free(msgopt_list[count].val);
free(msgopt_list);
xid = 0;
setlinebuf(stdout);
dbg = dummy;
- if (flag_chk(FLAG_v)) dbg = xprintf;
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
- retries = flag_get(FLAG_t, TT.retries, 3);
- if (flag_chk(FLAG_S)) {
+ if (toys.optflags & FLAG_v) dbg = xprintf;
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
+ retries = TT.retries;
+ if (toys.optflags & FLAG_S) {
openlog("UDHCPC :", LOG_PID, LOG_DAEMON);
infomode |= LOG_SYSTEM;
}
infomsg(infomode, "dhcp started");
- if (flag_chk(FLAG_O)) {
+ if (toys.optflags & FLAG_O) {
while (TT.req_opt) {
raw_opt[raw_optcount] = (uint8_t) strtoopt(TT.req_opt->arg, 1);
raw_optcount++;
TT.req_opt = TT.req_opt->next;
}
}
- if (flag_chk(FLAG_x)) {
+ if (toys.optflags & FLAG_x) {
while (TT.pkt_opt) {
(void) strtoopt(TT.pkt_opt->arg, 0);
TT.pkt_opt = TT.pkt_opt->next;
memset(&result, 0, sizeof(dhcpc_result_t));
state = (dhcpc_state_t*) xmalloc(sizeof(dhcpc_state_t));
memset(state, 0, sizeof(dhcpc_state_t));
- state->iface = flag_get(FLAG_i, TT.iface, "eth0");
+ state->iface = (toys.optflags & FLAG_i) ? TT.iface : "eth0";
if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr))
perror_exit("Failed to get interface %s", state->iface);
infomsg(infomode, "Sending discover...");
dhcpc_sendmsg(DHCPDISCOVER);
server = 0;
- timeout = flag_get(FLAG_T, TT.timeout, 3);
+ timeout = TT.timeout;
waited = 0;
packets++;
continue;
}
lease_fail:
run_script(NULL,"leasefail");
- if (flag_chk(FLAG_n)) {
+ if (toys.optflags & FLAG_n) {
infomsg(infomode, "Lease failed. Exiting");
goto ret_with_sockfd;
}
- if (flag_chk(FLAG_b)) {
+ if (toys.optflags & FLAG_b) {
infomsg(infomode, "Lease failed. Going Daemon mode");
daemon(0, 0);
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
toys.optflags &= ~FLAG_b;
toys.optflags |= FLAG_f;
}
- timeout = flag_get(FLAG_A, TT.tryagain, 20);
+ timeout = TT.tryagain;
waited = 0;
packets = 0;
continue;
dhcpc_sendmsg(DHCPREQUEST);
infomsg(infomode, "Sending select for %d.%d.%d.%d...",
(result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff);
- timeout = flag_get(FLAG_T, TT.timeout, 3);
+ timeout = TT.timeout;
waited = 0;
packets++;
continue;
continue;
case SIGTERM:
infomsg(infomode, "Received SIGTERM");
- if (flag_chk(FLAG_R)) release();
+ if (toys.optflags & FLAG_R) release();
goto ret_with_sockfd;
default: break;
}
(result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff,
result.lease_time,
(result.serverid.s_addr >> 24) & 0xff, (result.serverid.s_addr >> 16) & 0xff, (result.serverid.s_addr >> 8) & 0xff, (result.serverid.s_addr) & 0xff);
- if (flag_chk(FLAG_q)) {
- if (flag_chk(FLAG_R)) release();
+ if (toys.optflags & FLAG_q) {
+ if (toys.optflags & FLAG_R) release();
goto ret_with_sockfd;
}
toys.optflags &= ~FLAG_n;
- if (!flag_chk(FLAG_f)) {
+ if (!(toys.optflags & FLAG_f)) {
daemon(0, 0);
toys.optflags |= FLAG_f;
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
}
waited = 0;
continue;
#define LOG_CONSOLE 0x1
#define LOG_SYSTEM 0x2
-#define flag_get(f,v,d) ((toys.optflags & f) ? v : d)
-#define flag_chk(f) ((toys.optflags & f) ? 1 : 0)
-
typedef struct __attribute__((packed)) dhcp6_msg_s {
uint8_t msgtype, transaction_id[3], options[524];
} dhcp6_msg_t;
va_list p, t;
uint8_t infomode = LOG_SILENT;
- if (flag_chk(FLAG_S)) infomode |= LOG_SYSTEM;
- if(flag_chk(FLAG_v)) infomode |= LOG_CONSOLE;
+ if (toys.optflags & FLAG_S) infomode |= LOG_SYSTEM;
+ if(toys.optflags & FLAG_v) infomode |= LOG_CONSOLE;
va_start(p, format);
va_copy(t, p);
used = vsnprintf(NULL, 0, format, t);
struct stat sts;
pid_t pid;
char *argv[3];
- char *script = flag_get(FLAG_s, TT.script, "/usr/share/dhcp/default.script");
+ char *script = (toys.optflags & FLAG_s) ? TT.script
+ : "/usr/share/dhcp/default.script";
if (stat(script, &sts) == -1 && errno == ENOENT) return;
if (!res || fill_envp(res)) {
{
dbg("Lease failed.\n");
run_script(NULL, "leasefail");
- if (flag_chk(FLAG_n)) {
+ if (toys.optflags & FLAG_n) {
xclose(TT.sock);
xclose(TT.sock1);
error_exit("Lease Failed, Exiting.");
}
- if (flag_chk(FLAG_b)) {
+ if (toys.optflags & FLAG_b) {
dbg("Lease failed. Going to daemon mode.\n");
if (daemon(0,0)) perror_exit("daemonize");
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
toys.optflags &= ~FLAG_b;
toys.optflags |= FLAG_f;
}
case SIGTERM:
case SIGINT:
dbg((sig == SIGTERM)?"SIGTERM.\n":"SIGINT.\n");
- if (flag_chk(FLAG_R) && TT.state == DHCP6CONFIRM) send_msg(DHCP6RELEASE);
+ if ((toys.optflags & FLAG_R) && TT.state == DHCP6CONFIRM)
+ send_msg(DHCP6RELEASE);
if(sig == SIGINT) exit(0);
break;
default: break;
dbg = dummy;
TT.state = DHCP6SOLICIT;
- if (flag_chk(FLAG_v)) dbg = logit;
+ if (toys.optflags & FLAG_v) dbg = logit;
if (!TT.interface_name) TT.interface_name = "eth0";
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
if (!TT.retry) TT.retry = 3;
if (!TT.timeout) TT.timeout = 3;
if (!TT.errortimeout) TT.errortimeout = 20;
- if (flag_chk(FLAG_S)) {
+ if (toys.optflags & FLAG_S) {
openlog("DHCP6 :", LOG_PID, LOG_DAEMON);
dbg = logit;
}
TT.retries = 0;
run_script(&dhcp_data, (TT.state == DHCP6REQUEST) ?
"request" : "renew");
- if (flag_chk(FLAG_q)) {
- if (flag_chk(FLAG_R)) send_msg(DHCP6RELEASE);
+ if (toys.optflags & FLAG_q) {
+ if (toys.optflags & FLAG_R) send_msg(DHCP6RELEASE);
break;
}
TT.state = DHCP6CONFIRM;
set_timeout((dhcp_data.va_lf)?dhcp_data.va_lf:INT_MAX);
dbg("Setting timeout to intmax.");
- if (TT.state == DHCP6REQUEST || (!flag_chk(FLAG_f))) {
+ if (TT.state == DHCP6REQUEST || !(toys.optflags & FLAG_f)) {
dbg("Making it a daemon\n");
if (daemon(0,0)) perror_exit("daemonize");
toys.optflags |= FLAG_f;
- if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
+ if (toys.optflags & FLAG_p) write_pid(TT.pidfile);
}
dbg("Making it a foreground.\n");
continue;