2 /* Id: test.c,v 1.1 2001/09/17 19:06:59 bodo Exp */
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
16 #include <sys/select.h>
17 #include <sys/socket.h>
23 void test_process_init(int fd
, int client_p
, void *apparg
)
26 "test_process_init(fd = %d, client_p = %d, apparg = %p)\n", fd
,
30 void test_errflush(int child_p
, char *errbuf
, size_t num
, void *apparg
)
32 fputs(errbuf
, stderr
);
35 int main(int argc
, char *argv
[])
45 char infobuf
[TLS_INFO_SIZE
+ 1];
47 if (argc
> 1 && argv
[1][0] == '-') {
48 fputs("Usage: test [port] -- server\n"
49 " test num.num.num.num [port] -- client\n", stderr
);
54 if (strchr(argv
[1], '.')) {
59 fputs(client_p
? "Client\n" : "Server\n", stderr
);
62 struct tls_create_ctx_args a
= tls_create_ctx_defaultargs();
63 a
.client_p
= client_p
;
64 a
.certificate_file
= "cert.pem";
65 a
.key_file
= "cert.pem";
66 a
.ca_file
= "cacerts.pem";
68 ctx
= tls_create_ctx(a
, NULL
);
73 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
80 struct sockaddr_in addr
;
81 size_t addr_len
= sizeof addr
;
83 addr
.sin_family
= AF_INET
;
86 sscanf(argv
[2], "%d", &port
);
89 addr
.sin_port
= htons(port
);
90 addr
.sin_addr
.s_addr
= inet_addr(argv
[1]);
92 r
= connect(s
, &addr
, addr_len
);
98 fprintf(stderr
, "Connect (fd = %d).\n", fd
);
104 r
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&i
, sizeof i
);
106 perror("setsockopt");
112 struct sockaddr_in addr
;
113 size_t addr_len
= sizeof addr
;
116 sscanf(argv
[1], "%d", &port
);
119 addr
.sin_family
= AF_INET
;
120 addr
.sin_port
= htons(port
);
121 addr
.sin_addr
.s_addr
= INADDR_ANY
;
123 r
= bind(s
, &addr
, addr_len
);
136 fprintf(stderr
, "Listening at port %i.\n", port
);
138 fd
= accept(s
, NULL
, 0);
144 fprintf(stderr
, "Accept (fd = %d).\n", fd
);
147 conn_in
= fdopen(fd
, "r");
148 if (conn_in
== NULL
) {
152 conn_out
= fdopen(fd
, "w");
153 if (conn_out
== NULL
) {
158 setvbuf(conn_in
, NULL
, _IOLBF
, 256);
159 setvbuf(conn_out
, NULL
, _IOLBF
, 256);
161 while (fgets(buf
, sizeof buf
, stdin
) != NULL
) {
163 fprintf(conn_out
, "%.*s\r\n", (int)(strlen(buf
+ 1) - 1),
165 fprintf(stderr
, ">>> %.*s\n", (int)(strlen(buf
+ 1) - 1),
167 } else if (buf
[0] == 'C') {
168 fprintf(stderr
, "Closing.\n");
172 } else if (buf
[0] == 'R') {
175 sscanf(buf
+ 1, "%d", &lines
);
177 if (fgets(buf
, sizeof buf
, conn_in
) == NULL
) {
178 if (ferror(conn_in
)) {
179 fprintf(stderr
, "ERROR\n");
182 fprintf(stderr
, "CLOSED\n");
185 fprintf(stderr
, "<<< %s", buf
);
186 } while (--lines
> 0);
187 } else if (buf
[0] == 'T') {
192 struct tls_start_proxy_args a
= tls_start_proxy_defaultargs();
194 a
.client_p
= client_p
;
197 r
= tls_start_proxy(a
, NULL
);
201 fprintf(stderr
, "tls_start_proxy failed: %d\n", r
);
204 fputs("socketpair", stderr
);
207 fputs("FD_SETSIZE exceeded", stderr
);
210 fputs("pipe", stderr
);
213 fputs("fork", stderr
);
216 fputs("dup2", stderr
);
228 r
= read(infofd
, infobuf
, sizeof infobuf
- 1);
230 const char *info
= infobuf
;
234 while ((eol
= strchr(info
, '\n')) != NULL
) {
235 fprintf(stderr
, "+++ `%.*s'\n", eol
- info
, info
);
241 fprintf(stderr
, "W... write line to network\n"
242 "R[n] read line (n lines) from network\n"
244 "T start %sTLS proxy\n", tls
? "another " : "");