Thread: [srvx-commits] CVS: services/src ioset.c,1.7,1.8 ioset.h,1.3,1.4 proto-common.c,1.7,1.8 sockcheck.c,
Brought to you by:
entrope
|
From: Entrope <en...@us...> - 2002-08-14 03:04:02
|
Update of /cvsroot/srvx/services/src
In directory usw-pr-cvs1:/tmp/cvs-serv4402/src
Modified Files:
ioset.c ioset.h proto-common.c sockcheck.c
Log Message:
add new function, ioset_connect(), to handle common TCP connection code
use this in create_socket_client() and proxy-checking
Index: ioset.c
===================================================================
RCS file: /cvsroot/srvx/services/src/ioset.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** ioset.c 30 Jul 2002 02:15:11 -0000 1.7
--- ioset.c 14 Aug 2002 03:03:58 -0000 1.8
***************
*** 29,32 ****
--- 29,35 ----
#include <sys/select.h>
#endif
+ #ifdef HAVE_SYS_SOCKET_H
+ #include <sys/socket.h>
+ #endif
#ifndef IOSET_DEBUG
***************
*** 76,79 ****
--- 79,139 ----
}
+ struct io_fd *
+ ioset_connect(struct sockaddr *local, unsigned int sa_size, const char *peer, unsigned int port, int blocking, void (*connect_cb)(struct io_fd *fd))
+ {
+ int fd, res;
+ struct io_fd *io_fd;
+ struct sockaddr_in sin;
+ unsigned long ip;
+
+ if (!getipbyname(peer, &ip)) {
+ log(MAIN_LOG, LOG_ERROR, "getipbyname(%s) failed.\n", peer);
+ return NULL;
+ }
+ sin.sin_addr.s_addr = ip;
+ if (local) {
+ if ((fd = socket(local->sa_family, SOCK_STREAM, 0)) < 0) {
+ log(MAIN_LOG, LOG_ERROR, "socket() for %s returned errno %d (%s)\n", peer, errno, strerror(errno));
+ return NULL;
+ }
+ if (bind(fd, local, sa_size) < 0) {
+ log(MAIN_LOG, LOG_ERROR, "bind() of socket for %s (fd %d) returned errno %d (%s). Will use the default.\n", peer, fd, errno, strerror(errno));
+ }
+ } else {
+ if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+ log(MAIN_LOG, LOG_ERROR, "socket() for %s returned errno %d (%s)\n", peer, errno, strerror(errno));
+ return NULL;
+ }
+ }
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(port);
+ if (blocking) {
+ res = connect(fd, (struct sockaddr*)&sin, sizeof(sin));
+ io_fd = ioset_add(fd);
+ } else {
+ io_fd = ioset_add(fd);
+ res = connect(fd, (struct sockaddr*)&sin, sizeof(sin));
+ }
+ if (!io_fd) {
+ close(fd);
+ return NULL;
+ }
+ if (res < 0) {
+ switch (errno) {
+ case EINPROGRESS: /* only if !blocking */
+ return io_fd;
+ default:
+ log(MAIN_LOG, LOG_ERROR, "connect(%s:%d) (fd %d) returned errno %d (%s).\n", peer, port, io_fd->fd, errno, strerror(errno));
+ /* then fall through */
+ case EHOSTUNREACH:
+ case ECONNREFUSED:
+ ioset_close(io_fd->fd, 1);
+ return NULL;
+ }
+ }
+ if (connect_cb) connect_cb(io_fd);
+ return io_fd;
+ }
+
static void
ioset_try_write(struct io_fd *fd)
***************
*** 125,130 ****
void ioset_run(void)
{
! extern int socket_fd;
!
struct timeval select_timeout;
unsigned int nn;
--- 185,189 ----
void ioset_run(void)
{
! extern struct io_fd *socket_io_fd;
struct timeval select_timeout;
unsigned int nn;
***************
*** 134,140 ****
while (!quit_services) {
! while (socket_fd == -1) {
! uplink_connect();
! }
/* How long to sleep? (fill in select_timeout) */
--- 193,197 ----
while (!quit_services) {
! while (!socket_io_fd) uplink_connect();
/* How long to sleep? (fill in select_timeout) */
Index: ioset.h
===================================================================
RCS file: /cvsroot/srvx/services/src/ioset.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** ioset.h 2 Jul 2002 00:01:09 -0000 1.3
--- ioset.h 14 Aug 2002 03:03:58 -0000 1.4
***************
*** 22,25 ****
--- 22,28 ----
#define IOSET_H
+ /* Forward declare, since ioset_connect() takes a sockaddr argument. */
+ struct sockaddr;
+
struct io_fd {
int fd;
***************
*** 37,40 ****
--- 40,44 ----
struct io_fd *ioset_add(int fd);
+ struct io_fd *ioset_connect(struct sockaddr *local, unsigned int sa_size, const char *hostname, unsigned int port, int blocking, void (*connect_cb)(struct io_fd *fd));
void ioset_run(void);
void ioset_write(struct io_fd *fd, const char *buf, unsigned int nbw);
Index: proto-common.c
===================================================================
RCS file: /cvsroot/srvx/services/src/proto-common.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** proto-common.c 4 Aug 2002 21:12:57 -0000 1.7
--- proto-common.c 14 Aug 2002 03:03:58 -0000 1.8
***************
*** 33,44 ****
#endif
- int socket_fd = -1;
unsigned int lines_processed;
FILE *replay_file;
struct userList dead_users;
static char replay_line[MAXLEN+80];
! static struct io_fd *socket_io_fd;
! static int ping_freq = 120, ping_timeout = 30;
extern struct cManagerNode cManager;
--- 33,45 ----
#endif
unsigned int lines_processed;
FILE *replay_file;
struct userList dead_users;
+ struct io_fd *socket_io_fd;
static char replay_line[MAXLEN+80];
! static int ping_freq = 120;
! static int ping_timeout = 30;
! static int replay_connected;
extern struct cManagerNode cManager;
***************
*** 76,85 ****
{
while (!quit_services) {
! if (socket_fd < 0) {
! /* and this is to get some of the logging right */
self->link = self->boot = now;
cManager.uplink->state = AUTHENTICATING;
irc_introduce(cManager.uplink->password);
! socket_fd = 1;
} else if (!replay_read()) {
log(MAIN_LOG, LOG_ERROR, "Connection to server lost.\n");
--- 77,86 ----
{
while (!quit_services) {
! if (!replay_connected) {
! /* this time fudging is to get some of the logging right */
self->link = self->boot = now;
cManager.uplink->state = AUTHENTICATING;
irc_introduce(cManager.uplink->password);
! replay_connected = 1;
} else if (!replay_read()) {
log(MAIN_LOG, LOG_ERROR, "Connection to server lost.\n");
***************
*** 93,164 ****
create_socket_client(struct uplinkNode *target)
{
! int socket_desc;
! int port = target->port;
! const char *addr = target->host;
! struct sockaddr_in sain;
! struct hostent *host_info;
if (replay_file) return feof(replay_file) ? 0 : 1;
! log(MAIN_LOG, LOG_INFO, "Connecting to %s:%i...\n", addr, port);
!
! if (socket_fd != -1) {
! /* Leave the existing socket open, say we failed. */
! log(MAIN_LOG, LOG_ERROR, "Refusing to create second connection to %s:%d.\n", addr, port);
! return 0;
}
! if ((host_info = gethostbyname(addr)) == NULL) {
! log(MAIN_LOG, LOG_ERROR, "Resolve failed: %s\n", strerror(errno));
! target->state = DISCONNECTED;
! target->tries++;
! return 0;
! }
! memcpy((char*)&(sain.sin_addr), host_info->h_addr, host_info->h_length);
! sain.sin_family = host_info->h_addrtype;
! sain.sin_port = htons((u_short) port);
!
! if ((socket_desc = socket(host_info->h_addrtype, SOCK_STREAM, 0)) < 0) {
! log(MAIN_LOG, LOG_ERROR, "Socket create failed: %s\n", strerror(errno));
! log(MAIN_LOG, LOG_ERROR, MSG_FATAL_ERROR);
! exit(1);
! }
! if (cManager.uplink->bind_addr) {
! if (bind(socket_desc, (struct sockaddr*) cManager.uplink->bind_addr, sizeof(struct sockaddr)) < 0) {
! log(MAIN_LOG, LOG_ERROR, "Error: bind() in socket setup returned errno %d (%s)\n", errno, strerror(errno));
! log(MAIN_LOG, LOG_ERROR, MSG_FATAL_ERROR);
! exit(1);
! }
! }
! if (connect(socket_desc, (struct sockaddr *)&sain, sizeof(sain)) < 0) {
! log(MAIN_LOG, LOG_ERROR, "Connect failed: %s\n", strerror(errno));
! switch(errno)
! {
! case ECONNREFUSED:
! case ETIMEDOUT:
! case ENETUNREACH:
! case EHOSTUNREACH:
! case EAGAIN:
! target->state = DISCONNECTED;
! target->tries++;
! return 0;
! default:
! log(MAIN_LOG, LOG_ERROR, MSG_FATAL_ERROR);
! exit(1);
! }
}
-
- log(MAIN_LOG, LOG_INFO, "Connection to server established.\n");
- socket_fd = socket_desc;
- socket_io_fd = ioset_add(socket_fd);
socket_io_fd->readable_cb = uplink_readable;
socket_io_fd->wants_reads = 1;
!
cManager.uplink = target;
-
target->state = AUTHENTICATING;
target->tries = 0;
-
return 1;
}
--- 94,122 ----
create_socket_client(struct uplinkNode *target)
{
! int port = target->port;
! const char *addr = target->host;
if (replay_file) return feof(replay_file) ? 0 : 1;
! if (socket_io_fd) {
! /* Leave the existing socket open, say we failed. */
! log(MAIN_LOG, LOG_ERROR, "Refusing to create second connection to %s:%d.\n", addr, port);
! return 0;
}
! log(MAIN_LOG, LOG_INFO, "Connecting to %s:%i...\n", addr, port);
! socket_io_fd = ioset_connect((struct sockaddr*)cManager.uplink->bind_addr, sizeof(struct sockaddr), addr, port, 1, NULL);
! if (!socket_io_fd) {
! target->state = DISCONNECTED;
! target->tries++;
! return 0;
}
socket_io_fd->readable_cb = uplink_readable;
socket_io_fd->wants_reads = 1;
! log(MAIN_LOG, LOG_INFO, "Connection to server established.\n");
cManager.uplink = target;
target->state = AUTHENTICATING;
target->tries = 0;
return 1;
}
***************
*** 242,246 ****
int i, recv_len;
! recv_len = read(socket_fd, in+used, sizeof(in)-used);
if (recv_len < 0) {
/* if it would have blocked, pretend we succeeded */
--- 200,204 ----
int i, recv_len;
! recv_len = read(socket_io_fd->fd, in+used, sizeof(in)-used);
if (recv_len < 0) {
/* if it would have blocked, pretend we succeeded */
***************
*** 316,321 ****
close_socket(void)
{
! if (!replay_file) ioset_close(socket_fd, 1);
! socket_fd = -1;
cManager.uplink->state = DISCONNECTED;
if (self->uplink) DelServer(self->uplink, 0, NULL);
--- 274,283 ----
close_socket(void)
{
! if (replay_file) {
! replay_connected = 0;
! } else {
! ioset_close(socket_io_fd->fd, 1);
! socket_io_fd = NULL;
! }
cManager.uplink->state = DISCONNECTED;
if (self->uplink) DelServer(self->uplink, 0, NULL);
Index: sockcheck.c
===================================================================
RCS file: /cvsroot/srvx/services/src/sockcheck.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -C2 -r1.75 -r1.76
*** sockcheck.c 11 Aug 2002 03:26:15 -0000 1.75
--- sockcheck.c 14 Aug 2002 03:03:58 -0000 1.76
***************
*** 636,641 ****
sockcheck_begin_test(struct sockcheck_client *client)
{
- struct sockaddr_in sin;
- int fd;
struct io_fd *io_fd;
--- 636,639 ----
***************
*** 646,694 ****
do {
client->state = client->tests->list[client->test_index];
! if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
! log(PC_LOG, LOG_ERROR, "Error: socket() for client %s returned errno %d (%s)\n", client->addr->hostname, errno, strerror(errno));
! goto cleanup_1;
}
- if (sockcheck_conf.local_addr) {
- if (bind(fd, (struct sockaddr*)sockcheck_conf.local_addr, sizeof(struct sockaddr)) < 0) {
- log(PC_LOG, LOG_ERROR, "Error: bind() for client %s fd %d returned errno %d (%s)\n", client->addr->hostname, fd, errno, strerror(errno));
- }
- }
- sin.sin_family = AF_INET;
- sin.sin_port = htons(client->state->port);
- sin.sin_addr.s_addr = client->addr->addr;
- client->fd = io_fd = ioset_add(fd);
client->read_pos = 0;
client->read_used = 0;
io_fd->data = client;
- io_fd->connect_cb = sockcheck_connected;
io_fd->readable_cb = sockcheck_readable;
- if (connect(fd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
- switch (errno) {
- case EINPROGRESS:
- /* continue waiting for the connect() to complete */
- break;
- default:
- log(PC_LOG, LOG_ERROR, "Error: connect() for client %s:%d fd %d returned errno %d (%s)\n", client->addr->hostname, client->state->port, fd, errno, strerror(errno));
- case EHOSTUNREACH:
- case ECONNREFUSED:
- /* go on to next test.. */
- goto cleanup_2;
- }
- } else {
- log(PC_LOG, LOG_INFO, "\nUnexpected immediate success connecting to %s\n", client->addr->hostname);
- sockcheck_connected(io_fd);
- return;
- }
timeq_add(now + client->state->timeout, sockcheck_timeout_client, client);
if (SOCKCHECK_DEBUG) {
! log(PC_LOG, LOG_INFO, "Starting proxy check on %s:%d (test %d) with fd %d (%p)\n", client->addr->hostname, client->state->port, client->test_index, fd, io_fd);
}
return;
- cleanup_2:
- ioset_close(fd, 1);
- cleanup_1:
- client->test_index++;
- client->fd = NULL;
} while (client->test_index < client->tests->used);
/* Ran out of tests to run; accept this client. */
--- 644,661 ----
do {
client->state = client->tests->list[client->test_index];
! client->fd = io_fd = ioset_connect((struct sockaddr*)sockcheck_conf.local_addr, sizeof(struct sockaddr), client->addr->hostname, client->state->port, 0, sockcheck_connected);
! if (!io_fd) {
! client->test_index++;
! continue;
}
client->read_pos = 0;
client->read_used = 0;
io_fd->data = client;
io_fd->readable_cb = sockcheck_readable;
timeq_add(now + client->state->timeout, sockcheck_timeout_client, client);
if (SOCKCHECK_DEBUG) {
! log(PC_LOG, LOG_INFO, "Starting proxy check on %s:%d (test %d) with fd %d (%p)\n", client->addr->hostname, client->state->port, client->test_index, io_fd->fd, io_fd);
}
return;
} while (client->test_index < client->tests->used);
/* Ran out of tests to run; accept this client. */
|