Initial commit
This commit is contained in:
+130
@@ -0,0 +1,130 @@
|
||||
//
|
||||
// ESP_ByteUtil.h
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 4/7/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface ESP_ByteUtil : NSObject
|
||||
|
||||
#define ESPTOUCH_NSStringEncoding NSUTF8StringEncoding
|
||||
|
||||
/**
|
||||
* Convert uint8 into char( we treat char as uint8)
|
||||
*
|
||||
* @param uint8
|
||||
* the unit8 to be converted
|
||||
* @return the byte of the unint8
|
||||
*/
|
||||
+ (Byte) convertUint8toByte:(char) uint8;
|
||||
|
||||
/**
|
||||
* Convert char into uint8( we treat char as uint8 )
|
||||
*
|
||||
* @param b
|
||||
* the byte to be converted
|
||||
* @return the UInt8(uint8)
|
||||
*/
|
||||
+ (UInt8) convertByte2Uint8:(Byte) b;
|
||||
|
||||
/**
|
||||
* Convert byte to Hex String
|
||||
*
|
||||
* @param b
|
||||
* the byte to be converted
|
||||
* @return the Hex String
|
||||
*/
|
||||
+ (NSString *) convertByte2HexString:(Byte) b;
|
||||
|
||||
/**
|
||||
* Split uint8 to 2 bytes of high byte and low byte. e.g. 20 = 0x14 should
|
||||
* be split to [0x01,0x04] 0x01 is high byte and 0x04 is low byte
|
||||
*
|
||||
* @param uint8
|
||||
* the char(uint8)
|
||||
* @return the high and low bytes be split, byte[0] is high and byte[1] is
|
||||
* low
|
||||
*/
|
||||
|
||||
+ (NSData *) splitUint8To2Bytes: (UInt8) uint8;
|
||||
|
||||
/**
|
||||
* Combine 2 bytes (high byte and low byte) to one whole byte
|
||||
*
|
||||
* @param high
|
||||
* the high byte
|
||||
* @param low
|
||||
* the low byte
|
||||
* @return the whole byte
|
||||
*/
|
||||
|
||||
+ (Byte) combine2bytesToOneWithHigh: (Byte) high andLow: (Byte) low;
|
||||
|
||||
/**
|
||||
* Combine 2 bytes (high byte and low byte) to one UInt16
|
||||
*
|
||||
* @param high
|
||||
* the high byte
|
||||
* @param low
|
||||
* the low byte
|
||||
* @return the UInt8
|
||||
*/
|
||||
+ (UInt16) combine2bytesToU16WithHigh: (Byte) high andLow: (Byte) low;
|
||||
|
||||
/**
|
||||
* Generate the random byte to be sent
|
||||
*
|
||||
* @return the random byte
|
||||
*/
|
||||
+ (Byte) randomByte;
|
||||
|
||||
/**
|
||||
* Generate the random byte to be sent
|
||||
*
|
||||
* @param len
|
||||
* the len presented by u8
|
||||
* @return the byte[] to be sent
|
||||
*/
|
||||
+ (NSData *) randomBytes: (UInt8) len;
|
||||
|
||||
+ (NSData *) genSpecBytesWithU16: (UInt16) len;
|
||||
|
||||
/**
|
||||
* Generate the specific byte to be sent
|
||||
* @param len
|
||||
* the len presented by byte
|
||||
* @return the byte[]
|
||||
*/
|
||||
+ (NSData *) genSpecBytesWithU8:(Byte) len;
|
||||
|
||||
+ (NSString *) parseBssid:(Byte[]) bssidBytes Offset: (int) offset Count: (int) count;
|
||||
|
||||
/**
|
||||
* parse "24,-2,52,-102,-93,-60" to "18,fe,34,9a,a3,c4" parse the bssid from
|
||||
* hex to String
|
||||
*
|
||||
* @param bssidBytes
|
||||
* the hex bytes bssid, e.g. {24,-2,52,-102,-93,-60}
|
||||
* @param len
|
||||
* the len of bssidBytes
|
||||
* @return the String of bssid, e.g. 18fe349aa3c4
|
||||
*/
|
||||
+ (NSString *) parseBssid:(Byte[]) bssidBytes Len: (int) len;
|
||||
|
||||
/**
|
||||
* @param string the string to be used
|
||||
* @return the Byte[] of string according to ESPTOUCH_NSStringEncoding
|
||||
*/
|
||||
+ (NSData *) getBytesByNSString: (NSString *)string;
|
||||
|
||||
/**
|
||||
* get hex string transformed by data
|
||||
* @param data the data to be transformed
|
||||
* @return the hex String transformed by data
|
||||
*/
|
||||
+ (NSString *) getHexStringByData: (NSData *)data;
|
||||
|
||||
@end
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// ESP_ByteUtil.m
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 4/7/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ESP_ByteUtil.h"
|
||||
|
||||
@implementation ESP_ByteUtil
|
||||
|
||||
+ (Byte) convertUint8toByte:(char) uint8
|
||||
{
|
||||
return (Byte)uint8;
|
||||
}
|
||||
|
||||
+ (UInt8) convertByte2Uint8:(Byte) b
|
||||
{
|
||||
return (UInt8) (b & 0xff);
|
||||
}
|
||||
|
||||
+ (NSString *) convertByte2HexString:(Byte) b
|
||||
{
|
||||
UInt8 u8 = [self convertByte2Uint8:b];
|
||||
return [NSString stringWithFormat:@"%x",u8];
|
||||
}
|
||||
|
||||
+ (unsigned int)intFromHexString:(NSString *) hexStr
|
||||
{
|
||||
unsigned int hexInt = 0;
|
||||
|
||||
// Create scanner
|
||||
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
|
||||
|
||||
// Tell scanner to skip the # character
|
||||
// [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
|
||||
|
||||
// Scan hex value
|
||||
[scanner scanHexInt:&hexInt];
|
||||
|
||||
return hexInt;
|
||||
}
|
||||
|
||||
+ (NSData *) splitUint8To2Bytes: (UInt8) uint8
|
||||
{
|
||||
NSString *hexString = [NSString stringWithFormat:@"%x",uint8];
|
||||
Byte low;
|
||||
Byte high;
|
||||
if (hexString.length > 1)
|
||||
{
|
||||
high = [self intFromHexString:[hexString substringWithRange:NSMakeRange(0, 1)]];
|
||||
low =[self intFromHexString:[hexString substringWithRange:NSMakeRange(1, 1)]];
|
||||
}
|
||||
else
|
||||
{
|
||||
high = 0;
|
||||
low = [self intFromHexString:[hexString substringWithRange:NSMakeRange(0, 1)]];
|
||||
}
|
||||
Byte bytes[] = { high, low };
|
||||
NSData *data = [[NSData alloc] initWithBytes:bytes length:2];
|
||||
return data;
|
||||
}
|
||||
|
||||
+ (Byte) combine2bytesToOneWithHigh: (Byte) high andLow: (Byte) low
|
||||
{
|
||||
return (Byte) (high << 4 | low);
|
||||
}
|
||||
|
||||
+ (UInt16) combine2bytesToU16WithHigh: (Byte) high andLow: (Byte) low
|
||||
{
|
||||
UInt8 highU8 = [self convertByte2Uint8:high];
|
||||
UInt8 lowU8 = [self convertByte2Uint8:low];
|
||||
return (highU8 << 8 | lowU8);
|
||||
}
|
||||
|
||||
+ (Byte) randomByte
|
||||
{
|
||||
return arc4random() % 256;
|
||||
}
|
||||
|
||||
+ (NSData *) randomBytes: (UInt8) len
|
||||
{
|
||||
Byte bytes[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
bytes[i] = [self randomByte];
|
||||
}
|
||||
NSData *data = [[NSData alloc]initWithBytes:bytes length:len];
|
||||
return data;
|
||||
}
|
||||
|
||||
+ (NSData *) genSpecBytesWithU16:(UInt16)len
|
||||
{
|
||||
Byte bytes[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
bytes[i] = '1';
|
||||
}
|
||||
NSData *data = [[NSData alloc]initWithBytes:bytes length:len];
|
||||
return data;
|
||||
}
|
||||
|
||||
+ (NSData *) genSpecBytesWithU8:(Byte) len
|
||||
{
|
||||
UInt8 u8 = [self convertByte2Uint8:len];
|
||||
return [self genSpecBytesWithU16:u8];
|
||||
}
|
||||
|
||||
+ (NSString *) parseBssid:(Byte[]) bssidBytes Offset: (int) offset Count: (int) count
|
||||
{
|
||||
Byte bytes[count];
|
||||
|
||||
for (int i = 0; i < count; i++ )
|
||||
{
|
||||
bytes[i] = bssidBytes[i + offset];
|
||||
}
|
||||
return [self parseBssid:bytes Len:count];
|
||||
}
|
||||
|
||||
+ (NSString *) parseBssid:(Byte[]) bssidBytes Len:(int)len
|
||||
{
|
||||
NSMutableString *mStr = [[NSMutableString alloc]init];
|
||||
int k;
|
||||
NSString* hexK;
|
||||
NSString* str;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
k = 0xff & bssidBytes[i];
|
||||
hexK = [NSString stringWithFormat:@"%x", k];
|
||||
str = ((k < 16) ? ([NSString stringWithFormat:@"0%@",hexK ]) : (hexK));
|
||||
[mStr appendString:str];
|
||||
}
|
||||
return mStr;
|
||||
}
|
||||
|
||||
+ (NSData *) getBytesByNSString: (NSString *)string
|
||||
{
|
||||
NSUInteger numberOfBytes = [string lengthOfBytesUsingEncoding:ESPTOUCH_NSStringEncoding];
|
||||
Byte bytes[numberOfBytes];
|
||||
NSRange range = NSMakeRange(0, numberOfBytes);
|
||||
[string getBytes:bytes maxLength:numberOfBytes usedLength:nil encoding:ESPTOUCH_NSStringEncoding options:0 range:range remainingRange:NULL];
|
||||
NSData *data = [[NSData alloc]initWithBytes:bytes length:numberOfBytes];
|
||||
return data;
|
||||
}
|
||||
|
||||
+ (NSString *) getHexStringByData:(NSData *)data
|
||||
{
|
||||
NSMutableString* mStr = [[NSMutableString alloc]init];
|
||||
NSUInteger totalLen = [data length];
|
||||
Byte bytes[totalLen];
|
||||
[data getBytes:&bytes length:totalLen];
|
||||
for (int i = 0; i < totalLen; i++)
|
||||
{
|
||||
NSString *hexString = [[NSString alloc]initWithFormat:@"0x%.2x ",bytes[i]];
|
||||
[mStr appendString:hexString];
|
||||
}
|
||||
return mStr;
|
||||
}
|
||||
|
||||
@end
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// ESP_CRC8.h
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 3/23/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#define CRC_POLYNOM 0x8c
|
||||
|
||||
#define CRC_INITIAL 0x00
|
||||
|
||||
// the interface is copied from the interface Checksum from Java
|
||||
@interface ESP_CRC8 : NSObject
|
||||
|
||||
/**
|
||||
* Returns the current calculated checksum value.
|
||||
*
|
||||
* @return the checksum.
|
||||
*/
|
||||
- (long)getValue;
|
||||
|
||||
/**
|
||||
* Resets the checksum value applied before beginning calculations on a new
|
||||
* stream of data.
|
||||
*/
|
||||
- (void)reset;
|
||||
|
||||
/**
|
||||
* Updates the checksum with the given bytes.
|
||||
*
|
||||
* @param buf
|
||||
* the byte array from which to read the bytes.
|
||||
* @param off
|
||||
* the initial position in {@code buf} to read the bytes from.
|
||||
* @param nbytes
|
||||
* the number of bytes to read from {@code buf}.
|
||||
*/
|
||||
- (void)updateWithBuf:(Byte[])buf Off:(int)off Nbytes:(int)nbytes;
|
||||
|
||||
/**
|
||||
* Updates the checksum with the given bytes.
|
||||
*
|
||||
* @param buf
|
||||
* the byte array from which to read the bytes.
|
||||
* @param nbytes
|
||||
* the number of bytes to read from {@code buf}.
|
||||
*/
|
||||
- (void)updateWithBuf:(Byte [])buf Nbytes:(int)nbytes;
|
||||
|
||||
/**
|
||||
* Updates the checksum value with the given byte.
|
||||
*
|
||||
* @param val
|
||||
* the byte to update the checksum with.
|
||||
*/
|
||||
- (void)updateWithValue:(int)value;
|
||||
|
||||
@end
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// ESP_CRC8.m
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 3/23/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ESP_CRC8.h"
|
||||
|
||||
@implementation ESP_CRC8
|
||||
|
||||
static ushort crcTable[256];
|
||||
|
||||
NSNumber *_init;
|
||||
NSNumber *_value;
|
||||
|
||||
+(void) initialize
|
||||
{
|
||||
for (ushort dividend=0; dividend < 256; dividend++)
|
||||
{
|
||||
ushort remainder = dividend;
|
||||
for (ushort bit = 0; bit < 8; ++bit)
|
||||
{
|
||||
if ((remainder & 0x01) != 0)
|
||||
{
|
||||
remainder = (remainder >> 1) ^ CRC_POLYNOM;
|
||||
}
|
||||
else
|
||||
{
|
||||
remainder >>= 1;
|
||||
}
|
||||
crcTable[dividend] = (ushort) remainder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-(id) init
|
||||
{
|
||||
if(self = [super init])
|
||||
{
|
||||
_init = [[NSNumber alloc]initWithInt:CRC_INITIAL];
|
||||
_value = [[NSNumber alloc] initWithInt:CRC_INITIAL];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (long)getValue
|
||||
{
|
||||
return [_value unsignedShortValue] & 0xff;
|
||||
}
|
||||
|
||||
- (void)reset
|
||||
{
|
||||
_value = _init;
|
||||
}
|
||||
|
||||
- (void)updateWithBuf:(Byte[])buf Off:(int)off Nbytes:(int)nbytes
|
||||
{
|
||||
for (int i = 0; i < nbytes; i++)
|
||||
{
|
||||
int data = buf[off + i] ^ _value.intValue;
|
||||
int value = crcTable[data & 0xff] ^ (_value.intValue << 8);
|
||||
_value = [NSNumber numberWithInt:value];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateWithBuf:(Byte [])buf Nbytes:(int)nbytes
|
||||
{
|
||||
[self updateWithBuf:buf Off:0 Nbytes:nbytes];
|
||||
}
|
||||
|
||||
|
||||
- (void)updateWithValue:(int)value
|
||||
{
|
||||
Byte b[1] = { (Byte)value };
|
||||
[self updateWithBuf:b Nbytes:1];
|
||||
}
|
||||
|
||||
@end
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// ESPNetUtil.h
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 5/15/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface ESP_NetUtil : NSObject
|
||||
|
||||
/**
|
||||
* get the local ip address by IOS System
|
||||
*
|
||||
*/
|
||||
+ (NSData *) getLocalInetAddress;
|
||||
|
||||
/**
|
||||
* parse InetAddress
|
||||
*/
|
||||
+ (NSData *) parseInetAddrByData: (NSData *) inetAddrData andOffset: (int) offset andCount: (int) count;
|
||||
|
||||
/**
|
||||
* descrpion inetAddrData for print pretty
|
||||
*/
|
||||
+ (NSString *) descriptionInetAddrByData: (NSData *) inetAddrData;
|
||||
|
||||
/**
|
||||
* parse bssid
|
||||
*
|
||||
* @param bssid the bssid
|
||||
* @return byte converted from bssid
|
||||
*/
|
||||
+ (NSData *) parseBssid2bytes: (NSString *) bssid;
|
||||
|
||||
@end
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// ESPNetUtil.m
|
||||
// EspTouchDemo
|
||||
//
|
||||
// Created by 白 桦 on 5/15/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#import "ESP_NetUtil.h"
|
||||
#include "IPAddress.h"
|
||||
#include "ESP_ByteUtil.h"
|
||||
|
||||
// only support ipV4 at the moment
|
||||
#define IP_LEN 4
|
||||
|
||||
@implementation ESP_NetUtil
|
||||
|
||||
+ (NSData *) getLocalInetAddress
|
||||
{
|
||||
InitAddresses();
|
||||
GetIPAddresses();
|
||||
GetHWAddresses();
|
||||
NSData *localInetAddrData = nil;
|
||||
Byte localIpBytes[IP_LEN];
|
||||
for (int i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
static unsigned long localHost = 0x100007F; // 127.0.0.1
|
||||
unsigned long theAddr;
|
||||
|
||||
theAddr = ip_addrs[i];
|
||||
|
||||
if (theAddr == 0) break;
|
||||
if (theAddr == localHost) continue;
|
||||
// NSLog(@"theAddr=%lu",theAddr);
|
||||
|
||||
// hard coding
|
||||
localIpBytes[0] = (theAddr & 0xff) >> 0;
|
||||
localIpBytes[1] = (theAddr & 0xff00) >> 8;
|
||||
localIpBytes[2] = (theAddr & 0xff0000) >> 16;
|
||||
localIpBytes[3] = (theAddr & 0xff000000) >> 24;
|
||||
// NSLog(@"ESP_NetUtil:: %d.%d.%d.%d",localIpBytes[0],localIpBytes[1],localIpBytes[2],localIpBytes[3]);
|
||||
|
||||
// if (theAddr != 0) break;
|
||||
// NSLog(@"ESP_NetUtil:: Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);
|
||||
}
|
||||
|
||||
localInetAddrData = [[NSData alloc]initWithBytes:localIpBytes length:IP_LEN];
|
||||
|
||||
FreeAddresses();
|
||||
|
||||
Byte byte1 = 0;
|
||||
Byte byte2 = 0;
|
||||
Byte byte3 = 0;
|
||||
Byte byte4 = 0;
|
||||
|
||||
[localInetAddrData getBytes:&byte1 range:NSMakeRange(0, 1)];
|
||||
[localInetAddrData getBytes:&byte2 range:NSMakeRange(1, 1)];
|
||||
[localInetAddrData getBytes:&byte3 range:NSMakeRange(2, 1)];
|
||||
[localInetAddrData getBytes:&byte4 range:NSMakeRange(3, 1)];
|
||||
|
||||
|
||||
NSLog(@"ESP_NetUtil:: %d.%d.%d.%d", byte1,byte2,byte3,byte4);
|
||||
|
||||
return localInetAddrData;
|
||||
}
|
||||
|
||||
+ (NSData *) parseInetAddrByData: (NSData *) inetAddrData andOffset: (int) offset andCount: (int) count
|
||||
{
|
||||
return [inetAddrData subdataWithRange:NSMakeRange(offset, count)];
|
||||
}
|
||||
|
||||
+ (NSString *) descriptionInetAddrByData: (NSData *) inetAddrData
|
||||
{
|
||||
Byte inetAddrBytes[IP_LEN];
|
||||
[inetAddrData getBytes:inetAddrBytes length:IP_LEN];
|
||||
// hard coding
|
||||
return [NSString stringWithFormat:@"%d.%d.%d.%d",inetAddrBytes[0],inetAddrBytes[1],inetAddrBytes[2],inetAddrBytes[3]];
|
||||
}
|
||||
|
||||
+ (NSData *) parseBssid2bytes: (NSString *) bssid
|
||||
{
|
||||
NSArray *bssidArray = [bssid componentsSeparatedByString:@":"];
|
||||
NSInteger size = [bssidArray count];
|
||||
Byte bssidBytes[size];
|
||||
for (NSInteger i = 0; i < size; i++) {
|
||||
NSString *bssidStr = [bssidArray objectAtIndex:i];
|
||||
bssidBytes[i] = strtoul([bssidStr UTF8String], 0, 16);
|
||||
}
|
||||
return [[NSData alloc]initWithBytes:bssidBytes length:size];
|
||||
}
|
||||
|
||||
@end
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
//
|
||||
// IPAddress.c
|
||||
// LocalIpDemo
|
||||
//
|
||||
// Created by 白 桦 on 5/11/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/sockio.h>
|
||||
#include <net/if.h>
|
||||
#include <errno.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/ethernet.h>
|
||||
#include "IPAddress.h"
|
||||
|
||||
#define min(a,b) ((a) < (b) ? (a) : (b))
|
||||
#define max(a,b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
#define BUFFERSIZE 4000
|
||||
|
||||
char *if_names[MAXADDRS];
|
||||
char *ip_names[MAXADDRS];
|
||||
char *hw_addrs[MAXADDRS];
|
||||
unsigned long ip_addrs[MAXADDRS];
|
||||
|
||||
static int nextAddr = 0;
|
||||
|
||||
void InitAddresses()
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
|
||||
ip_addrs[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void FreeAddresses()
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
if (if_names[i] != 0) free(if_names[i]);
|
||||
if (ip_names[i] != 0) free(ip_names[i]);
|
||||
if (hw_addrs[i] != 0) free(hw_addrs[i]);
|
||||
ip_addrs[i] = 0;
|
||||
nextAddr = 0;
|
||||
}
|
||||
InitAddresses();
|
||||
}
|
||||
|
||||
void GetIPAddresses()
|
||||
{
|
||||
int i, len, flags;
|
||||
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
|
||||
struct ifconf ifc;
|
||||
struct ifreq *ifr, ifrcopy;
|
||||
struct sockaddr_in *sin;
|
||||
|
||||
char temp[80];
|
||||
|
||||
int sockfd;
|
||||
|
||||
for (i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
if_names[i] = ip_names[i] = NULL;
|
||||
ip_addrs[i] = 0;
|
||||
}
|
||||
|
||||
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
perror("socket failed");
|
||||
return;
|
||||
}
|
||||
|
||||
ifc.ifc_len = BUFFERSIZE;
|
||||
ifc.ifc_buf = buffer;
|
||||
|
||||
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
|
||||
{
|
||||
perror("ioctl error");
|
||||
return;
|
||||
}
|
||||
|
||||
lastname[0] = 0;
|
||||
|
||||
for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
|
||||
{
|
||||
ifr = (struct ifreq *)ptr;
|
||||
len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
|
||||
ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
|
||||
|
||||
if (ifr->ifr_addr.sa_family != AF_INET)
|
||||
{
|
||||
continue; // ignore if not desired address family
|
||||
}
|
||||
|
||||
if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
|
||||
{
|
||||
*cptr = 0; // replace colon will null
|
||||
}
|
||||
|
||||
if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
|
||||
{
|
||||
continue; /* already processed this interface */
|
||||
}
|
||||
|
||||
memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
|
||||
|
||||
ifrcopy = *ifr;
|
||||
ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
|
||||
flags = ifrcopy.ifr_flags;
|
||||
if ((flags & IFF_UP) == 0)
|
||||
{
|
||||
continue; // ignore if interface not up
|
||||
}
|
||||
|
||||
if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
|
||||
if (if_names[nextAddr] == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
strcpy(if_names[nextAddr], ifr->ifr_name);
|
||||
|
||||
sin = (struct sockaddr_in *)&ifr->ifr_addr;
|
||||
strcpy(temp, inet_ntoa(sin->sin_addr));
|
||||
|
||||
ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
|
||||
if (ip_names[nextAddr] == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
strcpy(ip_names[nextAddr], temp);
|
||||
|
||||
ip_addrs[nextAddr] = sin->sin_addr.s_addr;
|
||||
|
||||
++nextAddr;
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
void GetHWAddresses()
|
||||
{
|
||||
struct ifconf ifc;
|
||||
struct ifreq *ifr;
|
||||
int i, sockfd;
|
||||
char buffer[BUFFERSIZE], *cp, *cplim;
|
||||
char temp[80];
|
||||
|
||||
for (i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
hw_addrs[i] = NULL;
|
||||
}
|
||||
|
||||
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
perror("socket failed");
|
||||
return;
|
||||
}
|
||||
|
||||
ifc.ifc_len = BUFFERSIZE;
|
||||
ifc.ifc_buf = buffer;
|
||||
|
||||
if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
|
||||
{
|
||||
perror("ioctl error");
|
||||
close(sockfd);
|
||||
return;
|
||||
}
|
||||
|
||||
ifr = ifc.ifc_req;
|
||||
|
||||
cplim = buffer + ifc.ifc_len;
|
||||
|
||||
for (cp=buffer; cp < cplim; )
|
||||
{
|
||||
ifr = (struct ifreq *)cp;
|
||||
if (ifr->ifr_addr.sa_family == AF_LINK)
|
||||
{
|
||||
struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
|
||||
int a,b,c,d,e,f;
|
||||
int i;
|
||||
|
||||
strcpy(temp, (char *)ether_ntoa((const struct ether_addr *)LLADDR(sdl)));
|
||||
sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
|
||||
sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
|
||||
|
||||
for (i=0; i<MAXADDRS; ++i)
|
||||
{
|
||||
if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
|
||||
{
|
||||
if (hw_addrs[i] == NULL)
|
||||
{
|
||||
hw_addrs[i] = (char *)malloc(strlen(temp)+1);
|
||||
strcpy(hw_addrs[i], temp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
|
||||
}
|
||||
close(sockfd);
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// IPAddress.h
|
||||
// LocalIpDemo
|
||||
//
|
||||
// Created by 白 桦 on 5/11/15.
|
||||
// Copyright (c) 2015 白 桦. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef LocalIpDemo_IPAddress_h
|
||||
#define LocalIpDemo_IPAddress_h
|
||||
|
||||
#define MAXADDRS 32
|
||||
|
||||
extern char *if_names[MAXADDRS];
|
||||
extern char *ip_names[MAXADDRS];
|
||||
extern char *hw_addrs[MAXADDRS];
|
||||
extern unsigned long ip_addrs[MAXADDRS];
|
||||
|
||||
// Function prototypes
|
||||
|
||||
void InitAddresses();
|
||||
void FreeAddresses();
|
||||
void GetIPAddresses();
|
||||
void GetHWAddresses();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user