Advertisement
Guest User

Untitled

a guest
May 14th, 2012
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.12 KB | None | 0 0
  1. #include "client.h"
  2. #include <pcap.h>
  3. #include <pthread.h>
  4. #include <netinet/if_ether.h>
  5. #include <netinet/in.h>
  6. #include <netinet/tcp.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/types.h>    
  11. #include <sys/socket.h>
  12. #include <net/if.h>
  13. /*IOKIT HEADERS*/
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #include <IOKit/IOKitLib.h>
  16. #include <IOKit/network/IOEthernetInterface.h>
  17. #include <IOKit/network/IONetworkInterface.h>
  18. #include <IOKit/network/IOEthernetController.h>
  19. #include <IOKit/IOTypes.h>
  20.  
  21.  
  22. #define DEFAULT_PAYLOAD_SIZE MTU_-ETHER_HDR_LEN-((5*32)>>3)/*smallest ip header size*/-16
  23.  
  24. struct tx_session
  25. {
  26.     FILE *file;
  27.     union {
  28.         uint32_t ip;
  29.         char ip_c[4];
  30.     };
  31.     int port;
  32.     pthread_t thread;
  33.     char *head;
  34.     char *tail;
  35.     char window[SEND_WINDOW_SIZE];
  36. };
  37.  
  38. static union {uint32_t ip_n; char ip_c[4];} local_ip, local_ip_mask;
  39. static union {uint64_t eth_n; char eth_c[6];} local_eth;
  40. static char *device;
  41. static pcap_t *phandle;
  42. static struct bpf_program *bpf;
  43. unsigned char router_mac[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
  44.  
  45. static void autoset_device (void);
  46. static struct tx_session *tx_session_(const char *, const char *, int);
  47. static int transmit_t (struct tx_session *);
  48. static int send_arp (void);
  49. static void make_ether_h (uint64_t , uint64_t, u_char *, short);
  50.  
  51. int send_file_ (const char *fname, const char *destip, int port, char *capture_device)
  52. {
  53.     struct tx_session   *session = NULL;
  54.     char                errbuffer[PCAP_ERRBUF_SIZE];
  55.     device = NULL;
  56.     if (capture_device == NULL)
  57.         autoset_device();
  58.     else
  59.         device = capture_device;
  60.     if (device != NULL)
  61.     {
  62.         if ((bpf = malloc(sizeof(*bpf))) == NULL)
  63.             return -1;
  64.         phandle = pcap_open_live(device, MTU_, 0, TIMEOUT, errbuffer);
  65.     }
  66.     if ((session = tx_session_(fname,destip,port)) == NULL)
  67.         return -1;
  68.     if (pthread_create(&session->thread, NULL, (void *(*)())transmit_t, (void *)session) != 0)
  69.         return -1;
  70.     if (pthread_join(session->thread, NULL) != 0)
  71.         return -1;
  72.     return 0;
  73. }
  74.  
  75. static void autoset_device (void)
  76. {
  77.     struct ifreq    buffer;
  78.     pcap_if_t       *alldevs = NULL;
  79.     pcap_if_t       *iterator = NULL;
  80.     char            errbuffer[PCAP_ERRBUF_SIZE];
  81.     if (pcap_findalldevs(&alldevs, errbuffer) < 0) {
  82.         perror("ERROR ");
  83.         return;
  84.     }
  85.     if (alldevs == NULL) {
  86.         perror("ERROR");
  87.         return;
  88.     }
  89.     for (iterator = alldevs; iterator->next != NULL; iterator = iterator->next) {
  90.         if (pcap_lookupnet(iterator->name, (bpf_u_int32 *)&local_ip, (bpf_u_int32 *)&local_ip_mask, errbuffer) >= 0)
  91.             break;
  92.     }
  93.     if ((device = malloc(strlen(iterator->name)+1)) == NULL)
  94.         return;
  95.     strcpy (device,iterator->name);
  96.     device[strlen(iterator->name)] = '\0';
  97.     pcap_freealldevs(alldevs);
  98. }
  99.  
  100. static struct tx_session *tx_session_(const char *fname, const char *destip, int port)
  101. {
  102.     struct tx_session   *newtx = NULL;
  103.     char                ip[4];
  104.     if ((newtx = calloc(1,sizeof(*newtx))) == NULL)
  105.         return NULL;
  106.     if ((newtx->file = fopen(fname,"r")) == NULL) {
  107.         free (newtx);
  108.         return NULL;
  109.     }
  110.     newtx->head = newtx->tail = newtx->window;
  111.     if (sscanf(destip,"%d.%d.%d.%d",(int *)&newtx->ip_c[0],(int *)&newtx->ip_c[1],(int *)&newtx->ip_c[2],(int *)&newtx->ip_c[3]) < 4)
  112.         return NULL;
  113.     newtx->port = port;
  114.     return newtx;
  115. }
  116.  
  117. static int transmit_t (struct tx_session *session_s)
  118. {
  119.     char    *tx_buffer = NULL;
  120.     int     active = 1;
  121.     size_t  bread;
  122.     u_char  *buffer;
  123.     if ((tx_buffer = calloc(DEFAULT_PAYLOAD_SIZE,sizeof(*tx_buffer))) == NULL)
  124.         return -1;
  125.     if (*(uint64_t *)router_mac == 0xffffffffffff) {
  126.         buffer = malloc(sizeof(struct ether_header)+26/*sizeof arp header?*/);
  127.         //make_ether_h (uint64_t src, uint64_t dest, buffer, ETHERTYPE_ARP);
  128.     }
  129.     printf("%s\n",device);
  130.     while(active) {
  131.         bread = fread(tx_buffer, 1, DEFAULT_PAYLOAD_SIZE, session_s->file);
  132.         if (bread != DEFAULT_PAYLOAD_SIZE) {
  133.             if (feof(session_s->file) != 0) {
  134.                 active = 0;
  135.                 return 0;
  136.             }
  137.             else {
  138.                 free (tx_buffer);
  139.                 return -1;
  140.             }
  141.         }
  142.         printf("hey!\n");
  143.     }
  144. }
  145.  
  146. static void make_ether_h (uint64_t src, uint64_t dest, u_char *buffer, short type)
  147. {
  148.     struct ether_header *header = (struct ether_header *)buffer;
  149.     memcpy(header->ether_dhost, &dest, 6);
  150.     memcpy(header->ether_shost, &src, 6);
  151.     header->ether_type = type;
  152. }
  153.  
  154. /***************************PASTE OF IOKIT FUNCTIONS FROM EXAMPLE*****************************/
  155.  
  156. // Returns an iterator containing the primary (built-in) Ethernet interface. The caller is responsible for
  157. // releasing the iterator after the caller is done with it.
  158. static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices)
  159. {
  160.     kern_return_t           kernResult;
  161.     CFMutableDictionaryRef  matchingDict;
  162.     CFMutableDictionaryRef  propertyMatchDict;
  163.    
  164.     // Ethernet interfaces are instances of class kIOEthernetInterfaceClass.
  165.     // IOServiceMatching is a convenience function to create a dictionary with the key kIOProviderClassKey and
  166.     // the specified value.
  167.     matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);
  168.  
  169.     // Note that another option here would be:
  170.     // matchingDict = IOBSDMatching("en0");
  171.     // but en0: isn't necessarily the primary interface, especially on systems with multiple Ethernet ports.
  172.        
  173.     if (NULL == matchingDict) {
  174.         printf("IOServiceMatching returned a NULL dictionary.\n");
  175.     }
  176.     else {
  177.         // Each IONetworkInterface object has a Boolean property with the key kIOPrimaryInterface. Only the
  178.         // primary (built-in) interface has this property set to TRUE.
  179.        
  180.         // IOServiceGetMatchingServices uses the default matching criteria defined by IOService. This considers
  181.         // only the following properties plus any family-specific matching in this order of precedence
  182.         // (see IOService::passiveMatch):
  183.         //
  184.         // kIOProviderClassKey (IOServiceMatching)
  185.         // kIONameMatchKey (IOServiceNameMatching)
  186.         // kIOPropertyMatchKey
  187.         // kIOPathMatchKey
  188.         // kIOMatchedServiceCountKey
  189.         // family-specific matching
  190.         // kIOBSDNameKey (IOBSDNameMatching)
  191.         // kIOLocationMatchKey
  192.        
  193.         // The IONetworkingFamily does not define any family-specific matching. This means that in            
  194.         // order to have IOServiceGetMatchingServices consider the kIOPrimaryInterface property, we must
  195.         // add that property to a separate dictionary and then add that to our matching dictionary
  196.         // specifying kIOPropertyMatchKey.
  197.            
  198.         propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
  199.                                                       &kCFTypeDictionaryKeyCallBacks,
  200.                                                       &kCFTypeDictionaryValueCallBacks);
  201.    
  202.         if (NULL == propertyMatchDict) {
  203.             printf("CFDictionaryCreateMutable returned a NULL dictionary.\n");
  204.         }
  205.         else {
  206.             // Set the value in the dictionary of the property with the given key, or add the key
  207.             // to the dictionary if it doesn't exist. This call retains the value object passed in.
  208.             CFDictionarySetValue(propertyMatchDict, CFSTR(kIOPrimaryInterface), kCFBooleanTrue);
  209.            
  210.             // Now add the dictionary containing the matching value for kIOPrimaryInterface to our main
  211.             // matching dictionary. This call will retain propertyMatchDict, so we can release our reference
  212.             // on propertyMatchDict after adding it to matchingDict.
  213.             CFDictionarySetValue(matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
  214.             CFRelease(propertyMatchDict);
  215.         }
  216.     }
  217.    
  218.     // IOServiceGetMatchingServices retains the returned iterator, so release the iterator when we're done with it.
  219.     // IOServiceGetMatchingServices also consumes a reference on the matching dictionary so we don't need to release
  220.     // the dictionary explicitly.
  221.     kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, matchingServices);    
  222.     if (KERN_SUCCESS != kernResult) {
  223.         printf("IOServiceGetMatchingServices returned 0x%08x\n", kernResult);
  224.     }
  225.        
  226.     return kernResult;
  227. }
  228.    
  229. // Given an iterator across a set of Ethernet interfaces, return the MAC address of the last one.
  230. // If no interfaces are found the MAC address is set to an empty string.
  231. // In this sample the iterator should contain just the primary interface.
  232. static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress, UInt8 bufferSize)
  233. {
  234.     io_object_t     intfService;
  235.     io_object_t     controllerService;
  236.     kern_return_t   kernResult = KERN_FAILURE;
  237.    
  238.     // Make sure the caller provided enough buffer space. Protect against buffer overflow problems.
  239.     if (bufferSize < kIOEthernetAddressSize) {
  240.         return kernResult;
  241.     }
  242.    
  243.     // Initialize the returned address
  244.     bzero(MACAddress, bufferSize);
  245.    
  246.     // IOIteratorNext retains the returned object, so release it when we're done with it.
  247.     while ((intfService = IOIteratorNext(intfIterator)))
  248.     {
  249.         CFTypeRef   MACAddressAsCFData;        
  250.  
  251.         // IONetworkControllers can't be found directly by the IOServiceGetMatchingServices call,
  252.         // since they are hardware nubs and do not participate in driver matching. In other words,
  253.         // registerService() is never called on them. So we've found the IONetworkInterface and will
  254.         // get its parent controller by asking for it specifically.
  255.        
  256.         // IORegistryEntryGetParentEntry retains the returned object, so release it when we're done with it.
  257.         kernResult = IORegistryEntryGetParentEntry(intfService,
  258.                                                    kIOServicePlane,
  259.                                                    &controllerService);
  260.        
  261.         if (KERN_SUCCESS != kernResult) {
  262.             printf("IORegistryEntryGetParentEntry returned 0x%08x\n", kernResult);
  263.         }
  264.         else {
  265.             // Retrieve the MAC address property from the I/O Registry in the form of a CFData
  266.             MACAddressAsCFData = IORegistryEntryCreateCFProperty(controllerService,
  267.                                                                  CFSTR(kIOMACAddress),
  268.                                                                  kCFAllocatorDefault,
  269.                                                                  0);
  270.             if (MACAddressAsCFData) {
  271.                 CFShow(MACAddressAsCFData); // for display purposes only; output goes to stderr
  272.                
  273.                 // Get the raw bytes of the MAC address from the CFData
  274.                 CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
  275.                 CFRelease(MACAddressAsCFData);
  276.             }
  277.                
  278.             // Done with the parent Ethernet controller object so we release it.
  279.             (void) IOObjectRelease(controllerService);
  280.         }
  281.        
  282.         // Done with the Ethernet interface object so we release it.
  283.         (void) IOObjectRelease(intfService);
  284.     }
  285.        
  286.     return kernResult;
  287. }
  288.  
  289. int main (void)
  290. {
  291.     if (send_file ("../hash.c", "127.0.0.1", 9001) < 0)
  292.         printf("derp err\n");
  293.     return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement