[완료]리눅스 C소켓 프로그래밍 좀 도와주세요~

damedykim의 이미지

리눅스 RAW소켓프로그래밍 질문인데요. 리눅스 아파치? 서버에서 관리자권한으로 컴파일해서 실행시켰는데 protocol not suported라고 에러가 뜨더라구요. ㅜㅜ
소스코드는 제가 대충 이렇게 짜봣는데 소스코드에 이상이 있는 건가요? 어떤게 문제인지 좀 알려주세요. ㅜㅜ

#include <stdlib.h>
#include <string.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
 
int in_cksum(u_short *p, int n);
 
int main(int argc, char **argv)
{
   int icmp_socket,ip_socket,i;
   int ret;
   struct icmp *p, *rp;
   struct sockaddr_in addr, from;
   struct ip *ip, *rip;
   struct iphdr *iph;
   char buffer[1024];
   char buf[1024];
   int sl;
   int hlen;
 
   ip_socket = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
   if(ip_socket < 0)
   {
    perror("socket error : ");
    exit(0);
   }
 
   memset(buf, 0x00, 1024);
 
   const int on = 1;
   if(setsockopt(ip_socket,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on))<0){
  perror("socket error : ");
    exit(0);
   }
 
   iph = (struct iphdr *)buf;
   iph->saddr = inet_addr(argv[1]);
   iph->daddr = inet_addr(argv[1]);
 
   // ICMP에대응하는 RAW 소켓을 생성한다.
   icmp_socket = ip_socket;
   if(icmp_socket < 0)
   {
      perror("socket error : ");
      exit(0);
   }
 
   memset(buffer, 0x00, 1024);
 
   // icmp 패킷을 만든다.
   p = (struct icmp *)buffer;
   p->icmp_type = ICMP_ECHO;
   p->icmp_code = 0;
   p->icmp_cksum = 0;
   p->icmp_seq = 15;
   p->icmp_id = getpid();
 
   p->icmp_cksum = in_cksum((u_short *)p, 1000);
   memset(&addr, 0, sizeof(addr));
   addr.sin_addr.s_addr = inet_addr(argv[1]);
   //addr.sin_addr.d_addr = inet_addr(argv[1]);
   addr.sin_family = AF_INET;
 
   // icmp 패킷을 상대 호스트로 전송한다.
   for(i=0; i<1000; i++) {
   ret = sendto(icmp_socket,p,sizeof(*p), 0, (struct sockaddr *)&addr, sizeof(addr));
 
   if (ret< 0)
   {
      perror("sendto error : ");
      exit(0);
   }
   }
   // 상대 호스트로 부터 icmp 응답을 기다린다.
   sl = sizeof(from);
   ret = recvfrom(icmp_socket,buffer, 1024, 0, (struct sockaddr *)&from, &sl);
 
   if (ret < 0)
   {
      //printf("%d %d %d\n", ret, errno, EAGAIN);
      perror("recvfrom error : ");
      exit(0);
   }
 
   // 응답을 받았다면
   // 해당 호스트의 IP정보와
   // icmp 패킷 정보를 출력한다.
   ip = (struct ip *)buffer;
   hlen = ip->ip_hl*4;
   rp = (struct icmp *)(buffer+hlen);
   printf("reply from %s\n", inet_ntoa(from.sin_addr));
   printf("Type : %d \n", rp->icmp_type);
   printf("Code : %d \n", rp->icmp_code);
   printf("Seq  : %d \n", rp->icmp_seq);
   printf("Iden : %d \n", rp->icmp_id);
   return 1;
}
 
int in_cksum( u_short *p, int n )
{
   register u_short answer;
   register long sum = 0;
   u_short odd_byte = 0;
 
   while( n > 1 )
   {
      sum += *p++;
      n -= 2;
 
   }
 
   if( n == 1 )
   {
      *( u_char* )( &odd_byte ) = *( u_char* )p;
      sum += odd_byte;
 
   }
 
   sum = ( sum >> 16 ) + ( sum & 0xffff );
   sum += ( sum >> 16 );
   answer = ~sum;
 
   return ( answer );
 
}

gurugio의 이미지

어느 함수에서 에러가 나는 건가요?

damedykim의 이미지

답변해 주셔서 감사합니다. 이렇게 저렇게 하다 해결 했네요^__^

댓글 달기

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