Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 1 | Normally, we should use getsockopt(fd, SOL_SOCKET, SO_ERROR) on a pending |
| 2 | connect() to detect whether the connection correctly established or not. |
| 3 | |
| 4 | Unfortunately, getsockopt() does not report the status of a pending connection, |
| 5 | which means that it returns 0 if the connection is still pending. This has to |
| 6 | be expected, because as the name implies it, it only returns errors. |
| 7 | |
| 8 | With the speculative I/O, a new problem was introduced : if we pretend the |
| 9 | socket was indicated as ready and we go to the socket's write() function, |
| 10 | a pending connection will then inevitably be identified as established. |
| 11 | |
| 12 | In fact, there are solutions to this issue : |
| 13 | |
| 14 | - send() returns -EAGAIN if it cannot write, so that as long as there are |
| 15 | pending data in the buffer, we'll be informed about the status of the |
| 16 | connection |
| 17 | |
| 18 | - connect() on an already pending connection will return -1 with errno set to |
| 19 | one of the following values : |
| 20 | - EALREADY : connection already in progress |
| 21 | - EISCONN : connection already established |
| 22 | - anything else will indicate an error. |
| 23 | |
| 24 | => So instead of using getsockopt() on a pending connection with no data, we |
| 25 | will switch to connect(). This implies that the connection address must be |
| 26 | known within the socket's write() function. |
| 27 | |
| 28 | |