Peter Selinger
2007-Jan-19 07:07 UTC
[Nut-upsdev] Re: [nut-commits] svn commit r755 - in trunk: . clients
Great, thanks! Two questions: * what is a domain literal? Is this something like 192.168.0.1? In this case, the '[]' are probably unnecessary. * are you sure you want to use fprintf(stderr, ...) in a library? This doesn't seem like a good idea to me. Wouldn't it be more consistent to extend upscli_errlist[] ? -- Peter Arjen de Korte wrote:> > Author: adkorte-guest > Date: Thu Jan 18 22:07:08 2007 > New Revision: 755 > > Modified: > trunk/ChangeLog > trunk/clients/upsclient.c > trunk/clients/upsclient.h > Log: > This one is for you, Peter... :-) > > * clients/upsclient.c: > - hostname is now optional in upscli_splitname(), defaults to "localhost" > - added upscli_splitaddr() to split a hostname[:port] into separate > components > - [domain literals] are now allowed as valid hostnames > > Maybe we should make the default hostname for upscli_splitname configurable? > > Modified: trunk/ChangeLog > =============================================================================> --- trunk/ChangeLog (original) > +++ trunk/ChangeLog Thu Jan 18 22:07:08 2007 > @@ -1,3 +1,11 @@ > +Thu Jan 18 21:02:29 UTC 2007 / Arjen de Korte <arjen@de-korte.org> > + > + * clients/upsclient.c: > + - hostname is now optional in upscli_splitname(), defaults to "localhost" > + - added upscli_splitaddr() to split a hostname[:port] into separate > + components > + - [domain literals] are now allowed as valid hostnames > + > Thu Jan 18 18:29:43 UTC 2007 / Peter Selinger <selinger@users.sourceforge.net> > > * docs/developers.txt: added section on repository etiquette. > @@ -27,7 +35,6 @@ > * clients/upsclient.c: disable previous patch for upsclient (something is > broken somehow, but I don't have the time now to check it out) > > - > Mon Jan 15 20:20:36 UTC 2007 / Niels Baggesen <nba@users.sourceforge.net> > * drivers/upscode2.[ch]: > - major rework to support the new 3-phase variable set > > Modified: trunk/clients/upsclient.c > =============================================================================> --- trunk/clients/upsclient.c (original) > +++ trunk/clients/upsclient.c Thu Jan 18 22:07:08 2007 > @@ -912,60 +912,99 @@ > return 0; > } > > -/* split upsname@hostname[:port] into separate components */ > +/* split upsname[@hostname[:port]] into separate components */ > int upscli_splitname(const char *buf, char **upsname, char **hostname, int *port) > { > - char tmp[SMALLBUF], *s; > + char *s, tmp[SMALLBUF]; > > /* paranoia */ > if ((!buf) || (!upsname) || (!hostname) || (!port)) > return -1; > > - snprintf(tmp, sizeof(tmp), "%s", buf); > - > - /* split at the '@' character */ > - if ((s = strtok(tmp, "@")) == NULL) > + if (snprintf(tmp, SMALLBUF, "%s", buf) < 1) > { > - fprintf(stderr, "upscli_splitname: no UPS name specified (upsname@hostname)\n"); > + fprintf(stderr, "upscli_splitname: can't parse empty string\n"); > return -1; > } > > - if ((*upsname = strdup(s)) == NULL) > + s = strchr(tmp, '@'); > + > + if ((*upsname = strdup(strtok(tmp, "@"))) == NULL) > { > fprintf(stderr, "upscli_splitname: strdup failed\n"); > return -1; > } > > - if ((s = strtok(NULL, "\0")) == NULL) > + /* only a upsname is specified, fill in defaults */ > + if (s == NULL) > { > - fprintf(stderr, "upscli_splitname: no hostname specified (upsname@hostname)\n"); > - return -1; > + *hostname = "localhost"; > + *port = PORT; > + return 0; > } > > - if (*s == '[') > - s = strtok(s+1, "]"); /* address literal */ > - else > - s = strtok(s, ":\0"); /* hostname */ > - > - if (s == NULL) > + return upscli_splitaddr(s+1, hostname, port); > +} > + > +/* split hostname[:port] into separate components */ > +int upscli_splitaddr(const char *buf, char **hostname, int *port) > +{ > + char *s, tmp[SMALLBUF]; > + > + /* paranoia */ > + if ((!buf) || (!hostname) || (!port)) > + return -1; > + > + if (snprintf(tmp, SMALLBUF, "%s", buf) < 1) > { > - fprintf(stderr, "upscli_splitname: no hostname specified (upsname@hostname)\n"); > + fprintf(stderr, "upscli_splitaddr: can't parse empty string\n"); > return -1; > } > > - if ((*hostname = strdup(s)) == NULL) > + if (*tmp == '[') > { > - fprintf(stderr, "upscli_splitname: strdup failed\n"); > - return -1; > + if (strchr(tmp, ']') == NULL) > + { > + fprintf(stderr, "upscli_splitaddr: missing closing bracket in [domain literal]\n"); > + return -1; > + } > + > + if ((*hostname = strdup(strtok(tmp+1, "]"))) == NULL) > + { > + fprintf(stderr, "upscli_splitaddr: strdup failed\n"); > + return -1; > + } > + > + /* no port specified, use default */ > + if (((s = strtok(NULL, "\0")) == NULL) || (*s != ':')) > + { > + *port = PORT; > + return 0; > + } > } > + else > + { > + s = strchr(tmp, ':'); > > - /* skip the separator between hostname and port (if any) */ > - if (((s = strtok(NULL, "\0")) != NULL) && (*s == ':')) s++; > + if ((*hostname = strdup(strtok(tmp, ":"))) == NULL) > + { > + fprintf(stderr, "upscli_splitaddr: strdup failed\n"); > + return -1; > + } > + > + /* no port specified, use default */ > + if (s == NULL) > + { > + *port = PORT; > + return 0; > + } > + } > > - if (s == NULL) > - *port = PORT; > - else > - *port = strtol(s, NULL, 10); > + if ((*(++s) == '\0') || ((*port = strtol(s, NULL, 10)) < 1 )) > + { > + fprintf(stderr, "upscli_splitaddr: no port specified after ':' separator\n"); > + return -1; > + } > > return 0; > } > > Modified: trunk/clients/upsclient.h > =============================================================================> --- trunk/clients/upsclient.h (original) > +++ trunk/clients/upsclient.h Thu Jan 18 22:07:08 2007 > @@ -78,6 +78,8 @@ > int upscli_splitname(const char *buf, char **upsname, char **hostname, > int *port); > > +int upscli_splitaddr(const char *buf, char **hostname, int *port); > + > int upscli_sslcert(UPSCONN *ups, const char *file, const char *path, int verify); > > int upscli_disconnect(UPSCONN *ups); > > _______________________________________________ > nut-commits mailing list > nut-commits@lists.alioth.debian.org > http://lists.alioth.debian.org/mailman/listinfo/nut-commits >
Niels Baggesen
2007-Jan-19 07:32 UTC
[Nut-upsdev] Re: [nut-commits] svn commit r755 - in trunk: . clients
On Fri, Jan 19, 2007 at 02:06:23AM -0400, Peter Selinger wrote:> * what is a domain literal? Is this something like 192.168.0.1? In > this case, the '[]' are probably unnecessary.Not now that we have IPv6 support ... You cannot decide whether ::1:1234 includes a port number, unless you write it as [::1]:1234 or [::1:1234] And yes, this is the official standard way of writing it! /Niels -- Niels Baggesen - @home - ?rhus - Denmark - nba@users.sourceforge.net The purpose of computing is insight, not numbers --- R W Hamming
Seemingly Similar Threads
- Re: [nut-commits] svn commit r731
- Re: [nut-commits] svn commit r755 - in trunk: . clients
- Re: [nut-commits] svn commit r710 - in trunk: . clients server
- Re: [nut-commits] svn commit r708 - in trunk: . clients server
- upscli_splitname() for upsc_list (was: Re: Default NUT PORT)