0

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

4

1 回答 1

3

您没有调用connect()您的 UDP 套接字,因此它没有默认目标(供 使用send())。要么调用connect()设置默认目的地,要么sendto与显式目的地一起使用。

你也不应该发送超过你实际收到的(即。packet_length

于 2012-06-14T12:30:53.623 回答