c - Garbled characters when sniff the http data and store them using pcap -
i filter "port 80" , start catch using pcap_loop(phandle,-1,pcap_callback,null);
the pcap_callback function void pcap_callback(u_char* user,const struct pcap_pkthdr* header, const u_char* pkt_data){ file *fp=fopen("1.html","a"); ether_header * eheader=(ether_header*)pkt_data; if(eheader->ether_type==htons(ethertype_ip)){ ip_header* ih=(ip_header*)(pkt_data+14); if(ih->proto==htons(tcp_protocal)){ int ip_len=ntohs(ih->tlen); int find_http=false; char* ip_pkt_data=(char*)ih; int n=0; char buffer[buffer_max_length]; int bufsize=0; for(;n<ip_len;n++) { /* http or post request */ if(!find_http && ((n+3<ip_len && strncmp(ip_pkt_data+n,"get",strlen("get")) ==0 ) || (n+4<ip_len && strncmp(ip_pkt_data+n,"post",strlen("post")) == 0)) ) find_http = true; /* http response */ if(!find_http && n+8<ip_len && strncmp(ip_pkt_data+n,"http/1.1",strlen("http/1.1"))==0) find_http = true; /* if http found */ if(find_http) { buffer[bufsize] = ip_pkt_data[n]; /* copy http data buffer */ bufsize ++; } } /* print http content */ if(find_http) { buffer[bufsize] = '\0'; printf("%s\n", buffer); printf("\n**********************************************\n\n"); int i=0; char c; for(i=0;i<bufsize;i++) { c=buffer[i]; fputc(c,fp); } fclose(fp); } } } } and run , , visit google.com
it can print request/response headers @ screen, 1 or 2 wired characters(a little box within 4 numbers(1 or 0)). ,i store them html file, still mess. if cat 1.html @ shell, data after response headers multiple lines, of them show in white background , stick together.
if open in emacs, display ^@^@^s\234\252.if paste them here, show difference. guess maybe it picture or other file gif or course problem, because response headers show content-type: image/png.
when content-type: text/html; charset=utf-8, mess.
why? , how solve it?
thanks!!
Comments
Post a Comment