~ubuntu-branches/ubuntu/vivid/keepalived/vivid

« back to all changes in this revision

Viewing changes to genhash/layer4.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2011-10-25 16:10:58 UTC
  • mfrom: (3.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20111025161058-bgqn5elt0xo1tq0a
Tags: 1:1.2.2-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - debian/rules: DEB_UPDATE_RCD_PARAMS := explicit init start/stop
    parameters (don't stop at 0 and 6)
  - debian/init.d: init script header adapted to stop rule
  - debian/keepalived.postinst: Remove shutdown and reboot links
* Build with libnl3, thanks to a patch from Marc - A. Dahlhaus.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 *              as published by the Free Software Foundation; either version
20
20
 *              2 of the License, or (at your option) any later version.
21
21
 *
22
 
 * Copyright (C) 2001-2009 Alexandre Cassen, <acassen@freebox.fr>
 
22
 * Copyright (C) 2001-2011 Alexandre Cassen, <acassen@linux-vs.org>
23
23
 */
24
24
 
25
25
#include "layer4.h"
75
75
}
76
76
 
77
77
enum connect_result
78
 
tcp_socket_state(int fd, thread * thread_obj, uint32_t addr_ip, uint16_t addr_port,
79
 
                 int (*func) (struct _thread *))
 
78
tcp_socket_state(int fd, thread_t * thread, uint32_t addr_ip, uint16_t addr_port,
 
79
                 int (*func) (thread_t *))
80
80
{
81
81
        int status;
82
82
        socklen_t slen;
84
84
        TIMEVAL timer_min;
85
85
 
86
86
        /* Handle connection timeout */
87
 
        if (thread_obj->type == THREAD_WRITE_TIMEOUT) {
 
87
        if (thread->type == THREAD_WRITE_TIMEOUT) {
88
88
                DBG("TCP connection timeout to [%s:%d].\n",
89
89
                    inet_ntop2(addr_ip), ntohs(addr_port));
90
 
                close(thread_obj->u.fd);
 
90
                close(thread->u.fd);
91
91
                return connect_timeout;
92
92
        }
93
93
 
94
94
        /* Check file descriptor */
95
95
        slen = sizeof (status);
96
96
        if (getsockopt
97
 
            (thread_obj->u.fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen) < 0)
 
97
            (thread->u.fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen) < 0)
98
98
                ret = errno;
99
99
 
100
100
        /* Connection failed !!! */
101
101
        if (ret) {
102
102
                DBG("TCP connection failed to [%s:%d].\n",
103
103
                    inet_ntop2(addr_ip), ntohs(addr_port));
104
 
                close(thread_obj->u.fd);
 
104
                close(thread->u.fd);
105
105
                return connect_error;
106
106
        }
107
107
 
114
114
                DBG("TCP connection to [%s:%d] still IN_PROGRESS.\n",
115
115
                    inet_ntop2(addr_ip), ntohs(addr_port));
116
116
 
117
 
                timer_min = timer_sub_now(thread_obj->sands);
118
 
                thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj)
119
 
                                 , thread_obj->u.fd, TIMER_LONG(timer_min));
 
117
                timer_min = timer_sub_now(thread->sands);
 
118
                thread_add_write(thread->master, func, THREAD_ARG(thread)
 
119
                                 , thread->u.fd, TIMER_LONG(timer_min));
120
120
                return connect_in_progress;
121
121
        }
122
122
 
124
124
}
125
125
 
126
126
void
127
 
tcp_connection_state(int fd, enum connect_result status, thread * thread_obj,
128
 
                     int (*func) (struct _thread *)
 
127
tcp_connection_state(int fd, enum connect_result status, thread_t * thread,
 
128
                     int (*func) (thread_t *)
129
129
                     , long timeout)
130
130
{
131
131
        switch (status) {
134
134
                break;
135
135
 
136
136
        case connect_success:
137
 
                thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj),
 
137
                thread_add_write(thread->master, func, THREAD_ARG(thread),
138
138
                                 fd, timeout);
139
139
                break;
140
140
 
141
141
                /* Checking non-blocking connect, we wait until socket is writable */
142
142
        case connect_in_progress:
143
 
                thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj),
 
143
                thread_add_write(thread->master, func, THREAD_ARG(thread),
144
144
                                 fd, timeout);
145
145
                break;
146
146
 
150
150
}
151
151
 
152
152
int
153
 
tcp_check_thread(thread * thread_obj)
 
153
tcp_check_thread(thread_t * thread)
154
154
{
155
 
        SOCK *sock_obj = THREAD_ARG(thread_obj);
 
155
        SOCK *sock_obj = THREAD_ARG(thread);
156
156
        int ret = 1;
157
157
 
158
158
        sock_obj->status =
159
 
            tcp_socket_state(thread_obj->u.fd, thread_obj, req->addr_ip, req->addr_port,
 
159
            tcp_socket_state(thread->u.fd, thread, req->addr_ip, req->addr_port,
160
160
                             tcp_check_thread);
161
161
        switch (sock_obj->status) {
162
162
        case connect_error:
163
163
                DBG("Error connecting server [%s:%d].\n",
164
164
                    inet_ntop2(req->addr_ip), ntohs(req->addr_port));
165
 
                thread_add_terminate_event(thread_obj->master);
 
165
                thread_add_terminate_event(thread->master);
166
166
                return -1;
167
167
                break;
168
168
 
169
169
        case connect_timeout:
170
170
                DBG("Timeout connecting server [%s:%d].\n",
171
171
                    inet_ntop2(req->addr_ip), ntohs(req->addr_port));
172
 
                thread_add_terminate_event(thread_obj->master);
 
172
                thread_add_terminate_event(thread->master);
173
173
                return -1;
174
174
                break;
175
175
 
176
176
        case connect_success:{
177
177
                        if (req->ssl)
178
 
                                ret = ssl_connect(thread_obj);
 
178
                                ret = ssl_connect(thread);
179
179
 
180
180
                        if (ret) {
181
181
                                /* Remote WEB server is connected.
182
182
                                 * Unlock eventual locked socket.
183
183
                                 */
184
184
                                sock_obj->lock = 0;
185
 
                                thread_add_event(thread_obj->master,
 
185
                                thread_add_event(thread->master,
186
186
                                                 http_request_thread, sock_obj, 0);
187
187
                        } else {
188
188
                                DBG("Connection trouble to: [%s:%d].\n",
202
202
}
203
203
 
204
204
int
205
 
tcp_connect_thread(thread * thread_obj)
 
205
tcp_connect_thread(thread_t * thread)
206
206
{
207
 
        SOCK *sock_obj = THREAD_ARG(thread_obj);
 
207
        SOCK *sock_obj = THREAD_ARG(thread);
208
208
 
209
209
        if ((sock_obj->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
210
210
                DBG("WEB connection fail to create socket.\n");
214
214
        sock->status = tcp_connect(sock_obj->fd, req->addr_ip, req->addr_port);
215
215
 
216
216
        /* handle tcp connection status & register check worker thread */
217
 
        tcp_connection_state(sock_obj->fd, sock_obj->status, thread_obj, tcp_check_thread,
 
217
        tcp_connection_state(sock_obj->fd, sock_obj->status, thread, tcp_check_thread,
218
218
                             HTTP_CNX_TIMEOUT);
219
219
        return 0;
220
220
}