当前位置: 技术问答>linux和unix
一段raw_socket代码
来源: 互联网 发布时间:2016-05-30
本文导语: 找来一段linux下的raw_socket代码,.但是编译一直通不过,看了很久,也不明白为何出错,求大侠帮助! #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ICMPHEAD 8 ...
找来一段linux下的raw_socket代码,.但是编译一直通不过,看了很久,也不明白为何出错,求大侠帮助!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ICMPHEAD 8
#define MAXICMPLEN 200
using namespace std;
/*******************************************
class:RawSock
定义RawSock类
作用:成员变量sock存放原始套接字,error
存放错误代码
*******************************************/
class RawSock
{
public:
int sock;
int error;
RawSock(int protocol );
virtual ~RawSock();
int send(const void *msg, int msglen, sockaddr *addr, unsigned int len);
int send(const void *msg, int msglen, char *addr);
int receive(void *buf,int buflen,sockaddr*from, int *len);
int Error() {return error;}
};
RawSock::RawSock(int protocol) {
sock = socket(AF_INET, SOCK_RAW,protocol);
setuid(getuid());
if (sock == -1) error= 1;
else error = 0;
}
RawSock::~RawSock(){
close(sock);
}
int RawSock::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 RawSock::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 RawSock::receive(void* buf,int buflen,sockaddr*from,int* len)
{
if(error) return -1;
while(1){
int length = recvfrom(sock,buf,buflen,0,from,(socklen_t*)len);
if(length == -1)
if(errno == EINTR) continue;
else{
error = 3;
return -1;
}
return length;
}
}
raw_soc.c:1:19: iostream: 没有那个文件或目录
raw_soc.c:20: parse error before "namespace"
raw_soc.c:20: warning: data definition has no type or storage class
raw_soc.c:28: parse error before "RawSock"
raw_soc.c:29: syntax error before '{' token
raw_soc.c:33: warning: data definition has no type or storage class
raw_soc.c:34: parse error before '~' token
raw_soc.c:36: parse error before "sockaddr"
raw_soc.c:37: conflicting types for `send'
raw_soc.c:36: previous declaration of `send'
raw_soc.c:38: parse error before "sockaddr"
raw_soc.c:40: parse error before '}' token
raw_soc.c:43: parse error before ':' token
raw_soc.c:45: parse error before '(' token
raw_soc.c:54: parse error before ':' token
raw_soc.c:57: `sock' undeclared here (not in a function)
raw_soc.c:57: `msg' undeclared here (not in a function)
raw_soc.c:57: `msglen' undeclared here (not in a function)
raw_soc.c:57: parse error before "sockaddr"
raw_soc.c:64: parse error before ':' token
raw_soc.c:84: parse error before ':' token
raw_soc.c:116: warning: data definition has no type or storage class
raw_soc.c:117: warning: data definition has no type or storage class
raw_soc.c:118: parse error before '~' token
raw_soc.c:120: parse error before '*' token
raw_soc.c: In function `setCode':
raw_soc.c:121: `packet' undeclared (first use in this function)
raw_soc.c:121: (Each undeclared identifier is reported only once
raw_soc.c:121: for each function it appears in.)
raw_soc.c: In function `setId':
raw_soc.c:122: `packet' undeclared (first use in this function)
raw_soc.c: In function `setSeq':
raw_soc.c:123: `packet' undeclared (first use in this function)
raw_soc.c: In function `setType':
raw_soc.c:124: `packet' undeclared (first use in this function)
raw_soc.c: At top level:
raw_soc.c:125: parse error before '}' token
raw_soc.c:128: parse error before ':' token
raw_soc.c:131: `packet' used prior to declaration
raw_soc.c:131: `icmp' undeclared here (not in a function)
raw_soc.c:131: parse error before ')' token
raw_soc.c:142: redefinition of `packet'
raw_soc.c:131: `packet' previously defined here
raw_soc.c:142: `icmp' undeclared here (not in a function)
raw_soc.c:142: parse error before ')' token
raw_soc.c:154: parse error before ':' token
raw_soc.c:158: `addr' undeclared here (not in a function)
raw_soc.c:161: parse error before "while"
raw_soc.c:170: redefinition of `sum'
raw_soc.c:157: `sum' previously defined here
raw_soc.c:170: initializer element is not constant
raw_soc.c:170: warning: data definition has no type or storage class
raw_soc.c:171: parse error before '+=' token
raw_soc.c:172: conflicting types for `answer'
raw_soc.c:159: previous declaration of `answer'
raw_soc.c:172: warning: data definition has no type or storage class
raw_soc.c:173: parse error before "return"
raw_soc.c:176: parse error before ':' token
raw_soc.c:184: `host' undeclared here (not in a function)
raw_soc.c:184: warning: passing arg 1 of `send' makes pointer from integer without a cast
raw_soc.c:184: initializer element is not constant
raw_soc.c:185: parse error before "return"
raw_soc.c:190: parse error before ':' token
raw_soc.c:196: parse error before "if"
raw_soc.c:198: `buf' undeclared here (not in a function)
raw_soc.c:198: `from' undeclared here (not in a function)
raw_soc.c:198: initializer element is not constant
raw_soc.c:199: parse error before "if"
raw_soc.c:203: conflicting types for `ip'
raw_soc.c:194: previous declaration of `ip'
raw_soc.c:203: `buf' undeclared here (not in a function)
raw_soc.c:203: warning: data definition has no type or storage class
raw_soc.c:204: invalid type argument of `->'
错误一大把,但是怎么也不明白像raw_soc.c:1:19: iostream: 没有那个文件或目录
raw_soc.c:20: parse error before "namespace"
raw_soc.c:20: warning: data definition has no type or storage class
raw_soc.c:28: parse error before "RawSock"
这些为何出错,是不是linux下不支持类以及iostream标准?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ICMPHEAD 8
#define MAXICMPLEN 200
using namespace std;
/*******************************************
class:RawSock
定义RawSock类
作用:成员变量sock存放原始套接字,error
存放错误代码
*******************************************/
class RawSock
{
public:
int sock;
int error;
RawSock(int protocol );
virtual ~RawSock();
int send(const void *msg, int msglen, sockaddr *addr, unsigned int len);
int send(const void *msg, int msglen, char *addr);
int receive(void *buf,int buflen,sockaddr*from, int *len);
int Error() {return error;}
};
RawSock::RawSock(int protocol) {
sock = socket(AF_INET, SOCK_RAW,protocol);
setuid(getuid());
if (sock == -1) error= 1;
else error = 0;
}
RawSock::~RawSock(){
close(sock);
}
int RawSock::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 RawSock::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 RawSock::receive(void* buf,int buflen,sockaddr*from,int* len)
{
if(error) return -1;
while(1){
int length = recvfrom(sock,buf,buflen,0,from,(socklen_t*)len);
if(length == -1)
if(errno == EINTR) continue;
else{
error = 3;
return -1;
}
return length;
}
}
raw_soc.c:1:19: iostream: 没有那个文件或目录
raw_soc.c:20: parse error before "namespace"
raw_soc.c:20: warning: data definition has no type or storage class
raw_soc.c:28: parse error before "RawSock"
raw_soc.c:29: syntax error before '{' token
raw_soc.c:33: warning: data definition has no type or storage class
raw_soc.c:34: parse error before '~' token
raw_soc.c:36: parse error before "sockaddr"
raw_soc.c:37: conflicting types for `send'
raw_soc.c:36: previous declaration of `send'
raw_soc.c:38: parse error before "sockaddr"
raw_soc.c:40: parse error before '}' token
raw_soc.c:43: parse error before ':' token
raw_soc.c:45: parse error before '(' token
raw_soc.c:54: parse error before ':' token
raw_soc.c:57: `sock' undeclared here (not in a function)
raw_soc.c:57: `msg' undeclared here (not in a function)
raw_soc.c:57: `msglen' undeclared here (not in a function)
raw_soc.c:57: parse error before "sockaddr"
raw_soc.c:64: parse error before ':' token
raw_soc.c:84: parse error before ':' token
raw_soc.c:116: warning: data definition has no type or storage class
raw_soc.c:117: warning: data definition has no type or storage class
raw_soc.c:118: parse error before '~' token
raw_soc.c:120: parse error before '*' token
raw_soc.c: In function `setCode':
raw_soc.c:121: `packet' undeclared (first use in this function)
raw_soc.c:121: (Each undeclared identifier is reported only once
raw_soc.c:121: for each function it appears in.)
raw_soc.c: In function `setId':
raw_soc.c:122: `packet' undeclared (first use in this function)
raw_soc.c: In function `setSeq':
raw_soc.c:123: `packet' undeclared (first use in this function)
raw_soc.c: In function `setType':
raw_soc.c:124: `packet' undeclared (first use in this function)
raw_soc.c: At top level:
raw_soc.c:125: parse error before '}' token
raw_soc.c:128: parse error before ':' token
raw_soc.c:131: `packet' used prior to declaration
raw_soc.c:131: `icmp' undeclared here (not in a function)
raw_soc.c:131: parse error before ')' token
raw_soc.c:142: redefinition of `packet'
raw_soc.c:131: `packet' previously defined here
raw_soc.c:142: `icmp' undeclared here (not in a function)
raw_soc.c:142: parse error before ')' token
raw_soc.c:154: parse error before ':' token
raw_soc.c:158: `addr' undeclared here (not in a function)
raw_soc.c:161: parse error before "while"
raw_soc.c:170: redefinition of `sum'
raw_soc.c:157: `sum' previously defined here
raw_soc.c:170: initializer element is not constant
raw_soc.c:170: warning: data definition has no type or storage class
raw_soc.c:171: parse error before '+=' token
raw_soc.c:172: conflicting types for `answer'
raw_soc.c:159: previous declaration of `answer'
raw_soc.c:172: warning: data definition has no type or storage class
raw_soc.c:173: parse error before "return"
raw_soc.c:176: parse error before ':' token
raw_soc.c:184: `host' undeclared here (not in a function)
raw_soc.c:184: warning: passing arg 1 of `send' makes pointer from integer without a cast
raw_soc.c:184: initializer element is not constant
raw_soc.c:185: parse error before "return"
raw_soc.c:190: parse error before ':' token
raw_soc.c:196: parse error before "if"
raw_soc.c:198: `buf' undeclared here (not in a function)
raw_soc.c:198: `from' undeclared here (not in a function)
raw_soc.c:198: initializer element is not constant
raw_soc.c:199: parse error before "if"
raw_soc.c:203: conflicting types for `ip'
raw_soc.c:194: previous declaration of `ip'
raw_soc.c:203: `buf' undeclared here (not in a function)
raw_soc.c:203: warning: data definition has no type or storage class
raw_soc.c:204: invalid type argument of `->'
错误一大把,但是怎么也不明白像raw_soc.c:1:19: iostream: 没有那个文件或目录
raw_soc.c:20: parse error before "namespace"
raw_soc.c:20: warning: data definition has no type or storage class
raw_soc.c:28: parse error before "RawSock"
这些为何出错,是不是linux下不支持类以及iostream标准?
|
跟 gcc 一样的,如:
$ g++ -o raw_soc raw_soc.c
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。