check sum함수 확인 부탁드립니다.

이민영의 이미지

ip 헤더 20byte 에대한 체크섬 입니다. 제일 간단한건데...
해놓구도 잘 됬는지.. 확신이 안서서.. 이렇게 확인 부탁드립니다.
잘못된 부분 있으면 지적 부탁드립니다.
읽어주신 분들 모두 감사드립니다.

//ip.h
typedef unsigned char    u_int8_t;
typedef unsigned int     u_int16_t;


struct in_addr
{
    u_int8_t s_addr_hi_1;
    u_int8_t s_addr_hi_2;
    u_int8_t s_addr_low_1;
    u_int8_t s_addr_low_2;
};

struct ip {
    u_int8_t        ip_vhl;         /* header length, version */
    u_int8_t        ip_tos;         /* type of service */
    u_int16_t       ip_len;         /* total length */
    u_int16_t       ip_id;          /* identification */
    u_int16_t       ip_off;         /* fragment offset field */
    u_int8_t        ip_ttl;         /* time to live */
    u_int8_t        ip_p;           /* protocol */
    u_int16_t       ip_sum;         /* checksum */
    struct  in_addr ip_src,ip_dst;  /* source and dest address */
};



//dumpcode.h
void printchar(unsigned char c)
{
    if(isprint(c))
    printf("%c",c);
    else
    printf(".");
}

void dumpcode(unsigned char *buff, int len)
{
    int i;
    for(i=0;i<len;i++)
    {
        if(i%16==0)
        printf("0x%08x  ",&buff[i]);
                printf("%02x ",buff[i]);
        if(i%16-15==0)
        {
            int j;
            printf("  ");
            for(j=i-15;j<=i;j++)
                printchar(buff[j]);
                printf("\n");
        }
    }

    if(i%16!=0)
    {
        int j;
        int spaces=(len-i+16-i%16)*3+2;
        for(j=0;j<spaces;j++)
            printf(" ");
        for(j=i-i%16;j<len;j++)
            printchar(buff[j]);
    }

    printf("\n");
}





//check sum 
unsigned short cksum(struct ip *ip, int len)
{
    long sum = 0;


    while(len > 1)
    {

        sum += *((unsigned short *) ip)++;
        if(sum & 0x80000000)
            sum = (sum & 0xFFFF) + (sum >> 16);
        len -= 2;
    }

    if(len)
        sum += (unsigned short) * (unsigned char *) ip;

    while(sum >> 16)
        sum = (sum & 0xFFFF) + (sum >> 16);
  
    return sum;
}


void main(void)
{
    struct ip nip;
    unsigned int check;


    nip.ip_vhl  = 69;
    nip.ip_tos  = 0;
    nip.ip_len = 28;
    nip.ip_id  = 1;
    nip.ip_off = 0;
    nip.ip_ttl  = 4;
    nip.ip_p  = 17;
    nip.ip_sum  = 0;
    nip.ip_src.s_addr_hi_1 = 10;
    nip.ip_src.s_addr_hi_2 = 12;
    nip.ip_src.s_addr_low_1 = 14;
    nip.ip_src.s_addr_low_2 = 5;
    nip.ip_dst.s_addr_hi_1 = 12;
    nip.ip_dst.s_addr_hi_2 = 6;
    nip.ip_dst.s_addr_low_1 = 7;
    nip.ip_dst.s_addr_low_2 = 9;


    printf("address = %x\n", &nip);

    dumpcode(&nip,20);

    check = cksum(&nip, 20);

    printf("%x\n",check);
}


0xbfffea00  45 00 ff bf 1c 00 00 00 01 00 00 00 00 00 00 00   E...............
0xbfffea10  04 11 15 40                                       ...@
bugiii의 이미지

checksum 을 사용해야 할 이유가 기존 데이터와 호환이 되어야 하기 때문에 필요한 것이 아니라면, crc16, 32 를 사용하시는 것이 어떨까요?

이민영의 이미지

답글 감사합니다. ^^
crc16과 crc32두 에러 검출 알고리즘 이네요.

수학적인것만 나오면 머리가 핑글 핑글 거려요 ㅠ_ㅠ

특별히 뭣에 쓸려고 만든것은 아니고요, 네트웍 수업 들으면서, 로우 소켓에 관심이 가는데.. 우선은 페킷 공부를 할것 같아서 만들어본 것입니다.

그런데.. 값을 넣고 dump해봐도 제가 예상한 값이랑 틀려서, 뭐가 잘못 된건지
싶어 질문 올렸었습니다(2진 숫자도 두렵습니다. 눈에 익지 않아 한눈에 정보가 팍팍 들어오질 않아서요... ^^;)

답글 감사드립니다. ^^

댓글 달기

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