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>
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
)
26 nwio_tcpconf_t tcpconf
;
29 r
= ioctl(socket
, NWIOGTCPCONF
, &tcpconf
);
30 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
34 /* Bad file descriptor */
37 return _tcp_connect(socket
, address
, address_len
, &tcpconf
);
40 r
= ioctl(socket
, NWIOGUDPOPT
, &udpopt
);
41 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
45 /* Bad file descriptor */
48 return _udp_connect(socket
, address
, address_len
, &udpopt
);
51 fprintf(stderr
, "connect: not implemented for fd %d\n", socket
);
57 static int _tcp_connect(int socket
, const struct sockaddr
*address
,
58 socklen_t address_len
, nwio_tcpconf_t
*tcpconfp
)
61 struct sockaddr_in
*sinp
;
62 nwio_tcpconf_t tcpconf
;
65 if (address_len
!= sizeof(*sinp
))
70 sinp
= (struct sockaddr_in
*)address
;
71 if (sinp
->sin_family
!= AF_INET
)
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
91 tcpcl
.nwtcl_flags
= TCF_DEFAULT
;
93 r
= fcntl(socket
, F_GETFL
);
97 tcpcl
.nwtcl_flags
|= TCF_ASYNCH
;
99 r
= ioctl(socket
, NWIOTCPCONN
, &tcpcl
);
103 static int _udp_connect(int socket
, const struct sockaddr
*address
,
104 socklen_t address_len
, nwio_udpopt_t
*udpoptp
)
107 struct sockaddr_in
*sinp
;
108 nwio_udpopt_t udpopt
;
112 /* Unset remote address */
113 udpopt
.nwuo_flags
= NWUO_RP_ANY
| NWUO_RA_ANY
;
115 r
= ioctl(socket
, NWIOSUDPOPT
, &udpopt
);
119 if (address_len
!= sizeof(*sinp
))
124 sinp
= (struct sockaddr_in
*)address
;
125 if (sinp
->sin_family
!= AF_INET
)
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
);