ungureanuvladvictor

bootp.c

Jun 11th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libusb.h>
  5. #include <syslog.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8.  
  9. #include "bootp.h"
  10.  
  11. static const u_int8_t gateway[4] = { 192, 168, 0, 1 };
  12. static const u_int8_t your_ip[4] = { 192, 168, 0, 2 };
  13. static const u_int8_t BBB_ip[4] = { 192, 168, 0, 3 };
  14. static const u_int8_t BBB_hwaddr[6] = {12, 13 ,14 ,15 ,16, 17};
  15. static const u_int8_t magic_cookie[4] = { 99, 130, 83, 99 };
  16.  
  17. static const char servername[3] = {'v', 'v', 'u'};
  18. static const char filename[3] = {'M', 'L', 'O'};
  19.  
  20. static const u_int8_t vendor_stuff[] = {
  21.     BOOTP_OPTION_NETMASK,  4, 255, 255, 255, 0,
  22.     BOOTP_OPTION_GATEWAY,  4, 192, 168, 200, 31,
  23.     BOOTP_OPTION_DNS,      8, 198, 235, 216, 110,   204, 101, 251, 1,
  24.     BOOTP_OPTION_HOSTNAME, 3, 'v', 'v', 'u', 'h',
  25.     BOOTP_OPTION_DOMAIN,  7, 'B', 'B', 'B', 'b', 'o', 'o', 't'
  26. };
  27.  
  28. int setup_bootp_packet(const char *servername, const char *filename, bootp_packet *bpp) {
  29.     int vendorlen;
  30.  
  31.     memset(bpp, 0, sizeof(*bpp));
  32.     bpp->opcode = 2;
  33.     bpp->hw = 1;
  34.     bpp->hwlength = 6;
  35.     bpp->hopcount = 255;
  36.                          
  37.     bpp->xid = htonl(0);
  38.     bpp->secs = htons(0);
  39.     bpp->flags = htons(0);
  40.    
  41.     memset(&bpp->ciaddr, 0, 4);
  42.     memcpy(&bpp->hwaddr, &BBB_hwaddr, 6);
  43.     memcpy(&bpp->yiaddr, &your_ip, 4);
  44.     memcpy(&bpp->server_ip, &BBB_ip, 4);
  45.     memcpy(&bpp->bootp_gw_ip, &gateway, 4);
  46.    
  47.     strncpy((char *)&bpp->servername, servername, sizeof(bpp->servername));
  48.     strncpy((char *)&bpp->bootfile, filename, sizeof(bpp->bootfile));
  49.  
  50.     memcpy(&bpp->vendor, &magic_cookie, 4);
  51.    
  52.     vendorlen = 4;
  53.     memcpy(((u_int8_t *)&bpp->vendor) + 4, &vendor_stuff,
  54.             sizeof(vendor_stuff));
  55.     vendorlen += sizeof(vendor_stuff);
  56.    
  57.     bpp->vendor[vendorlen++] = 0xFF;
  58.     return vendorlen;
  59. }
  60.  
  61. int main(int argc, const char * argv[]) {
  62.     bootp_packet *breq = (bootp_packet *)malloc(sizeof(bootp_packet));
  63.     setup_bootp_packet(servername, filename, breq);
  64.    
  65.     /*unsigned char* address = (unsigned char*)&breq;
  66.     unsigned char *data = (unsigned char*)malloc(64*sizeof(unsigned char));
  67.     int actual;
  68.    
  69.  
  70.     libusb_device **devs;
  71.     libusb_device_handle *dev_handle;
  72.     libusb_context *ctx = NULL;
  73.  
  74.     int r;
  75.     ssize_t cnt;
  76.     r = libusb_init(&ctx);
  77.     if(r < 0) {
  78.         printf("Init error!\n");
  79.         return 1;
  80.     }
  81.     libusb_set_debug(ctx, 3);
  82.     cnt = libusb_get_device_list(ctx, &devs);
  83.     if(cnt < 0) {
  84.         printf("Cannot get device list!\n");
  85.         return 1;
  86.     }
  87.     printf("%zd devices in list!\n",cnt);
  88.  
  89.     dev_handle = libusb_open_device_with_vid_pid(ctx, 0x0451, 0x6141);
  90.     if(dev_handle == NULL) {
  91.         printf("Cannot open device!\n");
  92.         return 1;
  93.     }
  94.     else printf("Device opened!\n");
  95.  
  96.     libusb_free_device_list(devs, 1);
  97.     if(libusb_kernel_driver_active(dev_handle, 0) == 1) {
  98.         printf("Kernel has driver!\n");
  99.         if(libusb_detach_kernel_driver(dev_handle, 0) == 0)
  100.             printf("Kernel deattached!\n");
  101.     }
  102.     r = libusb_claim_interface(dev_handle, 0);
  103.     if(r < 0) {
  104.         printf("Cannot Claim Interface!\n");
  105.         return 1;
  106.     }
  107.     printf("Claimed interface!\n");
  108.  
  109.     r = libusb_bulk_transfer(dev_handle, (129 | LIBUSB_ENDPOINT_IN), data, 64, &actual, 0);
  110.     r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), address, sizeof(breq), &actual, 0);
  111.    
  112.     printf("%ld...%d\n",sizeof(breq),actual );
  113.    
  114.     r = libusb_release_interface(dev_handle, 0);
  115.     if(r!=0) {
  116.         printf("Cannot release interface!\n");
  117.         return 1;
  118.     }
  119.     libusb_close(dev_handle);
  120.     libusb_exit(ctx);
  121.     free(data);*/
  122.  
  123.     debug_bootp_packet(breq, sizeof(breq));
  124.     free(breq);
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment