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