X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Fdsi%2Fdsi_tcp.c;h=37a00bf40c2b163904412d72bb2d43808f36729d;hb=d525573d235fbbba664d69e90450c7b82eb05835;hp=9c44116a8494944fa15a96346c4b31e02ab9d6de;hpb=e2b3be108f5d8757324c98d63df42b0c11835f82;p=netatalk.git diff --git a/libatalk/dsi/dsi_tcp.c b/libatalk/dsi/dsi_tcp.c index 9c44116a..37a00bf4 100644 --- a/libatalk/dsi/dsi_tcp.c +++ b/libatalk/dsi/dsi_tcp.c @@ -1,6 +1,4 @@ /* - * $Id: dsi_tcp.c,v 1.25 2009-12-08 22:34:37 didg Exp $ - * * Copyright (c) 1997, 1998 Adrian Sun (asun@zoology.washington.edu) * All rights reserved. See COPYRIGHT. * @@ -17,9 +15,7 @@ #include #include #include -#ifdef HAVE_UNISTD_H #include -#endif /* HAVE_UNISTD_H */ #include #ifdef HAVE_NETDB_H #include @@ -27,10 +23,7 @@ #include #include #include - -#ifdef HAVE_STDINT_H #include -#endif /* HAVE_STDINT_H */ #include #ifdef TRU64 @@ -58,8 +51,7 @@ int deny_severity = log_warning; #include #include #include -#include -#include "dsi_private.h" +#include #define min(a,b) ((a) < (b) ? (a) : (b)) @@ -106,7 +98,7 @@ static int dsi_tcp_open(DSI *dsi) #ifdef TCPWRAP { struct request_info req; - request_init(&req, RQ_DAEMON, dsi->program, RQ_FILE, dsi->socket, NULL); + request_init(&req, RQ_DAEMON, "afpd", RQ_FILE, dsi->socket, NULL); fromhost(&req); if (!hosts_access(&req)) { LOG(deny_severity, logtype_dsi, "refused connect from %s", eval_client(&req)); @@ -124,7 +116,7 @@ static int dsi_tcp_open(DSI *dsi) if (0 == (pid = fork()) ) { /* child */ static struct itimerval timer = {{0, 0}, {DSI_TCPTIMEOUT, 0}}; struct sigaction newact, oldact; - u_int8_t block[DSI_BLOCKSIZ]; + uint8_t block[DSI_BLOCKSIZ]; size_t stored; /* reset signals */ @@ -272,14 +264,24 @@ iflist_done: #endif /* this needs to accept passed in addresses */ -int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, - const char *port, const int proxy) +int dsi_tcp_init(DSI *dsi, const char *hostname, const char *inaddress, const char *inport) { - int ret; + EC_INIT; int flag; + char *a = NULL, *b; + const char *address; + const char *port; struct addrinfo hints, *servinfo, *p; - dsi->protocol = DSI_TCPIP; + /* Check whether address is of the from IP:PORT and split */ + address = inaddress; + port = inport; + if (address && strchr(address, ':')) { + EC_NULL_LOG( address = a = strdup(address) ); + b = strchr(a, ':'); + *b = 0; + port = b + 1; + } /* Prepare hint for getaddrinfo */ memset(&hints, 0, sizeof hints); @@ -300,69 +302,64 @@ int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, hints.ai_family = AF_UNSPEC; #endif } - if ((ret = getaddrinfo(address ? address : NULL, port ? port : "548", &hints, &servinfo)) != 0) { + if ((ret = getaddrinfo(address ? address : NULL, port, &hints, &servinfo)) != 0) { LOG(log_error, logtype_dsi, "dsi_tcp_init: getaddrinfo: %s\n", gai_strerror(ret)); - return 0; + EC_FAIL; } - /* create a socket */ - if (proxy) - dsi->serversock = -1; - else { - /* loop through all the results and bind to the first we can */ - for (p = servinfo; p != NULL; p = p->ai_next) { - if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { - LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno)); - continue; - } + /* loop through all the results and bind to the first we can */ + for (p = servinfo; p != NULL; p = p->ai_next) { + if ((dsi->serversock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { + LOG(log_info, logtype_dsi, "dsi_tcp_init: socket: %s", strerror(errno)); + continue; + } - /* - * Set some socket options: - * SO_REUSEADDR deals w/ quick close/opens - * TCP_NODELAY diables Nagle - */ + /* + * Set some socket options: + * SO_REUSEADDR deals w/ quick close/opens + * TCP_NODELAY diables Nagle + */ #ifdef SO_REUSEADDR - flag = 1; - setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); + flag = 1; + setsockopt(dsi->serversock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)); #endif #if defined(FREEBSD) && defined(IPV6_BINDV6ONLY) - int on = 0; - setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on)); + int on = 0; + setsockopt(dsi->serversock, IPPROTO_IPV6, IPV6_BINDV6ONLY, (char *)&on, sizeof (on)); #endif #ifdef USE_TCP_NODELAY #ifndef SOL_TCP #define SOL_TCP IPPROTO_TCP #endif - flag = 1; - setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag)); + flag = 1; + setsockopt(dsi->serversock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag)); #endif /* USE_TCP_NODELAY */ - if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) { - close(dsi->serversock); - LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno)); - continue; - } - - if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) { - close(dsi->serversock); - LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno)); - continue; - } - - break; + if (bind(dsi->serversock, p->ai_addr, p->ai_addrlen) == -1) { + close(dsi->serversock); + LOG(log_info, logtype_dsi, "dsi_tcp_init: bind: %s\n", strerror(errno)); + continue; } - if (p == NULL) { - LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket"); - freeaddrinfo(servinfo); - return 0; + if (listen(dsi->serversock, DSI_TCPMAXPEND) < 0) { + close(dsi->serversock); + LOG(log_info, logtype_dsi, "dsi_tcp_init: listen: %s\n", strerror(errno)); + continue; } + + break; + } - /* Copy struct sockaddr to struct sockaddr_storage */ - memcpy(&dsi->server, p->ai_addr, p->ai_addrlen); + if (p == NULL) { + LOG(log_error, logtype_dsi, "dsi_tcp_init: no suitable network config for TCP socket"); freeaddrinfo(servinfo); - } /* if (proxy) */ + EC_FAIL; + } + + /* Copy struct sockaddr to struct sockaddr_storage */ + memcpy(&dsi->server, p->ai_addr, p->ai_addrlen); + freeaddrinfo(servinfo); /* Point protocol specific functions to tcp versions */ dsi->proto_open = dsi_tcp_open; @@ -372,7 +369,7 @@ int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, if (address) { /* address is a parameter, use it 'as is' */ - return 1; + goto EC_CLEANUP; } /* Prepare hint for getaddrinfo */ @@ -380,7 +377,7 @@ int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if ((ret = getaddrinfo(hostname, port ? port : "548", &hints, &servinfo)) != 0) { + if ((ret = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) { LOG(log_info, logtype_dsi, "dsi_tcp_init: getaddrinfo '%s': %s\n", hostname, gai_strerror(ret)); goto interfaces; } @@ -402,13 +399,17 @@ int dsi_tcp_init(DSI *dsi, const char *hostname, const char *address, /* Store found address in dsi->server */ memcpy(&dsi->server, p->ai_addr, p->ai_addrlen); freeaddrinfo(servinfo); - return 1; + goto EC_CLEANUP; } LOG(log_info, logtype_dsi, "dsi_tcp: hostname '%s' resolves to loopback address", hostname); freeaddrinfo(servinfo); interfaces: guess_interface(dsi, hostname, port ? port : "548"); - return 1; + +EC_CLEANUP: + if (a) + free(a); + EC_EXIT; }