/* Construct a domain name for SRV query from the given name, service and * protocol (service may be NULL in which case protocol isn't used). * Return negative value on error (malformed query), * or addition query flag(s) to use. */ static int build_srv_dn(dnsc_t *dn, const char *name, const char *srv, const char *proto) { unsigned p = 0, l; int isabs; if (srv) { l = add_sname(dn + p, srv); if (!l) return -1; p += l; l = add_sname(dn + p, proto); if (!l) return -1; p += l; } l = dns_ptodn(name, 0, dn + p, DNS_MAXDN - p, &isabs); if (!l) return -1; return isabs ? DNS_NOSRCH : 0; }
/* Prepare initial DHCPv4 message and add options as per message type */ static struct net_buf *prepare_message(struct net_if *iface, uint8_t type) { struct net_buf *buf; struct net_buf *frag; struct dhcp_msg *msg; buf = net_nbuf_get_reserve_tx(0); if (!buf) { return NULL; } frag = net_nbuf_get_reserve_data(net_if_get_ll_reserve(iface, NULL)); if (!frag) { goto fail; } net_nbuf_set_ll_reserve(buf, net_buf_headroom(frag)); net_nbuf_set_iface(buf, iface); net_nbuf_set_family(buf, AF_INET); net_nbuf_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); net_buf_frag_add(buf, frag); /* Leave room for IPv4 + UDP headers */ net_buf_add(buf->frags, NET_IPV4UDPH_LEN); if (net_buf_tailroom(frag) < sizeof(struct dhcp_msg)) { goto fail; } msg = (struct dhcp_msg *)(frag->data + NET_IPV4UDPH_LEN); memset(msg, 0, sizeof(struct dhcp_msg)); msg->op = DHCPV4_MSG_BOOT_REQUEST; msg->htype = HARDWARE_ETHERNET_TYPE; msg->hlen = HARDWARE_ETHERNET_LEN; msg->xid = htonl(iface->dhcpv4.xid); msg->flags = htons(DHCPV4_MSG_BROADCAST); if (iface->dhcpv4.state == NET_DHCPV4_INIT) { memset(msg->ciaddr, 0, sizeof(msg->ciaddr)); } else { memcpy(msg->ciaddr, iface->dhcpv4.requested_ip.s4_addr, 4); } memcpy(msg->chaddr, iface->link_addr.addr, iface->link_addr.len); net_buf_add(frag, sizeof(struct dhcp_msg)); if (!add_sname(buf) || !add_file(buf) || !add_cookie(buf) || !add_msg_type(buf, type)) { goto fail; } return buf; fail: net_nbuf_unref(buf); return NULL; }