发送:无效参数

时间:2012-06-14 10:50:50

标签: c sockets send packet sendto

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

1 个答案:

答案 0 :(得分:3)

您没有在UDP套接字上调用connect(),因此它没有默认目标(由send()使用)。要么调用connect()来设置默认目标,要么使用sendto和显式目的地。

您也不应该发送超过实际收到的数据(即packet_length