sock/child: remove global variable addrlen.
This changes listen_sock() to not return the addrlen of the used address from getaddrinfo call to the caller, stored in global addrlen in child.c. This was only used to be able to allocate enough space for the arguments to the later accept call depending on whether IPv4 or IPv6 is used. This removes the need to pass this info by always allocating sizeof(struct sockaddr_storage) instead, which is enough to carry both sockaddr_in and sockaddr_in6. Signed-off-by: Michael Adam <obnox@samba.org>
This commit is contained in:
parent
4bbd6e8626
commit
7eea1638bc
@ -33,7 +33,6 @@
|
|||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
|
|
||||||
static int listenfd;
|
static int listenfd;
|
||||||
static socklen_t addrlen;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Stores the internal data needed for each child (connection)
|
* Stores the internal data needed for each child (connection)
|
||||||
@ -188,7 +187,8 @@ static void child_main (struct child_s *ptr)
|
|||||||
struct sockaddr *cliaddr;
|
struct sockaddr *cliaddr;
|
||||||
socklen_t clilen;
|
socklen_t clilen;
|
||||||
|
|
||||||
cliaddr = (struct sockaddr *) safemalloc (addrlen);
|
cliaddr = (struct sockaddr *)
|
||||||
|
safemalloc (sizeof(struct sockaddr_storage));
|
||||||
if (!cliaddr) {
|
if (!cliaddr) {
|
||||||
log_message (LOG_CRIT,
|
log_message (LOG_CRIT,
|
||||||
"Could not allocate memory for child address.");
|
"Could not allocate memory for child address.");
|
||||||
@ -200,7 +200,7 @@ static void child_main (struct child_s *ptr)
|
|||||||
while (!config.quit) {
|
while (!config.quit) {
|
||||||
ptr->status = T_WAITING;
|
ptr->status = T_WAITING;
|
||||||
|
|
||||||
clilen = addrlen;
|
clilen = sizeof(struct sockaddr_storage);
|
||||||
|
|
||||||
connfd = accept (listenfd, cliaddr, &clilen);
|
connfd = accept (listenfd, cliaddr, &clilen);
|
||||||
|
|
||||||
@ -466,7 +466,7 @@ void child_kill_children (int sig)
|
|||||||
|
|
||||||
int child_listening_sock (uint16_t port)
|
int child_listening_sock (uint16_t port)
|
||||||
{
|
{
|
||||||
listenfd = listen_sock (port, &addrlen);
|
listenfd = listen_sock (port);
|
||||||
return listenfd;
|
return listenfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/sock.c
11
src/sock.c
@ -163,12 +163,10 @@ int socket_blocking (int sock)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start listening to a socket. Create a socket with the selected port.
|
* Start listening on a socket. Create a socket with the selected port.
|
||||||
* The size of the socket address will be returned to the caller through
|
* The socket fd is returned upon success, -1 upon error.
|
||||||
* the pointer, while the socket is returned as a default return.
|
|
||||||
* - rjkaes
|
|
||||||
*/
|
*/
|
||||||
int listen_sock (uint16_t port, socklen_t * addrlen)
|
int listen_sock (uint16_t port)
|
||||||
{
|
{
|
||||||
struct addrinfo hints, *result, *rp;
|
struct addrinfo hints, *result, *rp;
|
||||||
char portstr[6];
|
char portstr[6];
|
||||||
@ -176,7 +174,6 @@ int listen_sock (uint16_t port, socklen_t * addrlen)
|
|||||||
const int on = 1;
|
const int on = 1;
|
||||||
|
|
||||||
assert (port > 0);
|
assert (port > 0);
|
||||||
assert (addrlen != NULL);
|
|
||||||
|
|
||||||
memset (&hints, 0, sizeof (struct addrinfo));
|
memset (&hints, 0, sizeof (struct addrinfo));
|
||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = AF_UNSPEC;
|
||||||
@ -227,8 +224,6 @@ int listen_sock (uint16_t port, socklen_t * addrlen)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*addrlen = rp->ai_addrlen;
|
|
||||||
|
|
||||||
freeaddrinfo (result);
|
freeaddrinfo (result);
|
||||||
|
|
||||||
return listenfd;
|
return listenfd;
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#define MAXLINE (1024 * 4)
|
#define MAXLINE (1024 * 4)
|
||||||
|
|
||||||
extern int opensock (const char *host, int port, const char *bind_to);
|
extern int opensock (const char *host, int port, const char *bind_to);
|
||||||
extern int listen_sock (uint16_t port, socklen_t * addrlen);
|
extern int listen_sock (uint16_t port);
|
||||||
|
|
||||||
extern int socket_nonblocking (int sock);
|
extern int socket_nonblocking (int sock);
|
||||||
extern int socket_blocking (int sock);
|
extern int socket_blocking (int sock);
|
||||||
|
Loading…
Reference in New Issue
Block a user