struct net_buf *l2_buf_get_reserve(uint16_t reserve_head) #endif { struct net_buf *buf; buf = net_buf_get(&free_l2_bufs, reserve_head); if (!buf) { #ifdef DEBUG_L2_BUFS NET_ERR("Failed to get free L2 buffer (%s():%d)\n", caller, line); #else NET_ERR("Failed to get free L2 buffer\n"); #endif return NULL; } dec_free_l2_bufs(buf); NET_BUF_CHECK_IF_NOT_IN_USE(buf); #ifdef DEBUG_L2_BUFS NET_DBG("[%d] buf %p reserve %u ref %d (%s():%d)\n", get_free_l2_bufs(), buf, reserve_head, buf->ref, caller, line); #else NET_DBG("buf %p reserve %u ref %d\n", buf, reserve_head, buf->ref); #endif packetbuf_clear(buf); return buf; }
static int resolve_name(struct http_client_ctx *ctx, const char *server, enum dns_query_type type) { struct waiter dns_waiter; int ret; dns_waiter.ctx = ctx; k_sem_init(&dns_waiter.wait, 0, 1); ret = dns_get_addr_info(server, type, &ctx->dns_id, dns_cb, &dns_waiter, DNS_WAIT); if (ret < 0) { NET_ERR("Cannot resolve %s (%d)", server, ret); ctx->dns_id = 0; return ret; } /* Wait a little longer for the DNS to finish so that * the DNS will timeout before the semaphore. */ if (k_sem_take(&dns_waiter.wait, DNS_WAIT_SEM)) { NET_ERR("Timeout while resolving %s", server); ctx->dns_id = 0; return -ETIMEDOUT; } ctx->dns_id = 0; if (ctx->tcp.remote.family == AF_UNSPEC) { return -EINVAL; } return 0; }
int start_tcp(void) { int ret = 0; if (IS_ENABLED(CONFIG_NET_IPV6)) { ret = connect_tcp(&tcp6, CONFIG_NET_APP_PEER_IPV6_ADDR, &conf.ipv6, tls_result_ipv6, sizeof(tls_result_ipv6), net_app_tls_stack_ipv6, K_THREAD_STACK_SIZEOF( net_app_tls_stack_ipv6)); if (ret < 0) { NET_ERR("Cannot init IPv6 TCP client (%d)", ret); } } if (IS_ENABLED(CONFIG_NET_IPV4)) { ret = connect_tcp(&tcp4, CONFIG_NET_APP_PEER_IPV4_ADDR, &conf.ipv4, tls_result_ipv4, sizeof(tls_result_ipv4), net_app_tls_stack_ipv4, K_THREAD_STACK_SIZEOF( net_app_tls_stack_ipv4)); if (ret < 0) { NET_ERR("Cannot init IPv6 TCP client (%d)", ret); } } return ret; }
static int bt_connect(u32_t mgmt_request, struct net_if *iface, void *data, size_t len) { struct bt_context *ctxt = net_if_get_device(iface)->driver_data; bt_addr_le_t *addr = data; if (len != sizeof(*addr)) { NET_ERR("Invalid address"); return -EINVAL; } if (ctxt->ipsp_chan.chan.conn) { NET_ERR("No channels available"); return -ENOMEM; } if (default_conn) { return bt_l2cap_chan_connect(default_conn, &ctxt->ipsp_chan.chan, L2CAP_IPSP_PSM); } default_conn = bt_conn_create_le(addr, BT_LE_CONN_PARAM_DEFAULT); return 0; }
static int do_sync_http_req(struct http_client_ctx *ctx, enum http_method method, const char *url, const char *content_type, const char *payload) { struct http_client_request req = {}; int ret; req.method = method; req.url = url; req.protocol = " " HTTP_PROTOCOL HTTP_CRLF; ret = http_client_send_req(ctx, &req, NULL, result, sizeof(result), NULL, APP_REQ_TIMEOUT); if (ret < 0) { NET_ERR("Cannot send %s request (%d)", http_method_str(method), ret); goto out; } if (ctx->rsp.data_len > sizeof(result)) { NET_ERR("Result buffer overflow by %zd bytes", ctx->rsp.data_len - sizeof(result)); ret = -E2BIG; } else { NET_INFO("HTTP server response status: %s", ctx->rsp.http_status); if (ctx->parser.http_errno) { if (method == HTTP_OPTIONS) { /* Ignore error if OPTIONS is not found */ goto out; } NET_INFO("HTTP parser status: %s", http_errno_description(ctx->parser.http_errno)); ret = -EINVAL; goto out; } if (method != HTTP_HEAD) { if (ctx->rsp.body_found) { NET_INFO("HTTP body: %zd bytes, " "expected: %zd bytes", ctx->rsp.processed, ctx->rsp.content_length); } else { NET_ERR("Error detected during HTTP msg " "processing"); } } } out: return ret; }
static struct net_buf *ip_buf_get_reserve(enum ip_buf_type type, uint16_t reserve_head) #endif { struct net_buf *buf = NULL; /* Note that we do not reserve any space in front of the * buffer so buf->data points to first byte of the IP header. * This is done like this so that IP stack works the same * way as BT and 802.15.4 stacks. * * The reserve_head variable in the function will tell * the size of the IP + other headers if there are any. * That variable is only used to calculate the pointer * where the application data starts. */ switch (type) { case IP_BUF_RX: buf = net_buf_get(&free_rx_bufs, 0); dec_free_rx_bufs(buf); break; case IP_BUF_TX: buf = net_buf_get(&free_tx_bufs, 0); dec_free_tx_bufs(buf); break; } if (!buf) { #ifdef DEBUG_IP_BUFS NET_ERR("Failed to get free %s buffer (%s():%d)\n", type2str(type), caller, line); #else NET_ERR("Failed to get free %s buffer\n", type2str(type)); #endif return NULL; } ip_buf_type(buf) = type; ip_buf_appdata(buf) = buf->data + reserve_head; ip_buf_appdatalen(buf) = 0; ip_buf_reserve(buf) = reserve_head; net_buf_add(buf, reserve_head); NET_BUF_CHECK_IF_NOT_IN_USE(buf); #ifdef DEBUG_IP_BUFS NET_DBG("%s [%d] buf %p reserve %u ref %d (%s():%d)\n", type2str(type), get_frees(type), buf, reserve_head, buf->ref, caller, line); #else NET_DBG("%s buf %p reserve %u ref %d\n", type2str(type), buf, reserve_head, buf->ref); #endif return buf; }
static int connect_tcp(struct net_app_ctx *ctx, const char *peer, void *user_data, u8_t *result_buf, size_t result_buf_len, u8_t *stack, size_t stack_size) { struct data *data = user_data; int ret; ret = net_app_init_tcp_client(ctx, NULL, NULL, peer, PEER_PORT, WAIT_TIME, user_data); if (ret < 0) { NET_ERR("Cannot init %s TCP client (%d)", data->proto, ret); goto fail; } #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) net_app_set_net_pkt_pool(ctx, tx_tcp_slab, data_tcp_pool); #endif ret = net_app_set_cb(ctx, tcp_connected, tcp_received, NULL, NULL); if (ret < 0) { NET_ERR("Cannot set callbacks (%d)", ret); goto fail; } #if defined(CONFIG_NET_APP_TLS) ret = net_app_client_tls(ctx, result_buf, result_buf_len, INSTANCE_INFO, strlen(INSTANCE_INFO), setup_cert, HOSTNAME, NULL, &ssl_pool, stack, stack_size); if (ret < 0) { NET_ERR("Cannot init TLS"); goto fail; } #endif ret = net_app_connect(ctx, CONNECT_TIME); if (ret < 0) { NET_ERR("Cannot connect TCP (%d)", ret); goto fail; } fail: return ret; }
static void send_tcp_data(struct net_app_ctx *ctx, struct data *data) { struct net_pkt *pkt; size_t len; int ret; do { data->expecting_tcp = sys_rand32_get() % ipsum_len; } while (data->expecting_tcp == 0); data->received_tcp = 0; pkt = prepare_send_pkt(ctx, data->proto, data->expecting_tcp); if (!pkt) { return; } len = net_pkt_get_len(pkt); NET_ASSERT_INFO(data->expecting_tcp == len, "%s data to send %d bytes, real len %zu", data->proto, data->expecting_tcp, len); ret = net_app_send_pkt(ctx, pkt, NULL, 0, K_FOREVER, UINT_TO_POINTER(len)); if (ret < 0) { NET_ERR("Cannot send %s data to peer (%d)", data->proto, ret); net_pkt_unref(pkt); } }
void main(void) { int ret; ret = http_client_init(&http_ctx, SERVER_ADDR, SERVER_PORT); if (ret < 0) { NET_ERR("HTTP init failed (%d)", ret); panic(NULL); } http_client_set_net_pkt_pool(&http_ctx, tx_slab, data_pool); ret = do_sync_reqs(&http_ctx, MAX_ITERATIONS); if (ret < 0) { goto out; } ret = do_async_reqs(&http_ctx, MAX_ITERATIONS); if (ret < 0) { goto out; } out: http_client_release(&http_ctx); NET_INFO("Done!"); }
void ieee802154_init(struct net_if *iface) { struct ieee802154_context *ctx = net_if_l2_data(iface); const struct ieee802154_radio_api *radio = iface->dev->driver_api; const u8_t *mac = iface->link_addr.addr; u8_t long_addr[8]; NET_DBG("Initializing IEEE 802.15.4 stack on iface %p", iface); ieee802154_mgmt_init(iface); #ifdef CONFIG_NET_L2_IEEE802154_SECURITY if (ieee802154_security_init(&ctx->sec_ctx)) { NET_ERR("Initializing link-layer security failed"); } #endif sys_memcpy_swap(long_addr, mac, 8); radio->set_ieee_addr(iface->dev, long_addr); memcpy(ctx->ext_addr, long_addr, 8); if (!radio->set_txpower(iface->dev, CONFIG_NET_L2_IEEE802154_RADIO_DFLT_TX_POWER)) { ctx->tx_power = CONFIG_NET_L2_IEEE802154_RADIO_DFLT_TX_POWER; } radio->start(iface->dev); }
static bool ad_parse(struct net_buf_simple *ad, bool (*func)(u8_t type, const u8_t *data, u8_t data_len, void *user_data), void *user_data) { while (ad->len > 1) { u8_t len = net_buf_simple_pull_u8(ad); u8_t type; /* Check for early termination */ if (len == 0) { return false; } if (len > ad->len || ad->len < 1) { NET_ERR("AD malformed\n"); return false; } type = net_buf_simple_pull_u8(ad); if (func(type, ad->data, len - 1, user_data)) { return true; } net_buf_simple_pull(ad, len - 1); } return false; }
int net_set_mac(uint8_t *mac, uint8_t len) { if ((len > UIP_LLADDR_LEN) || (len != 6 && len != 8)) { NET_ERR("Wrong ll addr len, len %d, max %d\n", len, UIP_LLADDR_LEN); return -EINVAL; } linkaddr_set_node_addr((linkaddr_t *)mac); #ifdef CONFIG_NETWORKING_WITH_IPV6 { uip_ds6_addr_t *lladdr; uip_ds6_set_lladdr((uip_lladdr_t *)mac); lladdr = uip_ds6_get_link_local(-1); NET_DBG("Tentative link-local IPv6 address "); PRINT6ADDR(&lladdr->ipaddr); PRINTF("\n"); lladdr->state = ADDR_AUTOCONF; } #endif return 0; }
static void bt_scan_off(void) { int err; err = bt_le_scan_stop(); if (err) { NET_ERR("Stopping scanning failed (err %d)\n", err); } }
static void bt_passive_scan(void) { int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err) { NET_ERR("Bluetooth set passive scan failed (err %d)\n", err); } }
static void setup_tcp_accept(struct net_context *tcp_recv4, struct net_context *tcp_recv6) { int ret; #if defined(CONFIG_NET_IPV6) ret = net_context_accept(tcp_recv6, tcp_accepted, 0, NULL); if (ret < 0) { NET_ERR("Cannot receive IPv6 TCP packets (%d)", ret); } #endif /* CONFIG_NET_IPV6 */ #if defined(CONFIG_NET_IPV4) ret = net_context_accept(tcp_recv4, tcp_accepted, 0, NULL); if (ret < 0) { NET_ERR("Cannot receive IPv4 TCP packets (%d)", ret); } #endif /* CONFIG_NET_IPV4 */ }
static void setup_udp_recv(struct net_context *udp_recv4, struct net_context *udp_recv6) { int ret; #if defined(CONFIG_NET_IPV6) ret = net_context_recv(udp_recv6, udp_received, 0, NULL); if (ret < 0) { NET_ERR("Cannot receive IPv6 UDP packets"); } #endif /* CONFIG_NET_IPV6 */ #if defined(CONFIG_NET_IPV4) ret = net_context_recv(udp_recv4, udp_received, 0, NULL); if (ret < 0) { NET_ERR("Cannot receive IPv4 UDP packets"); } #endif /* CONFIG_NET_IPV4 */ }
void panic(const char *msg) { if (msg) { NET_ERR("%s", msg); } for (;;) { k_sleep(K_FOREVER); } }
static inline void init_app(void) { NET_INFO("Run echo server"); k_sem_init(&quit_lock, 0, UINT_MAX); #if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_APP_MY_IPV6_ADDR) if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &in6addr_my) < 0) { NET_ERR("Invalid IPv6 address %s", CONFIG_NET_APP_MY_IPV6_ADDR); } #endif do { struct net_if_addr *ifaddr; ifaddr = net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0); } while (0); #endif #if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_DHCPV4) net_dhcpv4_start(net_if_get_default()); #else #if defined(CONFIG_NET_APP_MY_IPV4_ADDR) if (net_addr_pton(AF_INET, CONFIG_NET_APP_MY_IPV4_ADDR, &in4addr_my) < 0) { NET_ERR("Invalid IPv4 address %s", CONFIG_NET_APP_MY_IPV4_ADDR); } net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); #endif #endif /* CONFIG_NET_DHCPV4 */ #endif /* CONFIG_NET_IPV4 */ }
static int ipsp_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan) { NET_DBG("Incoming conn %p", conn); if (bt_context_data.ipsp_chan.chan.conn) { NET_ERR("No channels available"); return -ENOMEM; } *chan = &bt_context_data.ipsp_chan.chan; return 0; }
static void setup_ipv6(struct net_if *iface) { char hr_addr[NET_IPV6_ADDR_LEN]; struct in6_addr addr; if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV6_ADDR); return; } net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv6 address: %s", net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN)); if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) { NET_ERR("Invalid address: %s", MCAST_IP6ADDR); return; } net_if_ipv6_maddr_add(iface, &addr); }
static int do_async_http_req(struct http_client_ctx *ctx, enum http_method method, const char *url, const char *content_type, const char *payload) { struct http_client_request req = {}; struct waiter waiter; int ret; req.method = method; req.url = url; req.protocol = " " HTTP_PROTOCOL HTTP_CRLF; k_sem_init(&waiter.wait, 0, 1); waiter.total_len = 0; ret = http_client_send_req(ctx, &req, response, result, sizeof(result), &waiter, APP_REQ_TIMEOUT); if (ret < 0 && ret != -EINPROGRESS) { NET_ERR("Cannot send %s request (%d)", http_method_str(method), ret); goto out; } if (k_sem_take(&waiter.wait, WAIT_TIME)) { NET_ERR("Timeout while waiting HTTP response"); http_client_release(ctx); ret = -ETIMEDOUT; goto out; } ret = 0; out: return ret; }
static void setup_device(void) { char hr_addr[NET_IPV6_ADDR_LEN]; struct net_if *iface; struct in6_addr addr; struct device *dev; dev = device_get_binding(CONFIG_IEEE802154_UPIPE_DRV_NAME); if (!dev) { NET_INFO("Cannot get UPIPE device\n"); return; } iface = net_if_lookup_by_dev(dev); if (!iface) { NET_INFO("Cannot get UPIPE network interface\n"); return; } if (net_addr_pton(AF_INET6, CONFIG_NET_APP_MY_IPV6_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV6_ADDR); return; } net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv6 address: %s", net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN)); if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) { NET_ERR("Invalid address: %s", MCAST_IP6ADDR); return; } NET_INFO("802.15.4 device up and running\n"); }
static void setup_ipv4(struct net_if *iface) { char hr_addr[NET_IPV4_ADDR_LEN]; struct in_addr addr; if (net_addr_pton(AF_INET, CONFIG_NET_APP_MY_IPV4_ADDR, &addr)) { NET_ERR("Invalid address: %s", CONFIG_NET_APP_MY_IPV4_ADDR); return; } net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); NET_INFO("IPv4 address: %s", net_addr_ntop(AF_INET, &addr, hr_addr, NET_IPV4_ADDR_LEN)); }
void main(void) { init_app(); #if defined(CONFIG_NET_L2_BLUETOOTH) if (bt_enable(NULL)) { NET_ERR("Bluetooth init failed"); return; } ipss_init(); ipss_advertise(); #endif #if defined(CONFIG_NET_L2_IEEE802154) if (ieee802154_sample_setup()) { NET_ERR("IEEE 802.15.4 setup failed"); return; } #endif k_thread_create(&thread_data, thread_stack, STACKSIZE, (k_thread_entry_t)receive, NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); }
static void tcp_accepted(struct net_context *context, struct sockaddr *addr, socklen_t addrlen, int error, void *user_data) { int ret; NET_DBG("Accept called, context %p error %d", context, error); ret = net_context_recv(context, tcp_received, 0, NULL); if (ret < 0) { NET_ERR("Cannot receive TCP packet (family %d)", net_context_get_family(context)); } }
static int setup_cert(struct net_app_ctx *ctx, void *cert) { #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) mbedtls_ssl_conf_psk(&ctx->tls.mbedtls.conf, client_psk, sizeof(client_psk), (const unsigned char *)client_psk_id, sizeof(client_psk_id) - 1); #endif #if defined(MBEDTLS_X509_CRT_PARSE_C) { mbedtls_x509_crt *ca_cert = cert; int ret; ret = mbedtls_x509_crt_parse_der(ca_cert, ca_certificate, sizeof(ca_certificate)); if (ret != 0) { NET_ERR("mbedtls_x509_crt_parse_der failed " "(-0x%x)", -ret); return ret; } mbedtls_ssl_conf_ca_chain(&ctx->tls.mbedtls.conf, ca_cert, NULL); /* In this example, we skip the certificate checks. In real * life scenarios, one should always verify the certificates. */ mbedtls_ssl_conf_authmode(&ctx->tls.mbedtls.conf, MBEDTLS_SSL_VERIFY_REQUIRED); mbedtls_ssl_conf_cert_profile(&ctx->tls.mbedtls.conf, &mbedtls_x509_crt_profile_default); #define VERIFY_CERTS 0 #if VERIFY_CERTS mbedtls_ssl_conf_authmode(&ctx->tls.mbedtls.conf, MBEDTLS_SSL_VERIFY_OPTIONAL); #else ; #endif /* VERIFY_CERTS */ } #endif /* MBEDTLS_X509_CRT_PARSE_C */ return 0; }
static int bt_disconnect(u32_t mgmt_request, struct net_if *iface, void *data, size_t len) { struct bt_context *ctxt = net_if_get_device(iface)->driver_data; if (!ctxt->ipsp_chan.chan.conn) { NET_ERR("Not connected"); return -ENOTCONN; } /* Release connect reference in case of central/router role */ if (default_conn) { bt_conn_unref(default_conn); default_conn = NULL; } return bt_l2cap_chan_disconnect(&ctxt->ipsp_chan.chan); }
void receive(void) { struct net_context *udp_recv4 = { 0 }; struct net_context *udp_recv6 = { 0 }; struct net_context *tcp_recv4 = { 0 }; struct net_context *tcp_recv6 = { 0 }; if (!get_context(&udp_recv4, &udp_recv6, &tcp_recv4, &tcp_recv6)) { NET_ERR("Cannot get network contexts"); return; } NET_INFO("Starting to wait"); #if defined(CONFIG_NET_TCP) setup_tcp_accept(tcp_recv4, tcp_recv6); #endif #if defined(CONFIG_NET_UDP) setup_udp_recv(udp_recv4, udp_recv6); #endif k_sem_take(&quit_lock, K_FOREVER); NET_INFO("Stopping..."); #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_UDP) net_context_put(udp_recv6); #endif #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_UDP) net_context_put(udp_recv4); #endif #if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_TCP) net_context_put(tcp_recv6); #endif #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_TCP) net_context_put(tcp_recv4); #endif }
bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_pkt *pkt, struct ieee802154_mpdu *mpdu) { struct ieee802154_context *ctx = net_if_l2_data(iface); u8_t level; if (!mpdu->mhr.fs->fc.security_enabled) { return true; } /* Section 7.2.3 (i) talks about "security level policy" conformance * but such policy does not seem to be detailed. So let's assume both * ends should have same security level. */ if (mpdu->mhr.aux_sec->control.security_level != ctx->sec_ctx.level) { return false; } /* ToDo: handle src short address * This will require to look up in nbr cache with short addr * in order to get the extended address related to it */ if (!ieee802154_decrypt_auth(&ctx->sec_ctx, net_pkt_ll(pkt), net_pkt_ll_reserve(pkt), net_pkt_get_len(pkt), net_pkt_ll_src(pkt)->addr, sys_le32_to_cpu( mpdu->mhr.aux_sec->frame_counter))) { NET_ERR("Could not decipher the frame"); return false; } level = ctx->sec_ctx.level; if (level >= IEEE802154_SECURITY_LEVEL_ENC) { level -= 4; } /* We remove tag size from frag's length, it is now useless */ pkt->frags->len -= level_2_tag_size[level]; return true; }
void rtmsg(void) { int s; struct rt_msghdr rtm; if (s < 0) { s = socket(PF_ROUTE, SOCK_RAW, 0); if (s < 0) NET_ERR(1, "socket"); } bzero((char *)&rtm, sizeof(rt_msghdr)); rtm->rtm_flags = flags; rtm->rtm_version = RTM_VERSION; switch (cmd) { case RTM_GET: rtm->rtm_addrs |= RTA_DST; } }