Come posso inviare un datagramma con un rimorchio Ethernet? Se uso SocketType.Raw, dovrò inviare l'intero header IP e non ho idea di come farlo.Come posso inviare un datagramma con un trailer Ethernet?
risposta
Forse qualcosa del genere?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <linux/if_packet.h> #include <linux/if_ether.h> #include <linux/if_arp.h> #include <sys/ioctl.h> int s; unsigned char buffer[513]; struct sockaddr_ll socket_address; int main (void) { unsigned char seq; unsigned int ra; int length; struct ifreq ifr; s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (s == -1) { printf("error creating socket\n"); return(1); } memset(&ifr,0, sizeof(struct ifreq)); strncpy(ifr.ifr_name,"eth0",IFNAMSIZ); if(ioctl(s, SIOCGIFINDEX, &ifr) < 0) { perror("ioctl SIOCGIFINDEX"); exit(1); } printf("index %d\n",ifr.ifr_ifindex); printf("socket created\n"); memset(&socket_address,0,sizeof(socket_address)); socket_address.sll_family = PF_PACKET; socket_address.sll_protocol = htons(ETH_P_ALL); socket_address.sll_ifindex = ifr.ifr_ifindex; if (bind(s, (struct sockaddr *)(&socket_address), sizeof(socket_address)) < 0) { perror("bind error"); exit(1); } printf("bound\n"); length=27; memset(buffer,0,sizeof(buffer)); //destination buffer[ 0]=0xFF; buffer[ 1]=0xFF; buffer[ 2]=0xFF; buffer[ 3]=0xFF; buffer[ 4]=0xFF; buffer[ 5]=0xFF; //source buffer[ 6]=0x00; buffer[ 7]=0x19; buffer[ 8]=0xd1; buffer[ 9]=0x02; buffer[10]=0xdc; buffer[11]=0xb3; //length buffer[12]=((length-14)>>8)&0xFF; buffer[13]=((length-14)>>0)&0xFF; //payload buffer[14]=0x12; buffer[15]=0x34; for(ra=0;ra<20;ra++) { buffer[16]=ra; if(send(s,buffer,length,0) < 0) { printf("sendto failed\n"); break; } else { printf("sent\n"); } } close(s); return(1); }
Questo dovrebbe dare un pacchetto non elaborato che è possibile vedere su wireshark. se vuoi avere l'ip eader, o renderlo un udp o qualcosa del genere puoi usare questo metodo e costruire l'intestazione tu stesso (è banale guardare il file rfcs o semplicemente usare wireshark per guardare un mucchio di altri header del pacchetto) . Si noti che per udp non è necessario calcolare un checksum. 0x0000 è un checksum valido che deve passare.
Se tutto quello che vuoi è un pacchetto udp con zeri alla fine che è un po 'lo stesso, probabilmente più facile, fammi sapere.
Questo trailer viene utilizzato per il rilievo dei frame Ethernet alla loro lunghezza minima (46 byte di payload). Quindi invia un piccolo pacchetto UDP - minore di 18 byte (come IP + UDP è normalmente di 28 byte)
Puoi mostrare un esempio per favore? Ho provato a inviare un semplice pacchetto udp con 17 byte, lo mando come dati. –
anche un datagramma vuoto o un datagramma da 1 byte mostra questo comportamento? Quanti byte totali (ethernet + ip + udp) ha mostrato etereo? – nos
60 byte ....... –