weixin_43454098 2020-06-22 18:50 采纳率: 85.7%
浏览 3549
已采纳

[Error] a function-definition is not allowed here before '{' token报错,帮忙看看是哪里出错了

#include "winsock2.h"
#include "ws2tcpip.h"
#include <iostream>
#include "stdio.h"
#pragma comment(1ib,"ws2_32. lib") 
#define I0_RCVALL_WSAIOW (IOC_VENDOR, 1)
#define BUFFER_SIZE 65535
typedef struct _IP_HEADER {
union {
BYTE Version; //版本(前4位)
BYTE H_Len; //IP 头长度
};
BYTE SerType; //服务类型
WORD Ttl_Len; //总长度
WORD ID; //标识
union{
WORD Flags;
WORD Frag0ff;
};
BYTE TTLive;
BYTE Protocol;
WORD Chksum;
DWORD SrcAddr;
DWORD DstAddr;
BYTE Options;
} IP_HEADER;
void getVersion (BYTE b, BYTE &version)
{
version = b>>4;
}
void getIHL (BYTE b, BYTE &result)
{
result = (b & 0x0f) *4;
}
const char * pSerType_getProcedence(BYTE b) {
switch (b>>5) {
case 7:
return "Network Control";
case 6:
return "Internet work Control";
case 5:
return "CRITIC/ECP";
case 4:
return "F1ash Override" ;
case 3:
return "Falsh";
case 2:
return " Immediate";
case 1:
return "Priority";
case 0:
return "Routine";
default:
return "Unknown" ;
}
}
const char * pSerType_getTOS(BYTE b) {
b=(b>>1)&0x0f;
switch(b) {
case 0:
return "Normal service";
case 1:
return" Minimize monetary cost" ;
case 2:
return "Maximize reliability";
case 4:
return "Maximize throughput";
case 8:
return "Minimize delay";
case 15:
return "Maximize security";
default:
return "Unknown";
}
}
void getFlags(WORD w, BYTE &DF,BYTE &MF) {
DF=(w>>14)&0x01;
MF=(w>>13)&0x01;
}
void getFragoff (WORD w, WORD &fragoff) {
fragoff=w&0x1ffff;
}
const char * getProtocol (BYTE Protocol) {
switch (Protocol) {
case 1:
return "ICMP";
case 2:
return "IGMP";
case 3:
return "GGP";
case 4:
return "IP in IP ";
case 6:
return "TCP";
case 8:
return "EGP";
case 17:
return "UDP";
case 41:
return "IPv6";
case 46:
return "OSPF" ;
default:
return "UNKNOWN" ;
}
void ippar(FILE * file,char *buffer)
{
IP_HEADER ip = *(IP_ HEADER *)buffer;
fseek(file,0,SEEK_END);
BYTE version;
getVersion(ip.Version,version) ;
fprintf(file,"版本=IPV%d\r\n",version) ;
BYTE headerLen;
getIHL(ip.H_Len,headerLen);
fprintf(file,"头长度=%d(BYTE)\r\n",headerLen) ;
fprintf(file,"服务类型%s, %s\r\n",pSerType_ getProcedence(ip.SerType),pSerType_ getTOS(ip.SerType));
fprintf(file,"数据报长度=%d(BYTE)\r\n",ip.Tt1_Len) ;
fprintf(file,"数据报ID=%d\r\n",ip. ID);
BYTE DF,MF;
getFlags(ip.Flags, DF,MF);
fprintf(file,"分段标志DF=%d, MF=%d\r\n",DF,MF);
WORD frag0ff;
getFragoff(ip.Frag0ff, frag0ff); 
fprintf(file,"分段偏移值=%d\r\n" , frag0ff);
fprintf(file,"生存期=%d(hops)\r\n",ip.TTLive);
fprintf(file,"协议=%s\r\n",getProtocol(ip.Protocol));
fprintf(file,"头校验和=0x%0x\r\n",ip.Chksum) ;
fprintf(file,"源IP地址=%s\r\n",inet_ntoa(*(in, _addr*)&ip. SrcAddr);
fprintf(file,"目的IP地址%s\r\n",inet _ntoa(*(in addr*)&ip. Dstddr);
fprintf(file,"___________________________________\r\n");
}
int main(int argc,char *argv[]){
if(argc!=2) {
cout<<end1<<"请以下格式输入命令行: PackParse packet_sum" <<endl;
return -1 ;
}
FILE *file;//以读写的方式建立一个文本文件logfile. txt
if((file=fopen (argv[1],"w+"))==NULL) {
printf("fail to open file %s", "logfile. txt");
return -1;
}
WSAData wsData; //启 动Socket,将Socket版本信息保存到wsData中
if (WSAStartup(MAKEWORD(2, 2),&wsData) !=0) {
printf("WSA startup failed!\n");
return -1;
}
fprintf(file, "Socket初始化... \r\n");
fprintf(file,"====================\r\n");
fprintf(file, "描述: %s\r\n",wsData.szDescription) ;
fprintf(file, "状态: %s\r\n", wsData.szSystemStatus) ;
fprintf(file,"====================\r\n");
SOCKET sock; // 创建原始套接字
if((sock=socket(AF_INET, S0CK_RAW,IPPROTO_IP))=INVALID_SOCKET) {
printf("Can not create socket!\n");
return- -1;
}
BOOL flag=true;
if (setsockopt (sock, IPPROTO_IP,IP_HDRINCL, (CHAR*)&flag, sizeof(flag))==SOCKET_ ERROR){ printf("setsockopt failed!\n");
return -1;
}
char hostName[128];
if (gethostname(hostName,100)==SOCKET_ERROR) {
printf("gethostname failed!\n");
return -1;
}
hostent *pHostIP;
if((pHost IP=gethostbyname (hostName))==NULL) {
printf (" gethostbyname failed!\n");
return -1;
}
printf("Hostname: %s\r\n" , pHostIP->h_ name) ;
printf(" IPAddress:%s\r\n", inet_ntoa (*((structin_ addr*) pHostIP->h_addr)));
sockaddr_ in addr_in;
addr_in.sin_addr=*(in_addr*) pHostIP->h_addr_list[0];
addr_in.sin_family=AF_INET;
addr_in.sin_port=htons(6000);
if(bind(sock,(PS0CKADDR)&addr_in,sizeof(addr_in))==SOCKET_ERROR) {
printf("bind failed");
return -1;
}
DWORD dwBufferLen[10];
DWORD dwBufferInLen=1;
DWORD dwBytesReturned=0;
if(WSAIoctl (sock,I0_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen),&dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL)==SOCKET_ERROR) {
printf("icotlsocket failed\n");
return -1;
}
char buffer[BUFFER_SIZE];
printf(*"==========开始解析=========\r\n");
while(true) {
int size=recv(sock,buffer,BUFFER_SIZE,0);
if(size>0) {
ippar(stdout,buffer) ;
ippar(file,buffer);
}
}
if (WSACleanup()==SOCKET_ERROR) {
printf ("WSACleanup failed!\n");
return -1;
}
fclose(file);
return 0;
}

在void ippar和int main这两段函数这里报错了

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-08-16 13:04
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 5月16日

悬赏问题

  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥35 关于shodan搜索网络摄像头的各种方法详解
  • ¥15 求PHDA糖尿病并发症数据集,有偿
  • ¥15 为什么AVL fire DVI 界面里面的response Editor project 中的Summary result 点不了
  • ¥20 中标麒麟系统V4.0,linux3.10.0的内核,3A4000处理器,如何安装英伟达或AMD的显卡驱动,
  • ¥20 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题
  • ¥15 python,pyopenGL的OBV渲染无法设置透明