]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MEDIUM] moved the sockaddr pointer to the fdtab structure
authorWilly Tarreau <w@1wt.eu>
Tue, 9 Oct 2007 15:14:37 +0000 (17:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 15 Oct 2007 15:14:01 +0000 (17:14 +0200)
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.

include/types/fd.h
src/backend.c
src/checks.c
src/client.c
src/proxy.c
src/stream_sock.c

index a1b6e9d4d4859108d0270bad6232de52817a05e7..3eacd7a32e0d285d248d2b1a1dc50f4296f667d9 100644 (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 */
 };
 
 /*
index 91c0b34b2942f51260b56931fe759ab3d84c1e00..b744703704c28832d19c15abd75e89a09bf630c9 100644 (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);
index cd6bd3007a38a5f1a02783f6c88b3f7613330a18..490ff02f26021402b66f99eaa0b558d2f041fe7f 100644 (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 */
index 1f58154c2f4eb42868850b245cffc80be1e46588..e887b7c7c4fa223a3507037c300fb3d52a900723 100644 (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)) ||
index bd33c84e0bafbc507bcc1c77193339a1c55beb2b..f2967de3118e2f190c993a55c02744ddff13a7fc 100644 (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++;
                }
index 45413432295a21d03d92ba3cf4335593923491ef..8c47d318cb48a42c16f116e32ec1b2f9de2f88f4 100644 (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) {