0

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?

fat
  • 6,435
  • 5
  • 44
  • 70
user1307697
  • 15
  • 1
  • 10
  • I think you might need to provide a bit more info and code here – mathematician1975 Jun 14 '12 at 10:57
  • @mathematician1975 i've edit! – user1307697 Jun 14 '12 at 11:11
  • Are u sure if sock2 is valid ? – Whoami Jun 14 '12 at 11:13
  • @whoami i don'see an error when i create the socket, and bind...why is the error? – user1307697 Jun 14 '12 at 11:19
  • Sometimes we may get this error, if the destination_address is not built properly. – Whoami Jun 14 '12 at 11:25
  • @Whoami but the destination address have all information that needed....i don't understand where is the error in my program! – user1307697 Jun 14 '12 at 11:37
  • Dont you need to listen and accept, or at least connect somewhere in there? – mathematician1975 Jun 14 '12 at 11:56
  • @mathematician1975 this isn't a communication client-server. I need to receive from port and redirect the packet to interface. I don't think thta listen and accept resolve my problem... – user1307697 Jun 14 '12 at 12:04
  • You call send() but you do not tell the socket where to send the data. You have to either: use sendto() or call connect() on sock2 to specify where to send the data. You call bind() with an argument named "destination_addr" , which does sound a bit strange, since bind() will be used as the source address on the socket when you send data.. – nos Jun 14 '12 at 12:33

1 Answers1

3

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)

Hasturkun
  • 35,395
  • 6
  • 71
  • 104