2 /* $Id: test.c,v 1.1.1.1 2009/07/19 23:05:18 christos Exp $ */
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
16 #include <sys/select.h>
17 #include <sys/socket.h>
24 test_process_init(int fd
, int client_p
, void *apparg
)
26 fprintf(stderr
, "test_process_init(fd = %d, client_p = %d, apparg = %p)\n", fd
, client_p
, apparg
);
30 test_errflush(int child_p
, char *errbuf
, size_t num
, void *apparg
)
32 fputs(errbuf
, stderr
);
37 main(int argc
, char *argv
[])
47 char infobuf
[TLS_INFO_SIZE
+ 1];
49 if (argc
> 1 && argv
[1][0] == '-') {
50 fputs("Usage: test [port] -- server\n"
51 " test num.num.num.num [port] -- client\n",
57 if (strchr(argv
[1], '.')) {
62 fputs(client_p
? "Client\n" : "Server\n", stderr
);
65 struct tls_create_ctx_args a
= tls_create_ctx_defaultargs();
66 a
.client_p
= client_p
;
67 a
.certificate_file
= "cert.pem";
68 a
.key_file
= "cert.pem";
69 a
.ca_file
= "cacerts.pem";
71 ctx
= tls_create_ctx(a
, NULL
);
76 s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
83 struct sockaddr_in addr
;
84 size_t addr_len
= sizeof addr
;
86 addr
.sin_family
= AF_INET
;
89 sscanf(argv
[2], "%d", &port
);
92 addr
.sin_port
= htons(port
);
93 addr
.sin_addr
.s_addr
= inet_addr(argv
[1]);
95 r
= connect(s
, &addr
, addr_len
);
101 fprintf(stderr
, "Connect (fd = %d).\n", fd
);
107 r
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (void *) &i
, sizeof i
);
109 perror("setsockopt");
115 struct sockaddr_in addr
;
116 size_t addr_len
= sizeof addr
;
119 sscanf(argv
[1], "%d", &port
);
122 addr
.sin_family
= AF_INET
;
123 addr
.sin_port
= htons(port
);
124 addr
.sin_addr
.s_addr
= INADDR_ANY
;
126 r
= bind(s
, &addr
, addr_len
);
139 fprintf(stderr
, "Listening at port %i.\n", port
);
141 fd
= accept(s
, NULL
, 0);
147 fprintf(stderr
, "Accept (fd = %d).\n", fd
);
150 conn_in
= fdopen(fd
, "r");
151 if (conn_in
== NULL
) {
155 conn_out
= fdopen(fd
, "w");
156 if (conn_out
== NULL
) {
161 setvbuf(conn_in
, NULL
, _IOLBF
, 256);
162 setvbuf(conn_out
, NULL
, _IOLBF
, 256);
164 while (fgets(buf
, sizeof buf
, stdin
) != NULL
) {
166 fprintf(conn_out
, "%.*s\r\n", (int)(strlen(buf
+ 1) - 1), buf
+ 1);
167 fprintf(stderr
, ">>> %.*s\n", (int)(strlen(buf
+ 1) - 1), buf
+ 1);
168 } else if (buf
[0] == 'C') {
169 fprintf(stderr
, "Closing.\n");
173 } else if (buf
[0] == 'R') {
176 sscanf(buf
+ 1, "%d", &lines
);
178 if (fgets(buf
, sizeof buf
, conn_in
) == NULL
) {
179 if (ferror(conn_in
)) {
180 fprintf(stderr
, "ERROR\n");
183 fprintf(stderr
, "CLOSED\n");
186 fprintf(stderr
, "<<< %s", buf
);
187 } while (--lines
> 0);
188 } else if (buf
[0] == 'T') {
193 struct tls_start_proxy_args a
= tls_start_proxy_defaultargs();
195 a
.client_p
= client_p
;
198 r
= tls_start_proxy(a
, NULL
);
202 fprintf(stderr
, "tls_start_proxy failed: %d\n", r
);
205 fputs("socketpair", stderr
); break;
207 fputs("FD_SETSIZE exceeded", stderr
); break;
209 fputs("pipe", stderr
); break;
211 fputs("fork", stderr
); break;
213 fputs("dup2", stderr
); break;
224 r
= read(infofd
, infobuf
, sizeof infobuf
- 1);
226 const char *info
= infobuf
;
230 while ((eol
= strchr(info
, '\n')) != NULL
) {
231 fprintf(stderr
, "+++ `%.*s'\n", eol
- info
, info
);
237 fprintf(stderr
, "W... write line to network\n"
238 "R[n] read line (n lines) from network\n"
240 "T start %sTLS proxy\n", tls
? "another " : "");