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 <jacob.e.keller@intel.com>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Jacob Keller 2012-08-20 10:56:45 -07:00 committed by Richard Cochran
parent 31865b2589
commit 733d4ccf9e
3 changed files with 28 additions and 6 deletions

View File

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

View File

@ -55,5 +55,6 @@ struct config {
};
int config_read(char *name, struct config *cfg);
int config_create_interface(char *name, struct config *cfg);
#endif

View File

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