相关文章推荐
怕考试的仙人球  ·  Using await version ...·  1 年前    · 
大气的手电筒  ·  spring ...·  1 年前    · 
发财的青椒  ·  sql server if判断-掘金·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a problem with this line of code. I must take a packet to a port and resend to interface(ex:eth0). My program successfully take packet from the port but when I resend (with send() ) to interface I have an error:
send:Invalid argument

The line of code is:

    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 is packet that I take from port! Where is the error?

You did not call connect() on your UDP socket, so it doesn't have a default destination (to be used by send()). either call connect() to set the default destination, or use sendto with the explicit destination.

You should also not be sending more than you actually received (ie. packet_length)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.