From: Leandro Lucarella Date: Mon, 28 Nov 2005 03:51:36 +0000 (+0000) Subject: Ejemplo básico de uso de raw sockets que manda un frame saludador por UDP. X-Git-Tag: 0.1-recibe-matriz-raw-por-udp~45 X-Git-Url: https://git.llucax.com/z.facultad/66.09/etherled.git/commitdiff_plain/e18ee2575f49d0dc428d117318ebd1a3c2c38855?ds=inline Ejemplo básico de uso de raw sockets que manda un frame saludador por UDP. --- diff --git a/pruebas/c/net_ethernet_packet.c b/pruebas/c/net_ethernet_packet.c new file mode 100644 index 0000000..74cd05f --- /dev/null +++ b/pruebas/c/net_ethernet_packet.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); + struct sockaddr_ll sll; + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, "eth0", sizeof(ifr)); + if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) + { + perror("ioctl()"); + return 1; + } + memset(&sll, 0, sizeof(struct sockaddr_ll)); + sll.sll_family = AF_PACKET; + sll.sll_halen = 6; + // Aparentemente no es necesario + //sll.sll_addr[0] = 0x00; + //sll.sll_addr[1] = 0x80; + //sll.sll_addr[2] = 0xc7; + //sll.sll_addr[3] = 0x42; + //sll.sll_addr[4] = 0x8d; + //sll.sll_addr[5] = 0x27; + sll.sll_ifindex = ifr.ifr_ifindex; + sll.sll_pkttype = PACKET_HOST; // nada de recibir broadcasts, etc. + unsigned char buf[] = // Mi supuesto frame + { + // Ethernet + 0x00, 0xd0, 0x09, 0xac, 0x32, 0xe0, // MAC destino + 0x00, 0x0c, 0x6e, 0x37, 0x19, 0xbf, // MAC origen + 0x08, 0x00, // Type IP + // IP + 0x45, // Version 4, Header length 5 (5*4 = 20) + 0x00, // TOS + 0x00, 0x21, // Total length 33 + 0x12, 0x34, // ID + 0x40, 0x00, // Fragmentación: Don't Fragment / Offset 0 + 0x40, // TTL 64 (porque parece que le gusta a la gente) + 0x11, // Protocol UDP + 0x00, 0x82, // Checsum + 0x0a, 0x0a, 0x0a, 0x02, // Source 10.10.10.2 + 0x0a, 0x0a, 0x0a, 0x01, // Destination 10.10.10.1 + // UDP + 0x81, 0x0d, // Src Port 33037 + 0x0b, 0x54, // Dst Port 2900 + 0x00, 0x0d, // Length 13 + 0x76, 0x8b, // Checksum + // Datos + 0x68, 0x6f, 0x6c, 0x61, 0x00 // "hola" + }; + // Trato de mandar... + if (sendto(s, buf, sizeof(buf), 0, (struct sockaddr*) &sll, + sizeof(struct sockaddr_ll)) < 0) + { + perror("send()"); + return 1; + } + close(s); + return 0; +} +