Linux と FreeBSD ではネットワークのプログラムが違う

2007-01-04 00:34:41 | | このエントリーを含むはてなブックマーク | Tag: c言語 unix ネットワーク パソコン プログラミング メモ 日記

ネットワークの授業の関係で、ヘッダを偽装したパケットを一つ流そうと思ってプログラムを書いたところコンパイルが通らない。

コンパイルエラー(1)

In file included from hoge.c:5:
/usr/include/netinet/ip.h:49: error: syntax error before "u_int"
/usr/include/netinet/ip.h:148: error: syntax error before "u_char"
/usr/include/netinet/ip.h:160: error: syntax error before "n_long"
/usr/include/netinet/ip.h:163: error: syntax error before "n_long"
/usr/include/netinet/ip.h:200: error: syntax error before "u_char"
In file included from hoge.c:6:
/usr/include/netinet/ip_icmp.h:45: error: syntax error before "u_int32_t"
/usr/include/netinet/ip_icmp.h:53: error: syntax error before "u_char"
/usr/include/netinet/ip_icmp.h:64: error: syntax error before "u_char"
/usr/include/netinet/ip_icmp.h:68: error: syntax error before "u_char"
/usr/include/netinet/ip_icmp.h:71: error: syntax error before "n_short"
/usr/include/netinet/ip_icmp.h:78: error: syntax error before "n_short"
/usr/include/netinet/ip_icmp.h:83: error: syntax error before "u_char"
/usr/include/netinet/ip_icmp.h:100: error: syntax error before "n_time"
/usr/include/netinet/ip_icmp.h:109: error: syntax error before "u_int32_t"
hoge.c: In function `main':
hoge.c:59: error: structure has no member named `type'
hoge.c:60: error: structure has no member named `code'
hoge.c:61: error: structure has no member named `checksum'
hoge.c:62: error: structure has no member named `un'
hoge.c:63: error: structure has no member named `un'
hoge.c:66: error: structure has no member named `checksum'
hoge.c:87: error: dereferencing pointer to incomplete type
hoge.c:90: error: structure has no member named `type'
hoge.c:93: error: structure has no member named `type'

この時のヘッダは以下の通り。

エラー時のヘッダ

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>

因みに Linux では、上のヘッダファイルのみでコンパイルが通る。

続いてヘッダファイル周りのコンパイルエラーを抑制するためのヘッダ。

コンパイルエラーを抑制するヘッダ

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>

これでプログラム自体のエラーだけになる。で、問題の部分。

コンパイルエラー部分のプログラム

    struct icmphdr hdr;

    /* Prepare ICMP header */
    hdr.type = ICMP_ECHO;
    hdr.code = 0;
    hdr.checksum = 0;
    hdr.un.echo.id = 0;
    hdr.un.echo.sequence = 0;
で、netinet/ip_icmp.h が怪しそうなので見てみる。

netinet/ip_icmp.h の 49 行目付近

/*
 * Structure of an icmp header.
 */
struct icmphdr {
        u_char  icmp_type;              /* type of message, see below */
        u_char  icmp_code;              /* type sub code */
        u_short icmp_cksum;             /* ones complement cksum of struct */
};

そもそも違うし……! というわけで、低レベルなプログラミングには気をつけましょう。

Related Entries

There is not related articles.

Trackbacks

Trackback URI: http://blog.c--v.net/trackback/2007/01/04/1

There is no trackback.

Comments

There is no comment.

Name
URI (Homepage or Email)
Body