this repo has no description
1#include "io_device_net.h"
2#include "cfutil.h"
3#include <cstring>
4#include <cstdlib>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <linux/if_arp.h>
8#include <util/debug.h>
9
10/*
11#define kIOInterfaceUnit "IOInterfaceUnit"
12#define kIOInterfaceType "IOInterfaceType"
13#define kIOMACAddress "IOMACAddress"
14#define kIOPrimaryInterface "IOPrimaryInterface"
15*/
16
17#define kIOEthernetAddressSize 6
18
19static CFTypeRef ConvertInterfaceType(io_device* d, CFAllocatorRef allocator);
20static CFTypeRef ConvertMACAddress(io_device* d, CFAllocatorRef allocator);
21
22static const property_mapping iface_properties[] = {
23 property_mapping{ CFSTR("IOMediaAddressLength"), nullptr, "addr_len", property_mapping::Number10 },
24 property_mapping{ CFSTR("IOInterfaceType"), ConvertInterfaceType },
25 property_mapping{ CFSTR("IOMaxTransferUnit"), nullptr, "mtu", property_mapping::Number10 },
26 property_mapping{ CFSTR("IOBuiltin"), [](io_device* d, CFAllocatorRef) -> CFTypeRef { return kCFBooleanFalse; } },
27 property_mapping{ CFSTR("IOPrimaryInterface"), [](io_device* d, CFAllocatorRef) -> CFTypeRef { return (strcmp(d->property("INTERFACE"), "eth0") == 0) ? kCFBooleanTrue : kCFBooleanFalse; } },
28};
29
30CFTypeRef io_device_net::property(CFStringRef name, CFAllocatorRef allocator)
31{
32 LOG << "io_device_net::property(): " << CFStringGetCStringPtr(name, kCFStringEncodingASCII) << std::endl;
33 CFTypeRef v = retrieve(iface_properties, sizeof(iface_properties) / sizeof(iface_properties[0]), name, allocator);
34 if (v)
35 return v;
36 else
37 return io_device::property(name, allocator);
38}
39
40void io_device_net::properties(CFMutableDictionaryRef dict, CFAllocatorRef allocator)
41{
42 io_device::properties(dict, allocator);
43 retrieveAll(dict, iface_properties, sizeof(iface_properties) / sizeof(iface_properties[0]), allocator);
44}
45
46static CFTypeRef ConvertInterfaceType(io_device* d, CFAllocatorRef allocator)
47{
48 int type = atoi(d->sysattr("type"));
49 int mtype = 0;
50
51 // This is not complete, but could suffice
52 switch (type)
53 {
54 case ARPHRD_ETHER:
55 case ARPHRD_IEEE80211: // linux/include/linux/if_arp.h
56 mtype = 6; // xnu-792/bsd/net/if_types.h
57 break;
58 case ARPHRD_LOOPBACK:
59 mtype = 0x18;
60 break;
61 case ARPHRD_PPP:
62 mtype = 0x17;
63 break;
64 case ARPHRD_ATM:
65 mtype = 0x25;
66 break;
67 case ARPHRD_IEEE1394:
68 mtype = 0x90;
69 break;
70 case ARPHRD_SLIP:
71 mtype = 0x1c;
72 break;
73 }
74
75 return intToCF(mtype, allocator);
76}
77
78io_device* io_device_net::parent()
79{
80 return new io_device_net_ctrl(udev_device_ref(m_device));
81}
82
83static const property_mapping ctrl_properties[] = {
84 property_mapping{ CFSTR("IOLinkSpeed"), nullptr, "speed", property_mapping::Number10 },
85 property_mapping{ CFSTR("IOLinkStatus"), [](io_device* d, CFAllocatorRef a) -> CFTypeRef { int act = atoi(d->sysattr("carrier")); return intToCF(act ? 3 : 1, a); } }, // if_media.h
86 property_mapping{ CFSTR("IOMACAddress"), ConvertMACAddress },
87};
88
89CFTypeRef io_device_net_ctrl::property(CFStringRef name, CFAllocatorRef allocator)
90{
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);
98}
99
100void io_device_net_ctrl::properties(CFMutableDictionaryRef dict, CFAllocatorRef allocator)
101{
102 io_device::properties(dict, allocator);
103}
104
105CFTypeRef ConvertMACAddress(io_device* d, CFAllocatorRef allocator)
106{
107 int len = atoi(d->sysattr("addr_len"));
108 const char* addr = d->sysattr("address");
109
110 if (!addr)
111 return nullptr;
112
113 UInt8* bytes = new UInt8[kIOEthernetAddressSize];
114 CFTypeRef rv;
115
116 memset(bytes, 0, kIOEthernetAddressSize);
117
118 for (int i = 0; i < std::min(len, kIOEthernetAddressSize); i++)
119 {
120 long byte = strtol(addr + i*3, nullptr, 16);
121 bytes[i] = UInt8(byte);
122 }
123
124 rv = CFDataCreate(allocator, bytes, kIOEthernetAddressSize);
125
126 delete [] bytes;
127 return rv;
128}
129