From 5f21c592b37dc8dba6eb21989afbb399188329a1 Mon Sep 17 00:00:00 2001 From: lefranc Date: Tue, 24 Nov 2009 09:03:58 +0000 Subject: cleo/appli/libspid: do not remove unchanged network param, closes #764 - libspid_network_set_ip does not remove anymore config lines different from address, netmask, broadcast and gateway git-svn-id: svn+ssh://pessac/svn/cesar/trunk@6459 017c9cb6-072f-447c-8318-d5b54f68fe89 --- cleopatre/application/libspid/src/network.c | 123 +++++++++++++--------------- 1 file changed, 57 insertions(+), 66 deletions(-) (limited to 'cleopatre/application/libspid') diff --git a/cleopatre/application/libspid/src/network.c b/cleopatre/application/libspid/src/network.c index c7ae34236c..200e4534dc 100644 --- a/cleopatre/application/libspid/src/network.c +++ b/cleopatre/application/libspid/src/network.c @@ -25,6 +25,13 @@ #include #include "libspid.h" +/* write ip state machine */ +enum { + SET_IP_SEARCH_IFACE, + SET_IP_PROCESS_IFACE, + SET_IP_DUMP, +}; + /** * Give the IP setting of a network interface.
* All data are coming from the configuration file content, not from the system status. @@ -195,12 +202,12 @@ libspid_error_t libspid_network_get_ip (const char *interface, libspid_ip_t *ip) libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_t *ip) { char line_buffer[LIBSPID_LINE_MAX_LEN]; - int skip = 0; /* flag that tells if we should keep these lines or not */ - int auto_exists = 0; /* flag set if line auto exists */ char out_filename[64]; char filename[] = LIBSPID_NETWORK_CONF_PATH; FILE *fp_in, *fp_out; int fd_in = 0; + int state = SET_IP_SEARCH_IFACE; + char label[16], value[16], dhcp[16]; if (interface == NULL || ip == NULL) { @@ -223,7 +230,7 @@ libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_ if ( (fp_out = fdopen(mkstemp(out_filename), "w")) == NULL ) { fclose(fp_in); - close(fd_in); + close(fd_in); return LIBSPID_ERROR_SYSTEM; } @@ -245,78 +252,62 @@ libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_ while ( fgets(line_buffer, LIBSPID_LINE_MAX_LEN - 1, fp_in) ) { - /* Start skipping lines (i.e. not writing them to output file) - * the moment we saw mentioned our interface, - * because section of configuration for a certain interface - * always begins with lines: - * auto - * or - * iface inet - * This way we will erase all previous configuration. - * New configuration will be added always in the end of a file. - */ - if ( ( strstr(line_buffer, "auto") != NULL ) || ( strstr(line_buffer, "iface") != NULL ) ) + switch(state) { - if ( strstr(line_buffer, interface) != NULL ) + case SET_IP_SEARCH_IFACE: + if((sscanf(line_buffer, "%16s %16s %*s %16s %*s\n", label, value, dhcp) > 0) + && !strcmp(label, "iface") && !strcmp(value, interface)) { - skip = 1; - /* check if it was a line that starts with "auto" */ - if ( strstr(line_buffer, "auto") != NULL ) + state = SET_IP_PROCESS_IFACE; + fprintf(fp_out, "iface %s inet %s\n", interface, (ip->mode == LIBSPID_IP_MODE_STATIC ? "static" : "dhcp")); + if ( strlen((char *)ip->address) > 0) { - auto_exists = 1; + fprintf(fp_out, " address %s\n", ip->address); + } + if ( strlen((char *)ip->netmask) > 0) + { + fprintf(fp_out, " netmask %s\n", ip->netmask); + } + if ( strlen((char *)ip->broadcast) > 0) + { + fprintf(fp_out, " broadcast %s\n", ip->broadcast); + } + if ( strlen((char *)ip->gateway) > 0) + { + fprintf(fp_out, " gateway %s\n", ip->gateway); } } else - { - skip = 0; - } - } - - if (skip == 1) - { - continue; - } - else - { - fprintf(fp_out, "%s", line_buffer); - } + fputs (line_buffer, fp_out); - } /* while */ - - /* append new configuration */ - fputc('\n', fp_out); - if (auto_exists) - { - fprintf(fp_out, "auto %s\n", interface); - } - switch (ip->mode) - { - case LIBSPID_IP_MODE_STATIC: - fprintf(fp_out, "iface %s inet static\n", interface); break; - case LIBSPID_IP_MODE_DHCP: - fprintf(fp_out, "iface %s inet dhcp\n", interface); + + case SET_IP_PROCESS_IFACE: + if(sscanf(line_buffer, "%16s %*s\n", label) == 1) + { + if (!strcmp(label, "iface")) + { + state = SET_IP_DUMP; + fputs (line_buffer, fp_out); + break; + } + if(strcmp (label, "address") && strcmp (label, "netmask") + && strcmp (label, "broadcast") && strcmp (label, "gateway")) + { + fputs (line_buffer, fp_out); + } + } + else + { + fputs (line_buffer, fp_out); + } break; + + case SET_IP_DUMP: default: - return LIBSPID_ERROR_PARAM; /* ip->mode not known */ + fputs (line_buffer, fp_out); break; - } - - if ( strlen((char *)ip->address) > 0) - { - fprintf(fp_out, "\taddress %s\n", ip->address); - } - if ( strlen((char *)ip->netmask) > 0) - { - fprintf(fp_out, "\tnetmask %s\n", ip->netmask); - } - if ( strlen((char *)ip->broadcast) > 0) - { - fprintf(fp_out, "\tbroadcast %s\n", ip->broadcast); - } - if ( strlen((char *)ip->gateway) > 0) - { - fprintf(fp_out, "\tgateway %s\n", ip->gateway); + } } fclose(fp_out); @@ -325,7 +316,7 @@ libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_ /* close lock */ fcntl(fd_in, F_UNLCK, &lock); #endif /* USE_LOCK */ - close(fd_in); + close(fd_in); if ( rename(out_filename, filename) < 0 ) { @@ -347,7 +338,7 @@ libspid_error_t libspid_network_set_ip (const char *interface, const libspid_ip_ */ libspid_error_t libspid_network_get_mac (const char *interface, unsigned char *mac) { - struct ifreq ifr; + struct ifreq ifr; int s; struct ether_addr ea; -- cgit v1.2.3