chiark / gitweb /
Infrastructure: Strip away crufty CVS $Id$ tags.
[mLib] / conn.c
CommitLineData
97f65b00 1/* -*-c-*-
97f65b00 2 *
3 * Nonblocking connect handling
4 *
a3ddb778 5 * (c) 1999 Straylight/Edgeware
97f65b00 6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
97f65b00 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
97f65b00 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
97f65b00 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
97f65b00 28/*----- Header files ------------------------------------------------------*/
29
30#include <errno.h>
5feb52af 31#include <limits.h>
97f65b00 32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <sys/types.h>
97f65b00 37#include <sys/socket.h>
97f65b00 38
39#include <unistd.h>
40#include <fcntl.h>
41
42#include "conn.h"
43#include "sel.h"
44
45/*----- Main code ---------------------------------------------------------*/
46
47/* --- @conn_connect@ --- *
48 *
49 * Arguments: @int fd@ = file descriptor to try to connect
50 * @unsigned mode@ = what we can do to the file
51 * @void *p@ = pointer to connection context
52 *
53 * Returns: ---
54 *
55 * Use: Handles select results for pending connections.
56 */
57
58static void conn_connect(int fd, unsigned mode, void *p)
59{
5feb52af 60#ifndef PATH_MAX
61# define PATH_MAX 1024
62#endif
63
97f65b00 64 conn *c = p;
5feb52af 65 char buf[PATH_MAX + 8]; /* Big enough */
d320826d 66 size_t sinsz;
97f65b00 67
5feb52af 68 sinsz = sizeof(buf);
89f167ab 69 sel_rmfile(&c->writer);
5feb52af 70 if (getpeername(fd, (struct sockaddr *)buf, &sinsz) < 0) {
97f65b00 71 int err;
d320826d 72 size_t errsz = sizeof(err);
97f65b00 73 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
74 errno = err;
97f65b00 75 close(fd);
89f167ab 76 c->func(-1, c->p);
97f65b00 77 } else
78 c->func(fd, c->p);
97f65b00 79}
80
f128ce74 81/* --- @conn_fd@ --- *
82 *
83 * Arguments: @conn *c@ = pointer to connection block
84 * @sel_state *s@ = pointer to select state to attach to
85 * @int fd@ = file descriptor of socket
86 * @void (*func)(int fd, void *p) = handler function
87 * @void *p@ = argument for the handler function
88 *
89 * Returns: ---
90 *
91 * Use: Sets up a nonblocking connect job. The socket should have a
92 * connect pending for it already.
93 */
94
95void conn_fd(conn *c, sel_state *s, int fd,
96 void (*func)(int /*fd*/, void */*p*/),
97 void *p)
98{
99 c->func = func;
100 c->p = p;
101 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
102 sel_addfile(&c->writer);
103}
104
97f65b00 105/* --- @conn_init@ --- *
106 *
107 * Arguments: @conn *c@ = pointer to connection block
108 * @sel_state *s@ = pointer to select state to attach to
5feb52af 109 * @int fd@ = file descriptor of socket to connect
110 * @struct sockaddr *dst@ = destination address
111 * @int dsz@ = size of destination address
97f65b00 112 * @void (*func)(int fd, void *p) = handler function
113 * @void *p@ = argument for the handler function
114 *
ff34c6e7 115 * Returns: Zero on success, nonzero on failure.
97f65b00 116 *
5feb52af 117 * Use: Sets up a nonblocking connect job. The socket should already
118 * be bound if you care about that sort of thing. When the
119 * connection completes, the handler function is called with the
120 * connected socket as an argument. If the connect fails rather
121 * than completes, the socket is closed, and the handler is
122 * informed of this by being passed a negative file descriptor.
123 * In either case, the select job is then removed.
97f65b00 124 */
125
ff34c6e7 126int conn_init(conn *c, sel_state *s, int fd,
127 struct sockaddr *dst, int dsz,
128 void (*func)(int /*fd*/, void */*p*/),
129 void *p)
97f65b00 130{
5feb52af 131 int f;
97f65b00 132
750c61c0 133 if ((f = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, f | O_NONBLOCK))
134 goto fail;
97f65b00 135
c9bb3f9e 136 if (!connect(fd, dst, dsz))
137 func(fd, p);
138 else if (errno != EINPROGRESS)
139 goto fail;
f128ce74 140 else
141 conn_fd(c, s, fd, func, p);
ff34c6e7 142 return (0);
750c61c0 143
144fail:
145 close(fd);
146 return (-1);
97f65b00 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
158void 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 -------------------------------------------------*/