this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Issue #10

+89 -31
+16 -12
src/IOKit/io_device.cpp
··· 1 1 #include "io_device.h" 2 2 #include <cstdlib> 3 3 #include <cstring> 4 + #include "io_device_net.h" 4 5 #include "cfutil.h" 6 + #include <util/debug.h> 5 7 6 8 static CFTypeRef ListDevSymlinks(io_device* d, CFAllocatorRef a); 7 9 ··· 17 19 io_device::io_device(struct udev_device* dev) 18 20 : m_device(dev) 19 21 { 20 - m_properties = udev_device_get_properties_list_entry(m_device); 21 - m_sysattrs = udev_device_get_sysattr_list_entry(m_device); 22 22 } 23 23 24 24 io_device::~io_device() ··· 26 26 udev_device_unref(m_device); 27 27 } 28 28 29 + io_device* io_device::create(udev_device* dev) 30 + { 31 + const char* subsystem = udev_device_get_subsystem(dev); 32 + 33 + LOG << "Creating an io_device for subsystem " << subsystem << std::endl; 34 + 35 + if (strcmp(subsystem, "net") == 0) 36 + return new io_device_net(dev); 37 + else 38 + return new io_device(dev); // generic device 39 + } 40 + 29 41 bool io_device::operator==(const io_object& that) 30 42 { 31 43 const io_device* dthat = dynamic_cast<const io_device*>(&that); ··· 37 49 38 50 const char* io_device::property(const char* name) 39 51 { 40 - struct udev_list_entry* p = udev_list_entry_get_by_name(m_properties, name); 41 - if (p != nullptr) 42 - return udev_list_entry_get_value(p); 43 - else 44 - return nullptr; 52 + return udev_device_get_property_value(m_device, name); 45 53 } 46 54 47 55 const char* io_device::sysattr(const char* name) 48 56 { 49 - struct udev_list_entry* p = udev_list_entry_get_by_name(m_sysattrs, name); 50 - if (p != nullptr) 51 - return udev_list_entry_get_value(p); 52 - else 53 - return nullptr; 57 + return udev_device_get_sysattr_value(m_device, name); 54 58 } 55 59 56 60 CFStringRef io_device::sysattrStr(const char* name, CFAllocatorRef allocator)
+2 -1
src/IOKit/io_device.h
··· 15 15 io_device(struct udev_device* device); 16 16 virtual ~io_device(); 17 17 18 + static io_device* create(udev_device* dev); 19 + 18 20 virtual void properties(CFMutableDictionaryRef dict, CFAllocatorRef allocator); 19 21 virtual CFTypeRef property(CFStringRef name, CFAllocatorRef allocator); 20 22 virtual io_device* parent(); ··· 32 34 void retrieveAll(CFMutableDictionaryRef dict, const property_mapping* mapping, size_t count, CFAllocatorRef allocator); 33 35 protected: 34 36 struct udev_device* m_device; 35 - struct udev_list_entry *m_properties, *m_sysattrs; 36 37 }; 37 38 38 39 struct property_mapping
+2 -2
src/IOKit/io_device_iterator.cpp
··· 6 6 io_device_iterator::io_device_iterator(udev_enumerate* uenum) 7 7 : m_uenum(uenum) 8 8 { 9 - m_udev = udev_new(); 9 + m_udev = udev_enumerate_get_udev(m_uenum); 10 10 reset(); 11 11 } 12 12 ··· 36 36 { 37 37 udev_device* dev = udev_device_new_from_syspath(m_udev, udev_list_entry_get_name(m_next)); 38 38 m_next = udev_list_entry_get_next(m_next); 39 - return new io_device(dev); 39 + return io_device::create(dev); 40 40 } 41 41 }
+16 -4
src/IOKit/io_device_net.cpp
··· 5 5 #include <sys/types.h> 6 6 #include <sys/socket.h> 7 7 #include <linux/if_arp.h> 8 + #include <util/debug.h> 8 9 9 10 /* 10 11 #define kIOInterfaceUnit "IOInterfaceUnit" ··· 12 13 #define kIOMACAddress "IOMACAddress" 13 14 #define kIOPrimaryInterface "IOPrimaryInterface" 14 15 */ 16 + 17 + #define kIOEthernetAddressSize 6 15 18 16 19 static CFTypeRef ConvertInterfaceType(io_device* d, CFAllocatorRef allocator); 17 20 static CFTypeRef ConvertMACAddress(io_device* d, CFAllocatorRef allocator); ··· 26 29 27 30 CFTypeRef io_device_net::property(CFStringRef name, CFAllocatorRef allocator) 28 31 { 32 + LOG << "io_device_net::property(): " << CFStringGetCStringPtr(name, kCFStringEncodingASCII) << std::endl; 29 33 CFTypeRef v = retrieve(iface_properties, sizeof(iface_properties) / sizeof(iface_properties[0]), name, allocator); 30 34 if (v) 31 35 return v; ··· 84 88 85 89 CFTypeRef io_device_net_ctrl::property(CFStringRef name, CFAllocatorRef allocator) 86 90 { 87 - return io_device::property(name, allocator); 91 + LOG << "io_device_net_ctrl::property(): " << CFStringGetCStringPtr(name, kCFStringEncodingASCII) << std::endl; 92 + 93 + CFTypeRef v = retrieve(ctrl_properties, sizeof(ctrl_properties) / sizeof(ctrl_properties[0]), name, allocator); 94 + if (v) 95 + return v; 96 + else 97 + return io_device::property(name, allocator); 88 98 } 89 99 90 100 void io_device_net_ctrl::properties(CFMutableDictionaryRef dict, CFAllocatorRef allocator) ··· 100 110 if (!addr) 101 111 return nullptr; 102 112 103 - UInt8* bytes = new UInt8[len]; 113 + UInt8* bytes = new UInt8[kIOEthernetAddressSize]; 104 114 CFTypeRef rv; 105 115 106 - for (int i = 0; i < len; i++) 116 + memset(bytes, 0, kIOEthernetAddressSize); 117 + 118 + for (int i = 0; i < std::min(len, kIOEthernetAddressSize); i++) 107 119 { 108 120 long byte = strtol(addr + i*3, nullptr, 16); 109 121 bytes[i] = UInt8(byte); 110 122 } 111 123 112 - rv = CFDataCreate(allocator, bytes, len); 124 + rv = CFDataCreate(allocator, bytes, kIOEthernetAddressSize); 113 125 114 126 delete [] bytes; 115 127 return rv;
+12 -2
src/IOKit/io_object.cpp
··· 28 28 29 29 int IOObjectRelease(io_object_t obj) 30 30 { 31 - obj->release(); 31 + if (obj) 32 + obj->release(); 32 33 return 0; 33 34 } 34 35 35 36 uint32_t IOObjectGetRetainCount(io_object_t obj) 36 37 { 38 + if (!obj) 39 + return 0; 37 40 return obj->refs(); 38 41 } 39 42 40 43 uint32_t IOObjectGetUserRetainCount(io_object_t obj) 41 44 { 45 + if (!obj) 46 + return 0; 42 47 return obj->refs(); 43 48 } 44 49 45 50 bool IOObjectIsEqualTo(io_object_t o1, io_object_t o2) 46 51 { 47 - return *o1 == *o2; 52 + if (o1 == o2) 53 + return true; 54 + if (!o1 || !o2) 55 + return false; 56 + else 57 + return *o1 == *o2; 48 58 }
+41 -10
src/IOKit/service.mm
··· 7 7 #include <stdlib.h> 8 8 #include <list> 9 9 #include <string> 10 + #include <fstream> 10 11 #include <sys/types.h> 11 12 #include <dirent.h> 13 + #include <cstdlib> 12 14 #include "../util/stlutils.h" 13 15 #include "../util/log.h" 14 16 #include "io_device.h" ··· 55 57 56 58 int IOServiceGetMatchingServices(void* port, CFDictionaryRef rules, io_device_iterator_t* iter) 57 59 { 58 - size_t size = CFDictionaryGetCount(rules); 59 - CFStringRef* keys = new CFStringRef[size]; 60 - CFDictionaryGetKeysAndValues(rules, (const void **) keys, nullptr); 60 + //size_t size = CFDictionaryGetCount(rules); 61 + //std::unique_ptr<CFStringRef[]> keys (new CFStringRef[size]); 62 + //CFDictionaryGetKeysAndValues(rules, (const void **) keys, nullptr); 61 63 struct udev* udev; 62 64 struct udev_enumerate* uenum; 63 65 struct udev_list_entry* list; ··· 74 76 75 77 if ((value = CFDictionaryGetValue(rules, CFSTR(kIOPropertyMatchKey)))) 76 78 filterByProperties(uenum, (CFDictionaryRef) value); 77 - 78 - delete [] keys; 79 - udev_unref(udev); 80 79 81 80 udev_enumerate_scan_devices(uenum); 82 81 *iter = new io_device_iterator(uenum); ··· 167 166 udev_enumerate_add_match_property(uenum, "driver", driver); 168 167 } 169 168 169 + static std::string detectPrimaryInterface() 170 + { 171 + std::ifstream routes; 172 + 173 + if (const char* iface = getenv("IOKIT_PRIMARY_IFACE")) 174 + return iface; 175 + 176 + routes.open("/proc/net/route"); 177 + 178 + if (routes.is_open()) 179 + { 180 + std::string ifaceName, destination; 181 + 182 + while (!routes.eof()) 183 + { 184 + routes >> ifaceName >> destination; 185 + 186 + if (destination == "00000000") 187 + { 188 + LOG << "Primary network interface detected to be " << ifaceName << std::endl; 189 + return ifaceName; 190 + } 191 + 192 + std::getline(routes, ifaceName); // drop the rest of the line 193 + } 194 + } 195 + 196 + return std::string(); 197 + } 198 + 170 199 void filterByProperties(struct udev_enumerate* uenum, CFDictionaryRef dict) 171 200 { 172 201 size_t size = CFDictionaryGetCount(dict); ··· 176 205 for (size_t i = 0; i < size; i++) 177 206 { 178 207 const void* value = CFDictionaryGetValue(dict, keys[i]); 179 - const char* svalue = nullptr; 180 208 const char* key = nullptr; 181 209 CFStringRef str = keys[i]; 210 + std::string svalue; 182 211 183 212 if (strCFEqual(str, "VendorID")) 184 213 { ··· 192 221 } 193 222 else if (strCFEqual(str, "IOPrimaryInterface")) 194 223 { 195 - key = "INTERFACE"; 196 - svalue = "eth0"; 224 + svalue = detectPrimaryInterface(); 225 + if (!svalue.empty()) 226 + key = "INTERFACE"; 227 + //svalue = "eth0"; 197 228 } 198 229 199 230 if (key != nullptr) 200 - udev_enumerate_add_match_property(uenum, key, svalue); 231 + udev_enumerate_add_match_property(uenum, key, svalue.c_str()); 201 232 else 202 233 LOG << "Ignoring property " << [(NSString*) str UTF8String] << " in IOKit filterByProperties\n"; 203 234 }