TEST(meta_test, rx_pkt)
{
    std_socket_address_t bind;
    t_std_error rc = std_sock_addr_from_ip_str (e_std_sock_INET4, "127.0.0.1",
                                                SOCK_PORT, &bind);
    int fd;
    rc = std_socket_create (e_std_sock_INET4, e_std_sock_type_DGRAM, 0, &bind, &fd);

    ASSERT_TRUE(STD_ERR_OK == rc);

    constexpr size_t MAXLEN = 256;
    uint8_t buf[MAXLEN];
    struct iovec sock_data = {buf, MAXLEN};
    std_socket_msg_t msg = { NULL, 0, &sock_data, 2, NULL, 0, 0};

    auto n = std_socket_op (std_socket_transit_o_READ, fd, &msg,
                            std_socket_transit_f_NONE, 0, &rc);

    ASSERT_TRUE(STD_ERR_OK == rc);
    if (n < 0) printf ("FAILED\r\n");
    else {
        std::cout << "Received bytes: " << n << std::endl;

        nas_pkt_meta_attr_it_t it;
        bool found=false;

        for (nas_pkt_meta_it_begin (buf, &it); nas_pkt_meta_it_valid (&it);
             nas_pkt_meta_it_next (&it)) {
            std_tlv_tag_t t = nas_pkt_meta_attr_type (it.attr);
            found=true;
            std::cout << "Found Packet Meta attr " << t << std::endl;

            if (t == NAS_PKT_META_RX_PORT) {
                hal_ifindex_t ifidx = nas_pkt_meta_attr_data_uint (it.attr);
                std::cout << "Received pkt on IfIndex " << ifidx << std::endl;
                EXPECT_TRUE (ifidx == 45);
            }
        }
        EXPECT_TRUE (found);
        // Offset to the actual packet data
        uint8_t* pkt = nas_pkt_data_offset (buf);
        uint32_t test = 0xbeeff00d;
        test = ntohl (test);
        uint32_t data = *((uint32_t*)pkt);

        EXPECT_TRUE (test == data);
    }

    rc = std_close (fd);
    ASSERT_TRUE(STD_ERR_OK == rc);
}
示例#2
0
文件: iface.c 项目: pierrix/DLFree
inet_iface_t
inet_iface_get_my_iface(const char** prio_pats, int npat){
  int i;
  int sockfd;
  const char *ip;
  const char *pat;

  struct ifreq *ifrp;
  struct ifconf ifc;
  struct sockaddr_in *addr;
  char buf[sizeof(struct ifreq) * INET_IFACE_MAX_IFS];

  sockfd = std_socket(AF_INET, SOCK_STREAM, 0);

  /* need to set user buffer and size prior to ioctl */
  /* must make sure that enough buffer size is allocated */
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = (char*) buf;

  /* get 'all current L3 interface addresses that are running' */
  /* ifc_buf is written and ifc_len is set */
  if(ioctl(sockfd, SIOCGIFCONF, &ifc) == -1){
    perror("ioctl");
    exit(1);
  }
  std_close(sockfd);

  if(ifc.ifc_len == sizeof(buf)){ /* overflow is not an error */
    fprintf(stderr, "ifc.ifc_buf overflow\n");
    exit(1);
  }

  /* first, respect user specified priorities */
  for(i = 0; i < npat && prio_pats != NULL; i++){
    pat = prio_pats[i];
    /* simply iterate over all iface addresses that were set */
    for(ifrp = ifc.ifc_req; (char*)ifrp < (char*)ifc.ifc_buf + ifc.ifc_len; ifrp ++){
      /* if INET */
      if(ifrp -> ifr_addr.sa_family == AF_INET){
	addr = (struct sockaddr_in*)&(ifrp -> ifr_addr);
	ip = std_inet_ntoa(addr -> sin_addr);
	
	if(strncmp(ip, pat, strlen(pat)) == 0)
	  return inet_iface_create(&addr -> sin_addr);
      }
    }
  }

  /* by default, ignore 10.** and 127.** addresses TODO: ad-hoc */
  for(ifrp = ifc.ifc_req; (char*)ifrp < (char*)ifc.ifc_buf + ifc.ifc_len; ifrp ++){
    /* if INET */
    if(ifrp -> ifr_addr.sa_family == AF_INET){
      addr = (struct sockaddr_in*)&(ifrp -> ifr_addr);
      ip = std_inet_ntoa(addr -> sin_addr);
      
      if(strncmp("10.", ip, 3) && strncmp("127.", ip, 4))
	return inet_iface_create(&addr -> sin_addr);
    }
  }
  
  return NULL;
}