program linux network

本文介绍了一个简单的HTTP服务器的实现方法,并提供了一个用于抓取网络数据包的工具。服务器能够处理HTTP请求并返回固定响应,适用于协议分析和测试。抓包工具则通过设置网卡为混杂模式来捕获网络数据包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /*********************
server.c
实现一个简单的服务器
编译命令:gcc -o server server.c
一个简单的http服务器
关于这个服务器,/有一个巧妙的用途,可以用来分析http协议,测试
如何请求一个文件或者是一个图片等等(用浏览器连接这个服务器程序)
,事实上这个程序也正是为这个目的而写的,用同样的方法还可以测试
其他的协议,如ftp协议.另外他还可以作为一个代理服务器来使用,不
过要一个客户端的配合,或者是一个客户线程的配合.可以说,只要你愿意,
你可以把他变成你想要的任何东西
 ********************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>


#define PORT 80  //服务器监听的端口号
#define BACKLOG 10 //最大同时请求数

int main(int argc,char *argv[])
{
  int sockfd,new_fd;//用来监听的socket是 sockfd,用来传输数据的socket是 new_fd
  struct sockaddr_in host_addr;//本机的地址
  struct sockaddr_in client_addr;//客户的地址
  int sin_size;
  ///创建socket
  if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)))
      {
    perror("socket");
    exit(1);
      }

     //为地址负值
     host_addr.sin_family = AF_INET;
     host_addr.sin_port = htons(PORT);//端口号
     host_addr.sin_addr.s_addr = INADDR_ANY;//本机ip
     bzero(&(host_addr.sin_zero),8);//初始化host_addr.sin_zero为零

     //绑定端口
     if( -1 == bind(sockfd,(struct sockaddr *)&host_addr,sizeof(struct sockaddr)))
  {
    perror("bind");
    exit(1);
  }
     //监听端口
     if(-1 == listen(sockfd,BACKLOG))
     {
       perror("listen");
       exit(1);
     }
    //循环监测是否有请求来到,有就建立连接
    while(1)
     {
       sin_size = sizeof(struct sockaddr_in);
       //接受请求并建立一个新的socket
       new_fd = accept(sockfd,(struct sockaddr *)&client_addr,&sin_size);
       if(new_fd == -1)
         {
           perror("accept");
           exit(1);
         }
       //创建一个子进程
       if(!fork())
         {
           char buf[1024];
           int retval = recv(new_fd,buf,sizeof(buf),0);//接收数据
           if(retval == -1)//接受出错
         {
           perror("recv");
           close(new_fd);
           exit(1);
         }
           else
         {
           buf[retval] = 0;
           printf("%s",buf);
           //把请求写进文件,以便以后查询
           FILE *pfile;
           pfile = fopen("req","wb");
           fwrite(buf,sizeof(char),retval,pfile);
           fclose(pfile);          
         }
           char *ans = "Content-Type:text/html/n/nhello";
           if(-1 == send(new_fd,ans,strlen(ans),0))
         {
           perror("send");
           close(new_fd);
           exit(1);
         }
           close(new_fd);//父进程不再需要该socket
           waitpid(-1,NULL,WNOHANG);//等待子进程结束
         }
     }
    close(sockfd);
  return 0;
}

/************************
client.c
简单的客户端程序
可以用来测试服务器的回应,这对于协议的分析是很有用的
www.54sh.com
made by efish
 ***********************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>

#define PORT 80 //要连接的端口号
int main(int argc,char *argv[])
{
  int server;//指向服务器连接的socket
  struct sockaddr_in my_addr;//本机地址
  struct sockaddr_in his_addr;//服务器的地址
  int slen;

  slen = sizeof(his_addr);
  //创建socket
  if(-1 == (server = socket(AF_INET,SOCK_STREAM,0)))
    {     
    perror("sock");
    exit(1);
    }
  //初始化地址
  his_addr.sin_family = AF_INET;
  his_addr.sin_port = htons(PORT);//端口
  his_addr.sin_addr.s_addr = INADDR_ANY;//连接本机的服务器
  bzero(&(his_addr.sin_zero),8);//?

  //连接主机
  if(-1 == connect(server,(struct sockaddr *)&his_addr,sizeof(struct sockaddr)))
    {
      perror("connect");
      close(server);
      exit(1);
    }
  //发送消息给服务器
  char *msg = "Hello,this is from client!";//要发送的消息
  if(-1 == send(server,msg,strlen(msg),0))
    {
      perror("send");
      close(server);
      exit(1);
    }
  //从服务器获取数据
  char buf[1024];
  int retval = recv(server,buf,sizeof(buf),0);
  if(-1 == retval)
    {
      perror("recv");
      close(server);
      exit(1);
    }
  else
    {
      buf[retval] = 0;
      printf("%s",buf);
    }
  //关闭连接
  close(server);
  return 0;
}

/*********************
catcher.c
编译命令:gcc -o catcher catcher.c
获取原始数据包
*********************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> 
#include <netinet/ip.h>
#include <string.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/if_ether.h>
#include <net/ethernet.h>
#include <errno.h>


void die(char *why, int n)
{
  perror(why);
  exit(n);
}

int do_promisc(char *nif, int sock )
{
struct ifreq ifr;
              
strncpy(ifr.ifr_name, nif,strlen(nif)+1);
   if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1))  //获得flag
   {       
     die("ioctl", 2);
   }
 
   ifr.ifr_flags |= IFF_PROMISC;  //重置flag标志
 
   if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 )  //改变模式
   {
     die("ioctl", 3);
   }
}
//修改网卡成PROMISC(混杂)模式

char buf[40960];

main()
{
struct sockaddr_in addr;
struct ether_header *peth;
struct iphdr *pip;       
struct tcphdr *ptcp;
struct udphdr *pudp;

char mac[16];
int i,sock, r, len;       
char *data;
char *ptemp;
char ss[32],dd[32];

if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)  //建立socket
//man socket可以看到上面几个宏的意思
{
        die("socket", 1);
}

do_promisc("eth0", sock);    //eth0为网卡名称

system("ifconfig");

for(;;)
{
     len = sizeof(addr);

     r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len);
     //调试的时候可以增加一个输出r的语句判断是否抓到包
     buf[r] = 0;
     printf("%s",buf);
     ptemp = buf;
     peth = (struct ether_header *)ptemp;

     ptemp += sizeof(struct ether_header); //指针后移eth头的长度
     pip = (struct ip *)ptemp; //pip指向ip层的包头

     ptemp += sizeof(struct ip);//指针后移ip头的长度

     switch(pip->protocol)   //根据不同协议判断指针类型
     {
         case IPPROTO_TCP:
         ptcp = (struct tcphdr *)ptemp;       //ptcp指向tcp头部
    if(0 != strcmp(inet_ntoa(*(struct in_addr*)&(pip->saddr)),inet_ntoa(*(struct in_addr*)&(pip->daddr))))
      {
printf("TCP pkt :FORM:[%s]:[%d]/n",inet_ntoa(*(struct in_addr*)&(pip->saddr)),ntohs(ptcp->source));
         printf("TCP pkt :TO:[%s]:[%d]/n",inet_ntoa(*(struct in_addr*)&(pip->daddr)),ntohs(ptcp->dest));
      }       
         break;
       
         case IPPROTO_UDP:
         pudp = (struct udphdr *)ptemp;      //ptcp指向udp头部
              printf("UDP pkt:/n len:%d payload len:%d from %s:%d to %s:%d/n",
             r,
             ntohs(pudp->len),
             inet_ntoa(*(struct in_addr*)&(pip->saddr)),
             ntohs(pudp->source),
             inet_ntoa(*(struct in_addr*)&(pip->daddr)),
             ntohs(pudp->dest)
         );
         break;
       
         case  IPPROTO_ICMP:
         printf("ICMP pkt:%s/n",inet_ntoa(*(struct in_addr*)&(pip->saddr)));
         break;
       
         case  IPPROTO_IGMP:
         printf("IGMP pkt:/n");
         break;
       
         default:
         printf("Unkown pkt, protocl:%d/n", pip->protocol);
         break;
    } //end switch

     //perror("dump");
 }
 
}

/*
[playmud@fc3 test]$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)

************************eth的结构**************************************
struct ether_header
{
  u_int8_t  ether_dhost[ETH_ALEN];      // destination eth addr
  u_int8_t  ether_shost[ETH_ALEN];      // source ether addr  
  u_int16_t ether_type;                 // packet type ID field
} __attribute__ ((__packed__));

***********************IP的结构***********************************
struct iphdr
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ihl:4;
    unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
    unsigned int version:4;
    unsigned int ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
    u_int8_t tos;
    u_int16_t tot_len;
    u_int16_t id;
    u_int16_t frag_off;
    u_int8_t ttl;
    u_int8_t protocol;
    u_int16_t check;
    u_int32_t saddr;
    u_int32_t daddr;
  };

***********************TCP的结构****************************
struct tcphdr
  {
    u_int16_t source;
    u_int16_t dest;
    u_int32_t seq;
    u_int32_t ack_seq;
#  if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int16_t res1:4;
    u_int16_t doff:4;
    u_int16_t fin:1;
    u_int16_t syn:1;
    u_int16_t rst:1;
    u_int16_t psh:1;
    u_int16_t ack:1;
    u_int16_t urg:1;
    u_int16_t res2:2;
#  elif __BYTE_ORDER == __BIG_ENDIAN
    u_int16_t doff:4;
    u_int16_t res1:4;
    u_int16_t res2:2;
    u_int16_t urg:1;
    u_int16_t ack:1;
    u_int16_t psh:1;
    u_int16_t rst:1;
    u_int16_t syn:1;
    u_int16_t fin:1;
#  else
#   error "Adjust your <bits/endian.h> defines"
#  endif
    u_int16_t window;
    u_int16_t check;
    u_int16_t urg_ptr;
};
***********************UDP的结构*****************************
struct udphdr
{
  u_int16_t source;
  u_int16_t dest;
  u_int16_t len;
  u_int16_t check;
};

*************************************************************
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值