[질문/libpcap]C언어 출력부분 세그폴트문제입니다.
글쓴이: jocdoc / 작성시간: 토, 2008/03/22 - 8:36오후
#include <sys/time.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <pcap.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#define PCAP_SNAPSHOT 1514
#define PCAP_TIMEOUT 1000
int chcnt = 0;
void packet_view(unsigned char *, const struct pcap_pkthdr *, const unsigned char *);
int main(int argc, char *argv[])
{
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 net, mask;
struct in_addr net_addr, mask_addr;
pcap_t *pd;
struct bpf_program fcode;
char *filter_rule;
if(argc != 2) {
printf("Usage : %s filter_rule\n", argv[0]);
return -1;
}
filter_rule = argv[1];
if((dev = pcap_lookupdev(errbuf)) == NULL) {
fprintf(stdout, "\nerror : pcap_lookupdev()\n");
perror(errbuf);
exit(1);
}
if(pcap_lookupnet(dev, &net, &mask, errbuf) < 0) {
fprintf(stdout, "\nerror : pcap_lookupnet()\n");
perror(errbuf);
exit(1);
}
--------------중략-----------------------
void packet_view(unsigned char *user, const struct pcap_pkthdr *h, const unsigned char *p)
{
struct iphdr *iph;
struct tcphdr *tcph;
struct ether_header *eh;
int temp = 0;
char *test1;
unsigned char *tcpdata, temp_char;
eh = (struct ether_header *)p;
if(ntohs(eh->ether_type) == ETHERTYPE_IP) // IP 패킷이면.
{
iph = (struct iphdr *)(sizeof(struct ether_header) + p);
printf("\n=======================================================\n");
printf("IP Address: %s\n", inet_ntoa(iph->saddr));
printf("-------------------------------------------------------\n");
if(iph->protocol == IPPROTO_TCP) //packet의 ip부분을 건너뛴 곳에서부터 tcp header의 시작이 된다.
{
tcph = (struct tcphdr *)(sizeof(struct ether_header) + (iph->ihl * 4) + p);
// *p가 가리키는 첫주소로부터 ip헤더와 tcp헤더와 이더넷헤더의 끝을 구한 다음 데이터영역을 구한다.
tcpdata = (unsigned char *)(p + sizeof(struct ether_header) + (iph->ihl*4) + (tcph->doff * 4));
printf("\n=================== TCP DATA(CHAR) =================\n");
// temp = 40byte, 40 <=
for(temp = (iph->ihl * 4) + (tcph->doff * 4);temp <= ntohs(iph->tot_len) - 1;temp++)
{
temp_char = *tcpdata;
if((temp_char == 0x0d) && (*(tcpdata+1) == 0x0a))
{
fprintf(stdout, "\n");
tcpdata += 2;
temp++;
continue;
}
temp_char = ((temp_char >= ' ') && (temp_char < 0x7f))? temp_char : '.';
printf("%c", *tcpdata);
tcpdata++;
}
/* if(strchr(tcpdata, test) == 0 ) {
printf(" 같은 문자열이 있습니다.");
} else {
printf(" 같은 문자열이 없습니다.");
}
*/
//------------------------------------
<span>test1 = strstr(tcpdata, "USER")+5;
if( test1 == NULL ) {
printf("찾을 문자열이 없습니다.\n");
exit(1);
}
printf("%s\n", test1);</span>
//----------이 루틴에서 저 위에 printf로 출력을 할라하면 세그폴트가 뜹니다.
//무엇이 문제인가요....
}
}
}Forums:


null terminate OTL
null terminate
OTL
댓글 달기