进入联盟内的结构

问题描述 投票:0回答:1

我正在做一个外部库,它的C语言代码如下。

struct ndpi_flow_struct {

....

  union {
    struct ndpi_flow_tcp_struct tcp;
    struct ndpi_flow_udp_struct udp;
  } l4;

  struct ndpi_id_struct *server_id;
  /* HTTP host or DNS query */
  u_char host_server_name[256];

  struct {
    ndpi_http_method method;
    char *url, *content_type;
    u_int8_t  num_request_headers, num_response_headers;
    u_int8_t  request_version; /* 0=1.0 and 1=1.1. Create an enum for this? */
    u_char response_status_code[5]; /* 200, 404, etc. */
  } http;

  union {
    /* the only fields useful for nDPI and ntopng */
    struct {
      u_int8_t num_queries, num_answers, reply_code;
      u_int16_t query_type, query_class, rsp_type;
    } dns;

    struct {
      u_int8_t request_code;
      u_int8_t version;
    } ntp;

    struct {
      char client_certificate[48], server_certificate[48];
    } ssl;

    struct {
      char client_signature[48], server_signature[48];
    } ssh;

    struct {
      char answer[96];
    } mdns;

    struct {
      char version[96];
    } ubntac2;

    struct {
      /* Via HTTP User-Agent */
      u_char detected_os[32];
      /* Via HTTP X-Forwarded-For */
      u_char nat_ip[24];
    } http;

    struct {
      /* Bittorrent hash */
      u_char hash[20];
    } bittorrent;

    struct {
      char fingerprint[48];
      char class_ident[48];
    } dhcp;
  } protos;

而当我这样做的时候,

if(flow->ndpi_flow!=NULL && flow->ndpi_flow->detected_os!=NULL){
        snprintf(text[c], textSize, "%s", flow->ndpi_flow->detected_os);

编译器(gcc 4.9.2)说:

错误: 'struct ndpi_flow_struct' 没有名为 'detected_os' 的成员。

我在网上看到C11应该用这种方式读取union里面的struct字段的值.先谢谢了!

c struct union
1个回答
1
投票

detected_os 是以下结构的成员 http的成员,它是 protos所以你需要 flow->ndpi_flow->protos.http.detected_os.

© www.soinside.com 2019 - 2024. All rights reserved.