[MEDIUM] moved the sockaddr pointer to the fdtab structure

The stream_sock_* functions had to know about sessions just in
order to get the server's address for a connect() operation. This
is not desirable, particularly for non-IP protocols (eg: PF_UNIX).

Put a pointer to the peer's sockaddr_storage or sockaddr address
in the fdtab structure so that we never need to look further.

With this small change, the stream_sock.c file is now 100% protocol
independant.
This commit is contained in:
Willy Tarreau 2007-10-09 17:14:37 +02:00
parent d9db9274fe
commit e94ebd0e37
6 changed files with 14 additions and 5 deletions

View File

@ -22,6 +22,7 @@
#ifndef _TYPES_FD_H
#define _TYPES_FD_H
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
@ -63,6 +64,8 @@ struct fdtab {
struct task *owner; /* the session (or proxy) associated with this fd */
unsigned char state; /* the state of this fd */
unsigned char ev; /* event seen in return of poll() : FD_POLL_* */
struct sockaddr *peeraddr; /* pointer to peer's network address, or NULL if unset */
socklen_t peerlen; /* peer's address length, or 0 if unset */
};
/*

View File

@ -545,7 +545,10 @@ int connect_server(struct session *s)
fdtab[fd].cb[DIR_RD].b = s->rep;
fdtab[fd].cb[DIR_WR].f = &stream_sock_write;
fdtab[fd].cb[DIR_WR].b = s->req;
fdtab[fd].peeraddr = (struct sockaddr *)&s->srv_addr;
fdtab[fd].peerlen = sizeof(s->srv_addr);
EV_FD_SET(fd, DIR_WR); /* for connect status */
fd_insert(fd);

View File

@ -403,6 +403,8 @@ void process_chk(struct task *t, struct timeval *next)
fdtab[fd].cb[DIR_RD].b = NULL;
fdtab[fd].cb[DIR_WR].f = &event_srv_chk_w;
fdtab[fd].cb[DIR_WR].b = NULL;
fdtab[fd].peeraddr = (struct sockaddr *)&sa;
fdtab[fd].peerlen = sizeof(sa);
fdtab[fd].state = FD_STCONN; /* connection in progress */
fdtab[fd].ev = 0;
EV_FD_SET(fd, DIR_WR); /* for connect status */

View File

@ -397,6 +397,8 @@ int event_accept(int fd) {
fdtab[cfd].cb[DIR_RD].b = s->req;
fdtab[cfd].cb[DIR_WR].f = &stream_sock_write;
fdtab[cfd].cb[DIR_WR].b = s->rep;
fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
fdtab[cfd].peerlen = sizeof(s->cli_addr);
fdtab[cfd].ev = 0;
if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||

View File

@ -158,6 +158,8 @@ int start_proxies(int verbose)
fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL;
fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */
fdtab[fd].state = FD_STLISTEN;
fdtab[fd].peeraddr = NULL;
fdtab[fd].peerlen = 0;
fdtab[fd].ev = 0;
listeners++;
}

View File

@ -27,7 +27,6 @@
#include <types/buffers.h>
#include <types/global.h>
#include <types/polling.h>
#include <types/session.h>
#include <proto/client.h>
#include <proto/fd.h>
@ -229,8 +228,6 @@ int stream_sock_write(int fd) {
if (max == 0) {
/* may be we have received a connection acknowledgement in TCP mode without data */
if (likely(fdtab[fd].state == FD_STCONN)) {
struct session *s = fdtab[fd].owner->context;
/* We have no data to send to check the connection, and
* getsockopt() will not inform us whether the connection
* is still pending. So we'll reuse connect() to check the
@ -240,7 +237,7 @@ int stream_sock_write(int fd) {
* - connecting (EALREADY, EINPROGRESS)
* - connected (EISCONN, 0)
*/
if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == 0))
if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
errno = 0;
if (errno == EALREADY || errno == EINPROGRESS) {