source: trunk/minix/lib/ip/connect.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.0 KB
Line 
1#include <errno.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <string.h>
5#include <sys/ioctl.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8
9#include <net/gen/in.h>
10#include <net/gen/tcp.h>
11#include <net/gen/tcp_io.h>
12#include <net/gen/udp.h>
13#include <net/gen/udp_io.h>
14
15#define DEBUG 0
16
17static int _tcp_connect(int socket, const struct sockaddr *address,
18 socklen_t address_len, nwio_tcpconf_t *tcpconfp);
19static int _udp_connect(int socket, const struct sockaddr *address,
20 socklen_t address_len, nwio_udpopt_t *udpoptp);
21
22int connect(int socket, const struct sockaddr *address,
23 socklen_t address_len)
24{
25 int r;
26 nwio_tcpconf_t tcpconf;
27 nwio_udpopt_t udpopt;
28
29 r= ioctl(socket, NWIOGTCPCONF, &tcpconf);
30 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
31 {
32 if (r == -1)
33 {
34 /* Bad file descriptor */
35 return -1;
36 }
37 return _tcp_connect(socket, address, address_len, &tcpconf);
38 }
39
40 r= ioctl(socket, NWIOGUDPOPT, &udpopt);
41 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
42 {
43 if (r == -1)
44 {
45 /* Bad file descriptor */
46 return -1;
47 }
48 return _udp_connect(socket, address, address_len, &udpopt);
49 }
50#if DEBUG
51 fprintf(stderr, "connect: not implemented for fd %d\n", socket);
52#endif
53 errno= ENOSYS;
54 return -1;
55}
56
57static int _tcp_connect(int socket, const struct sockaddr *address,
58 socklen_t address_len, nwio_tcpconf_t *tcpconfp)
59{
60 int r;
61 struct sockaddr_in *sinp;
62 nwio_tcpconf_t tcpconf;
63 nwio_tcpcl_t tcpcl;
64
65 if (address_len != sizeof(*sinp))
66 {
67 errno= EINVAL;
68 return -1;
69 }
70 sinp= (struct sockaddr_in *)address;
71 if (sinp->sin_family != AF_INET)
72 {
73 errno= EINVAL;
74 return -1;
75 }
76 tcpconf.nwtc_flags= NWTC_SET_RA | NWTC_SET_RP;
77 if ((tcpconfp->nwtc_flags & NWTC_LOCPORT_MASK) == NWTC_LP_UNSET)
78 tcpconf.nwtc_flags |= NWTC_LP_SEL;
79 tcpconf.nwtc_remaddr= sinp->sin_addr.s_addr;
80 tcpconf.nwtc_remport= sinp->sin_port;
81
82 if (ioctl(socket, NWIOSTCPCONF, &tcpconf) == -1)
83 {
84 /* Ignore EISCONN error. The NWIOTCPCONN ioctl will get the
85 * right error.
86 */
87 if (errno != EISCONN)
88 return -1;
89 }
90
91 tcpcl.nwtcl_flags= TCF_DEFAULT;
92
93 r= fcntl(socket, F_GETFL);
94 if (r == 1)
95 return -1;
96 if (r & O_NONBLOCK)
97 tcpcl.nwtcl_flags |= TCF_ASYNCH;
98
99 r= ioctl(socket, NWIOTCPCONN, &tcpcl);
100 return r;
101}
102
103static int _udp_connect(int socket, const struct sockaddr *address,
104 socklen_t address_len, nwio_udpopt_t *udpoptp)
105{
106 int r;
107 struct sockaddr_in *sinp;
108 nwio_udpopt_t udpopt;
109
110 if (address == NULL)
111 {
112 /* Unset remote address */
113 udpopt.nwuo_flags= NWUO_RP_ANY | NWUO_RA_ANY;
114
115 r= ioctl(socket, NWIOSUDPOPT, &udpopt);
116 return r;
117 }
118
119 if (address_len != sizeof(*sinp))
120 {
121 errno= EINVAL;
122 return -1;
123 }
124 sinp= (struct sockaddr_in *)address;
125 if (sinp->sin_family != AF_INET)
126 {
127 errno= EINVAL;
128 return -1;
129 }
130 udpopt.nwuo_flags= NWUO_RP_SET | NWUO_RA_SET;
131 if ((udpoptp->nwuo_flags & NWUO_LOCPORT_MASK) == NWUO_LP_ANY)
132 udpopt.nwuo_flags |= NWUO_LP_SEL;
133 udpopt.nwuo_remaddr= sinp->sin_addr.s_addr;
134 udpopt.nwuo_remport= sinp->sin_port;
135
136 r= ioctl(socket, NWIOSUDPOPT, &udpopt);
137 return r;
138}
Note: See TracBrowser for help on using the repository browser.