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