Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <linux/if_packet.h>
- #include <linux/if_ether.h>
- #include <linux/if_arp.h>
- #include <errno.h>
- int main()
- {
- int s; /*socketdescriptor */
- char *opt;
- opt = "wlan0";
- s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
- if (s == -1) {
- printf("error creating socket\n");
- exit(-1);
- }
- setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, opt, 5);
- //#define ETH_FRAME_LEN 1518
- /*target address*/
- struct sockaddr_ll socket_address;
- /*buffer for ethernet frame*/
- void *buffer = (void *)malloc(ETH_FRAME_LEN);
- /*pointer to ethenet header*/
- unsigned char *etherhead = buffer;
- /*userdata in ethernet frame*/
- unsigned char *data = buffer + 14;
- /*another pointer to ethernet header*/
- struct ethhdr *eh = (struct ethhdr *)etherhead;
- int send_result = 0;
- /*our MAC address*/
- unsigned char src_mac[6] = { 0x00, 0x26, 0xc7, 0x0c, 0xc4, 0x8e };
- /*other host MAC address*/
- unsigned char dest_mac[6] = { 0x00, 0x1d, 0x60, 0x66, 0x6d, 0x12 };
- /*prepare sockaddr_ll*/
- /*RAW communication*/
- socket_address.sll_family = PF_PACKET;
- /*we don't use a protocoll above ethernet layer
- ->just use anything here*/
- socket_address.sll_protocol = htons(ETH_P_IP);
- /*index of the network device
- see full code later how to retrieve it*/
- socket_address.sll_ifindex = 2;
- /*ARP hardware identifier is ethernet*/
- socket_address.sll_hatype = ARPHRD_ETHER;
- /*target is another host*/
- socket_address.sll_pkttype = PACKET_OTHERHOST;
- /*address length*/
- socket_address.sll_halen = ETH_ALEN;
- /*MAC - begin*/
- socket_address.sll_addr[0] = dest_mac[0];
- socket_address.sll_addr[1] = dest_mac[1];
- socket_address.sll_addr[2] = dest_mac[2];
- socket_address.sll_addr[3] = dest_mac[3];
- socket_address.sll_addr[4] = dest_mac[4];
- socket_address.sll_addr[5] = dest_mac[5];
- /*MAC - end*/
- socket_address.sll_addr[6] = 0x00; /*not used */
- socket_address.sll_addr[7] = 0x00; /*not used */
- /*set the frame header*/
- memcpy((void *)buffer, (void *)dest_mac, ETH_ALEN);
- memcpy((void *)(buffer + ETH_ALEN), (void *)src_mac, ETH_ALEN);
- eh->h_proto = 0x00;
- /*fill the frame with some data*/
- int j;
- for (j = 0; j < 1500; j++) {
- data[j] =
- (unsigned char)((int)(255.0 * rand() / (RAND_MAX + 1.0)));
- }
- /*send the packet*/
- send_result =
- sendto(s, buffer, ETH_FRAME_LEN, 0,
- (struct sockaddr *)&socket_address, sizeof(socket_address));
- if (send_result == -1) {
- printf("sendto: %s (%d)\n", strerror(errno), errno);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment