3 * Copyright (C) Ralph Boehme 2015
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "lib/tevent/tevent.h"
21 #include "lib/async_req/async_sock.h"
26 #include <sys/types.h>
29 int main(int argc, const char *argv[])
31 int result, listen_sock, status, exit_status;
33 struct sockaddr_in addr = { 0 };
36 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
37 if (listen_sock == -1) {
38 perror("socket() failed");
42 addr.sin_family = AF_INET;
43 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
45 for (port = 1024; port < UINT16_MAX; port++) {
46 addr.sin_port = htons(port);
47 result = bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
53 if (port == UINT16_MAX) {
54 printf("Huh, no free port?\n");
58 result = listen(listen_sock, 1);
60 perror("listen() failed");
72 struct tevent_context *ev;
73 struct tevent_req *req;
76 ev = tevent_context_init(NULL);
78 fprintf(stderr, "tevent_context_init failed\n");
82 fd = socket(PF_INET, SOCK_STREAM, 0);
88 memset(&addr, 0, sizeof(addr));
89 addr.sin_family = AF_INET;
90 addr.sin_port = htons(port);
91 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
93 req = async_connect_send(ev, ev, fd,
94 (struct sockaddr *)&addr,
95 sizeof(struct sockaddr_in),
98 if (!tevent_req_poll(req, ev)) {
99 perror("tevent_req_poll() failed");
104 result = async_connect_recv(req, &status);
111 result = waitpid(pid, &status, 0);
117 if (!WIFEXITED(status)) {
118 printf("child status: %d\n", status);
122 exit_status = WEXITSTATUS(status);
123 printf("test done: status=%d\n", exit_status);
125 if (exit_status != 0) {