icmp ping 에서 recvfrom 에서 블락이 걸립니다 ㅠㅠ 죄송하지만 이유를 모르겠어요 ㅠ

ktkmyfeeling의 이미지

recvfrom 에서 블락이 걸립니다 ㅠㅠ 죄송하지만 이유를 모르겠어요 ㅠ

printf로 찍어본 결과 test3 까지는 출력이 되는데 그 이후로 부터는 먹통이 되어 버립니다.ㅠ

책에 있는 예제로 해서

현재 있는 ip.h 와 icmp.h 파일로 변경도 하였고

컴파일 오류도 나지 않습니다.

그런데 실행만 시키면 recvfrom 에서 sendto 를 받지 못하는것 같습니다. ㅠㅠ

이유를 잘 모르겠어어 이렇게 글 올립니다.

고수님들 이유 좀 알려주세요 ㅠㅠ

#include
#include
#include
#include
#include
#include
#include
#include
#include

void sig_alrm(int);
void send_msg(void);
void handlePing(void);
unsigned short cksum_in(unsigned short *, int);
void tv_sub(struct timeval *, struct timeval *);

struct timeval *tvsend, tvrecv;

int sd;
pid_t pid;
int sl;

int nsent =0;

struct sockaddr_in sasend;
struct sockaddr_in sarecv;
int salen;

main(int argc, char *argv[ ]){
if(argc != 2){
printf("usage : ping doman_name\n");
exit(-1);
}

bzero((char *)&sasend, sizeof(sasend));
sasend.sin_family = AF_INET;
sasend.sin_addr.s_addr = inet_addr(argv[1]);
salen = sizeof(sasend);

pid = getpid() & 0xffff;

printf("test1\n");

handlePing();
}

void handlePing(void){
int len, hlen, icmplen;
struct timeval tval;
char rbuf[1500];

fd_set readfd;
struct ip *iph;
struct icmp *icmp;

double rtt;

signal(SIGALRM, sig_alrm);

printf("test2\n");

if((sd=socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))<0){
printf("socket open error\n");
exit(-1);
}

printf("test3\n");

sl = sizeof(sarecv);

for(;;){
len = recvfrom(sd, rbuf, sizeof(rbuf), 0, (struct sockaddr *)&sarecv, &sl);
printf("tttest\n");
if(len<0){
printf("read error\n");
exit(-1);
}

printf("test4\n");

iph = (struct ip *)rbuf;
//dereferencing pointer to incomplete type
hlen = iph->ip_hl * 4;
//dereferencing pointer to incomplete type

printf("test4\n");

if(iph->ip_p != IPPROTO_ICMP){
return;
}

printf("test5\n");

//dereferencing pointer to incomplete type
if(iph->ip_src.s_addr == sasend.sin_addr.s_addr){
icmp = (struct icmp *)(rbuf + hlen);
icmplen = len - hlen;

printf("test6\n");

if(icmp->icmp_type == ICMP_ECHOREPLY){
if(icmp->icmp_id != pid){
return;
}

printf("test7\n");

gettimeofday(&tvrecv, NULL);
tvsend = (struct timeval *) icmp->icmp_data;
tv_sub(&tvrecv, tvsend);
//`rtt' undeclared (first use in this function)
//my_ping.c:87: error: (Each undeclared identifier is reported only once
//my_ping.c:87: error: for each function it appears in.)
rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
//dereferencing pointer to incomplete type
printf("%d byte form ** : seq = %u, ttl=%d, rtt=%.3f ms\n", icmplen, icmp->icmp_seq, iph->ip_ttl, rtt);
}
}
}
}

void sig_alrm(int signo){


printf("test8\n");

send_msg();

alarm(1);
return;
}

void send_msg(void){
int len;
struct icmp *icmp;
char sendbuf[1500];
int datalen = 56;

icmp = (struct icmp *) sendbuf;

icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_id = pid;
icmp->icmp_seq = nsent++;
memset(icmp->icmp_data, 0xa5, datalen);

printf("test9\n");

gettimeofday((struct timeval *) icmp->icmp_data, NULL);

len = 8 + datalen;
icmp->icmp_cksum=0;
icmp->icmp_cksum = cksum_in((unsigned short *)icmp, len);

sendto(sd, sendbuf, len, 0, (struct sockaddr *)&sasend, salen);
}
void tv_sub(struct timeval *out, struct timeval *in){
if((out->tv_usec -= in->tv_usec)<0){
--out->tv_sec;
out->tv_usec +=1000000;
}
out->tv_sec -= in->tv_sec;
}

unsigned short cksum_in(unsigned short *addr, int len){
unsigned long sum = 0;
unsigned short answer = 0;
unsigned short *w = addr;
int nleft = len;

while(nleft>1){
sum+= *w++;
if(sum&0x80000000){
sum=(sum&0xffff)+(sum>>16);
}
nleft -=2;
}

if(nleft == 1){
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum = sum + answer;
}

while(sum>>16){
sum = (sum & 0xffff)+(sum>>16);
}

return(sum==0xffff)?sum:~sum;
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.