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; }