2013-05-16 2 views
0

아래 배열을 나타내는 * p가 있는데 어떻게 읽습니까? 내가 그것을 읽을 수있는 가장 빠른 방법과 C에서 문자열 변수에 스트로크가 필요합니다. 어떤 권고하시기 바랍니다?문자열로 배열을 읽는 가장 빠른 방법

char *hexValue=""; 
while(p) { 
    printf("\n\nstr: %s\n", p); 
    asprintf(&hexValue,"%s%02x",hexValue,p); 
    p++; 
} 

다음은 전체 기능 코드입니다. 지원되는 입력 인수 두번 hexValue 지나가는 경우

while(p) { 

들어

void dummyProcesssPacket(const struct pfring_pkthdr *h, 
      const u_char *p, const u_char *user_bytes) { 
    long threadId = (long)user_bytes; 
    printf("\nIN Dummy\n"); 
    numPkts[threadId]++, numBytes[threadId] += h->len+24 /* 8 Preamble + 4 CRC + 12 IFG */; 

#ifdef ENABLE_BPF 
    if (userspace_bpf && bpf_filter(filter.bf_insns, p, h->caplen, h->len) == 0) 
    return; /* rejected */ 

    numPktsFiltered[threadId]++; 
#endif 

    if(touch_payload) { 
    volatile int __attribute__ ((unused)) i; 

    i = p[12] + p[13]; 
    } 

    if(verbose) { 
    printf("\nverbose :"); 
    struct ether_header *ehdr; 
    char buf1[32], buf2[32]; 
    struct ip *ip; 
    int s; 
    uint usec; 
    uint nsec=0; 


    int i=0; 
    int j=0,line=0,packSize=h->caplen; 
    char *hexValue=""; 
    while(j<packSize/16) 
    { 
     printf("%06x: ",line++); 
     for(i=0;i<16;i++) 
     { 
      printf("%02x ",p[j*16+i]); 
      asprintf(&hexValue,"%s%02x",hexValue,p[j*16+i]); 
     } 
     printf(" |"); 
     /*for(i=0;i<16;i++) 
     { 
      if(isprint(p[j*16+i])) 
      printf("%c",p[j*16+i]); 
      else 
      printf("."); 
     }*/ 
     printf("|\n"); 
     j++; 
    } 
    printf("\nTotal hex value is %s",hexValue); 




    if(h->ts.tv_sec == 0) { 
     memset((void*)&h->extended_hdr.parsed_pkt, 0, sizeof(struct pkt_parsing_info)); 
     pfring_parse_pkt((u_char*)p, (struct pfring_pkthdr*)h, 5, 1, 1); 
    } 

    s = (h->ts.tv_sec + thiszone) % 86400; 

    if(h->extended_hdr.timestamp_ns) { 
     if (pd->dna.dna_dev.mem_info.device_model != intel_igb_82580 /* other than intel_igb_82580 */) 
     s = ((h->extended_hdr.timestamp_ns/1000000000) + thiszone) % 86400; 
     /* "else" intel_igb_82580 has 40 bit ts, using gettimeofday seconds: 
     * be careful with drifts mixing sys time and hw timestamp */ 
     usec = (h->extended_hdr.timestamp_ns/1000) % 1000000; 
     nsec = h->extended_hdr.timestamp_ns % 1000; 
    } else { 
     usec = h->ts.tv_usec; 
    } 

    printf("%02d:%02d:%02d.%06u%03u ", 
     s/3600, (s % 3600)/60, s % 60, 
     usec, nsec); 

    ehdr = (struct ether_header *) p; 
    printf("\n\nBefore Extexted :%d",use_extended_pkt_header); 
    if(use_extended_pkt_header) { 
     printf("\nafter USE EXE"); 
     printf("%s[if_index=%d]", 
     h->extended_hdr.rx_direction ? "[RX]" : "[TX]", 
     h->extended_hdr.if_index); 

     printf("[%s -> %s] ", 
     etheraddr_string(h->extended_hdr.parsed_pkt.smac, buf1), 
     etheraddr_string(h->extended_hdr.parsed_pkt.dmac, buf2));  

     if(h->extended_hdr.parsed_pkt.offset.vlan_offset) 
    printf("[vlan %u] ", h->extended_hdr.parsed_pkt.vlan_id); 

     if (h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/ || h->extended_hdr.parsed_pkt.eth_type == 0x86DD /* IPv6*/) { 

     if(h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/) { 
     printf("[IPv4][%s:%d ", intoa(h->extended_hdr.parsed_pkt.ipv4_src), h->extended_hdr.parsed_pkt.l4_src_port); 
     printf("-> %s:%d] ", intoa(h->extended_hdr.parsed_pkt.ipv4_dst), h->extended_hdr.parsed_pkt.l4_dst_port); 




        char sql_lite[1500]; 
        int lastID = mysql_insert_id(conn); 
        printf("\n\n\nLAT IS %d",lastID); 
        char *hexValue=""; 
        while(p) { 
        printf("\n\nstr: %s\n", p); 
        asprintf(&hexValue,"%s%02x",hexValue,p); 
        p++; 
       } 
        //printf("\n\n char size : p size %lu ",sizeof(p)); 
        sprintf(sql_lite, "insert into tblPL1 values ('%d','%s','%s')",'2013-05-11 20:20:20',p[0]); 
       puts(sql_lite); 
       //error = sqlite3_exec(conn, sql_lite, 0, 0, 0); 




     } else { 
      printf("[IPv6][%s:%d ", in6toa(h->extended_hdr.parsed_pkt.ipv6_src), h->extended_hdr.parsed_pkt.l4_src_port); 
      printf("-> %s:%d] ", in6toa(h->extended_hdr.parsed_pkt.ipv6_dst), h->extended_hdr.parsed_pkt.l4_dst_port); 
     } 

    printf("[l3_proto=%s]", proto2str(h->extended_hdr.parsed_pkt.l3_proto)); 

    if(h->extended_hdr.parsed_pkt.tunnel.tunnel_id != NO_TUNNEL_ID) { 
     printf("[TEID=0x%08X][tunneled_proto=%s]", 
     h->extended_hdr.parsed_pkt.tunnel.tunnel_id, 
     proto2str(h->extended_hdr.parsed_pkt.tunnel.tunneled_proto)); 

     if(h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/) { 
     printf("[IPv4][%s:%d ", 
      intoa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_src.v4), 
      h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_src_port); 
     printf("-> %s:%d] ", 
      intoa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_dst.v4), 
      h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_dst_port); 
     } else { 
     printf("[IPv6][%s:%d ", 
      in6toa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_src.v6), 
      h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_src_port); 
     printf("-> %s:%d] ", 
      in6toa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_dst.v6), 
      h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_dst_port); 
     } 
    } 

    printf("[hash=%u][tos=%d][tcp_seq_num=%u]", 
     h->extended_hdr.pkt_hash, 
      h->extended_hdr.parsed_pkt.ipv4_tos, 
     h->extended_hdr.parsed_pkt.tcp.seq_num); 

     } else { 
    if(h->extended_hdr.parsed_pkt.eth_type == 0x0806 /* ARP */) 
     printf("[ARP]"); 
    else 
     printf("[eth_type=0x%04X]", h->extended_hdr.parsed_pkt.eth_type); 
     } 

     printf(" [caplen=%d][len=%d][parsed_header_len=%d][eth_offset=%d][l3_offset=%d][l4_offset=%d][payload_offset=%d]\n", 
     h->caplen, h->len, h->extended_hdr.parsed_header_len, 
     h->extended_hdr.parsed_pkt.offset.eth_offset, 
     h->extended_hdr.parsed_pkt.offset.l3_offset, 
     h->extended_hdr.parsed_pkt.offset.l4_offset, 
     h->extended_hdr.parsed_pkt.offset.payload_offset); 

    } else { 
     printf("[%s -> %s][eth_type=0x%04X][caplen=%d][len=%d] (use -m for details)\n", 
     etheraddr_string(ehdr->ether_shost, buf1), 
     etheraddr_string(ehdr->ether_dhost, buf2), 
     ntohs(ehdr->ether_type), 
     h->caplen, h->len); 
    } 
    } 

    if(verbose == 2) { 
     int i; 

     for(i = 0; i < h->caplen; i++) 
     printf("%02X ", p[i]); 
     printf("\n"); 
    } 

    if(unlikely(add_drop_rule)) { 
    if(h->ts.tv_sec == 0) 
     pfring_parse_pkt((u_char*)p, (struct pfring_pkthdr*)h, 4, 0, 1); 

    drop_packet_rule(h); 
    } 
} 
+0

