udp checksum 구하는 함수에 대해서

ssstone의 이미지

커널에서 작업을 하다가 udp checksum을 구해야 하는 부분이 있어서 원래 커널에 있는 부분을 사용할려니깐 pseudo header를 만들수 없는 상황입니다.

그래서 어쩔수 없이 pseduo header에 대한 부분은 직접 계산하도록 함수를 하나 만들었습니다. 하지만 sniffer로 보면 정확하지 않다고 나오는 군요.

혹시 구현해보신 경험이 있으신분들이 계시면 조언 부탁드립니다.
지금은 어쩔수 없이 checksum을 사용하지 않도로 0을 넣어서 처리해서 동작시키고 있습니다.

static unsigned short udp_sum_calc(unsigned short udp_len, unsigned int saddr, unsigned int daddr, char *buff)
{
    unsigned int sum=0;
    unsigned short word16;
    int i, padding=0;

    if(udp_len%2==1) {
        padding=1;
        buff[udp_len]=0;
    }
    
    // calulate the sum of all 16 bits
    for(i=0;i<udp_len+padding;i=i+2) {
        word16 = ((buff[i]<<8) & 0xFF00) + (buff[i+1] & 0xFF);
        sum = sum + (unsigned int)(word16);
    }

    // IP source and destination address
    word16 = (saddr & 0xFFFF0000) >> 16;
    sum = sum + (unsigned int)(word16);
    word16 = saddr & 0x0000FFFF;
    sum = sum + (unsigned int)(word16);

    word16 = (daddr & 0xFFFF0000) >> 16;
    sum = sum + (unsigned int)(word16);
    word16 = daddr & 0x0000FFFF;
    sum = sum + (unsigned int)(word16);

    // the protocol number and the length of the UDP Packet
    sum = sum + 17 + udp_len;

    // keep only the last 16 bits of the 32 bit calculated sum and the carries
    sum = (sum & 0xFFFF ) + (sum >> 16);

    // take the one's complement of sum
    sum = ~sum;

    return (unsigned short)(sum);
}

아래와 같이 호출을 합니다.
uh는 udp data의 헤더를 가리키는 포인터입니다. iph는 ip의 헤더이구요.
udp data부분은 이미 모두 완성되어 있는 상태이며, udp의 헤더(포트, 길이)도 이미 세팅해 두었습니다.

uh->check = 0; 
uh->check = udp_sum_calc( ntohs(uh->len), iph->saddr, iph->daddr, (char *)uh); 
익명 사용자의 이미지

* google 에게 udp checksum 두개 키워드로 물어보고, 결과중 첫번째 링크를 누르니.....
http://www.netfor2.com/udpsum.htm
...
그외 다수개의 링크들이 널렸군요.
검증은 목마른 사람의 몫으로...

ssstone의 이미지

저도 그거랑 RFC를 참조해서 만들었는데요. 동작을 제대로 하지

댓글 달기

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