chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / sel / conn.c
1 /* -*-c-*-
2  *
3  * Nonblocking connect handling
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <errno.h>
31 #include <limits.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "conn.h"
40 #include "sel.h"
41
42 /*----- Main code ---------------------------------------------------------*/
43
44 /* --- @conn_connect@ --- *
45  *
46  * Arguments:   @int fd@ = file descriptor to try to connect
47  *              @unsigned mode@ = what we can do to the file
48  *              @void *p@ = pointer to connection context
49  *
50  * Returns:     ---
51  *
52  * Use:         Handles select results for pending connections.
53  */
54
55 static void conn_connect(int fd, unsigned mode, void *p)
56 {
57 #ifndef PATH_MAX
58 #  define PATH_MAX 1024
59 #endif
60
61   conn *c = p;
62   char buf[PATH_MAX + 8]; /* Big enough */
63   socklen_t sinsz;
64
65   sinsz = sizeof(buf);
66   sel_rmfile(&c->writer);
67   if (getpeername(fd, (struct sockaddr *)buf, &sinsz) < 0) {
68     int err;
69     socklen_t errsz = sizeof(err);
70     if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
71       errno = err;
72     close(fd);
73     c->func(-1, c->p);
74   } else
75     c->func(fd, c->p);
76 }
77
78 /* --- @conn_fd@ --- *
79  *
80  * Arguments:   @conn *c@ = pointer to connection block
81  *              @sel_state *s@ = pointer to select state to attach to
82  *              @int fd@ = file descriptor of socket
83  *              @void (*func)(int fd, void *p) = handler function
84  *              @void *p@ = argument for the handler function
85  *
86  * Returns:     ---
87  *
88  * Use:         Sets up a nonblocking connect job.  The socket should have a
89  *              connect pending for it already.
90  */
91
92 void conn_fd(conn *c, sel_state *s, int fd,
93              void (*func)(int /*fd*/, void */*p*/),
94              void *p)
95 {
96   c->func = func;
97   c->p = p;
98   sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
99   sel_addfile(&c->writer);
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
106  *              @int fd@ = file descriptor of socket to connect
107  *              @struct sockaddr *dst@ = destination address
108  *              @int dsz@ = size of destination address
109  *              @void (*func)(int fd, void *p) = handler function
110  *              @void *p@ = argument for the handler function
111  *
112  * Returns:     Zero on success, nonzero on failure.
113  *
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.
121  */
122
123 int conn_init(conn *c, sel_state *s, int fd,
124               struct sockaddr *dst, int dsz,
125               void (*func)(int /*fd*/, void */*p*/),
126               void *p)
127 {
128   int f;
129
130   if ((f = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, f | O_NONBLOCK))
131     goto fail;
132
133   if (!connect(fd, dst, dsz))
134     func(fd, p);
135   else if (errno != EINPROGRESS)
136     goto fail;
137   else
138     conn_fd(c, s, fd, func, p);
139   return (0);
140
141 fail:
142   close(fd);
143   return (-1);
144 }
145
146 /* --- @conn_kill@ --- *
147  *
148  * Arguments:   @conn *c@ = pointer to connection to dispose of
149  *
150  * Returns:     ---
151  *
152  * Use:         Disposes of a connection when it's not wanted any more.
153  */
154
155 void conn_kill(conn *c)
156 {
157   if (c->writer.fd != -1) {
158     close(c->writer.fd);
159     sel_rmfile(&c->writer);
160     c->writer.fd = -1;
161   }
162 }
163
164 /*----- That's all, folks -------------------------------------------------*/