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