chiark / gitweb /
Version bump.
[mLib] / conn.c
CommitLineData
97f65b00 1/* -*-c-*-
2 *
750c61c0 3 * $Id: conn.c,v 1.9 2003/10/12 14:54:00 mdw Exp $
97f65b00 4 *
5 * Nonblocking connect handling
6 *
a3ddb778 7 * (c) 1999 Straylight/Edgeware
97f65b00 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: conn.c,v $
750c61c0 33 * Revision 1.9 2003/10/12 14:54:00 mdw
34 * Finish the work.
35 *
f128ce74 36 * Revision 1.8 2003/10/12 14:47:10 mdw
37 * New interface for messing with preconnected sockets.
38 *
c9bb3f9e 39 * Revision 1.7 2002/01/13 13:28:44 mdw
40 * Rearrange @conn_init@ to be a bit more comprehensible.
41 *
ff34c6e7 42 * Revision 1.6 2001/06/22 19:35:20 mdw
43 * Interface change to @conn_init@ -- return error rather than calling the
44 * function. This reduces the number of different environments the
45 * callback has to cope with, and the old behaviour is easily simulatable
46 * with the new, while simulating the new behaviour was awkward and
47 * painful.
48 *
d320826d 49 * Revision 1.5 2000/10/08 11:17:26 mdw
50 * (conn_connect): Change sizes to be @size_t@.
51 *
89f167ab 52 * Revision 1.4 1999/07/26 23:21:02 mdw
53 * Bug fix: remove the selector before doing the callback, in case client
54 * adds a writer for the connected socket.
55 *
5feb52af 56 * Revision 1.3 1999/05/23 12:12:37 mdw
57 * Interface change to make the `conn' selector useful for generic stream
58 * sockets rather than just IPv4 ones.
59 *
a3ddb778 60 * Revision 1.2 1999/05/15 10:33:32 mdw
61 * Fix copyright notices.
62 *
97f65b00 63 * Revision 1.1 1999/05/14 21:01:14 mdw
64 * Integrated `select' handling bits from the background resolver project.
65 *
66 */
67
68/*----- Header files ------------------------------------------------------*/
69
70#include <errno.h>
5feb52af 71#include <limits.h>
97f65b00 72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75
76#include <sys/types.h>
97f65b00 77#include <sys/socket.h>
97f65b00 78
79#include <unistd.h>
80#include <fcntl.h>
81
82#include "conn.h"
83#include "sel.h"
84
85/*----- Main code ---------------------------------------------------------*/
86
87/* --- @conn_connect@ --- *
88 *
89 * Arguments: @int fd@ = file descriptor to try to connect
90 * @unsigned mode@ = what we can do to the file
91 * @void *p@ = pointer to connection context
92 *
93 * Returns: ---
94 *
95 * Use: Handles select results for pending connections.
96 */
97
98static void conn_connect(int fd, unsigned mode, void *p)
99{
5feb52af 100#ifndef PATH_MAX
101# define PATH_MAX 1024
102#endif
103
97f65b00 104 conn *c = p;
5feb52af 105 char buf[PATH_MAX + 8]; /* Big enough */
d320826d 106 size_t sinsz;
97f65b00 107
5feb52af 108 sinsz = sizeof(buf);
89f167ab 109 sel_rmfile(&c->writer);
5feb52af 110 if (getpeername(fd, (struct sockaddr *)buf, &sinsz) < 0) {
97f65b00 111 int err;
d320826d 112 size_t errsz = sizeof(err);
97f65b00 113 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
114 errno = err;
97f65b00 115 close(fd);
89f167ab 116 c->func(-1, c->p);
97f65b00 117 } else
118 c->func(fd, c->p);
97f65b00 119}
120
f128ce74 121/* --- @conn_fd@ --- *
122 *
123 * Arguments: @conn *c@ = pointer to connection block
124 * @sel_state *s@ = pointer to select state to attach to
125 * @int fd@ = file descriptor of socket
126 * @void (*func)(int fd, void *p) = handler function
127 * @void *p@ = argument for the handler function
128 *
129 * Returns: ---
130 *
131 * Use: Sets up a nonblocking connect job. The socket should have a
132 * connect pending for it already.
133 */
134
135void conn_fd(conn *c, sel_state *s, int fd,
136 void (*func)(int /*fd*/, void */*p*/),
137 void *p)
138{
139 c->func = func;
140 c->p = p;
141 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
142 sel_addfile(&c->writer);
143}
144
97f65b00 145/* --- @conn_init@ --- *
146 *
147 * Arguments: @conn *c@ = pointer to connection block
148 * @sel_state *s@ = pointer to select state to attach to
5feb52af 149 * @int fd@ = file descriptor of socket to connect
150 * @struct sockaddr *dst@ = destination address
151 * @int dsz@ = size of destination address
97f65b00 152 * @void (*func)(int fd, void *p) = handler function
153 * @void *p@ = argument for the handler function
154 *
ff34c6e7 155 * Returns: Zero on success, nonzero on failure.
97f65b00 156 *
5feb52af 157 * Use: Sets up a nonblocking connect job. The socket should already
158 * be bound if you care about that sort of thing. When the
159 * connection completes, the handler function is called with the
160 * connected socket as an argument. If the connect fails rather
161 * than completes, the socket is closed, and the handler is
162 * informed of this by being passed a negative file descriptor.
163 * In either case, the select job is then removed.
97f65b00 164 */
165
ff34c6e7 166int conn_init(conn *c, sel_state *s, int fd,
167 struct sockaddr *dst, int dsz,
168 void (*func)(int /*fd*/, void */*p*/),
169 void *p)
97f65b00 170{
5feb52af 171 int f;
97f65b00 172
750c61c0 173 if ((f = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, f | O_NONBLOCK))
174 goto fail;
97f65b00 175
c9bb3f9e 176 if (!connect(fd, dst, dsz))
177 func(fd, p);
178 else if (errno != EINPROGRESS)
179 goto fail;
f128ce74 180 else
181 conn_fd(c, s, fd, func, p);
ff34c6e7 182 return (0);
750c61c0 183
184fail:
185 close(fd);
186 return (-1);
97f65b00 187}
188
189/* --- @conn_kill@ --- *
190 *
191 * Arguments: @conn *c@ = pointer to connection to dispose of
192 *
193 * Returns: ---
194 *
195 * Use: Disposes of a connection when it's not wanted any more.
196 */
197
198void conn_kill(conn *c)
199{
200 if (c->writer.fd != -1) {
201 close(c->writer.fd);
202 sel_rmfile(&c->writer);
203 c->writer.fd = -1;
204 }
205}
206
207/*----- That's all, folks -------------------------------------------------*/