1 #include <err.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sysexits.h>
5 #include <netinet/in.h>
6 #include <sys/event.h>
7 #include <sys/socket.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 
11 int
12 main(int argc, char *argv[])
13 {
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;
19 
20     if ((fd_l = socket(PF_INET, SOCK_STREAM, 0)) == -1)
21         err(EX_OSERR, "socket(2) failure");
22 
23     if ((fd_c = socket(PF_INET, SOCK_STREAM, 0)) == -1)
24         err(EX_OSERR, "socket(2) failure");
25 
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));
30 
31     if (bind(fd_l, (struct sockaddr *)&sa_local, sizeof(struct sockaddr)) == -1)
32         err(EX_OSERR, "bind(2) failure");
33 
34     if (getsockname(fd_l, (struct sockaddr *)&sa_local, &sin_size) == -1)
35         err(EX_OSERR, "getsockname(2) failure");
36 
37     if (listen(fd_l, 1) == -1)
38         err(EX_OSERR, "listen(2) failure");
39 
40     if (connect(fd_c, (struct sockaddr *)&sa_local, sizeof(struct sockaddr)) == -1)
41         err(EX_OSERR, "connect(2) failure");
42 
43     fd_n = accept(fd_l, (struct sockaddr *)&sa_remote, &sin_size);
44 
45     if ((kq = kqueue()) == -1)
46         err(EX_OSERR, "kqueue(2) failure");
47 
48     if (send(fd_c, "x", 1, MSG_OOB) == -1)
49         err(EX_OSERR, "send(2) failure");
50 
51     EV_SET(&changelist[0], fd_n, EVFILT_EXCEPT, EV_ADD|EV_ENABLE, NOTE_OOB, 0, NULL);
52 
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");
56 
57     if (i == 1 && eventlist[0].fflags & NOTE_OOB) {
58         printf("ok\n");
59     }
60 
61     return (0);
62 }
63