Linux下C语言网络编程tcp.h头文件问题,求帮助

jch64899426 2011-05-11 05:50:40
各位大大,我最近在写一个Linux下的端口扫描软件,需要自己设置发送包的ip和tcp首部内容。
我就包含了
#include <netinet/ip.h>
#include <netinet/tcp.h>这两个文件。然后在后面的程序中,用到了如下代码:

iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr);
iph->ip_id = htonl (54321);
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0;
iph->ip_src = *(struct in_addr *)phost->h_addr_list[0];
iph->ip_dst.s_addr = sin.sin_addr.s_addr;
tcph->th_sport = htons (5554);
tcph->th_dport = htons (Port);
tcph->th_seq = random ();
tcph->th_ack = 0;
tcph->th_x2 = 0;
tcph->th_off = 0;
tcph->th_flags = TH_SYN;
tcph->th_win = htonl (65535);
tcph->th_sum = 0;
tcph->th_urp = 0;

用GCC编译发生错误,提示
structure has no member named "th_sport"
structure has no member named "th_dport"
structure has no member named "th_seq"
.
.
.
就是tcp.h定义的所有的变量都用不了,但是ip.h的都可以。实在是不知道为什么,请各位帮帮忙吧,谢谢了
...全文
1918 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cikerexue 2012-04-19
  • 打赏
  • 举报
回复
我的linux-2.6.23版本里面tcphdr没有LZ程序里面的元素th_sport" "th_dport""th_seq"
struct tcphdr {
__be16 source;
__be16 dest;
__be32 seq;
__be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
__u16 doff:4,
res1:4,
cwr:1,
ece:1,
urg:1,
ack:1,
psh:1,
rst:1,
syn:1,
fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
__be16 window;
__sum16 check;
__be16 urg_ptr;
};
.
wliangde 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

struct tcphdr {
__be16 source;
__be16 dest;
__be32 seq;
__be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
……
[/Quote] 楼主贴的是<netinet/tcp.h>里的结构定义。
可能是__FAVOR_BSD 这个没定义。
luciferisnotsatan 2012-04-17
  • 打赏
  • 举报
回复
# ifdef __FAVOR_BSD
typedef u_int32_t tcp_seq;
/*
* TCP header.
* Per RFC 793, September, 1981.
*/
struct tcphdr
{
u_int16_t th_sport; /* source port */
u_int16_t th_dport; /* destination port */
...
# else /* !__FAVOR_BSD */
struct tcphdr
{
u_int16_t source;
u_int16_t dest;

__FAVOR_BSD 这个宏定义了没?
luciferisnotsatan 2012-04-17
  • 打赏
  • 举报
回复
打开tcp.h看看里面是怎么样的
nice_cxf 2012-04-17
  • 打赏
  • 举报
回复
struct tcphdr {
__be16 source;
__be16 dest;
__be32 seq;
__be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
那有什么th_sport等变量啊?
wliangde 2012-04-17
  • 打赏
  • 举报
回复
是 struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof(struct ip));
jch64899426 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jjajun 的回复:]

首先检查一下你的tcp.h的文件路径对不对。
其次看看你定义的结构体本身是不是外部不允许访问
[/Quote]

tcp.h头文件的路径应该是对的,因为编译时候并没有提示找不到该头文件,而且同样的ip.h都可以用。下面是我定义的结构体,您看看有没有问题:

struct ip *iph = (struct ip*)datagram;
struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof(struct ip);
jjajun 2011-05-11
  • 打赏
  • 举报
回复
首先检查一下你的tcp.h的文件路径对不对。
其次看看你定义的结构体本身是不是外部不允许访问

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