chiark / gitweb /
New function allowing an atom's length to be specified at intern time.
[mLib] / conn.c
CommitLineData
97f65b00 1/* -*-c-*-
2 *
d320826d 3 * $Id: conn.c,v 1.5 2000/10/08 11:17:26 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 $
d320826d 33 * Revision 1.5 2000/10/08 11:17:26 mdw
34 * (conn_connect): Change sizes to be @size_t@.
35 *
89f167ab 36 * Revision 1.4 1999/07/26 23:21:02 mdw
37 * Bug fix: remove the selector before doing the callback, in case client
38 * adds a writer for the connected socket.
39 *
5feb52af 40 * Revision 1.3 1999/05/23 12:12:37 mdw
41 * Interface change to make the `conn' selector useful for generic stream
42 * sockets rather than just IPv4 ones.
43 *
a3ddb778 44 * Revision 1.2 1999/05/15 10:33:32 mdw
45 * Fix copyright notices.
46 *
97f65b00 47 * Revision 1.1 1999/05/14 21:01:14 mdw
48 * Integrated `select' handling bits from the background resolver project.
49 *
50 */
51
52/*----- Header files ------------------------------------------------------*/
53
54#include <errno.h>
5feb52af 55#include <limits.h>
97f65b00 56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59
60#include <sys/types.h>
97f65b00 61#include <sys/socket.h>
97f65b00 62
63#include <unistd.h>
64#include <fcntl.h>
65
66#include "conn.h"
67#include "sel.h"
68
69/*----- Main code ---------------------------------------------------------*/
70
71/* --- @conn_connect@ --- *
72 *
73 * Arguments: @int fd@ = file descriptor to try to connect
74 * @unsigned mode@ = what we can do to the file
75 * @void *p@ = pointer to connection context
76 *
77 * Returns: ---
78 *
79 * Use: Handles select results for pending connections.
80 */
81
82static void conn_connect(int fd, unsigned mode, void *p)
83{
5feb52af 84#ifndef PATH_MAX
85# define PATH_MAX 1024
86#endif
87
97f65b00 88 conn *c = p;
5feb52af 89 char buf[PATH_MAX + 8]; /* Big enough */
d320826d 90 size_t sinsz;
97f65b00 91
5feb52af 92 sinsz = sizeof(buf);
89f167ab 93 sel_rmfile(&c->writer);
5feb52af 94 if (getpeername(fd, (struct sockaddr *)buf, &sinsz) < 0) {
97f65b00 95 int err;
d320826d 96 size_t errsz = sizeof(err);
97f65b00 97 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
98 errno = err;
97f65b00 99 close(fd);
89f167ab 100 c->func(-1, c->p);
97f65b00 101 } else
102 c->func(fd, c->p);
97f65b00 103}
104
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 *
115 * Returns: ---
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
5feb52af 126void conn_init(conn *c, sel_state *s, int fd,
127 struct sockaddr *dst, int dsz,
97f65b00 128 void (*func)(int /*fd*/, void */*p*/),
129 void *p)
130{
5feb52af 131 int f;
97f65b00 132
5feb52af 133 if ((f = fcntl(fd, F_GETFL)) < 0 ||
134 fcntl(fd, F_SETFL, f | O_NONBLOCK))
97f65b00 135 goto fail;
136
5feb52af 137 if (connect(fd, dst, dsz) < 0) {
138 if (errno != EINPROGRESS)
139 goto fail;
140 c->func = func;
141 c->p = p;
142 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
143 sel_addfile(&c->writer);
144 } else
145 func(fd, p);
97f65b00 146
147 return;
148
149 /* --- Something went pear-shaped --- */
150
97f65b00 151fail:
5feb52af 152 close(fd);
97f65b00 153 func(-1, p);
154}
155
156/* --- @conn_kill@ --- *
157 *
158 * Arguments: @conn *c@ = pointer to connection to dispose of
159 *
160 * Returns: ---
161 *
162 * Use: Disposes of a connection when it's not wanted any more.
163 */
164
165void conn_kill(conn *c)
166{
167 if (c->writer.fd != -1) {
168 close(c->writer.fd);
169 sel_rmfile(&c->writer);
170 c->writer.fd = -1;
171 }
172}
173
174/*----- That's all, folks -------------------------------------------------*/