Guest User

rawether.c

a guest
Sep 6th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <linux/if_packet.h>
  6. #include <linux/if_ether.h>
  7. #include <linux/if_arp.h>
  8. #include <errno.h>
  9.  
  10. int main()
  11. {
  12.     int s;          /*socketdescriptor */
  13.     char *opt;
  14.     opt = "wlan0";
  15.  
  16.     s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  17.     if (s == -1) {
  18.         printf("error creating socket\n");
  19.         exit(-1);
  20.     }  
  21.     setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, opt, 5);
  22. //#define ETH_FRAME_LEN 1518
  23.  
  24. /*target address*/
  25.     struct sockaddr_ll socket_address;
  26.  
  27. /*buffer for ethernet frame*/
  28.     void *buffer = (void *)malloc(ETH_FRAME_LEN);
  29.  
  30. /*pointer to ethenet header*/
  31.     unsigned char *etherhead = buffer;
  32.  
  33. /*userdata in ethernet frame*/
  34.     unsigned char *data = buffer + 14;
  35.  
  36. /*another pointer to ethernet header*/
  37.     struct ethhdr *eh = (struct ethhdr *)etherhead;
  38.  
  39.     int send_result = 0;
  40.  
  41. /*our MAC address*/
  42.     unsigned char src_mac[6] = { 0x00, 0x26, 0xc7, 0x0c, 0xc4, 0x8e };
  43.  
  44. /*other host MAC address*/
  45.     unsigned char dest_mac[6] = { 0x00, 0x1d, 0x60, 0x66, 0x6d, 0x12 };
  46.  
  47. /*prepare sockaddr_ll*/
  48.  
  49. /*RAW communication*/
  50.     socket_address.sll_family = PF_PACKET;
  51. /*we don't use a protocoll above ethernet layer
  52.   ->just use anything here*/
  53.     socket_address.sll_protocol = htons(ETH_P_IP);
  54.  
  55. /*index of the network device
  56. see full code later how to retrieve it*/
  57.     socket_address.sll_ifindex = 2;
  58.  
  59. /*ARP hardware identifier is ethernet*/
  60.     socket_address.sll_hatype = ARPHRD_ETHER;
  61.  
  62. /*target is another host*/
  63.     socket_address.sll_pkttype = PACKET_OTHERHOST;
  64.  
  65. /*address length*/
  66.     socket_address.sll_halen = ETH_ALEN;
  67. /*MAC - begin*/
  68.     socket_address.sll_addr[0] = dest_mac[0];
  69.     socket_address.sll_addr[1] = dest_mac[1];
  70.     socket_address.sll_addr[2] = dest_mac[2];
  71.  
  72.     socket_address.sll_addr[3] = dest_mac[3];
  73.     socket_address.sll_addr[4] = dest_mac[4];
  74.     socket_address.sll_addr[5] = dest_mac[5];
  75. /*MAC - end*/
  76.     socket_address.sll_addr[6] = 0x00;  /*not used */
  77.     socket_address.sll_addr[7] = 0x00;  /*not used */
  78.  
  79. /*set the frame header*/
  80.     memcpy((void *)buffer, (void *)dest_mac, ETH_ALEN);
  81.     memcpy((void *)(buffer + ETH_ALEN), (void *)src_mac, ETH_ALEN);
  82.     eh->h_proto = 0x00;
  83. /*fill the frame with some data*/
  84.     int j;
  85.     for (j = 0; j < 1500; j++) {
  86.         data[j] =
  87.             (unsigned char)((int)(255.0 * rand() / (RAND_MAX + 1.0)));
  88.     }
  89.  
  90. /*send the packet*/
  91.     send_result =
  92.         sendto(s, buffer, ETH_FRAME_LEN, 0,
  93.            (struct sockaddr *)&socket_address, sizeof(socket_address));
  94.     if (send_result == -1) {
  95.         printf("sendto: %s (%d)\n", strerror(errno), errno);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment