我对这行代码有疑问。我必须将一个数据包带到一个端口并重新发送到接口(例如:eth0)。我的程序成功地从端口获取数据包,但是当我重新发送(使用send()
)到接口时出现错误:
发送:无效参数
代码行是:
sock1=socket(AF_INET6,SOCK_DGRAM,0);
sock2=socket(AF_INET6,SOCK_DGRAM,0);
fd=fopen("port.txt","r+");
if(fd) {
while(!feof(fd)){
fscanf(fd,"%d",&port);
fscanf(fd, "%s",interface);
}
}
memset(&source_addr,0,sizeof(struct sockaddr_in6));
source_addr.sin6_family=AF_INET6;
source_addr.sin6_port=htons(port);
source_addr.sin6_addr=in6addr_any;
if(bind(sock1,(struct sockaddr*)&source_addr,sizeof(struct sockaddr_in6))==-1){perror("bind");}
//if(connect(sock1,(struct sockaddr*)&source_addr,sizeof(struct sockaddr_in6))==-1){perror("connect");}
//Device dove inviare
memset(&freq,0,sizeof(struct ifreq));
strncpy(freq.ifr_name,interface,IFNAMSIZ);
if(ioctl(sock2,SIOCGIFINDEX,&freq)==-1){perror("ioctl");}
memset(&destination_addr,0,sizeof(struct sockaddr_in6));
destination_addr.sin6_family=AF_INET6:
destination_addr.sin6_scope_id=htonl(2)
inet_pton(AF_INET6,"2001::620:40b:555:110",(void*)&destination_addr.sin6_addr.s6_addr);
if(bind(sock2,(struct sockaddr*)&destination_addr,sizeof(struct sockaddr_in6)==-1)
{perror("bind");}
if((buff=malloc(BUFFER_LENGTH))==NULL){ perror("malloc");}
packet_length=recv(sock1,buff,BUFFER_LENGTH,0);
if(packet_length<0){perror("recv");}
printf("La lunghezza è %d\n",packet_length);
packet=(unsigned char*)buff;
Sniffer(packet,packet_length,' ');
packet_length2=send(sock2,buff,BUFFER_LENGTH,0);
buff是我从端口获取的数据包!错误在哪里?