blob: ddc796c6b9f7b1707604ac1d7f1bc24337518752 [file] [log] [blame]
you.chen5ef374a2023-12-26 17:25:16 +08001#include <stdio.h>
2#include <string.h>
3#include <stdbool.h>
4#include <stdlib.h>
5#include "dhcp/lynq_dhcp.h"
6
7bool isVaildIp(const char *ip)
8{
9 int dots = 0; /*number of char*/
10 int setions = 0; /*part of ip(0-255)*/
11
12 if (NULL == ip || *ip == '.') { /*排除输入参数为NULL, 或者一个字符为'.'的字符串*/
13 return false;
14 }
15
16 while (*ip) {
17
18 if (*ip == '.') {
19 dots ++;
20 if (setions >= 0 && setions <= 255) { /*judge ip is vaild*/
21 setions = 0;
22 ip++;
23 continue;
24 }
25 return false;
26 }
27 else if (*ip >= '0' && *ip <= '9') { /*judge is number*/
28 setions = setions * 10 + (*ip - '0'); /*求每一段总和*/
29 } else
30 return false;
31 ip++;
32 }
33
34 if (setions >= 0 && setions <= 255) { /*判断IP最后一段是否合法*/
35 if (dots == 3) {
36 return true;
37 }
38 }
39
40 return false;
41}
42
43void Restart_Dhcp(char* if_name)
44{
45 char gyCmd[128] = {0};
46 sprintf(gyCmd,"ifconfig %s down",if_name);
47 system(gyCmd);
48 //printf("gyCmd=%s\n",gyCmd);
49 sprintf(gyCmd,"ifconfig %s up",if_name);
50 system(gyCmd);
51 //printf("gyCmd=%s\n",gyCmd);
52}
53
54void lynq_dhcp_get_interfacelist(char* interface_name)
55{
56 char ifname[IFNAME_CNT][IFNAME] = {0};
57
58 char buf[IFNAME]={0};
59 int i=0;
60 FILE * fp = NULL;
61 char gyCmd[128] = {0};
62 //1.Start from the second row and loop to get the first column of data;2.Remove the space from start of line;3.Remove the ":" from end of line;
63 sprintf (gyCmd, "cat /proc/net/dev | awk '{i++; if(i>2){print $1}}' | sed 's/^[\t]*//g' | sed 's/[:]*$//g'");
64
65 //ifconfig | grep encap |grep -v lo | cut -d ' ' -f1
66 //sprintf (gyCmd, "ifconfig | grep flags |grep -v lo | cut -d ':' -f1");
67 fp = popen (gyCmd, "r");
68
69 while(fgets(buf, sizeof(buf), fp)!=NULL){
70 strtok(buf, "\n");
71 strcat(interface_name,buf);
72 strcat(interface_name,";");
73 }
74
75 pclose(fp);
76}
77
78void lynq_dhcp_get_address(char* inet_addr,int type)
79{
80 char ifname[IFNAME_CNT][IFNAME] = {0};
81 int i=0;
82 char buf[32]={0};
83 char interface_name[IFNAME] = {0};
84 char gyCmd[128] = {0};
85 FILE * fp = NULL;
86
87 lynq_dhcp_get_interfacelist(interface_name);
88 char delims[] = ";";
89 char *result = NULL;
90 result = strtok( interface_name, delims );
91 while( result != NULL ) {
92 strcpy(ifname[i++],result);
93 result = strtok( NULL, delims );
94 }
95 for(i=0;i<IFNAME_CNT;i++){
96 if(strlen(ifname[i])){
97 //printf("[%s][%d]ifname[%s]\n",__FUNCTION__,__LINE__,ifname[i]);
98 switch (type) {
99 case DHCP_IPADDR:
100 sprintf(gyCmd,"ifconfig %s | grep \"inet addr:\" | awk '{print $2}' | awk 'NR==1'",ifname[i]);
101 break;
102 case DHCP_NETMASK:
103 sprintf(gyCmd,"ifconfig %s | grep \"inet addr:\" | awk '{print $4}' | awk 'NR==1'",ifname[i]);
104 break;
105 case DHCP_GATEWAY:
106 sprintf(gyCmd,"route -n | grep %s | awk 'NR==1' | awk '{print $2}'",ifname[i]);
107 break;
108 case DHCP_BROADCAST:
109 sprintf(gyCmd,"ifconfig %s | grep \"inet addr:\" | awk '{print $6}' | awk 'NR==1'",ifname[i]);
110 break;
111 default:
112 break;
113 }
114 //printf("[%s][%d]gyCmd[%s]\n",__FUNCTION__,__LINE__,gyCmd);
115 fp = popen (gyCmd, "r");
116 if(fgets(buf, sizeof(buf), fp)!=NULL){
117 buf[strlen(buf)-1]='\0';
118 //printf("[%s][%d]strlen(buf)[%d][%s]\n",__FUNCTION__,__LINE__,strlen(buf),buf);
119 if(strlen(buf)>1){
120 if(strstr(buf, "Device not found") != NULL ){
121 strcat(inet_addr,ifname[i]);
122 strcat(inet_addr,":Not Found!;");
123 }else{
124 strcat(inet_addr,ifname[i]);
125 strcat(inet_addr,":");
126 strcat(inet_addr,buf);
127 strcat(inet_addr,";");
128 }
129
130 //printf("[%s][%d]addr[%s][%d]\n",__FUNCTION__,__LINE__,addr[i],i);
131 }else if(!strlen(buf)){
132 strcat(inet_addr,ifname[i]);
133 strcat(inet_addr,":Not Found!;");
134 //printf("[%s][%d]addr[%s][%d]\n",__FUNCTION__,__LINE__,addr[i],i);
135 }
136 }else{
137 strcat(inet_addr,ifname[i]);
138 strcat(inet_addr,":Not Found!;");
139
140 }
141 pclose(fp);
142 }
143 }
144 //printf("[%s][%d]addr[%s][%d]\n",__FUNCTION__,__LINE__,addr[i],i);
145
146}
147
148#if 0
149//ifconfig eth1 | grep "inet addr:" | awk '{print $2}' | cut -c 6-
150char* Get_Dhcp_Ip(char addr[16][64])
151{
152 char ifname[IFNAME_CNT][IFNAME] = {0};
153 int i=0;
154 char buf[IFNAME]={0};
155
156 char gyCmd[128] = {0};
157 FILE * fp = NULL;
158 Search_Interface(ifname);
159 for(i=0;i<IFNAME_CNT;i++){
160 if(strlen(ifname[i])){
161 printf("[%s][%d]ifname[%s]\n",__FUNCTION__,__LINE__,ifname[i]);
162 sprintf(gyCmd,"ifconfig %s | grep \"inet\" | awk '{print $2}' | awk 'NR==1'",ifname[i]);
163 fp = popen (gyCmd, "r");
164 while(fgets(buf, sizeof(buf), fp)!=NULL){
165 if(strstr(buf, "Device not found") != NULL ){
166 strcpy(addr[i],ifname[i]);
167 strcat(addr[i],":Not Found!");
168 }else{
169 if(strlen(buf)>1){
170 strtok(buf, "\n");
171
172 strcpy(addr[i],ifname[i]);
173 strcat(addr[i],":");
174 strcat(addr[i],buf);
175 //printf("[%s][%d]addr[%s][%d]\n",__FUNCTION__,__LINE__,addr[i],i);
176 }
177 }
178 }
179 pclose(fp);
180 }
181 }
182
183 return addr;
184}
185
186char* Get_Dhcp_NetMask(char addr[16][64])
187{
188 char ifname[IFNAME_CNT][IFNAME] = {0};
189 int i=0;
190 char buf[IFNAME]={0};
191
192 char gyCmd[128] = {0};
193 FILE * fp = NULL;
194 Search_Interface(ifname);
195 for(i=0;i<IFNAME_CNT;i++){
196 if(strlen(ifname[i])){
197 printf("[%s][%d]ifname[%s]\n",__FUNCTION__,__LINE__,ifname[i]);
198 sprintf(gyCmd,"ifconfig %s | grep \"inet\" | awk '{print $4}' | awk 'NR==1'",ifname[i]);
199 fp = popen (gyCmd, "r");
200 while(fgets(buf, sizeof(buf), fp)!=NULL){
201 if(strstr(buf, "Device not found") != NULL ){
202 strcpy(addr[i],ifname[i]);
203 strcat(addr[i],":Not Found!");
204 }else{
205 if(strlen(buf)>1){
206 strtok(buf, "\n");
207
208 strcpy(addr[i],ifname[i]);
209 strcat(addr[i],":");
210 strcat(addr[i],buf);
211 //printf("[%s][%d]addr[%s][%d]\n",__FUNCTION__,__LINE__,addr[i],i);
212 }
213 }
214 }
215 pclose(fp);
216 }
217 }
218
219 return addr;
220}
221#endif
222
223int lynq_dhcp_set_address(char *inet_addr,char *interface_name ,int type)
224{
225 char ifname[IFNAME_CNT][IFNAME] = {0};
226 int i=0;
227 int find=0;
228 char gyCmd[128] = {0};
229 char ifn[IFNAME] = {0};
230 char delims[] = ";";
231 char *result = NULL;
232 if (!isVaildIp(inet_addr)) {
233 return DHCP_FAIL_INVALID_IP;
234 }
235 lynq_dhcp_get_interfacelist(ifn);
236
237 result = strtok( ifn, delims );
238 while( result != NULL ) {
239 strcpy(ifname[i++],result);
240 result = strtok( NULL, delims );
241 }
242 if(interface_name==NULL)
243 return DHCP_FAIL_INVALID_IFNAME;
244 for(i=0;i<IFNAME_CNT;i++){
245 if(strlen(ifname[i]))
246 if(!strcmp(ifname[i],interface_name))
247 find=1;
248 }
249
250 switch (type) {
251 case DHCP_IPADDR:
252 sprintf(gyCmd,"ifconfig %s %s",interface_name,inet_addr);
253 system(gyCmd);
254 //printf("gyCmd=%s\n",gyCmd);
255 break;
256 case DHCP_NETMASK:
257 sprintf(gyCmd,"ifconfig %s netmask %s",interface_name,inet_addr);
258 system(gyCmd);
259 break;
260 case DHCP_GATEWAY:
261 sprintf(gyCmd,"route add %s gw %s ",interface_name,inet_addr);
262 system(gyCmd);
263 break;
264 case DHCP_BROADCAST:
265 sprintf(gyCmd,"ifconfig %s broadcast %s",interface_name,inet_addr);
266 system(gyCmd);
267 break;
268 default:
269 break;
270 }
271
272 Restart_Dhcp(interface_name);
273 if(find)
274 return DHCP_SUCCESS;
275 else
276 return DHCP_FAIL_NO_DEV;
277}
278
279int lynq_dhcp_enable(char* interface_name,int enable)
280{
281 char ifname[IFNAME_CNT][IFNAME] = {0};
282 int i=0;
283 char gyCmd[128] = {0};
284 int find=0;
285 char ifn[IFNAME] = {0};
286 char delims[] = ";";
287 char *result = NULL;
288 if(interface_name==NULL)
289 return DHCP_FAIL_INVALID_IFNAME;
290 lynq_dhcp_get_interfacelist(ifn);
291 result = strtok( ifn, delims );
292 while( result != NULL ) {
293 strcpy(ifname[i++],result);
294 result = strtok( NULL, delims );
295 }
296 for(i=0;i<IFNAME_CNT;i++){
297 if(strlen(ifname[i]))
298 if(!strcmp(ifname[i],interface_name))
299 find=1;
300 }
301 switch (enable) {
302 case DHCP_ON:
303 sprintf(gyCmd,"ifconfig %s up",interface_name);
304 break;
305 case DHCP_OFF:
306 sprintf(gyCmd,"ifconfig %s down",interface_name);
307 break;
308 default:
309 break;
310 }
311
312 if(find)
313 return DHCP_SUCCESS;
314 else
315 return DHCP_FAIL_NO_DEV;
316}
317#if 0
318//my_test1 0 192.168.125.55 eth1 0
319int main(int argc, char *argv[])
320{
321 char ip_addr[16][64] = {0};
322 int i;
323 char* buf=NULL;
324 //Get_Dhcp_Ip(ip_addr);
325 printf("%d\n",atoi(argv[1]));
326 Get_Dhcp(ip_addr,atoi(argv[1]));
327 for(i=0;i<IFNAME_CNT;i++){
328 printf("[%s][%d]out[%s][%d]\n",__FUNCTION__,__LINE__,ip_addr[i],i);
329 }
330 buf=Set_Dhcp(argv[2],argv[3],argv[4]);
331 printf("buf=%s\n",buf);
332 return 0;
333}
334#endif