⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 180.htm

📁 unix高级编程原吗
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="155.htm">上一层</a>][<a href="181.htm">下一篇</a>]
<hr><p align="left"><small>发信人: digger (欧阳疯), 信区: Socket <br>

标  题: RAW sockets example code: ipicmp.c <br>

发信站: 华南网木棉站 (Wed Aug  5 19:59:56 1998), 转信 <br>

/* Attempt to send an ICMP packet, writing the ICMP and IP headers. <br>

 * (Lacks adding the time to the end of the packet). <br>

 * Thamer Al-Herbish shadows@whitefang.com <br>

 */ <br>

#include <stdlib.h> <br>

#include <stdio.h> <br>

#include <sys/types.h> <br>

#include <sys/socket.h> <br>

#include <netinet/in.h> <br>

#include <arpa/inet.h> <br>

#include <netinet/in_systm.h> <br>

#include <netinet/ip.h> <br>

#include <netinet/ip_icmp.h> <br>

#if defined(SOLARIS_CKSUM_BUG) <br>

#include <netinet/tcp.h> <br>

#endif <br>

#include <strings.h> <br>

#include <unistd.h> <br>

#if !defined(HAVE_INCKSUM) <br>

#include <checksum.h> <br>



#endif <br>

#if !defined(IPVERSION) <br>

#define IPVERSION 4  /* Incase some system does not have this definition. */ <br>

  <br>

#endif               /* We'll always be using 4 as the version anyway. */ <br>

#if !defined(LINUX) <br>

#define BUFFSIZE (sizeof(struct icmp) + sizeof(struct ip)) <br>

#else <br>

#define BUFFSIZE (sizeof(struct icmphdr) + sizeof(struct iphdr)) <br>

#endif <br>

int main(int argc,char *argv[]) <br>

{ <br>

#if !defined (LINUX) <br>

  struct icmp *icmphdr; <br>

  struct ip *iphdr; <br>

#else <br>

  struct icmphdr *icmphdr; <br>

  struct iphdr *iphdr; <br>

#endif <br>

  unsigned char buff[BUFFSIZE]; <br>

  int sockd; <br>

  struct sockaddr_in mysocket; <br>



  int on = 1; <br>

 if(argc < 3) { <br>

   fprintf(stderr,"usage: %s source-ip dest-ip\n",argv[0]); <br>

   exit(-1); <br>

 } <br>

  if((sockd = socket(AF_INET,SOCK_RAW,IPPROTO_RAW)) < 0)  { <br>

    perror("socket"); <br>

    exit(-1); <br>

  } <br>

  if(setsockopt(sockd,IPPROTO_IP,IP_HDRINCL,(char *)&on,sizeof(on)) < 0)  { <br>

    perror("setsockopt"); <br>

    exit(-1); <br>

  } <br>

#if !defined(LINUX) <br>

  iphdr = (struct ip *)buff; <br>

  bzero((char *)iphdr,sizeof(struct ip)); <br>

  iphdr->ip_hl = 5; <br>

  iphdr->ip_v = IPVERSION; <br>

#ifdef IP_LEN_HORDER <br>

  iphdr->ip_len = (sizeof(struct ip) + sizeof(struct icmp)); <br>

#else <br>

  iphdr->ip_len = htons(sizeof(struct ip) + sizeof(struct icmp)); <br>



#endif <br>

  iphdr->ip_id = htons(getpid()); <br>

  iphdr->ip_ttl = 60; <br>

  iphdr->ip_p = IPPROTO_ICMP; <br>

  iphdr->ip_src.s_addr = inet_addr(argv[1]); <br>

  iphdr->ip_dst.s_addr = inet_addr(argv[2]); <br>

  iphdr->ip_sum = in_cksum((unsigned short *)iphdr,sizeof(struct ip)); <br>

  icmphdr = (struct icmp *)(buff + sizeof(struct ip)); <br>

  bzero((char *)icmphdr,sizeof(struct icmp)); <br>

  icmphdr->icmp_type = ICMP_ECHO; <br>

  icmphdr->icmp_seq = getpid(); <br>

  icmphdr->icmp_id = getpid(); <br>

  icmphdr->icmp_cksum = in_cksum((unsigned short *)icmphdr,sizeof(struct icm <br>

p)); <br>

#else <br>

  iphdr = (struct iphdr *)buff; <br>

  bzero((char *)iphdr,sizeof(struct iphdr)); <br>

  iphdr->ihl = 5; <br>

  iphdr->version = IPVERSION; <br>

#ifdef IP_LEN_HORDER <br>

  iphdr->tot_len = (sizeof(struct iphdr) + sizeof(struct icmphdr)); <br>

#else <br>

#else <br>

  iphdr->tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr)); <br>

#endif <br>

  iphdr->id = htons(getpid()); <br>

  iphdr->ttl = 60; <br>

  iphdr->protocol = IPPROTO_ICMP; <br>

  iphdr->saddr = inet_addr(argv[1]); <br>

  iphdr->daddr = inet_addr(argv[2]); <br>

  iphdr->check = in_cksum((unsigned short *)iphdr,sizeof(struct iphdr)); <br>

  icmphdr = (struct icmphdr *)(buff +sizeof(struct iphdr)); <br>

  bzero((char *)icmphdr,sizeof(struct icmphdr)); <br>

  icmphdr->type = ICMP_ECHO; <br>

  icmphdr->un.echo.sequence = getpid(); <br>

  icmphdr->un.echo.id =  getpid(); <br>

  icmphdr->checksum = in_cksum((unsigned short *)icmphdr, <br>

                                sizeof(struct icmphdr)); <br>

#endif /* LINUX */ <br>

  bzero((char *)&mysocket,sizeof(mysocket)); <br>

  mysocket.sin_family = AF_INET; <br>

  mysocket.sin_addr.s_addr = inet_addr(argv[2]); <br>

  if(sendto(sockd,(char *)buff,sizeof(buff),0x0, <br>

            (struct sockaddr *)&mysocket,sizeof(mysocket)) < 0)  { <br>

    perror("sendto"); <br>



    exit(-1); <br>

  } <br>

 exit(0); <br>

} <br>

-- <br>

                #################### <br>

                #  遥  眺  淑  女  # <br>

                ####################################### <br>

                                   #  君  子  好  球  # <br>

                                   #################### <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="155.htm">上一层</a>][<a href="181.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -