[질문/libpcap]C언어 출력부분 세그폴트문제입니다.

jocdoc의 이미지

#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로 출력을 할라하면 세그폴트가 뜹니다.
                        //무엇이 문제인가요....
		}
	}
}
bushi의 이미지

null terminate

OTL

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.