From 733d4ccf9eb12c84403ff0ede548016d00ac2caf Mon Sep 17 00:00:00 2001 From: Jacob Keller Date: Mon, 20 Aug 2012 10:56:45 -0700 Subject: [PATCH] ptp4l: new config_create_interface function this patch adds a support function that creates a new port based on the current default settings and adds it to the iface list. The function returns the index into the cfg where the port was created. If a port is attempted to be created multiple times, future attempts just return the index -v2 * Move assignment of pointer into array after bounds check [ RC - fix off by one return code from config_create_interface ] Signed-off-by: Jacob Keller Signed-off-by: Richard Cochran --- config.c | 26 ++++++++++++++++++++++++++ config.h | 1 + ptp4l.c | 7 +------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config.c b/config.c index 27867b0..8ce8ad4 100644 --- a/config.c +++ b/config.c @@ -155,3 +155,29 @@ int config_read(char *name, struct config *cfg) fclose(fp); return 0; } + +/* returns the number matching that interface, or -1 on failure */ +int config_create_interface(char *name, struct config *cfg) +{ + struct interface *iface; + int i; + + if (cfg->nports >= MAX_PORTS) { + return -1; + } + + iface = &cfg->iface[cfg->nports]; + + /* only create each interface once (by name) */ + for(i = 0; i < cfg->nports; i++) { + if (0 == strncmp(name, cfg->iface[i].name, MAX_IFNAME_SIZE)) + return i; + } + + strncpy(iface->name, name, MAX_IFNAME_SIZE); + iface->dm = cfg->dm; + iface->transport = cfg->transport; + cfg->nports++; + + return i; +} diff --git a/config.h b/config.h index 306fd48..cdade16 100644 --- a/config.h +++ b/config.h @@ -55,5 +55,6 @@ struct config { }; int config_read(char *name, struct config *cfg); +int config_create_interface(char *name, struct config *cfg); #endif diff --git a/ptp4l.c b/ptp4l.c index 8c47ee1..7aa872a 100644 --- a/ptp4l.c +++ b/ptp4l.c @@ -151,12 +151,7 @@ int main(int argc, char *argv[]) config = optarg; break; case 'i': - if (*nports < MAX_PORTS) { - strncpy(iface[*nports].name, optarg, MAX_IFNAME_SIZE); - iface[*nports].dm = *dm; - iface[*nports].transport = *transport; - (*nports)++; - } else { + if (config_create_interface(optarg, &cfg_settings) < 0) { fprintf(stderr, "too many interfaces\n"); return -1; }