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