summaryrefslogtreecommitdiff
path: root/application/spidlib
diff options
context:
space:
mode:
authorJérome Lefranc2008-11-28 17:00:31 +0100
committerJérome Lefranc2008-11-28 17:00:31 +0100
commitbff0200978655dec669336b70f1ac41784a18561 (patch)
tree29ae8d6b907b145306db46c9babd22705632c565 /application/spidlib
parent9c0c6e5afe319c06c1bfee6dfc19404e83014851 (diff)
[spidlib] add get_ip_data + unit test
Diffstat (limited to 'application/spidlib')
-rw-r--r--application/spidlib/Makefile2
-rw-r--r--application/spidlib/check/testfiles/network_file_18
-rw-r--r--application/spidlib/check/testfiles/network_file_218
-rw-r--r--application/spidlib/src/network/ip_addr.c9
-rw-r--r--application/spidlib/src/network/ip_misc.c262
-rw-r--r--application/spidlib/src/network/mac.c11
6 files changed, 306 insertions, 4 deletions
diff --git a/application/spidlib/Makefile b/application/spidlib/Makefile
index d44e8c1835..a3071ab99a 100644
--- a/application/spidlib/Makefile
+++ b/application/spidlib/Makefile
@@ -9,7 +9,7 @@ INCLUDES=spidlib.h ethernet.h network.h plc.h services.h system.h
OBJS = 8021x_supplicant_state.o 8021x_supplicant_identity.o 8021x_supplicant_password.o \
8021x_auth_server_addr.o 8021x_auth_server_port.o 8021x_auth_server_secret.o 8021x_auth_mode.o 8021x_auth_state.o \
afe_version.o bands.o bridge.o board_version.o boot_version.o bootlog_messages.o bssid.o\
- cpuinfo.o default_gain.o duplex.o functions.o ftp_state.o host.o hostname.o http_state.o interrupts.o ip_addr.o \
+ cpuinfo.o default_gain.o duplex.o functions.o ftp_state.o host.o hostname.o http_state.o interrupts.o ip_addr.o ip_misc.o \
ip_netmask.o ip_mode.o mac.o mac_limit.o mac_list.o meminfo.o \
mode.o nb_hosts.o processes.o read_config_param.o route_default.o snmp_state.o software_version.o serial_state.o \
serial_number.o speed.o static_spy.o system_version.o telnet_state.o tei.o uptime.o \
diff --git a/application/spidlib/check/testfiles/network_file_1 b/application/spidlib/check/testfiles/network_file_1
new file mode 100644
index 0000000000..a4f3c67822
--- /dev/null
+++ b/application/spidlib/check/testfiles/network_file_1
@@ -0,0 +1,8 @@
+eth0 Link encap:Ethernet HWaddr 00:13:D7:20:04:29
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
+ RX packets:5 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:32055 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:1000
+ RX bytes:396 (396.0 B) TX bytes:1923373 (1.8 MiB)
+ Interrupt:1
+
diff --git a/application/spidlib/check/testfiles/network_file_2 b/application/spidlib/check/testfiles/network_file_2
new file mode 100644
index 0000000000..5550d88d9f
--- /dev/null
+++ b/application/spidlib/check/testfiles/network_file_2
@@ -0,0 +1,18 @@
+auto lo eth0 plc0 br0
+
+iface lo inet loopback
+
+iface eth0 inet static
+ address 0.0.0.0
+ netmask 255.255.255.0
+
+iface plc0 inet static
+ address 192.168.2.2
+ netmask 255.255.255.255
+
+iface br0 inet static
+ address 192.168.2.1
+ netmask 255.255.255.0
+ broadcast 192.168.2.255
+ gateway 192.168.2.144
+
diff --git a/application/spidlib/src/network/ip_addr.c b/application/spidlib/src/network/ip_addr.c
index d2c085e53e..5d32b23773 100644
--- a/application/spidlib/src/network/ip_addr.c
+++ b/application/spidlib/src/network/ip_addr.c
@@ -35,10 +35,15 @@
int
spidlib_get_ip_addr(const char *iface, char *ip, int buffer_len)
{
-#if 0
FILE *fp;
char buffer[128];
- char *p;
+ char *ptr;
+
+ if((iface == NULL) || (ip == NULL) || (buffer_len < IP_ADDRESS_STRING_MAX_LENGTH))
+ return -(errno = EINVAL);
+#if 0
+
+
int state;
spidlib_boolean_t found_iface = SPIDLIB_FALSE;
spidlib_boolean_t found_ip = SPIDLIB_FALSE;
diff --git a/application/spidlib/src/network/ip_misc.c b/application/spidlib/src/network/ip_misc.c
new file mode 100644
index 0000000000..96e38ea1c7
--- /dev/null
+++ b/application/spidlib/src/network/ip_misc.c
@@ -0,0 +1,262 @@
+/*-----------------------------------------------------------------------------
+ File: ip_misc.c
+ Description: Functions to retrieve and set IP data.
+ Project: SPiDLIB
+ Target: SPiDCOM modem w/ SPC200
+ Version: 0.1
+ Revision list: -
+ Company: SPiDCOM
+ Author: Jerome Lefranc
+ Date: Nov 2008.
+-----------------------------------------------------------------------------*/
+
+/*---------------------- Include files --------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "spidlib.h"
+
+/*-----------------------------------------------------------------------------
+ Function: spidlib_get_ip_data
+ Input: char *iface - pointer to string containing interface.
+ char *text - pointer to beginning of buffer where output information is
+ expected
+ int buffer_len - max. length of resulting string
+ Output: 0 - Function returned successfully
+ -errno - There were errors
+ Error handling: Look for output value. In case of error check errno variable:
+ EINVAL - invalid buffer length
+ ENOENT - there were errors while reading entry
+ EFAULT - no IP address field found in a file
+ Note: -
+-----------------------------------------------------------------------------*/
+int
+spidlib_get_ip_data(const char *iface, char *address, char *netmask, char* broadcast, char* gateway)
+{
+ FILE *fp;
+ char buffer[128], label[32], value[32];
+ char *ptr;
+ int result;
+
+ /* check args */
+ if((iface == NULL) || (address == NULL) || (netmask == NULL)
+ || (broadcast == NULL) || (gateway == NULL))
+ return -(errno = EINVAL);
+
+ /* Check for valid interface */
+ if(!SPIDLIB_IS_INTERFACE_VALID(iface))
+ return (-(errno = EINVAL));
+
+ strcpy(address, "0.0.0.0");
+ strcpy(netmask, "255.255.255.0");
+ strcpy(broadcast, "0.0.0.0");
+ strcpy(gateway, "0.0.0.0");
+
+#ifdef CHECK
+ fp = stdin;
+#else
+ if((fp = fopen(NETWORK_INTERFACES_PATH, "r")) == NULL)
+ {
+ syslog(LOG_ERR, "spidlib_get_ip_data: cannot open %s (errno=%d)\n", NETWORK_INTERFACES_PATH, errno);
+ return -errno;
+ }
+#endif
+
+ while(fgets(buffer, 128 - 1, fp))
+ {
+ printf("buffer='%s'\n", buffer);
+ /*if((*/result = sscanf(buffer, "%s %s %*n %*n %*n\n", label, value)/*) > 0)*/;
+ {
+ printf("label='%s', value='%s'\n", label, value);
+ if(strcmp(value, iface))
+ continue;
+ while(fscanf(fp, "%s %s\n", label, value) == 2)
+ {
+ if(!strcmp(label, "address"))
+ {
+ strcpy(address, value);
+ continue;
+ }
+ if(!strcmp(label, "netmask"))
+ {
+ strcpy(netmask, value);
+ continue;
+ }
+ if(!strcmp(label, "broadcast"))
+ {
+ strcpy(broadcast, value);
+ continue;
+ }
+ if(!strcmp(label, "gateway"))
+ {
+ strcpy(gateway, value);
+ continue;
+ }
+ }
+#ifndef CHECK
+ fclose(fp);
+#endif
+ return 0;
+ }
+ }
+#ifndef CHECK
+ fclose(fp);
+#endif
+ return -(errno = ENOENT);
+}
+
+/*-----------------------------------------------------------------------------
+ Function: spidlib_check_ip_addr
+ Input: char *text - pointer to beginning of buffer where IP address to be
+ checked is located
+ Output: 0 - Function returned successfully
+ -errno - There were errors during check
+ Error handling: Look for output value. In case of error check errno variable:
+ ERANGE - string too long
+ EINVAL - invalid format of the string Note: -
+-----------------------------------------------------------------------------*/
+#if 0
+ int
+spidlib_check_ip_addr(char *text)
+{
+ /* Check max. string length */
+ if ( strlen(text) > IP_ADDRESS_STRING_MAX_LENGTH )
+ return (-(errno = ERANGE));
+
+ /* Check for valid string */
+ if ( test_ip_addr(text) )
+ return (-(errno = EINVAL));
+
+ return 0;
+}
+
+/*-----------------------------------------------------------------------------
+ Function: spidlib_set_ip_addr
+ Input: char *text - pointer to beginning of buffer where IP address to be
+ set is located
+ Output: 0 - Function returned successfully
+ -errno - There were errors during check
+ Error handling: Look for output value. In case of error check errno variable:
+ EINVAL - buffer length less or equal to zero or invalid format
+ of input string
+ EFAULT - system call to ifconfig failed
+ ERANGE - input string too long
+ Note: -
+-----------------------------------------------------------------------------*/
+int
+spidlib_set_ip_addr(const char *iface, char *text)
+{
+ FILE *fp_read, *fp_write;
+ char buffer[128];
+ char tmp_buffer[128];
+ char *p;
+ int state;
+ spidlib_boolean_t found_iface = SPIDLIB_FALSE;
+ spidlib_boolean_t found_ip = SPIDLIB_FALSE;
+ int result;
+
+ /* Checking IP address format */
+ result = spidlib_check_ip_addr(text);
+ if ( result )
+ {
+ errno = -result;
+ return (result);
+ }
+
+ /* Check for valid interface */
+ if (strcmp(iface,"br0") && strcmp(iface,"plc0") && strcmp(iface,"eth0"))
+ {
+ return (-(errno = EINVAL));
+ }
+
+ /* Change IP address */
+ if ( (NULL != (fp_read = fopen(NETWORK_INTERFACES_PATH,"r")))
+ && (NULL != (fp_write = fopen(NETWORK_INTERFACES_PATH,"w"))) )
+ {
+ state = 0;
+ while(fgets(buffer,128,fp_read))
+ {
+ memcpy(tmp_buffer, buffer, 128);
+ if ((p=strtok(buffer," \t\n")))
+ {
+ /* Write into new file */
+ if (strcmp(p,"address") || !found_iface)
+ fputs(tmp_buffer, fp_write);
+
+ if (!strcmp(p,"auto"))
+ continue;
+ else if (!strcmp(p,"iface"))
+ state = 1;
+ else if (!strcmp(p,"address") && found_iface)
+ state = 2;
+ else
+ {
+ state = 0;
+ continue;
+ }
+
+ if ((p=strtok(NULL," \t\n")))
+ {
+ switch(state)
+ {
+ /* Look for iface */
+ case 1:
+ if (!strcmp(p,iface))
+ {
+ found_iface = SPIDLIB_TRUE;
+ continue;
+ }
+ else
+ {
+ found_iface = SPIDLIB_FALSE;
+ state = 0;
+ continue;
+ }
+ break;
+ /* Get address from file, flag address found */
+ case 2:
+ /* Write into new file */
+ fprintf(fp_write, "\taddress %s\n", text);
+
+ state = 0;
+ found_ip = SPIDLIB_TRUE;
+ break;
+ default:
+ state = 0;
+ continue;
+ }
+ }
+ }
+ else
+ {
+ /* Write into new file */
+ fputs(tmp_buffer, fp_write);
+ }
+ }
+
+ fclose(fp_read);
+ fclose(fp_write);
+
+ /* Replace old file by new one */
+ if (0 != rename(NETWORK_INTERFACES_PATH, NETWORK_INTERFACES_PATH))
+ {
+ return errno;
+ }
+
+ /* Check if IP found */
+ if ( !found_ip )
+ {
+ return (-(errno = EFAULT));
+ }
+ }
+ else
+ {
+ /* Return 'No such file or directory' error */
+ return (-(errno = ENOENT));
+ }
+
+ return 0;
+}
+#endif
diff --git a/application/spidlib/src/network/mac.c b/application/spidlib/src/network/mac.c
index 6884d53325..7e47732f5e 100644
--- a/application/spidlib/src/network/mac.c
+++ b/application/spidlib/src/network/mac.c
@@ -51,10 +51,15 @@ spidlib_get_mac(const char *iface, char *mac, int buffer_len)
return (-(errno = EINVAL));
/* start ifconfig */
- if ((fp = popen("ifconfig","r")) == NULL)
+#ifdef CHECK
+ fp = stdin;
+#else
+ sprintf(buffer, "ifconfig %s", iface);
+ if ((fp = popen(buffer,"r")) == NULL)
{
return -errno;
}
+#endif
/* get 1st line */
if(fgets(buffer, 128 - 1, fp) == NULL)
@@ -75,10 +80,14 @@ spidlib_get_mac(const char *iface, char *mac, int buffer_len)
/* give mac */
strncpy(mac, ptr, MAC_STRING_MAX_LEN);
mac[MAC_STRING_MAX_LEN - 1] = '\0';
+#ifndef CHECK
fclose(fp);
+#endif
return 0;
error:
+#ifndef CHECK
fclose(fp);
+#endif
return -errno;
}