Skip to content

Instantly share code, notes, and snippets.

@banjun
Last active December 15, 2015 19:19
Show Gist options
  • Save banjun/5310157 to your computer and use it in GitHub Desktop.
Save banjun/5310157 to your computer and use it in GitHub Desktop.
[iOS][iPhone] basic ifconfig view for Master-Detail project template
#import "BJMasterViewController.h"
#import "BJDetailViewController.h"
#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@interface BJMasterViewController ()
@property (strong) NSMutableArray *interfaces;
@end
@implementation BJMasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
self.interfaces = [NSMutableArray array];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)applicationDidBecomeActive:(NSNotification *)notif
{
[self insertNewObject:nil];
}
- (void)insertNewObject:(id)sender
{
self.interfaces = [NSMutableArray array];
struct ifaddrs *ifbase;
getifaddrs(&ifbase);
for (struct ifaddrs *ifaddr = ifbase; ifaddr; ifaddr = ifaddr->ifa_next) {
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithDictionary:
@{
@"name": @(ifaddr->ifa_name),
@"flags": @(ifaddr->ifa_flags),
@"family": @(ifaddr->ifa_addr->sa_family),
}];
if (ifaddr->ifa_addr->sa_family == AF_LINK) {
info[@"MAC"] = @(link_ntoa((struct sockaddr_dl *)ifaddr->ifa_addr));
}
if (ifaddr->ifa_addr->sa_family == AF_INET) {
info[@"IP"] = @(inet_ntoa(((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr));
}
if (ifaddr->ifa_addr->sa_family == AF_INET6) {
struct in6_addr addr = ((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr;
__uint32_t *addr32 = addr.__u6_addr.__u6_addr32;
info[@"IP"] = [NSString stringWithFormat:@"%08x:%08x:%08x:%08x", ntohl(addr32[0]), ntohl(addr32[1]), ntohl(addr32[2]), ntohl(addr32[3])];
}
[self.interfaces addObject:info];
}
freeifaddrs(ifbase);
[self.tableView reloadData];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.interfaces.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:11];
}
NSDictionary *info = self.interfaces[indexPath.row];
NSString *text = [NSString stringWithFormat:@"%@,%04X,%02d", info[@"name"], [info[@"flags"] intValue], [info[@"family"] intValue]];
if (info[@"MAC"]) text = [text stringByAppendingFormat:@",%@", info[@"MAC"]];
if (info[@"IP"]) text = [text stringByAppendingFormat:@",%@", info[@"IP"]];
cell.textLabel.text = text;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment