5 #include <netinet/in.h>
7 #include <sys/socket.h>
12 main(int argc
, char *argv
[])
14 struct sockaddr_in sa_local
, sa_remote
;
15 socklen_t sin_size
= sizeof(struct sockaddr_in
);
16 struct timespec timeout
;
17 struct kevent eventlist
[1], changelist
[1];
18 int kq
, fd_l
, fd_c
, fd_n
, i
;
20 if ((fd_l
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1)
21 err(EX_OSERR
, "socket(2) failure");
23 if ((fd_c
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1)
24 err(EX_OSERR
, "socket(2) failure");
26 sa_local
.sin_family
= AF_INET
;
27 sa_local
.sin_port
= 0;
28 sa_local
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
29 memset(&(sa_local
.sin_zero
), 0, sizeof(sa_local
.sin_zero
));
31 if (bind(fd_l
, (struct sockaddr
*)&sa_local
, sizeof(struct sockaddr
)) == -1)
32 err(EX_OSERR
, "bind(2) failure");
34 if (getsockname(fd_l
, (struct sockaddr
*)&sa_local
, &sin_size
) == -1)
35 err(EX_OSERR
, "getsockname(2) failure");
37 if (listen(fd_l
, 1) == -1)
38 err(EX_OSERR
, "listen(2) failure");
40 if (connect(fd_c
, (struct sockaddr
*)&sa_local
, sizeof(struct sockaddr
)) == -1)
41 err(EX_OSERR
, "connect(2) failure");
43 fd_n
= accept(fd_l
, (struct sockaddr
*)&sa_remote
, &sin_size
);
45 if ((kq
= kqueue()) == -1)
46 err(EX_OSERR
, "kqueue(2) failure");
48 if (send(fd_c
, "x", 1, MSG_OOB
) == -1)
49 err(EX_OSERR
, "send(2) failure");
51 EV_SET(&changelist
[0], fd_n
, EVFILT_EXCEPT
, EV_ADD
|EV_ENABLE
, NOTE_OOB
, 0, NULL
);
53 memset(&timeout
, 0, sizeof(timeout
));
54 if ((i
= kevent(kq
, &changelist
[0], 1, &eventlist
[0], 1, &timeout
)) == -1)
55 err(EX_OSERR
, "kevent(2) failure");
57 if (i
== 1 && eventlist
[0].fflags
& NOTE_OOB
) {