#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include "DDYWifiGateway.h"
#include <net/if.h>
#include <string.h>
#include <TargetConditionals.h>
#define CTL_NET 4 /* network, see socket.h */
#if defined(BSD) || defined(__APPLE__)
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
int getLocalIP(in_addr_t * addr)
{
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
NET_RT_FLAGS, RTF_GATEWAY};
size_t l;
char * buf, * p;
struct rt_msghdr * rt;
struct sockaddr * sa;
struct sockaddr * sa_tab[RTAX_MAX];
int i;
int r = -1;
if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
return -1;
}
if(l>0) {
buf = malloc(l);
if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
return -1;
}
for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
rt = (struct rt_msghdr *)p;
sa = (struct sockaddr *)(rt + 1);
for(i=0; i<RTAX_MAX; i++) {
if(rt->rtm_addrs & (1 << i)) {
sa_tab[i] = sa;
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
} else {
sa_tab[i] = NULL;
}
}
if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
&& sa_tab[RTAX_DST]->sa_family == AF_INET
&& sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
char ifName[128];
if_indextoname(rt->rtm_index,ifName);
if(strcmp("en0",ifName)==0){
*addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
r = 0;
}
}
}
}
free(buf);
}
return r;
}
unsigned char * getdefaultgateway(in_addr_t * addr)
{
unsigned char * octet=malloc(4);
#if 0
/* net.route.0.inet.dump.0.0 ? */
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
NET_RT_DUMP, 0, 0/*tableid*/};
#endif
/* net.route.0.inet.flags.gateway */
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
NET_RT_FLAGS, RTF_GATEWAY};
size_t l;
char * buf, * p;
struct rt_msghdr * rt;
struct sockaddr * sa;
struct sockaddr * sa_tab[RTAX_MAX];
int i;
if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
return octet;
}
if(l>0) {
buf = malloc(l);
if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
return octet;
}
for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
rt = (struct rt_msghdr *)p;
sa = (struct sockaddr *)(rt + 1);
for(i=0; i<RTAX_MAX; i++) {
if(rt->rtm_addrs & (1 << i)) {
sa_tab[i] = sa;
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
} else {
sa_tab[i] = NULL;
}
}
if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
&& sa_tab[RTAX_DST]->sa_family == AF_INET
&& sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
for (int i=0; i<4; i++){
octet[i] = ( ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr >> (i*8) ) & 0xFF;
}
}
}
free(buf);
}
return octet;
}
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
iOS 设备信息,电池循环次数,电池充电状况,电池电量,机身颜色,越狱破解状况,wifi信息,网关信息,系统各种信息.zip (54个子文件)
cm
.DS_Store 6KB
DDYDeviceInfo
.DS_Store 6KB
DDYDeviceInfo.xcodeproj
project.pbxproj 24KB
xcuserdata
SmartMesh.xcuserdatad
xcschemes
xcschememanagement.plist 336B
project.xcworkspace
xcshareddata
IDEWorkspaceChecks.plist 238B
contents.xcworkspacedata 158B
xcuserdata
SmartMesh.xcuserdatad
UserInterfaceState.xcuserstate 51KB
DDYDeviceInfo
.DS_Store 6KB
System
.DS_Store 6KB
ViewController.h 78B
Base.lproj
LaunchScreen.storyboard 2KB
AppDelegate.h 142B
AppDelegate.m 529B
main.m 207B
ViewController.m 3KB
Info.plist 4KB
New Group
.DS_Store 6KB
DDYTestViewController.h 197B
IOKit
OSKext.h 99KB
pwr_mgt
IOPMKeys.h 3KB
IOPMLibDefs.h 2KB
IOPMLib.h 54KB
IOPM.h 34KB
IOKit.framework
IOKit.tbd 20B
Versions
Current 1B
A
IOKit.tbd 74KB
IOKitLib.h 67KB
IOKit.h 10B
hid
IOHIDSession.h 7KB
IOHIDEvent.h 13KB
IOHIDUserDevice.h 2KB
IOHIDService.h 5KB
IOHIDNotification.h 3KB
IOHIDEventTypes.h 13KB
IOHIDEventSystem.h 4KB
IOHIDEventQueue.h 3KB
IOHIDEventData.h 61KB
IOHIDDisplay.h 5KB
IOHIDEventSystemClient.h 3KB
IOReturn.h 7KB
ps
IOPowerSources.h 11KB
IOPSKeys.h 29KB
IOUPSPlugIn.h 11KB
IOTypes.h 6KB
IOKitKeys.h 7KB
OSMessageNotification.h 4KB
IOMessage.h 9KB
DDYTestViewController.m 6KB
DDYSystemInfo
DDYWifiGateway.c 4KB
DDYSystemInfo.h 3KB
DDYSystemInfo.m 47KB
DDYWifiGateway.h 9KB
DDYBatteryInfo
DDYBatteryInfo.h 2KB
DDYBatteryInfo.m 2KB
共 54 条
- 1
资源评论
妄北y
- 粉丝: 9667
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功