swanctl: Stop logging with Ctrl+C on Windows as well
[strongswan.git] / src / swanctl / commands / initiate.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "command.h"
17
18 #include <errno.h>
19
20
21 CALLBACK(log_cb, void,
22 bool *raw, char *name, vici_res_t *msg)
23 {
24 if (*raw)
25 {
26 vici_dump(msg, "log", stdout);
27 }
28 else
29 {
30 printf("[%s] %s\n",
31 vici_find_str(msg, " ", "group"),
32 vici_find_str(msg, "", "msg"));
33 }
34 }
35
36 static int initiate(vici_conn_t *conn)
37 {
38 vici_req_t *req;
39 vici_res_t *res;
40 bool raw = FALSE;
41 char *arg, *child = NULL;
42 int ret = 0, timeout = 0, level = 1;
43
44 while (TRUE)
45 {
46 switch (command_getopt(&arg))
47 {
48 case 'h':
49 return command_usage(NULL);
50 case 'r':
51 raw = TRUE;
52 continue;
53 case 'c':
54 child = arg;
55 continue;
56 case 't':
57 timeout = atoi(arg);
58 continue;
59 case 'l':
60 level = atoi(arg);
61 continue;
62 case EOF:
63 break;
64 default:
65 return command_usage("invalid --initiate option");
66 }
67 break;
68 }
69
70 if (vici_register(conn, "control-log", log_cb, &raw) != 0)
71 {
72 fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
73 return errno;
74 }
75 req = vici_begin("initiate");
76 if (child)
77 {
78 vici_add_key_valuef(req, "child", "%s", child);
79 }
80 if (timeout)
81 {
82 vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
83 }
84 vici_add_key_valuef(req, "loglevel", "%d", level);
85 res = vici_submit(req, conn);
86 if (!res)
87 {
88 fprintf(stderr, "initiate request failed: %s\n", strerror(errno));
89 return errno;
90 }
91 if (raw)
92 {
93 vici_dump(res, "initiate reply", stdout);
94 }
95 else
96 {
97 if (streq(vici_find_str(res, "no", "success"), "yes"))
98 {
99 printf("initiate completed successfully\n");
100 }
101 else
102 {
103 fprintf(stderr, "initiate failed: %s\n",
104 vici_find_str(res, "", "errmsg"));
105 ret = 1;
106 }
107 }
108 vici_free_res(res);
109 return ret;
110 }
111
112 /**
113 * Register the command.
114 */
115 static void __attribute__ ((constructor))reg()
116 {
117 command_register((command_t) {
118 initiate, 'i', "initiate", "initiate a connection",
119 {"--child <name> [--timeout <s>] [--raw]"},
120 {
121 {"help", 'h', 0, "show usage information"},
122 {"child", 'c', 1, "initate a CHILD_SA configuration"},
123 {"timeout", 't', 1, "timeout in seconds before detaching"},
124 {"raw", 'r', 0, "dump raw response message"},
125 {"loglevel", 'l', 1, "verbosity of redirected log"},
126 }
127 });
128 }