[Nut-upsdev] Re: [nut-commits] svn commit r755 - in trunk: . clients

Peter Selinger selinger at mathstat.dal.ca
Fri Jan 19 07:06:23 CET 2007


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 at 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 at 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 at 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 at 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 at 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 at 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 at 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 at lists.alioth.debian.org
> http://lists.alioth.debian.org/mailman/listinfo/nut-commits
> 




More information about the Nut-upsdev mailing list