[Frugalware-darcs] setup-current: netconfig: new functions: ifup(),
ifdown(), setdns()
VMiklos
vmiklos at frugalware.org
Thu Mar 16 18:09:33 CET 2006
[netconfig: new functions: ifup(), ifdown(), setdns()
VMiklos <vmiklos at frugalware.org>**20060118120551] {
hunk ./src/config/netconfig.c 79
- int i, j, n=0;
+ int i, n=0;
hunk ./src/config/netconfig.c 84
- interface_t *iface;
+ interface_t *iface=NULL;
hunk ./src/config/netconfig.c 190
+int is_dhcp(interface_t *iface)
+{
+ int i, dhcp=0;
+ for (i=0; i<g_list_length(iface->options); i++)
+ {
+ if(!strcmp((char*)g_list_nth_data(iface->options, i), "dhcp"))
+ dhcp=1;
+ }
+ return(dhcp);
+}
+
+int ifdown(interface_t *iface)
+{
+ int dhcp, i;
+ char *ptr;
+ FILE *fp;
+
+ dhcp = is_dhcp(iface);
+ if(dhcp)
+ {
+ char line[7];
+ ptr = g_strdup_printf("/etc/dhcpc/dhcpcd-%s.pid", iface->name);
+ fp = fopen(ptr, "r");
+ free(ptr);
+ if(fp != NULL)
+ {
+ fgets(line, 6, fp);
+ fclose(fp);
+ i = atoi(line);
+ if(i>0 && !nco_dryrun)
+ return(kill(i, 15));
+ else if (i>0)
+ return(printf("kill(%d, 15);\n", i));
+ }
+ }
+ else
+ {
+ if(g_list_length(iface->options)>1)
+ for (i=0; i<g_list_length(iface->options); i++)
+ {
+ ptr = g_strdup_printf("ifconfig %s 0.0.0.0", iface->name);
+ nc_system(ptr);
+ free(ptr);
+ }
+ ptr = g_strdup_printf("ifconfig %s down", iface->name);
+ nc_system(ptr);
+ free(ptr);
+ return(0);
+ }
+ return(1);
+}
+
+int ifup(interface_t *iface)
+{
+ int dhcp, i;
+ char *ptr;
+
+ dhcp = is_dhcp(iface);
+ // initialize the device
+ if(strlen(iface->mac))
+ {
+ ptr = g_strdup_printf("ifconfig %s hw ether %s", iface->name, iface->mac);
+ nc_system(ptr);
+ free(ptr);
+ }
+ if(strlen(iface->essid))
+ {
+ ptr = g_strdup_printf("iwconfig %s essid %s", iface->name, iface->essid);
+ nc_system(ptr);
+ free(ptr);
+ }
+
+ // set up the interface
+ if(dhcp)
+ {
+ ptr = g_strdup_printf("dhcpcd -t 10 %s", iface->name);
+ nc_system(ptr);
+ free(ptr);
+ }
+ else if(g_list_length(iface->options)==1)
+ {
+ ptr = g_strdup_printf("ifconfig %s %s",
+ iface->name, (char*)g_list_nth_data(iface->options, 0));
+ nc_system(ptr);
+ free(ptr);
+ }
+ else
+ {
+ ptr = g_strdup_printf("ifconfig %s 0.0.0.0", iface->name);
+ nc_system(ptr);
+ free(ptr);
+ for (i=0; i<g_list_length(iface->options); i++)
+ {
+ ptr = g_strdup_printf("ifconfig %s:%d %s",
+ iface->name, i+1, (char*)g_list_nth_data(iface->options, i));
+ nc_system(ptr);
+ free(ptr);
+ }
+ }
+
+ // setup the gateway
+ if(!dhcp && strlen(iface->gateway))
+ {
+ ptr = g_strdup_printf("route add %s", iface->gateway);
+ nc_system(ptr);
+ free(ptr);
+ }
+ return(0);
+}
+
+int setdns(profile_t* profile)
+{
+ int i;
+ FILE *fp=NULL;
+
+ if(g_list_length(profile->dnses))
+ {
+ if(!nco_dryrun)
+ fp = fopen("/etc/resolv.conf", "w");
+ if(nco_dryrun || (fp != NULL))
+ {
+ for (i=0; i<g_list_length(profile->dnses); i++)
+ if(nco_dryrun)
+ printf("nameserver %s\n", (char*)g_list_nth_data(profile->dnses, i));
+ else
+ fprintf(fp, "nameserver %s\n", (char*)g_list_nth_data(profile->dnses, i));
+ if(!nco_dryrun)
+ fclose(fp);
+ }
+ return(1);
+ }
+ return(0);
+}
+
hunk ./src/config/netconfig.c 336
- char *fn=NULL, *ptr;
- int i, j;
+ char *fn=NULL;
+ int i;
hunk ./src/config/netconfig.c 341
- FILE *fp=NULL, *input = stdin;
+ FILE *input = stdin;
hunk ./src/config/netconfig.c 374
- // step 1: shut down the interfaces
- int dhcp=0;
hunk ./src/config/netconfig.c 375
- for (j=0; j<g_list_length(iface->options); j++)
- {
- if(!strcmp((char*)g_list_nth_data(iface->options, j), "dhcp"))
- dhcp=1;
- }
- if(dhcp)
- {
- char line[7];
- ptr = g_strdup_printf("/etc/dhcpc/dhcpcd-%s.pid", iface->name);
- fp = fopen(ptr, "r");
- free(ptr);
- if(fp != NULL)
- {
- fgets(line, 6, fp);
- fclose(fp);
- j = atoi(line);
- if(j>0 && !nco_dryrun)
- kill(j, 15);
- else if (j>0)
- printf("kill(%d, 15);\n", j);
- }
- }
- else
- {
- if(g_list_length(iface->options)>1)
- for (j=0; j<g_list_length(iface->options); j++)
- {
- ptr = g_strdup_printf("ifconfig %s 0.0.0.0", iface->name);
- nc_system(ptr);
- free(ptr);
- }
- ptr = g_strdup_printf("ifconfig %s down", iface->name);
- nc_system(ptr);
- free(ptr);
- }
- // step2: bring up the interfaces
- if(strlen(iface->mac))
- {
- ptr = g_strdup_printf("ifconfig %s hw ether %s", iface->name, iface->mac);
- nc_system(ptr);
- free(ptr);
- }
- if(strlen(iface->essid))
- {
- ptr = g_strdup_printf("iwconfig %s essid %s", iface->name, iface->essid);
- nc_system(ptr);
- free(ptr);
- }
- if(dhcp)
- {
- ptr = g_strdup_printf("dhcpcd -t 10 %s", iface->name);
- nc_system(ptr);
- free(ptr);
- }
- else if(g_list_length(iface->options)==1)
- {
- ptr = g_strdup_printf("ifconfig %s %s",
- iface->name, (char*)g_list_nth_data(iface->options, 0));
- nc_system(ptr);
- free(ptr);
- }
- else
- {
- ptr = g_strdup_printf("ifconfig %s 0.0.0.0", iface->name);
- nc_system(ptr);
- free(ptr);
- for (j=0; j<g_list_length(iface->options); j++)
- {
- ptr = g_strdup_printf("ifconfig %s:%d %s",
- iface->name, j+1, (char*)g_list_nth_data(iface->options, j));
- nc_system(ptr);
- free(ptr);
- }
- }
- // step3: setup the gateway
- if(!dhcp && strlen(iface->gateway))
- {
- ptr = g_strdup_printf("route add %s", iface->gateway);
- nc_system(ptr);
- free(ptr);
- }
- }
- // step4: set dns
- if(g_list_length(profile->dnses))
- {
- if(!nco_dryrun)
- fp = fopen("/etc/resolv.conf", "w");
- if(nco_dryrun || (fp != NULL))
- {
- for (i=0; i<g_list_length(profile->dnses); i++)
- if(nco_dryrun)
- printf("nameserver %s\n", (char*)g_list_nth_data(profile->dnses, i));
- else
- fprintf(fp, "nameserver %s\n", (char*)g_list_nth_data(profile->dnses, i));
- if(!nco_dryrun)
- fclose(fp);
- }
+ ifdown(iface);
+ ifup(iface);
hunk ./src/config/netconfig.c 378
+ setdns(profile);
}
More information about the Frugalware-darcs
mailing list