aboutsummaryrefslogtreecommitdiff
path: root/src/addrfmt.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2009-10-12 23:41:21 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2009-10-12 23:42:04 +0300
commit8a4ba77068e5d7f6eab2cc1c1c10f31dcbccf7a6 (patch)
treec70f38d943c778684bb9dbfc7db018e7efb21bf1 /src/addrfmt.c
parentaf04ed630f18e9003756317cc627a11084d0d59a (diff)
downloadpies-8a4ba77068e5d7f6eab2cc1c1c10f31dcbccf7a6.tar.gz
pies-8a4ba77068e5d7f6eab2cc1c1c10f31dcbccf7a6.tar.bz2
Fix make distcheck and check-docs.
* doc/Makefile.am: Fix `check-*' goals. * doc/pies.texi: Update and rearrange material. Document new configuration. * lib/Makefile.am (libpies_a_SOURCES): Remove nls.c * src/Makefile.am (EXTRA_DIST): Remove pies.rc, add pp-setup. (INCLUDES): Add $(top_builddir)/gnu * src/pies.c: Minor changes. * src/progman.c: Minor changes. * README-hacking: New file.
Diffstat (limited to 'src/addrfmt.c')
-rw-r--r--src/addrfmt.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/addrfmt.c b/src/addrfmt.c
new file mode 100644
index 0000000..0687445
--- /dev/null
+++ b/src/addrfmt.c
@@ -0,0 +1,133 @@
+/* This file is part of Pies
+ Copyright (C) 2009 Sergey Poznyakoff
+
+ Pies is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Pies is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Pies. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#include "pies.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+int
+str2port (char *str)
+{
+ struct servent *serv;
+ char *p;
+ int port;
+
+ /* First try to read it from /etc/services */
+ serv = getservbyname (str, "tcp");
+
+ if (serv != NULL)
+ port = ntohs(serv->s_port);
+ else
+ {
+ unsigned long l;
+ /* Not in services, maybe a number? */
+ l = strtoul (str, &p, 0);
+
+ if (*p || l < 0 || l > USHRT_MAX)
+ return -1;
+
+ port = l;
+ }
+
+ return port;
+}
+
+static size_t
+_my_stpcpy (char **pbuf, size_t *psize, const char *src)
+{
+ size_t slen = strlen (src);
+ if (pbuf == NULL || *pbuf == NULL)
+ return slen;
+ else
+ {
+ char *buf = *pbuf;
+ size_t size = *psize;
+ if (size > slen)
+ size = slen;
+ memcpy (buf, src, size);
+ *psize -= size;
+ *pbuf += size;
+ if (*psize)
+ **pbuf = 0;
+ else
+ (*pbuf)[-1] = 0;
+ return size;
+ }
+}
+
+#define S_UN_NAME(sa, salen) \
+ ((salen < offsetof (struct sockaddr_un,sun_path)) ? "" : (sa)->sun_path)
+
+void
+sockaddr_to_str (const struct sockaddr *sa, int salen,
+ char *bufptr, size_t buflen,
+ size_t *plen)
+{
+ char buf[INT_BUFSIZE_BOUND (uintmax_t)]; /* FIXME: too much */
+ size_t len = 0;
+ switch (sa->sa_family)
+ {
+ case AF_INET:
+ {
+ struct sockaddr_in s_in = *(struct sockaddr_in *)sa;
+ len += _my_stpcpy (&bufptr, &buflen, inet_ntoa(s_in.sin_addr));
+ len += _my_stpcpy (&bufptr, &buflen, ":");
+ len += _my_stpcpy (&bufptr, &buflen,
+ umaxtostr(ntohs (s_in.sin_port), buf));
+ break;
+ }
+
+ case AF_UNIX:
+ {
+ struct sockaddr_un *s_un = (struct sockaddr_un *)sa;
+ if (S_UN_NAME(s_un, salen)[0] == 0)
+ len += _my_stpcpy (&bufptr, &buflen, "anonymous socket");
+ else
+ {
+ len += _my_stpcpy (&bufptr, &buflen, "socket ");
+ len += _my_stpcpy (&bufptr, &buflen, s_un->sun_path);
+ }
+ break;
+ }
+
+ default:
+ len += _my_stpcpy (&bufptr, &buflen, "{Unsupported family: ");
+ len += _my_stpcpy (&bufptr, &buflen, umaxtostr (sa->sa_family, buf));
+ len += _my_stpcpy (&bufptr, &buflen, "}");
+ }
+ if (plen)
+ *plen = len + 1;
+}
+
+char *
+sockaddr_to_astr (const struct sockaddr *sa, int salen)
+{
+ size_t size;
+ char *p;
+
+ sockaddr_to_str(sa, salen, NULL, 0, &size);
+ p = xmalloc (size);
+ sockaddr_to_str(sa, salen, p, size, NULL);
+ return p;
+}

Return to:

Send suggestions and report system problems to the System administrator.