remote pool_handle_t
[tlspool] / tool / pingpool-async.c
1 /* pingpool.c -- Show the input/output of a PING operation.
2  *
3  * From: Rick van Rein <rick@openfortress.nl>
4  */
5
6
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <assert.h>
12
13 #include <tlspool/commands.h>
14 #include <tlspool/async.h>
15
16
17 void print_pioc_ping (pingpool_t *pp, char *prefix) {
18         char *date = pp->YYYYMMDD_producer;
19         char *producer = date + 8;
20         char facil [256];
21         facil [0] = facil [1] = '\0';
22         if (pp->facilities & PIOF_FACILITY_STARTTLS) {
23                 strcat (facil, ",starttls");
24                 pp->facilities &= ~PIOF_FACILITY_STARTTLS;
25         }
26         if (pp->facilities & PIOF_FACILITY_STARTGSS) {
27                 strcat (facil, ",startgss");
28                 pp->facilities &= ~PIOF_FACILITY_STARTGSS;
29         }
30         if (pp->facilities & PIOF_FACILITY_STARTSSH) {
31                 strcat (facil, ",startssh");
32                 pp->facilities &= ~PIOF_FACILITY_STARTSSH;
33         }
34         if (pp->facilities) {
35                 sprintf (facil + strlen (facil), ",0%08x", pp->facilities);
36         }
37         printf ("%s specdate: %.4s-%.2s-%.2s\n", prefix, date, date+4, date+6);
38         printf ("%s specfrom: %s\n", prefix, producer);
39         printf ("%s facility: %s\n", prefix, facil + 1);
40 }
41
42
43 volatile bool callback_done = false;
44
45 void cb_ping_done (struct tlspool_async_request *cbdata, int opt_fd) {
46         assert (opt_fd < 0);
47         callback_done = true;
48 }
49
50
51 int main (int argc, char *argv []) {
52         char *sockpath = NULL;
53         struct tlspool_async_pool mypool;
54         struct tlspool_async_request myreq;
55         pingpool_t *pp = &myreq.cmd.pio_data.pioc_ping;
56
57         if (argc > 2) {
58                 fprintf (stderr, "Usage: %s [socketfile]\n", argv [0]);
59                 exit (1);
60         }
61         if (argc == 2) {
62                 sockpath = argv [1];
63         }
64         assert (tlspool_async_open (&mypool, sizeof (struct tlspool_command),
65                         TLSPOOL_IDENTITY_V2, PIOF_FACILITY_STARTTLS,
66                         sockpath));
67         print_pioc_ping (&mypool.pingdata, "Initial ");
68         printf ("\n");
69
70         memset (&myreq, 0, sizeof (myreq));
71         strcpy (pp->YYYYMMDD_producer, TLSPOOL_IDENTITY_V2);
72         pp->facilities = PIOF_FACILITY_ALL_CURRENT;
73         printf ("\n");
74         print_pioc_ping (pp, "Client  ");
75         printf ("\n");
76         //
77         // We now setup the request, and allow it to callback to us.
78         // We will loop until this has happened, which is not the best
79         // programming practice, but alright for a simple demonstration.
80         // In production use, we should of course use locks.
81         //
82         myreq.cmd.pio_cmd = PIOC_PING_V2;
83         myreq.cbfunc = cb_ping_done;
84         pp->facilities = ~0L;
85         //
86         // What we do now is not what any normal program should do; we ask
87         // for all the facilities that the TLS Pool can provide.  That may
88         // include things we never heard of, and may need to mention as an
89         // integer flag value.  For a ping utility, that's useful, but for
90         // any sane program it would be a bad example to follow.
91         //
92         assert (tlspool_async_request (&mypool, &myreq, -1));
93         while (!callback_done) {
94                 tlspool_async_process (&mypool);
95         }
96         print_pioc_ping (pp, "TLS Pool");
97         printf ("\n");
98
99         assert (tlspool_async_close (&mypool, true));
100
101         return 0;
102 }
103