Merge pull request #115 from hfmanson/master
[tlspool] / tool / runterminal.c
1 /* runterminal.c -- shared testing code loop for message loop */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdint.h>
7
8 #include <unistd.h>
9 #include <poll.h>
10 #include <errno.h>
11 #include <signal.h>
12
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15
16 #include <tlspool/starttls.h>
17
18 void runterminal (int chanio, int *sigcont, starttls_t *tlsdata,
19                   uint32_t startflags, const char *localid, const char *remoteid) {
20         struct pollfd inout [2];
21         ssize_t sz;
22         char buf [512];
23         inout [0].fd = 0;
24         inout [1].fd = chanio;
25         inout [0].events = inout [1].events = POLLIN;
26         while (1) {
27                 if (*sigcont) {
28                         *sigcont = 0;
29                         printf ("Received SIGCONT, will now initiate TLS handshake renegotiation\n");
30                         tlsdata->flags = startflags;
31                         if (localid)
32                                 strcpy (tlsdata->localid, localid);
33                         if (remoteid)
34                                 strcpy (tlsdata->remoteid, remoteid);
35                         if (-1 == tlspool_starttls (-1, tlsdata, NULL, NULL)) {
36                                 printf ("TLS handshake renegotiation failed, terminating\n");
37                                 break;
38                         }
39                         printf ("TLS handshake renegotiation completed successfully\n");
40                 }
41                 if (poll (inout, 2, -1) == -1) {
42                         if (*sigcont) {
43                                 continue;
44                         } else {
45                                 break;
46                         }
47                 }
48                 if ((inout [0].revents | inout [1].revents) & ~POLLIN) {
49                         break;
50                 }
51                 if (inout [0].revents & POLLIN) {
52                         sz = read (0, buf, sizeof (buf));
53                         printf ("Read %ld bytes, sigcont==%d (should be 0 for proper operation)\n", sz, *sigcont);
54                         if (sz == -1) {
55                                 break;
56                         } else if (sz == 0) {
57                                 errno = 0;
58                                 break;
59                         } else if (send (chanio, buf, sz, MSG_DONTWAIT) != sz) {
60                                 break;
61                         } else {
62                                 printf ("Sent %ld bytes\n", sz);
63                         }
64                 }
65                 if (inout [1].revents & POLLIN) {
66                         sz = recv (chanio, buf, sizeof (buf), MSG_DONTWAIT);
67                         printf ("Received %ld bytes, sigcont==%d (should be 0 for proper operation)\n", sz, *sigcont);
68                         if (sz == -1) {
69                                 break;
70                         } else if (sz == 0) {
71                                 errno = 0;
72                                 break;
73                         } else if (write (1, buf, sz) != sz) {
74                                 break;
75                         } else {
76                                 printf ("Printed %ld bytes\n", sz);
77                         }
78                 }
79         }
80 }