. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / ip / connect.c
blobf1ae13d588a5017d7fec199b05084f5d6b467701
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>
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>
15 #define DEBUG 0
17 static int _tcp_connect(int socket, const struct sockaddr *address,
18 socklen_t address_len, nwio_tcpconf_t *tcpconfp);
19 static int _udp_connect(int socket, const struct sockaddr *address,
20 socklen_t address_len, nwio_udpopt_t *udpoptp);
22 int connect(int socket, const struct sockaddr *address,
23 socklen_t address_len)
25 int r;
26 nwio_tcpconf_t tcpconf;
27 nwio_udpopt_t udpopt;
29 r= ioctl(socket, NWIOGTCPCONF, &tcpconf);
30 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
32 if (r == -1)
34 /* Bad file descriptor */
35 return -1;
37 return _tcp_connect(socket, address, address_len, &tcpconf);
40 r= ioctl(socket, NWIOGUDPOPT, &udpopt);
41 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
43 if (r == -1)
45 /* Bad file descriptor */
46 return -1;
48 return _udp_connect(socket, address, address_len, &udpopt);
50 #if DEBUG
51 fprintf(stderr, "connect: not implemented for fd %d\n", socket);
52 #endif
53 errno= ENOSYS;
54 return -1;
57 static int _tcp_connect(int socket, const struct sockaddr *address,
58 socklen_t address_len, nwio_tcpconf_t *tcpconfp)
60 int r;
61 struct sockaddr_in *sinp;
62 nwio_tcpconf_t tcpconf;
63 nwio_tcpcl_t tcpcl;
65 if (address_len != sizeof(*sinp))
67 errno= EINVAL;
68 return -1;
70 sinp= (struct sockaddr_in *)address;
71 if (sinp->sin_family != AF_INET)
73 errno= EINVAL;
74 return -1;
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;
82 if (ioctl(socket, NWIOSTCPCONF, &tcpconf) == -1)
84 /* Ignore EISCONN error. The NWIOTCPCONN ioctl will get the
85 * right error.
87 if (errno != EISCONN)
88 return -1;
91 tcpcl.nwtcl_flags= TCF_DEFAULT;
93 r= fcntl(socket, F_GETFL);
94 if (r == 1)
95 return -1;
96 if (r & O_NONBLOCK)
97 tcpcl.nwtcl_flags |= TCF_ASYNCH;
99 r= ioctl(socket, NWIOTCPCONN, &tcpcl);
100 return r;
103 static int _udp_connect(int socket, const struct sockaddr *address,
104 socklen_t address_len, nwio_udpopt_t *udpoptp)
106 int r;
107 struct sockaddr_in *sinp;
108 nwio_udpopt_t udpopt;
110 if (address == NULL)
112 /* Unset remote address */
113 udpopt.nwuo_flags= NWUO_RP_ANY | NWUO_RA_ANY;
115 r= ioctl(socket, NWIOSUDPOPT, &udpopt);
116 return r;
119 if (address_len != sizeof(*sinp))
121 errno= EINVAL;
122 return -1;
124 sinp= (struct sockaddr_in *)address;
125 if (sinp->sin_family != AF_INET)
127 errno= EINVAL;
128 return -1;
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;
136 r= ioctl(socket, NWIOSUDPOPT, &udpopt);
137 return r;