3 * $Id: conn.c,v 1.1 1999/05/14 21:01:14 mdw Exp $
5 * Nonblocking connect handling
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/05/14 21:01:14 mdw
34 * Integrated `select' handling bits from the background resolver project.
38 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @conn_connect@ --- *
60 * Arguments: @int fd@ = file descriptor to try to connect
61 * @unsigned mode@ = what we can do to the file
62 * @void *p@ = pointer to connection context
66 * Use: Handles select results for pending connections.
69 static void conn_connect(int fd, unsigned mode, void *p)
72 struct sockaddr_in sin;
76 if (getpeername(fd, (struct sockaddr *)&sin, &sinsz) < 0) {
78 int errsz = sizeof(err);
79 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
85 sel_rmfile(&c->writer);
88 /* --- @conn_init@ --- *
90 * Arguments: @conn *c@ = pointer to connection block
91 * @sel_state *s@ = pointer to select state to attach to
92 * @unsigned long saddr@ = source IP address
93 * @unsigned short sport@ = source port
94 * @unsigned long daddr@ = destination IP address
95 * @unsigned short dport@ = destination port
96 * @void (*func)(int fd, void *p) = handler function
97 * @void *p@ = argument for the handler function
101 * Use: Sets up a nonblocking connect job.
104 void conn_init(conn *c, sel_state *s,
106 unsigned short sport,
109 void (*func)(int /*fd*/, void */*p*/),
114 /* --- Make a socket to do the connecting with --- */
117 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
120 /* --- Make the socket nonblocking --- */
125 if ((f = fcntl(fd, F_GETFL)) < 0 ||
126 fcntl(fd, F_SETFL, f | O_NONBLOCK))
130 /* --- Set up the source address and bind it to the socket --- */
133 struct sockaddr_in sin;
135 memset(&sin, 0, sizeof(sin));
136 sin.sin_family = AF_INET;
137 sin.sin_addr.s_addr = saddr;
138 sin.sin_port = sport;
139 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
143 /* --- Finally, set up the destination and try the connect --- */
146 struct sockaddr_in sin;
148 memset(&sin, 0, sizeof(sin));
149 sin.sin_family = AF_INET;
150 sin.sin_addr.s_addr = daddr;
151 sin.sin_port = dport;
152 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
153 if (errno != EINPROGRESS)
157 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
158 sel_addfile(&c->writer);
163 /* --- Everything is set up now --- */
167 /* --- Something went pear-shaped --- */
175 /* --- @conn_kill@ --- *
177 * Arguments: @conn *c@ = pointer to connection to dispose of
181 * Use: Disposes of a connection when it's not wanted any more.
184 void conn_kill(conn *c)
186 if (c->writer.fd != -1) {
188 sel_rmfile(&c->writer);
193 /*----- That's all, folks -------------------------------------------------*/