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

📄 myraw.cpp

📁 简单路由器实例。该程序是一个专用的IP路由器
💻 CPP
字号:

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>   /* inet_ntoa */
#include <unistd.h>      /* close */
#include <stdio.h>       /* Basic I/O routines          */
#include <sys/types.h>   /* standard system types       */
#include <netinet/in.h>  /* Internet address structures */
#include <sys/socket.h>  /* socket interface functions  */
#include <netdb.h>       /* host to IP resolution       */
#include <netinet/in_systm.h>
#include <netinet/ip.h>  /* ICMP */
#include "myraw.h"


/****************
class: myRaw
******************/

myRaw::myRaw(int protocol = 0) 
{
   sock = socket(AF_INET, SOCK_RAW,protocol);
   setuid(getuid());
   
   if (sock == -1) error= 1;
   else error = 0;
}

myRaw::~myRaw()
{
   close(sock);
}

int myRaw::send(const void* msg,int msglen,sockaddr* to, unsigned int len) 
{
   if (error) return -1;
   int length = sendto(sock,msg,msglen,0,(const sockaddr*)to,len); 
   if (length == -1) {
      error = 2;
      return -1;
      }
   return length; 
}

int myRaw::send(const void* msg,int msglen,char* hostname) 
{
   sockaddr_in sin;        // Sock Internet address
   
   if (error) return -1;
   
   if(hostname) {
    hostent *hostnm = gethostbyname(hostname); 
    if(hostnm == (struct hostent *) 0) {
      return -1;
    }
    sin.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
   }
   else   
    return -1; 
   sin.sin_family = AF_INET;
   
   return send(msg,msglen,(sockaddr *)&sin, sizeof(sin));
}

int myRaw::sendIP(const void* msg,int msglen,sockaddr* to, unsigned int len) 
{
	int on = 1;
    setsockopt(sock,IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));
	
    return send(msg, msglen, to, len); 
}

int myRaw::receive(void* buf,int buflen,sockaddr* from,int* len)
{
   if (error) return -1;
   while (1) {
      int length = recvfrom(sock,buf,buflen,0,from,len); 
      if (length == -1) 
        if (errno == EINTR) continue;
      else {
        error = 3;
        return -1;
        }
      return length;
      }
}

⌨️ 快捷键说明

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