3 * Nonblocking connect handling
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
33 #include <sys/types.h>
34 #include <sys/socket.h>
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @conn_connect@ --- *
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
52 * Use: Handles select results for pending connections.
55 static void conn_connect(int fd, unsigned mode, void *p)
58 # define PATH_MAX 1024
62 char buf[PATH_MAX + 8]; /* Big enough */
66 sel_rmfile(&c->writer);
67 if (getpeername(fd, (struct sockaddr *)buf, &sinsz) < 0) {
69 socklen_t errsz = sizeof(err);
70 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
78 /* --- @conn_fd@ --- *
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
88 * Use: Sets up a nonblocking connect job. The socket should have a
89 * connect pending for it already.
92 void conn_fd(conn *c, sel_state *s, int fd,
93 void (*func)(int /*fd*/, void */*p*/),
98 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
99 sel_addfile(&c->writer);
102 /* --- @conn_init@ --- *
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
112 * Returns: Zero on success, nonzero on failure.
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.
123 int conn_init(conn *c, sel_state *s, int fd,
124 struct sockaddr *dst, int dsz,
125 void (*func)(int /*fd*/, void */*p*/),
130 if ((f = fcntl(fd, F_GETFL)) < 0 || fcntl(fd, F_SETFL, f | O_NONBLOCK))
133 if (!connect(fd, dst, dsz))
135 else if (errno != EINPROGRESS)
138 conn_fd(c, s, fd, func, p);
146 /* --- @conn_kill@ --- *
148 * Arguments: @conn *c@ = pointer to connection to dispose of
152 * Use: Disposes of a connection when it's not wanted any more.
155 void conn_kill(conn *c)
157 if (c->writer.fd != -1) {
159 sel_rmfile(&c->writer);
164 /*----- That's all, folks -------------------------------------------------*/