void message_transmit(uint8_t *input_buffer, size_t input_buffer_size, ip_addr_t *addr, uint16_t port) { struct netconn *conn; struct netbuf *buf; conn = netconn_new(NETCONN_UDP); if (conn == NULL) { // TODO: Do something useful return; } buf = netbuf_new(); if (buf == NULL) { // TODO: Do something useful return; } netbuf_ref(buf, input_buffer, input_buffer_size); netconn_sendto(conn, buf, addr, port); netbuf_delete(buf); netconn_delete(conn); }
nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size) { struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle; ip_addr_t ip_addr; nsapi_addr_t addr = address.get_addr(); if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return err_remap(err); } err = netconn_sendto(s->conn, buf, &ip_addr, address.get_port()); netbuf_delete(buf); if (err != ERR_OK) { return err_remap(err); } return size; }
static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg) { if(dhcpmsg->htype != DHCP_HTYPE_ETH) return; if(dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) return; dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr); if(!freelease) { printf("DHCP Server: All leases taken.\r\n"); return; /* Nothing available, so do nothing */ } /* Reuse the DISCOVER buffer for the OFFER response */ dhcpmsg->op = DHCP_BOOTREPLY; bzero(dhcpmsg->options, DHCP_OPTIONS_LEN); ip_addr_copy(dhcpmsg->yiaddr, state->first_client_addr); ip4_addr4(&(dhcpmsg->yiaddr)) += (freelease - state->leases); uint8_t *opt = (uint8_t *)&dhcpmsg->options; opt = add_dhcp_option_byte(opt, DHCP_OPTION_MESSAGE_TYPE, DHCP_OFFER); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_SERVER_ID, &state->server_if->ip_addr, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_SUBNET_MASK, &state->server_if->netmask, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_END, NULL, 0); struct netbuf *netbuf = netbuf_new(); netbuf_alloc(netbuf, sizeof(struct dhcp_msg)); netbuf_take(netbuf, dhcpmsg, sizeof(struct dhcp_msg)); netconn_sendto(state->nc, netbuf, IP_ADDR_BROADCAST, 68); netbuf_delete(netbuf); }
static void send_dhcp_nak(struct dhcp_msg *dhcpmsg) { /* Reuse 'dhcpmsg' for the NAK */ dhcpmsg->op = DHCP_BOOTREPLY; bzero(dhcpmsg->options, DHCP_OPTIONS_LEN); uint8_t *opt = (uint8_t *)&dhcpmsg->options; opt = add_dhcp_option_byte(opt, DHCP_OPTION_MESSAGE_TYPE, DHCP_NAK); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_SERVER_ID, &state->server_if->ip_addr, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_END, NULL, 0); struct netbuf *netbuf = netbuf_new(); netbuf_alloc(netbuf, sizeof(struct dhcp_msg)); netbuf_take(netbuf, dhcpmsg, sizeof(struct dhcp_msg)); netconn_sendto(state->nc, netbuf, IP_ADDR_BROADCAST, 68); netbuf_delete(netbuf); }
static int lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size) { struct lwip_socket *s = (struct lwip_socket *)handle; if (addr.version != NSAPI_IPv4) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return lwip_err_remap(err);; } err = netconn_sendto(s->conn, buf, (ip_addr_t *)addr.bytes, port); netbuf_delete(buf); if (err != ERR_OK) { return lwip_err_remap(err); } return size; }
static nsapi_size_or_error_t mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size) { struct lwip_socket *s = (struct lwip_socket *)handle; ip_addr_t ip_addr; if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) { return NSAPI_ERROR_PARAMETER; } struct netbuf *buf = netbuf_new(); err_t err = netbuf_ref(buf, data, (u16_t)size); if (err != ERR_OK) { netbuf_free(buf); return mbed_lwip_err_remap(err); } err = netconn_sendto(s->conn, buf, &ip_addr, port); netbuf_delete(buf); if (err != ERR_OK) { return mbed_lwip_err_remap(err); } return size; }
static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) { static char ipbuf[16]; if(dhcpmsg->htype != DHCP_HTYPE_ETH) return; if(dhcpmsg->hlen > NETIF_MAX_HWADDR_LEN) return; ip_addr_t requested_ip; uint8_t *requested_ip_opt = find_dhcp_option(dhcpmsg, DHCP_OPTION_REQUESTED_IP, 4, NULL); if(requested_ip_opt) { memcpy(&requested_ip.addr, requested_ip_opt, 4); } else if(ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) { ip_addr_copy(requested_ip, dhcpmsg->ciaddr); } else { printf("DHCP Server Error: No requested IP\r\n"); send_dhcp_nak(dhcpmsg); return; } /* Test the first 4 octets match */ if(ip4_addr1(&requested_ip) != ip4_addr1(&state->first_client_addr) || ip4_addr2(&requested_ip) != ip4_addr2(&state->first_client_addr) || ip4_addr3(&requested_ip) != ip4_addr3(&state->first_client_addr)) { sprintf_ipaddr(&requested_ip, ipbuf); printf("DHCP Server Error: %s not an allowed IP\r\n", ipbuf); send_dhcp_nak(dhcpmsg); return; } /* Test the last octet is in the MAXCLIENTS range */ int16_t octet_offs = ip4_addr4(&requested_ip) - ip4_addr4(&state->first_client_addr); if(octet_offs < 0 || octet_offs >= state->max_leases) { printf("DHCP Server Error: Address out of range\r\n"); send_dhcp_nak(dhcpmsg); return; } dhcp_lease_t *requested_lease = state->leases + octet_offs; if(requested_lease->expires != 0 && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen)) { printf("DHCP Server Error: Lease for address already taken\r\n"); send_dhcp_nak(dhcpmsg); return; } memcpy(requested_lease->hwaddr, dhcpmsg->chaddr, dhcpmsg->hlen); sprintf_ipaddr(&requested_ip, ipbuf); printf("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", ipbuf, requested_lease->hwaddr[0], requested_lease->hwaddr[1], requested_lease->hwaddr[2], requested_lease->hwaddr[3], requested_lease->hwaddr[4], requested_lease->hwaddr[5]); requested_lease->expires = DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ; /* Reuse the REQUEST message as the ACK message */ dhcpmsg->op = DHCP_BOOTREPLY; bzero(dhcpmsg->options, DHCP_OPTIONS_LEN); ip_addr_copy(dhcpmsg->yiaddr, requested_ip); uint8_t *opt = (uint8_t *)&dhcpmsg->options; opt = add_dhcp_option_byte(opt, DHCP_OPTION_MESSAGE_TYPE, DHCP_ACK); uint32_t expiry = htonl(DHCPSERVER_LEASE_TIME); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_LEASE_TIME, &expiry, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_SERVER_ID, &state->server_if->ip_addr, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_SUBNET_MASK, &state->server_if->netmask, 4); opt = add_dhcp_option_bytes(opt, DHCP_OPTION_END, NULL, 0); struct netbuf *netbuf = netbuf_new(); netbuf_alloc(netbuf, sizeof(struct dhcp_msg)); netbuf_take(netbuf, dhcpmsg, sizeof(struct dhcp_msg)); netconn_sendto(state->nc, netbuf, IP_ADDR_BROADCAST, 68); netbuf_delete(netbuf); }