summaryrefslogtreecommitdiff
path: root/libmailutils/server/ipsrv.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2011-03-30 00:49:43 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2011-03-30 15:51:47 +0300
commitadca074d616ac562095a92e01f2b181e6d3593c8 (patch)
treef4c11a1c11fcb137cfada7643e6f284465a75c16 /libmailutils/server/ipsrv.c
parent827cb66a4c639139c3f11c7af26aa5149f25a7e0 (diff)
downloadmailutils-adca074d616ac562095a92e01f2b181e6d3593c8.tar.gz
mailutils-adca074d616ac562095a92e01f2b181e6d3593c8.tar.bz2
Implement IPv6 support.
* am/ipv6.m4: New file. * examples/sa.c: New file. * include/mailutils/cidr.h: New file. * include/mailutils/sockaddr.h: New file. * libmailutils/cidr/Makefile.am: New file. * libmailutils/cidr/fromsa.c: New file. * libmailutils/cidr/fromstr.c: New file. * libmailutils/cidr/match.c: New file. * libmailutils/cidr/tosa.c: New file. * libmailutils/cidr/tostr.c: New file. * libmailutils/sockaddr/Makefile.am: New file. * libmailutils/sockaddr/copy.c: New file. * libmailutils/sockaddr/create.c: New file. * libmailutils/sockaddr/free.c: New file. * libmailutils/sockaddr/fromnode.c: New file. * libmailutils/sockaddr/insert.c: New file. * libmailutils/sockaddr/ipaddr.c: New file. * libmailutils/sockaddr/str.c: New file. * libmailutils/sockaddr/unlink.c: New file. * libmailutils/sockaddr/url.c: New file. * libmailutils/tests/cidr.c: New file. * configure.ac: Call MU_ENABLE_IPV6. Build libmailutils/sockaddr and libmailutils/cidr. * examples/.gitignore: Add mblconv and sa * examples/Makefile.am: (noinst_PROGRAMS): Add sa. * examples/aclck.c: Use new ACL API. * examples/echosrv.c: Use new mserv API. * include/mailutils/Makefile.am (pkginclude_HEADERS): Add cidr.h and sockaddr.h * include/mailutils/acl.h (mu_acl_append, mu_acl_prepend) (mu_acl_insert): Change signatures. * include/mailutils/debug.h (mu_sockaddr_to_str): Remove proto. * include/mailutils/mailutils.h: Include cidr.h and sockaddr.h * include/mailutils/server.h (mu_ip_server_create): Change signature. (mu_ip_server_get_sockaddr): Likewise. (mu_m_server_set_default_address) (mu_m_server_get_default_address): Remove. * include/mailutils/stream.h (mu_tcp_stream_create_from_sa): New proto. * include/mailutils/types.hin (mu_cidr, mu_sockaddr): New structs. * include/mailutils/url.h (MU_URL_IPV6): New flag. (MU_URL_PARSE_DSLASH_OPTIONAL): New parse flag. * libmailutils/Makefile.am: Descend into cidr and sockaddr. Link in libcidr and libsockaddr. * libmailutils/diag/debug.c (mu_debug_log_begin): Flush mu_strerr. * libmailutils/diag/errors (MU_ERR_NONAME) (MU_ERR_BADFLAGS,MU_ERR_SOCKTYPE) (MU_ERR_FAMILY,MU_ERR_SERVICE): New errors. * libmailutils/server/acl.c: Rewrite API using mu_cidr. * libmailutils/server/ipsrv.c: Rewrite AI using mu_sockaddr. * libmailutils/server/msrv.c: Likewise. * libmailutils/stream/tcp.c: Likewise. * libmailutils/tests/.gitignore: Add cidr. * libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add cidr. * libmailutils/tests/url-parse.c: Support command line options to tune the parsing. * libmailutils/tests/url.at: Pass options to url-parse. * libmailutils/url/create.c (getkn): Return meaningful error code. (_mu_url_ctx_parse_host): Accept IPv6 addresses. Set the MU_URL_IPV6 flag if one is given. (_mu_url_ctx_parse): Unless MU_URL_PARSE_DSLASH_OPTIONAL flag is given, request :// after scheme part. (mu_url_create): Add MU_URL_PARSE_DSLASH_OPTIONAL flag. * libmu_cfg/acl.c: Use new ACL API. * mu/acl.c: Likewise. * libproto/mailer/smtp.c (smtp_open): Use mu_tcp_stream_create_from_sa * libproto/pop/mbox.c (pop_open): Likewise. * mu/imap.c (com_connect): Likewise. * mu/pop.c (com_connect): Likewise. * testsuite/smtpsend.c (main): Likewise.
Diffstat (limited to 'libmailutils/server/ipsrv.c')
-rw-r--r--libmailutils/server/ipsrv.c83
1 files changed, 37 insertions, 46 deletions
diff --git a/libmailutils/server/ipsrv.c b/libmailutils/server/ipsrv.c
index a80290834..73b2e361e 100644
--- a/libmailutils/server/ipsrv.c
+++ b/libmailutils/server/ipsrv.c
@@ -34,13 +34,13 @@
#include <mailutils/diag.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
+#include <mailutils/sockaddr.h>
struct _mu_ip_server
{
char *ident;
- struct sockaddr *addr;
- int addrlen;
+ struct mu_sockaddr *addr;
int fd;
int type;
mu_acl_t acl;
@@ -66,8 +66,7 @@ struct _mu_ip_server
#define IDENTSTR(s) ((s)->ident ? (s)->ident : "default")
int
-mu_ip_server_create (mu_ip_server_t *psrv, struct sockaddr *addr,
- int addrlen, int type)
+mu_ip_server_create (mu_ip_server_t *psrv, struct mu_sockaddr *addr, int type)
{
struct _mu_ip_server *srv;
@@ -84,14 +83,7 @@ mu_ip_server_create (mu_ip_server_t *psrv, struct sockaddr *addr,
srv = calloc (1, sizeof *srv);
if (!srv)
return ENOMEM;
- srv->addr = calloc (1, addrlen);
- if (!srv->addr)
- {
- free (srv);
- return ENOMEM;
- }
- memcpy (srv->addr, addr, addrlen);
- srv->addrlen = addrlen;
+ srv->addr = addr;
srv->type = type;
srv->fd = -1;
switch (type)
@@ -120,7 +112,7 @@ mu_ip_server_destroy (mu_ip_server_t *psrv)
if (srv->f_free)
srv->f_free (srv->data);
close (srv->fd);
- free (srv->addr);
+ mu_sockaddr_free (srv->addr);
free (srv->ident);
if (srv->type == MU_IP_UDP && srv->v.udp_data.buf)
free (srv->v.udp_data.buf);
@@ -234,6 +226,11 @@ mu_address_family_to_domain (int family)
case AF_INET:
return PF_INET;
+#ifdef MAILUTILS_IPV6
+ case AF_INET6:
+ return PF_INET6;
+#endif
+
default:
abort ();
}
@@ -247,14 +244,11 @@ mu_ip_server_open (mu_ip_server_t srv)
if (!srv || srv->fd != -1)
return EINVAL;
- if (mu_debug_level_p (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0))
- {
- char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
- mu_debug_log ("opening server \"%s\" %s", IDENTSTR (srv), p);
- free (p);
- }
+ mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0,
+ ("opening server \"%s\" %s", IDENTSTR (srv),
+ mu_sockaddr_str (srv->addr)));
- fd = socket (mu_address_family_to_domain (srv->addr->sa_family),
+ fd = socket (mu_address_family_to_domain (srv->addr->addr->sa_family),
((srv->type == MU_IP_UDP) ? SOCK_DGRAM : SOCK_STREAM), 0);
if (fd == -1)
{
@@ -263,7 +257,7 @@ mu_ip_server_open (mu_ip_server_t srv)
return errno;
}
- switch (srv->addr->sa_family)
+ switch (srv->addr->addr->sa_family)
{
case AF_UNIX:
{
@@ -299,7 +293,7 @@ mu_ip_server_open (mu_ip_server_t srv)
}
break;
- case AF_INET:
+ default:
{
int t;
@@ -308,7 +302,7 @@ mu_ip_server_open (mu_ip_server_t srv)
}
}
- if (bind (fd, srv->addr, srv->addrlen) == -1)
+ if (bind (fd, srv->addr->addr, srv->addr->addrlen) == -1)
{
mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_ERROR,
("%s: bind: %s", IDENTSTR (srv), mu_strerror (errno)));
@@ -336,12 +330,9 @@ mu_ip_server_shutdown (mu_ip_server_t srv)
{
if (!srv || srv->fd != -1)
return EINVAL;
- if (mu_debug_level_p (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0))
- {
- char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
- mu_debug_log ("closing server \"%s\" %s", IDENTSTR (srv), p);
- free (p);
- }
+ mu_debug (MU_DEBCAT_SERVER, MU_DEBUG_TRACE0,
+ ("closing server \"%s\" %s", IDENTSTR (srv),
+ mu_sockaddr_str (srv->addr)));
close (srv->fd);
return 0;
}
@@ -356,6 +347,9 @@ mu_ip_tcp_accept (mu_ip_server_t srv, void *call_data)
struct sockaddr sa;
struct sockaddr_in s_in;
struct sockaddr_un s_un;
+#ifdef MAILUTILS_IPV6
+ struct sockaddr_in6 s_in6;
+#endif
} client;
socklen_t size = sizeof (client);
@@ -407,6 +401,9 @@ mu_ip_udp_accept (mu_ip_server_t srv, void *call_data)
struct sockaddr sa;
struct sockaddr_in s_in;
struct sockaddr_un s_un;
+#ifdef MAILUTILS_IPV6
+ struct sockaddr_in6 s_in6;
+#endif
} client;
fd_set rdset;
@@ -463,7 +460,7 @@ mu_ip_udp_accept (mu_ip_server_t srv, void *call_data)
IDENTSTR (srv), strerror (rc)));
if (res == mu_acl_result_deny)
{
- char *p = mu_sockaddr_to_astr (srv->addr, srv->addrlen);
+ char *p = mu_sockaddr_to_astr (&client.sa, salen);
mu_diag_output (MU_DIAG_INFO, "Denying connection from %s", p);
free (p);
return 0;
@@ -528,22 +525,16 @@ mu_udp_server_get_rdata (mu_ip_server_t srv, char **pbuf, size_t *pbufsize)
}
int
-mu_ip_server_get_sockaddr (mu_ip_server_t srv, struct sockaddr *s, int *size)
+mu_ip_server_get_sockaddr (mu_ip_server_t srv, struct mu_sockaddr **psa)
{
- int len;
-
- if (!srv || !s)
+ if (!srv || !psa)
return EINVAL;
- if (s == 0)
- len = srv->addrlen;
- else
- {
- len = srv->addrlen;
- if (*size < len)
- return MU_ERR_BUFSPACE;
- memcpy (s, srv->addr, len);
- }
- *size = len;
- return 0;
+ return mu_sockaddr_copy (psa, srv->addr);
}
-
+
+const char *
+mu_ip_server_addrstr (mu_ip_server_t srv)
+{
+ return mu_sockaddr_str (srv->addr);
+}
+

Return to:

Send suggestions and report system problems to the System administrator.