Dear All,
I have a struct as below and a pointer to the struct as such const struct pfring_pkthdr *h. I want to get all the value to be made one string delimited by comma to be inserted into db. So how to do it in C? E.g I want to combin is caplen,le,pkt_hash,parsed_header_len,eth_type,vlan_id,ip_version,ip_src,ip_dist.

struct pfring_pkthdr {
  struct timeval ts;    
  u_int32_t caplen;     
  u_int32_t len;        
  struct pfring_extended_pkthdr extended_hdr; 
};

struct pfring_extended_pkthdr {
  u_int64_t timestamp_ns; 
  u_int8_t rx_direction;    
  int if_index;                                         
  u_int32_t pkt_hash;     
  u_int16_t parsed_header_len;  
  struct pkt_parsing_info parsed_pkt; 
};

struct pkt_parsing_info {
  u_int8_t dmac[ETH_ALEN], smac[ETH_ALEN];  
  u_int16_t eth_type;   
  u_int16_t vlan_id;    
  u_int8_t  ip_version;
  u_int8_t  l3_proto, ip_tos; 
  ip_addr   ip_src, ip_dst; 
  u_int16_t l4_src_port, l4_dst_port;
  struct {
    u_int8_t flags;   
    u_int32_t seq_num, ack_num; 
  } tcp;
  u_int16_t last_matched_plugin_id; 
  u_int16_t last_matched_rule_id; 
  struct pkt_offset offset; 
  packet_user_detail pkt_detail;
};

On way would be to use sprintf to format your insert/update string.
The parameters will be the elements of the struct.

Dear Thines,
I saw an example like this. I must have the char b declared is it? So in my case I should put something big right?

#include <stdio.h>
 
   void main()
   {
      char b[100];
      int i = 42;
      float f = 1.1234f;
      sprintf( b, "Formatted data:  %d / %f", i, f );
      puts( b );
   }

I must have the char b declared is it?

Yes, there must be an array (or simulated array) with enough space allocated to pass to sprintf().

So in my case I should put something big right?

Let's do the math. Following are the fields you said you want to serialize and their maximum character lengths:

10 -- u_int32_t caplen;
10 -- u_int32_t len;
10 -- u_int32_t pkt_hash;
5  -- u_int16_t parsed_header_len;
5  -- u_int16_t eth_type;
5  -- u_int16_t vlan_id;
3  -- u_int8_t  ip_version;
15 -- ip_addr   ip_src;
15 -- ip_addr   ip_dst;

ip_addr is an unknown, but I'll assume it's another structure with four (or six) fields corresponding to each part of an IP address. I'll further assume assume you want to format an IPv4 address, for simplicity. Given these numbers, plus the space for comma separation and a null character at the end, the size of your array needs to be 87 or more.

Dear Nicole,
Just for trial I did something like this.It works so just would like to confirm am I doing the right steps?

char combine[100];
sprintf(combine,"%u,%d",h->extended_hdr.pkt_hash,h->extended_pkt.ipv4_tos);
char queryString[150];
sprintf(queryString,"Insert int tblTest set value='%s'",combine)
if(mysql_real_query(conn,queryString,strlen(queryString)))
{
  fprintf(stderr,"%s\n",mysql_error(conn));

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.