s4/heimdal/lib/krb5/pac.c: Align PAC buffers to match Windows
[Samba.git] / lib / async_req / async_connect_send_test.c
blobd570c609d7ff252f5d4e7778fdc7b57d8e3571fc
1 /*
2 * Test async connect
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/>.
19 #include "replace.h"
20 #include <tevent.h>
21 #include "lib/async_req/async_sock.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
29 int main(int argc, const char *argv[])
31 int result, listen_sock, status, exit_status;
32 uint16_t port;
33 struct sockaddr_in addr = { 0 };
34 pid_t pid;
36 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
37 if (listen_sock == -1) {
38 perror("socket() failed");
39 exit(1);
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));
48 if (result == 0) {
49 break;
53 if (port == UINT16_MAX) {
54 printf("Huh, no free port?\n");
55 return 1;
58 result = listen(listen_sock, 1);
59 if (result == -1) {
60 perror("listen() failed");
61 close(listen_sock);
62 return 1;
65 pid = fork();
66 if (pid == -1) {
67 perror("fork");
68 return 1;
71 if (pid == 0) {
72 struct tevent_context *ev;
73 struct tevent_req *req;
74 int fd;
76 ev = tevent_context_init(NULL);
77 if (ev == NULL) {
78 fprintf(stderr, "tevent_context_init failed\n");
79 return 1;
82 fd = socket(PF_INET, SOCK_STREAM, 0);
83 if (fd == -1) {
84 perror("socket");
85 return 1;
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),
96 NULL, NULL, NULL);
98 if (!tevent_req_poll(req, ev)) {
99 perror("tevent_req_poll() failed");
100 return 1;
103 status = 0;
104 result = async_connect_recv(req, &status);
105 if (result != 0) {
106 return status;
108 return 0;
111 result = waitpid(pid, &status, 0);
112 if (result == -1) {
113 perror("waitpid");
114 return 1;
117 if (!WIFEXITED(status)) {
118 printf("child status: %d\n", status);
119 return 2;
122 exit_status = WEXITSTATUS(status);
123 printf("test done: status=%d\n", exit_status);
125 if (exit_status != 0) {
126 return exit_status;
129 return 0;