p 선언은 무엇입니까? 'char * p'입니까? – MOHAMED

+0

const 인 경우 const u_char * p – new14

+0

이므로 p ++로 변경할 수 없습니다. 당신은 컴파일에 오류가 발생합니다 – MOHAMED

답변

1

1)

while(*p) { 

2)

asprintf(&hexValue,"%s%02x",hexValue,p); 

들어 I는 모르겠습니다되어야 asprinf에 의해,하지만 난 reco charachter 코드가 제공됩니다 :

3) 당신은 Explaination이

char *hexValue=""; 
while(p) { 
    printf("\n\nstr: %s\n", p); 
    asprintf(&hexValue,"%s%02x",hexValue,p); 
    p++; 
} 

char *hexValue=malloc(((h->caplen)*2)+1); 
char *q=hexValue; 
int i; 
for (i=0;i<h->caplen;q+=2,i++) 
     sprintf(q,"%02X",*(p+i)); 
printf("%s\n", hexValue); 

에 의해 변경할 수 있습니다 찍어요이 일어나는 놀라운 일이 없다는 것을 mmend 자신감을 다른 변수를 사용하는 2 자리의 16 진수. asprintf 형식 지정자 "%02X"에서했던 것처럼. 그래서 당신의 p의 모든 요소에 대해 각 p 요소의 16 진수 코드를 작성하려면 2 개의 char이 필요합니다. 그래서 hexValue의 길이 = 2 * p의 길이입니다. 그리고 이것이 널 종결 문자에 사용되기 때문에 1을 더했습니다. 당신이 2 characheters에서 코드를 작성해야하기 때문에 처음 hexadecimal에 각 for 반복 Q에 대한 지적되고있다

for (;*p;q+=2,p++) 
    sprintf(q,"%02X",*p); 

q에 관한

2 q+=2로 증가합니다.

편집 segfault에 따라 나는 작은 프로그램에서 코드를 실행하려고했습니다. 별도의 프로그램에서 다음 코드를 시도해보고 segfault가 있는지 확인하십시오.

int main() 

{ 

    const unsigned char pp[] = {'a','b','c','d'}; 
    const unsigned char *p = pp; 
    int caplen = 4; 
    char *hexValue=malloc(((caplen)*2)+1); 
    char *q=hexValue; 
    int i; 
    for (i=0;i<caplen;q+=2,i++) 
      sprintf(q,"%02X",*(p+i)); 
    printf("%s\n", hexValue); 

} 
+0

@Mohamaded 그래서 당신이 추천하는 수정은 무엇입니까? – new14

+0

@ 해당 16 진수 값을 읽을 수있는 값으로 변환하고 하나의 단일 문자열로 패킷의 모든 페이로드 세부 정보를 변환해야합니다. – new14

+0

@ new14 답변을 업데이트합니다. 3에서 제안 된 새 코드를 참조하십시오.) – MOHAMED

관련 문제