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
parent
31865b2589
commit
733d4ccf9e
26
config.c
26
config.c
|
@ -155,3 +155,29 @@ int config_read(char *name, struct config *cfg)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|
1
config.h
1
config.h
|
@ -55,5 +55,6 @@ struct config {
|
||||||
};
|
};
|
||||||
|
|
||||||
int config_read(char *name, struct config *cfg);
|
int config_read(char *name, struct config *cfg);
|
||||||
|
int config_create_interface(char *name, struct config *cfg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
7
ptp4l.c
7
ptp4l.c
|
@ -151,12 +151,7 @@ int main(int argc, char *argv[])
|
||||||
config = optarg;
|
config = optarg;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
if (*nports < MAX_PORTS) {
|
if (config_create_interface(optarg, &cfg_settings) < 0) {
|
||||||
strncpy(iface[*nports].name, optarg, MAX_IFNAME_SIZE);
|
|
||||||
iface[*nports].dm = *dm;
|
|
||||||
iface[*nports].transport = *transport;
|
|
||||||
(*nports)++;
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "too many interfaces\n");
|
fprintf(stderr, "too many interfaces\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue