Commit dcd1dba6 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Use a structure for local sockets.

parent 2f3ca807
...@@ -619,8 +619,8 @@ main(int argc, char **argv) ...@@ -619,8 +619,8 @@ main(int argc, char **argv)
maxfd = MAX(maxfd, local_server_socket); maxfd = MAX(maxfd, local_server_socket);
} }
for(i = 0; i < num_local_sockets; i++) { for(i = 0; i < num_local_sockets; i++) {
FD_SET(local_sockets[i], &readfds); FD_SET(local_sockets[i].fd, &readfds);
maxfd = MAX(maxfd, local_sockets[i]); maxfd = MAX(maxfd, local_sockets[i].fd);
} }
#endif #endif
rc = select(maxfd + 1, &readfds, NULL, NULL, &tv); rc = select(maxfd + 1, &readfds, NULL, NULL, &tv);
...@@ -677,15 +677,15 @@ main(int argc, char **argv) ...@@ -677,15 +677,15 @@ main(int argc, char **argv)
i = 0; i = 0;
while(i < num_local_sockets) { while(i < num_local_sockets) {
if(FD_ISSET(local_sockets[i], &readfds)) { if(FD_ISSET(local_sockets[i].fd, &readfds)) {
rc = local_read(local_sockets[i]); rc = local_read(&local_sockets[i]);
if(rc <= 0) { if(rc <= 0) {
if(rc < 0) { if(rc < 0) {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
perror("read(local_socket)"); perror("read(local_socket)");
} }
close(local_sockets[i]); close(local_sockets[i].fd);
local_sockets[i] = local_sockets[--num_local_sockets]; local_sockets[i] = local_sockets[--num_local_sockets];
continue; continue;
} }
...@@ -917,8 +917,8 @@ accept_local_connections(fd_set *readfds) ...@@ -917,8 +917,8 @@ accept_local_connections(fd_set *readfds)
return -1; return -1;
} }
local_sockets[num_local_sockets++] = s; local_sockets[num_local_sockets++].fd = s;
local_notify_all_1(s); local_notify_all_1(&local_sockets[num_local_sockets - 1]);
return 1; return 1;
} }
......
...@@ -44,18 +44,19 @@ int dummy; ...@@ -44,18 +44,19 @@ int dummy;
#else #else
int local_server_socket = -1, local_sockets[MAX_LOCAL_SOCKETS]; int local_server_socket = -1;
struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
int num_local_sockets = 0; int num_local_sockets = 0;
int local_server_port = -1; int local_server_port = -1;
int int
local_read(int s) local_read(struct local_socket *s)
{ {
int rc; int rc;
char buf[500]; char buf[500];
/* Ignore anything that comes in, except for EOF */ /* Ignore anything that comes in, except for EOF */
rc = read(s, buf, 500); rc = read(s->fd, buf, 500);
if(rc <= 0) if(rc <= 0)
return rc; return rc;
...@@ -95,7 +96,7 @@ write_timeout(int fd, const void *buf, int len) ...@@ -95,7 +96,7 @@ write_timeout(int fd, const void *buf, int len)
} }
static void static void
local_notify_self_1(int s) local_notify_self_1(struct local_socket *s)
{ {
char buf[512]; char buf[512];
char host[64]; char host[64];
...@@ -112,13 +113,13 @@ local_notify_self_1(int s) ...@@ -112,13 +113,13 @@ local_notify_self_1(int s)
if(rc < 0 || rc >= 512) if(rc < 0 || rc >= 512)
goto fail; goto fail;
rc = write_timeout(s, buf, rc); rc = write_timeout(s->fd, buf, rc);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
return; return;
fail: fail:
shutdown(s, 1); shutdown(s->fd, 1);
return; return;
} }
...@@ -134,7 +135,8 @@ local_kind(int kind) ...@@ -134,7 +135,8 @@ local_kind(int kind)
} }
static void static void
local_notify_neighbour_1(int s, struct neighbour *neigh, int kind) local_notify_neighbour_1(struct local_socket *s,
struct neighbour *neigh, int kind)
{ {
char buf[512], rttbuf[64]; char buf[512], rttbuf[64];
int rc; int rc;
...@@ -165,13 +167,13 @@ local_notify_neighbour_1(int s, struct neighbour *neigh, int kind) ...@@ -165,13 +167,13 @@ local_notify_neighbour_1(int s, struct neighbour *neigh, int kind)
if(rc < 0 || rc >= 512) if(rc < 0 || rc >= 512)
goto fail; goto fail;
rc = write_timeout(s, buf, rc); rc = write_timeout(s->fd, buf, rc);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
return; return;
fail: fail:
shutdown(s, 1); shutdown(s->fd, 1);
return; return;
} }
...@@ -180,11 +182,11 @@ local_notify_neighbour(struct neighbour *neigh, int kind) ...@@ -180,11 +182,11 @@ local_notify_neighbour(struct neighbour *neigh, int kind)
{ {
int i; int i;
for(i = 0; i < num_local_sockets; i++) for(i = 0; i < num_local_sockets; i++)
local_notify_neighbour_1(local_sockets[i], neigh, kind); local_notify_neighbour_1(&local_sockets[i], neigh, kind);
} }
static void static void
local_notify_xroute_1(int s, struct xroute *xroute, int kind) local_notify_xroute_1(struct local_socket *s, struct xroute *xroute, int kind)
{ {
char buf[512]; char buf[512];
int rc; int rc;
...@@ -200,13 +202,13 @@ local_notify_xroute_1(int s, struct xroute *xroute, int kind) ...@@ -200,13 +202,13 @@ local_notify_xroute_1(int s, struct xroute *xroute, int kind)
if(rc < 0 || rc >= 512) if(rc < 0 || rc >= 512)
goto fail; goto fail;
rc = write_timeout(s, buf, rc); rc = write_timeout(s->fd, buf, rc);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
return; return;
fail: fail:
shutdown(s, 1); shutdown(s->fd, 1);
return; return;
} }
...@@ -215,11 +217,11 @@ local_notify_xroute(struct xroute *xroute, int kind) ...@@ -215,11 +217,11 @@ local_notify_xroute(struct xroute *xroute, int kind)
{ {
int i; int i;
for(i = 0; i < num_local_sockets; i++) for(i = 0; i < num_local_sockets; i++)
local_notify_xroute_1(local_sockets[i], xroute, kind); local_notify_xroute_1(&local_sockets[i], xroute, kind);
} }
static void static void
local_notify_route_1(int s, struct babel_route *route, int kind) local_notify_route_1(struct local_socket *s, struct babel_route *route, int kind)
{ {
char buf[512]; char buf[512];
int rc; int rc;
...@@ -243,13 +245,13 @@ local_notify_route_1(int s, struct babel_route *route, int kind) ...@@ -243,13 +245,13 @@ local_notify_route_1(int s, struct babel_route *route, int kind)
if(rc < 0 || rc >= 512) if(rc < 0 || rc >= 512)
goto fail; goto fail;
rc = write_timeout(s, buf, rc); rc = write_timeout(s->fd, buf, rc);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
return; return;
fail: fail:
shutdown(s, 1); shutdown(s->fd, 1);
return; return;
} }
...@@ -258,11 +260,11 @@ local_notify_route(struct babel_route *route, int kind) ...@@ -258,11 +260,11 @@ local_notify_route(struct babel_route *route, int kind)
{ {
int i; int i;
for(i = 0; i < num_local_sockets; i++) for(i = 0; i < num_local_sockets; i++)
local_notify_route_1(local_sockets[i], route, kind); local_notify_route_1(&local_sockets[i], route, kind);
} }
void void
local_notify_all_1(int s) local_notify_all_1(struct local_socket *s)
{ {
int rc; int rc;
struct neighbour *neigh; struct neighbour *neigh;
...@@ -271,14 +273,14 @@ local_notify_all_1(int s) ...@@ -271,14 +273,14 @@ local_notify_all_1(int s)
struct xroute_stream *xroutes; struct xroute_stream *xroutes;
struct route_stream *routes; struct route_stream *routes;
rc = write_timeout(s, header, strlen(header)); rc = write_timeout(s->fd, header, strlen(header));
if(rc < 0) if(rc < 0)
goto fail; goto fail;
rc = snprintf(buf, 512, "version %s\n", BABELD_VERSION); rc = snprintf(buf, 512, "version %s\n", BABELD_VERSION);
if(rc < 0 || rc >= 512) if(rc < 0 || rc >= 512)
goto fail; goto fail;
rc = write_timeout(s, buf, rc); rc = write_timeout(s->fd, buf, rc);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
...@@ -309,13 +311,13 @@ local_notify_all_1(int s) ...@@ -309,13 +311,13 @@ local_notify_all_1(int s)
route_stream_done(routes); route_stream_done(routes);
} }
rc = write_timeout(s, "done\n", 5); rc = write_timeout(s->fd, "done\n", 5);
if(rc < 0) if(rc < 0)
goto fail; goto fail;
return; return;
fail: fail:
shutdown(s, 1); shutdown(s->fd, 1);
return; return;
} }
......
...@@ -34,15 +34,20 @@ struct xroute; ...@@ -34,15 +34,20 @@ struct xroute;
#define MAX_LOCAL_SOCKETS 4 #define MAX_LOCAL_SOCKETS 4
#endif #endif
extern int local_server_socket, local_sockets[MAX_LOCAL_SOCKETS]; struct local_socket {
int fd;
};
extern int local_server_socket;
extern struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
extern int num_local_sockets; extern int num_local_sockets;
extern int local_server_port; extern int local_server_port;
int local_read(int s); int local_read(struct local_socket *s);
void local_notify_neighbour(struct neighbour *neigh, int kind); void local_notify_neighbour(struct neighbour *neigh, int kind);
void local_notify_xroute(struct xroute *xroute, int kind); void local_notify_xroute(struct xroute *xroute, int kind);
void local_notify_route(struct babel_route *route, int kind); void local_notify_route(struct babel_route *route, int kind);
void local_notify_all_1(int s); void local_notify_all_1(struct local_socket *s);
#else #else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment