3 * $Id: conn.c,v 1.2 1999/05/15 10:33:32 mdw Exp $
5 * Nonblocking connect handling
7 * (c) 1999 Straylight/Edgeware
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.2 1999/05/15 10:33:32 mdw
34 * Fix copyright notices.
36 * Revision 1.1 1999/05/14 21:01:14 mdw
37 * Integrated `select' handling bits from the background resolver project.
41 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @conn_connect@ --- *
63 * Arguments: @int fd@ = file descriptor to try to connect
64 * @unsigned mode@ = what we can do to the file
65 * @void *p@ = pointer to connection context
69 * Use: Handles select results for pending connections.
72 static void conn_connect(int fd, unsigned mode, void *p)
75 struct sockaddr_in sin;
79 if (getpeername(fd, (struct sockaddr *)&sin, &sinsz) < 0) {
81 int errsz = sizeof(err);
82 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errsz) == 0)
88 sel_rmfile(&c->writer);
91 /* --- @conn_init@ --- *
93 * Arguments: @conn *c@ = pointer to connection block
94 * @sel_state *s@ = pointer to select state to attach to
95 * @unsigned long saddr@ = source IP address
96 * @unsigned short sport@ = source port
97 * @unsigned long daddr@ = destination IP address
98 * @unsigned short dport@ = destination port
99 * @void (*func)(int fd, void *p) = handler function
100 * @void *p@ = argument for the handler function
104 * Use: Sets up a nonblocking connect job.
107 void conn_init(conn *c, sel_state *s,
109 unsigned short sport,
112 void (*func)(int /*fd*/, void */*p*/),
117 /* --- Make a socket to do the connecting with --- */
120 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
123 /* --- Make the socket nonblocking --- */
128 if ((f = fcntl(fd, F_GETFL)) < 0 ||
129 fcntl(fd, F_SETFL, f | O_NONBLOCK))
133 /* --- Set up the source address and bind it to the socket --- */
136 struct sockaddr_in sin;
138 memset(&sin, 0, sizeof(sin));
139 sin.sin_family = AF_INET;
140 sin.sin_addr.s_addr = saddr;
141 sin.sin_port = sport;
142 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
146 /* --- Finally, set up the destination and try the connect --- */
149 struct sockaddr_in sin;
151 memset(&sin, 0, sizeof(sin));
152 sin.sin_family = AF_INET;
153 sin.sin_addr.s_addr = daddr;
154 sin.sin_port = dport;
155 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
156 if (errno != EINPROGRESS)
160 sel_initfile(s, &c->writer, fd, SEL_WRITE, conn_connect, c);
161 sel_addfile(&c->writer);
166 /* --- Everything is set up now --- */
170 /* --- Something went pear-shaped --- */
178 /* --- @conn_kill@ --- *
180 * Arguments: @conn *c@ = pointer to connection to dispose of
184 * Use: Disposes of a connection when it's not wanted any more.
187 void conn_kill(conn *c)
189 if (c->writer.fd != -1) {
191 sel_rmfile(&c->writer);
196 /*----- That's all, folks -------------------------------------------------*/