发送:无效的参数

问题描述:

我有这行代码的问题。我必须拿一个数据包到一个端口并重新发送到接口(例如: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是我从港口拿的包!错误在哪里?

+0

我想你可能需要提供更多的信息和代码在这里 – mathematician1975

+0

@ mathematician1975我的编辑! – user1307697

+0

你确定sock2是否有效? – Whoami

您没有在您的UDP套接字上调用connect(),所以它没有默认目的地(将被send()使用)。要么拨打connect()来设置默认目的地,要么使用带有明确目的地的sendto

你也应该不会比你实际收到派遣更多的(即packet_length

+0

connect是一条评论...我didn不叫它.... – user1307697