static int _netif_set_addr(kernel_pid_t dev, netopt_t opt, char *addr_str) { uint8_t addr[MAX_ADDR_LEN]; size_t addr_len = gnrc_netif_addr_from_str(addr, sizeof(addr), addr_str); if (addr_len == 0) { puts("error: unable to parse address.\n" "Must be of format [0-9a-fA-F]{2}(:[0-9a-fA-F]{2})*\n" "(hex pairs delimited by colons)"); return 1; } if (gnrc_netapi_set(dev, opt, 0, addr, addr_len) < 0) { printf("error: unable to set "); _print_netopt(opt); puts(""); return 1; } printf("success: set "); _print_netopt(opt); printf(" on interface %" PRIkernel_pid " to %s\n", dev, addr_str); return 0; }
static void test_ng_netif_addr_from_str__ill_extra_delimitter(void) { static const char str[] = "05::cd"; uint8_t out[sizeof(str)]; TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str)); }
static void test_ng_netif_addr_from_str__ill_formated2(void) { static const char str[] = TEST_STRING8; uint8_t out[sizeof(str)]; TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str)); }
static void test_ng_netif_addr_from_str__out_too_short(void) { static const char str[] = "05:cd"; uint8_t out[1]; TEST_ASSERT_EQUAL_INT(0, gnrc_netif_addr_from_str(out, sizeof(out), str)); }
static int _netif_addrm_l2filter(kernel_pid_t dev, char *val, bool add) { uint8_t addr[MAX_ADDR_LEN]; size_t addr_len = gnrc_netif_addr_from_str(addr, sizeof(addr), val); if ((addr_len == 0) || (addr_len > L2FILTER_ADDR_MAXLEN)) { puts("error: given address is invalid"); return 1; } if (add) { if (gnrc_netapi_set(dev, NETOPT_L2FILTER, 0, addr, addr_len) < 0) { puts("unable to add link layer address to filter"); return 1; } puts("successfully added address to filter"); } else { if (gnrc_netapi_set(dev, NETOPT_L2FILTER_RM, 0, addr, addr_len) < 0) { puts("unable to remove link layer address from filter"); return 1; } puts("successfully removed address to filter"); } return 0; }
static struct ccnl_face_s *_intern_face_get(char *addr_str) { /* initialize address with 0xFF for broadcast */ size_t addr_len = (strlen(addr_str) + 1) / 3; uint8_t relay_addr[addr_len]; memset(relay_addr, UINT8_MAX, addr_len); addr_len = gnrc_netif_addr_from_str(relay_addr, sizeof(relay_addr), addr_str); if (addr_len == 0) { printf("Error: %s is not a valid link layer address\n", addr_str); return NULL; } sockunion sun; sun.sa.sa_family = AF_PACKET; memcpy(&(sun.linklayer.sll_addr), relay_addr, addr_len); sun.linklayer.sll_halen = addr_len; sun.linklayer.sll_protocol = htons(ETHERTYPE_NDN); int i; uint16_t devType; for(i = 0; i < ccnl_relay.ifcount; i++) { gnrc_netapi_get(ccnl_relay.ifs[i].if_pid, NETOPT_DEVICE_TYPE, 0, &devType, sizeof(devType)); if( (addr_len == 8 && devType == NETDEV2_TYPE_IEEE802154) || (addr_len == 6 && devType == NETDEV2_TYPE_ETHERNET) ) break; } return ccnl_get_face_or_create(&ccnl_relay, ccnl_relay.ifs[i].if_pid, &sun.sa, sizeof(sun.linklayer)); }
static int _ipv6_nc_add(kernel_pid_t iface, char *ipv6_addr_str, char *l2_addr_str) { ipv6_addr_t ipv6_addr; uint8_t l2_addr[MAX_L2_ADDR_LEN]; size_t l2_addr_len; if (ipv6_addr_from_str(&ipv6_addr, ipv6_addr_str) == NULL) { puts("error: unable to parse IPv6 address."); return 1; } if ((l2_addr_len = gnrc_netif_addr_from_str(l2_addr, sizeof(l2_addr), l2_addr_str)) == 0) { puts("error: unable to parse link-layer address."); return 1; } if (gnrc_ipv6_nc_add(iface, &ipv6_addr, l2_addr, l2_addr_len, 0) == NULL) { puts("error: unable to add address to neighbor cache."); return 1; } printf("success: added IPv6 address %s to neighbor cache\n", ipv6_addr_str); return 0; }
/* shell commands */ int _netif_send(int argc, char **argv) { kernel_pid_t dev; uint8_t addr[MAX_ADDR_LEN]; size_t addr_len; gnrc_pktsnip_t *pkt, *hdr; gnrc_netif_hdr_t *nethdr; uint8_t flags = 0x00; if (argc < 4) { printf("usage: %s <if> [<L2 addr>|bcast] <data>\n", argv[0]); return 1; } /* parse interface */ dev = atoi(argv[1]); if (!_is_iface(dev)) { puts("error: invalid interface given"); return 1; } /* parse address */ addr_len = gnrc_netif_addr_from_str(addr, sizeof(addr), argv[2]); if (addr_len == 0) { if (strcmp(argv[2], "bcast") == 0) { flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST; } else { puts("error: invalid address given"); return 1; } } /* put packet together */ pkt = gnrc_pktbuf_add(NULL, argv[3], strlen(argv[3]), GNRC_NETTYPE_UNDEF); if (pkt == NULL) { puts("error: packet buffer full"); return 1; } hdr = gnrc_netif_hdr_build(NULL, 0, addr, addr_len); if (hdr == NULL) { puts("error: packet buffer full"); gnrc_pktbuf_release(pkt); return 1; } LL_PREPEND(pkt, hdr); nethdr = (gnrc_netif_hdr_t *)hdr->data; nethdr->flags = flags; /* and send it */ if (gnrc_netapi_send(dev, pkt) < 1) { puts("error: unable to send"); gnrc_pktbuf_release(pkt); return 1; } return 0; }
static void test_ng_netif_addr_from_str__success(void) { static const char str[] = "05:cd"; uint8_t out[2]; TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str)); TEST_ASSERT_EQUAL_INT(0x05, out[0]); TEST_ASSERT_EQUAL_INT(0xcd, out[1]); }
static void test_ng_netif_addr_from_str__zero_omitted_back(void) { static const char str[] = "05:c"; uint8_t out[2]; TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str)); TEST_ASSERT_EQUAL_INT(0x05, out[0]); TEST_ASSERT_EQUAL_INT(0x0c, out[1]); }
static void test_gnrc_netif_addr_from_str__dash_delimitter(void) { static const char str[] = "05-cd"; uint8_t out[2]; TEST_ASSERT_EQUAL_INT(2, gnrc_netif_addr_from_str(out, sizeof(out), str)); TEST_ASSERT_EQUAL_INT(0x05, out[0]); TEST_ASSERT_EQUAL_INT(0xcd, out[1]); }
static void test_ng_netif_addr_from_str__omitted_delimitter(void) { static const char str[] = "4567:cd"; uint8_t out[3]; TEST_ASSERT_EQUAL_INT(3, gnrc_netif_addr_from_str(out, sizeof(out), str)); TEST_ASSERT_EQUAL_INT(0x45, out[0]); TEST_ASSERT_EQUAL_INT(0x67, out[1]); TEST_ASSERT_EQUAL_INT(0xcd, out[2]); }
/* shell commands */ int _netif_send(int argc, char **argv) { kernel_pid_t dev; uint8_t addr[MAX_ADDR_LEN]; size_t addr_len; gnrc_pktsnip_t *pkt; gnrc_netif_hdr_t *nethdr; uint8_t flags = 0x00; if (argc < 4) { printf("usage: %s <if> [<addr>|bcast] <data>\n", argv[0]); return 1; } /* parse interface */ dev = (kernel_pid_t)atoi(argv[1]); if (!_is_iface(dev)) { puts("error: invalid interface given"); return 1; } /* parse address */ addr_len = gnrc_netif_addr_from_str(addr, sizeof(addr), argv[2]); if (addr_len == 0) { if (strcmp(argv[2], "bcast") == 0) { flags |= GNRC_NETIF_HDR_FLAGS_BROADCAST; } else { puts("error: invalid address given"); return 1; } } /* put packet together */ pkt = gnrc_pktbuf_add(NULL, argv[3], strlen(argv[3]), GNRC_NETTYPE_UNDEF); pkt = gnrc_pktbuf_add(pkt, NULL, sizeof(gnrc_netif_hdr_t) + addr_len, GNRC_NETTYPE_NETIF); nethdr = (gnrc_netif_hdr_t *)pkt->data; gnrc_netif_hdr_init(nethdr, 0, addr_len); gnrc_netif_hdr_set_dst_addr(nethdr, addr, addr_len); nethdr->flags = flags; /* and send it */ gnrc_netapi_send(dev, pkt); return 0; }