/* Parse string to IP in network order */ int FAST_FUNC udhcp_str2nip(const char *str, void *arg) { len_and_sockaddr *lsa; lsa = host_and_af2sockaddr(str, 0, AF_INET); if (!lsa) return 0; /* arg maybe unaligned */ move_to_unaligned32((uint32_t*)arg, lsa->u.sin.sin_addr.s_addr); free(lsa); return 1; }
int FAST_FUNC addattr32(struct nlmsghdr *n, int maxlen, int type, uint32_t data) { int len = RTA_LENGTH(4); struct rtattr *rta; if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) return -1; rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); rta->rta_type = type; rta->rta_len = len; move_to_unaligned32(RTA_DATA(rta), data); n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; return 0; }
int FAST_FUNC rta_addattr32(struct rtattr *rta, int maxlen, int type, uint32_t data) { int len = RTA_LENGTH(4); struct rtattr *subrta; if (RTA_ALIGN(rta->rta_len) + len > maxlen) { return -1; } subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len)); subrta->rta_type = type; subrta->rta_len = len; move_to_unaligned32(RTA_DATA(subrta), data); rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; return 0; }
/* add a one to four byte option to a packet */ int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) { const struct dhcp_option *dh; for (dh = dhcp_options; dh->code; dh++) { if (dh->code == code) { uint8_t option[6], len; option[OPT_CODE] = code; len = dhcp_option_lengths[dh->flags & TYPE_MASK]; option[OPT_LEN] = len; if (BB_BIG_ENDIAN) data <<= 8 * (4 - len); /* Assignment is unaligned! */ move_to_unaligned32(&option[OPT_DATA], data); return add_option_string(optionptr, option); } } bb_error_msg("cannot add option 0x%02x", code); return 0; }
/* Add an one to four byte option to a packet */ void FAST_FUNC udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) { const struct dhcp_optflag *dh; for (dh = dhcp_optflags; dh->code; dh++) { if (dh->code == code) { uint8_t option[6], len; option[OPT_CODE] = code; len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK]; option[OPT_LEN] = len; if (BB_BIG_ENDIAN) data <<= 8 * (4 - len); /* Assignment is unaligned! */ move_to_unaligned32(&option[OPT_DATA], data); udhcp_add_binary_option(packet, option); return; } } bb_error_msg("can't add option 0x%02x", code); }