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