(init_socket): bind to a particular address

(init_sockets): get all local addresses and bind to them all


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@5260 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1998-11-29 07:40:46 +00:00
parent 0941387f9a
commit 0dbe934433

View File

@@ -147,14 +147,21 @@ struct descr {
time_t timeout; time_t timeout;
}; };
/*
* Create the socket (family, type, port) in `d'
*/
static void static void
init_socket(struct descr *d, int family, int type, int port) init_socket(struct descr *d, krb5_address *a, int family, int type, int port)
{ {
krb5_error_code ret; krb5_error_code ret;
struct sockaddr *sa; struct sockaddr *sa;
void *sa_buf; void *sa_buf;
int sa_size; int sa_size;
memset(d, 0, sizeof(*d));
d->s = -1;
sa_size = krb5_max_sockaddr_size (); sa_size = krb5_max_sockaddr_size ();
sa_buf = malloc(sa_size); sa_buf = malloc(sa_size);
if (sa_buf == NULL) { if (sa_buf == NULL) {
@@ -163,7 +170,17 @@ init_socket(struct descr *d, int family, int type, int port)
} }
sa = (struct sockaddr *)sa_buf; sa = (struct sockaddr *)sa_buf;
memset(d, 0, sizeof(*d)); ret = krb5_addr2sockaddr (a, sa, &sa_size, port);
if (ret) {
krb5_warn(context, ret, "krb5_anyaddr");
close(d->s);
d->s = -1;
goto out;
}
if (sa->sa_family != family)
goto out;
d->s = socket(family, type, 0); d->s = socket(family, type, 0);
if(d->s < 0){ if(d->s < 0){
krb5_warn(context, errno, "socket(%d, %d, 0)", family, type); krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
@@ -177,13 +194,6 @@ init_socket(struct descr *d, int family, int type, int port)
} }
#endif #endif
d->type = type; d->type = type;
ret = krb5_anyaddr (family, sa, &sa_size, port);
if (ret) {
krb5_warn(context, ret, "krb5_anyaddr");
close(d->s);
d->s = -1;
goto out;
}
if(bind(d->s, sa, sa_size) < 0){ if(bind(d->s, sa, sa_size) < 0){
krb5_warn(context, errno, "bind(%d)", ntohs(port)); krb5_warn(context, errno, "bind(%d)", ntohs(port));
@@ -200,24 +210,46 @@ out:
free (sa_buf); free (sa_buf);
} }
/*
* Allocate descriptors for all the sockets that we should listen on
* and return the number of them.
*/
static int static int
init_sockets(struct descr **desc) init_sockets(struct descr **desc)
{ {
int i; krb5_error_code ret;
int i, j;
struct descr *d; struct descr *d;
int num = 0; int num = 0;
krb5_addresses addresses;
ret = krb5_get_all_server_addrs (&addresses);
if (ret)
krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
parse_ports(port_str); parse_ports(port_str);
d = malloc(num_ports * sizeof(*d)); d = malloc(addresses.len * num_ports * sizeof(*d));
if (d == NULL) if (d == NULL)
krb5_errx(context, 1, "malloc(%u) failed", num_ports * sizeof(*d)); krb5_errx(context, 1, "malloc(%u) failed", num_ports * sizeof(*d));
for (i = 0; i < num_ports; i++){ for (i = 0; i < num_ports; i++){
init_socket(&d[num], ports[i].family, ports[i].type, ports[i].port); for (j = 0; j < addresses.len; ++j) {
if(d[num].s != -1){ init_socket(&d[num], &addresses.val[j],
kdc_log(5, "listening to port %u/%s", ntohs(ports[i].port), ports[i].family, ports[i].type, ports[i].port);
(ports[i].type == SOCK_STREAM) ? "tcp" : "udp"); /* XXX */ if(d[num].s != -1){
num++; char a_str[80];
int len;
krb5_print_address (&addresses.val[j], a_str,
sizeof(a_str), &len);
kdc_log(5, "listening on %s port %u/%s",
a_str,
ntohs(ports[i].port),
(ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
/* XXX */
num++;
}
} }
} }
d = realloc(d, num * sizeof(*d)); d = realloc(d, num * sizeof(*d));
@@ -535,6 +567,7 @@ loop(void)
{ {
struct descr *d; struct descr *d;
int ndescr; int ndescr;
ndescr = init_sockets(&d); ndescr = init_sockets(&d);
if(ndescr <= 0) if(ndescr <= 0)
krb5_errx(context, 1, "No sockets!"); krb5_errx(context, 1, "No sockets!");