97f65b00 |
1 | /* -*-c-*- |
2 | * |
a3ddb778 |
3 | * $Id: conn.c,v 1.2 1999/05/15 10:33:32 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 $ |
a3ddb778 |
33 | * Revision 1.2 1999/05/15 10:33:32 mdw |
34 | * Fix copyright notices. |
35 | * |
97f65b00 |
36 | * Revision 1.1 1999/05/14 21:01:14 mdw |
37 | * Integrated `select' handling bits from the background resolver project. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <errno.h> |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | #include <sys/types.h> |
49 | |
50 | #include <sys/socket.h> |
51 | #include <netinet/in.h> |
52 | |
53 | #include <unistd.h> |
54 | #include <fcntl.h> |
55 | |
56 | #include "conn.h" |
57 | #include "sel.h" |
58 | |
59 | /*----- Main code ---------------------------------------------------------*/ |
60 | |
61 | /* --- @conn_connect@ --- * |
62 | * |
63 | * Arguments: @int fd@ = file descriptor to try to connect |
64 | * @unsigned mode@ = what we can do to the file |
65 | * @void *p@ = pointer to connection context |
66 | * |
67 | * Returns: --- |
68 | * |
69 | * Use: Handles select results for pending connections. |
70 | */ |
71 | |
72 | static void conn_connect(int fd, unsigned mode, void *p) |
73 | { |
74 | conn *c = p; |
75 | struct sockaddr_in sin; |
76 | int sinsz; |
77 | |
78 | sinsz = sizeof(sin); |
79 | if (getpeername(fd, (struct sockaddr *)&sin, &sinsz) < 0) { |
80 | int err; |
81 | int errsz = sizeof(err); |
82 | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0) |
83 | errno = err; |
84 | c->func(-1, c->p); |
85 | close(fd); |
86 | } else |
87 | c->func(fd, c->p); |
88 | sel_rmfile(&c->writer); |
89 | } |
90 | |
91 | /* --- @conn_init@ --- * |
92 | * |
93 | * Arguments: @conn *c@ = pointer to connection block |
94 | * @sel_state *s@ = pointer to select state to attach to |
95 | * @unsigned long saddr@ = source IP address |
96 | * @unsigned short sport@ = source port |
97 | * @unsigned long daddr@ = destination IP address |
98 | * @unsigned short dport@ = destination port |
99 | * @void (*func)(int fd, void *p) = handler function |
100 | * @void *p@ = argument for the handler function |
101 | * |
102 | * Returns: --- |
103 | * |
104 | * Use: Sets up a nonblocking connect job. |
105 | */ |
106 | |
107 | void conn_init(conn *c, sel_state *s, |
108 | unsigned long saddr, |
109 | unsigned short sport, |
110 | unsigned long daddr, |
111 | unsigned long dport, |
112 | void (*func)(int /*fd*/, void */*p*/), |
113 | void *p) |
114 | { |
115 | int fd; |
116 | |
117 | /* --- Make a socket to do the connecting with --- */ |
118 | |
119 | c->writer.fd = -1; |
120 | if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) |
121 | goto fail; |
122 | |
123 | /* --- Make the socket nonblocking --- */ |
124 | |
125 | { |
126 | int f; |
127 | |
128 | if ((f = fcntl(fd, F_GETFL)) < 0 || |
129 | fcntl(fd, F_SETFL, f | O_NONBLOCK)) |
130 | goto fail_close; |
131 | } |
132 | |
133 | /* --- Set up the source address and bind it to the socket --- */ |
134 | |
135 | { |
136 | struct sockaddr_in sin; |
137 | |
138 | memset(&sin, 0, sizeof(sin)); |
139 | sin.sin_family = AF_INET; |
140 | sin.sin_addr.s_addr = saddr; |
141 | sin.sin_port = sport; |
142 | if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
143 | goto fail_close; |
144 | } |
145 | |
146 | /* --- Finally, set up the destination and try the connect --- */ |
147 | |
148 | { |
149 | struct sockaddr_in sin; |
150 | |
151 | memset(&sin, 0, sizeof(sin)); |
152 | sin.sin_family = AF_INET; |
153 | sin.sin_addr.s_addr = daddr; |
154 | sin.sin_port = dport; |
155 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { |
156 | if (errno != EINPROGRESS) |
157 | goto fail_close; |
158 | c->func = func; |
159 | c->p = p; |
160 | sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c); |
161 | sel_addfile(&c->writer); |
162 | } else |
163 | func(fd, p); |
164 | } |
165 | |
166 | /* --- Everything is set up now --- */ |
167 | |
168 | return; |
169 | |
170 | /* --- Something went pear-shaped --- */ |
171 | |
172 | fail_close: |
173 | close(fd); |
174 | fail: |
175 | func(-1, p); |
176 | } |
177 | |
178 | /* --- @conn_kill@ --- * |
179 | * |
180 | * Arguments: @conn *c@ = pointer to connection to dispose of |
181 | * |
182 | * Returns: --- |
183 | * |
184 | * Use: Disposes of a connection when it's not wanted any more. |
185 | */ |
186 | |
187 | void conn_kill(conn *c) |
188 | { |
189 | if (c->writer.fd != -1) { |
190 | close(c->writer.fd); |
191 | sel_rmfile(&c->writer); |
192 | c->writer.fd = -1; |
193 | } |
194 | } |
195 | |
196 | /*----- That's all, folks -------------------------------------------------*/ |