文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>常用Linux网络操作

常用Linux网络操作

时间:2011-03-17  来源:如是晴朗

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <net/if.h>
10 #include <netdb.h>
11 #include <syslog.h>
12 #include <linux/sockios.h>
13 #include <net/if_arp.h>
14
15 int get_local_ip(char *localip) //获取本地 eth0 的地址
16 {
17 int err = -1;
18 int s;
19
20 s = socket(AF_INET,SOCK_DGRAM,0); //创建套接字
21 if(s < 0) {
22 printf("socket create error\n");
23 return -1;
24 }
25
26 struct ifreq ifr; //方便操作设置指向sockaddr_in的指针
27 struct sockaddr_in *sin = (struct sockaddr_in *)&(ifr.ifr_addr);
28 memset(localip,0,16);
29 memcpy(ifr.ifr_name,"eth0",5); //查询 eth0
30
31 err = ioctl(s,SIOCGIFADDR,&ifr);
32 if(!err) {
33 inet_ntop(AF_INET,&sin->sin_addr.s_addr,localip,16); //整型转换成点分四段的字符串
34 syslog(LOG_USER|LOG_INFO,"process %d get localip:%s\n",getpid(),localip);
35 }
36 close(s);
37 return 0;
38 }
39
40 int get_local_mac(unsigned char *localmac)
41 {
42 int err = -1;
43 int s;
44
45 s = socket(AF_INET,SOCK_DGRAM,0); //创建套接字
46 if(s < 0) {
47 printf("socket create error\n");
48 return -1;
49 }
50
51 struct ifreq ifr;
52 memcpy(ifr.ifr_name,"eth0",5); //查询 eth0
53 err = ioctl(s,SIOCGIFHWADDR,&ifr);
54
55 if(!err) {
56 memcpy(localmac,ifr.ifr_hwaddr.sa_data,6);
57 syslog(LOG_USER|LOG_INFO,"process %d get localmac:%02x-%02x-%02x-%02x-%02x-%02x\n",
58 getpid(),localmac[0],localmac[1],localmac[2],localmac[3],localmac[4],localmac[5]);
59 }
60 close(s);
61 return 0;
62 }
63
64 int set_local_ip(char *ip)
65 {
66 int err = -1;
67 int s;
68
69 s = socket(AF_INET,SOCK_DGRAM,0); //创建套接字
70 if(s < 0) {
71 printf("socket create error\n");
72 return -1;
73 }
74
75 struct ifreq ifr;
76 struct sockaddr_in *sin = (struct sockaddr_in *)&(ifr.ifr_addr);
77
78 memset(&ifr,0,sizeof(ifr));
79 memcpy(ifr.ifr_name,"eth0",5); //设置 eth0 的地址
80 inet_pton(AF_INET,ip,&(sin->sin_addr.s_addr)); //转换成网络字节序
81
82 sin->sin_family = AF_INET; //协议
83 err = ioctl(s,SIOCSIFADDR,&ifr);
84 if(err) {
85 syslog(LOG_USER|LOG_INFO,"process %d set ip error\n",getpid());
86 printf("%s\n",strerror(errno));
87 }
88 else
89 syslog(LOG_USER|LOG_INFO,"process %d set ip %s\n",getpid(),ip);
90
91 close(s);
92 return 0;
93 }
94
95 int get_host_mac(char *ip)
96 {
97 int s;
98 struct arpreq arp;
99 struct sockaddr_in *addr = (struct sockaddr_in *)&arp.arp_pa;
100 unsigned char mac[6];
101 int err = -1;
102
103 s = socket(AF_INET,SOCK_DGRAM,0); //创建套接字
104 if(s < 0) {
105 printf("socket create error\n");
106 return -1;
107 }
108
109 addr->sin_family = AF_INET; //填充arp成员arp_pa
110 addr->sin_addr.s_addr = inet_addr(ip);
111 if(addr->sin_addr.s_addr == INADDR_NONE) {
112 syslog(LOG_USER|LOG_INFO,"pid = %d: ip %s ,error format\n",getpid(),ip);
113 return -1;
114 }
115
116 strcpy(arp.arp_dev,"eth0"); //查询 eth0 的 mac 地址
117 err = ioctl(s,SIOCGARP,&arp);
118 if(err < 0) {
119 syslog(LOG_USER|LOG_INFO,"pid = %d: ioctl error\n",getpid());
120 return -1;
121 }
122 else {
123 memcpy(mac,(unsigned char *)&arp.arp_ha.sa_data,6);
124 syslog(LOG_USER|LOG_INFO,"process %d get %s mac:%02x-%02x-%02x-%02x-%02x-%02x\n",
125 getpid(),ip,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
126 }
127 close(s);
128 return 0;
129 }
130
131 int main()
132 {
133 // char localip[16];
134 // unsigned char localmac[6];
135 // get_local_ip(localip);
136 // printf("localip = %s\n",localip);
137 // get_local_mac(localmac);
138 // printf("process %d get localmac:%02x-%02x-%02x-%02x-%02x-%02x\n",
139 // getpid(),localmac[0],localmac[1],localmac[2],localmac[3],localmac[4],localmac[5]);
140 // set_local_ip("192.168.1.155");
141 // get_host_mac("192.168.1.1");
142 return 0;
143 }
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载