2 * Copyright (C) 2014 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 CALLBACK(log_cb
, void,
22 command_format_options_t
*format
, char *name
, vici_res_t
*msg
)
24 if (*format
& COMMAND_FORMAT_RAW
)
26 vici_dump(msg
, "log", *format
& COMMAND_FORMAT_PRETTY
, stdout
);
31 vici_find_str(msg
, " ", "group"),
32 vici_find_str(msg
, "", "msg"));
36 static int initiate(vici_conn_t
*conn
)
40 command_format_options_t format
= COMMAND_FORMAT_NONE
;
41 char *arg
, *child
= NULL
, *ike
= NULL
;
42 int ret
= 0, timeout
= 0, level
= 1;
46 switch (command_getopt(&arg
))
49 return command_usage(NULL
);
51 format
|= COMMAND_FORMAT_PRETTY
;
52 /* fall through to raw */
54 format
|= COMMAND_FORMAT_RAW
;
71 return command_usage("invalid --initiate option");
76 if (vici_register(conn
, "control-log", log_cb
, &format
) != 0)
79 fprintf(stderr
, "registering for log failed: %s\n", strerror(errno
));
82 req
= vici_begin("initiate");
85 vici_add_key_valuef(req
, "child", "%s", child
);
89 vici_add_key_valuef(req
, "ike", "%s", ike
);
93 vici_add_key_valuef(req
, "timeout", "%d", timeout
* 1000);
95 vici_add_key_valuef(req
, "loglevel", "%d", level
);
96 res
= vici_submit(req
, conn
);
100 fprintf(stderr
, "initiate request failed: %s\n", strerror(errno
));
103 if (format
& COMMAND_FORMAT_RAW
)
105 vici_dump(res
, "initiate reply", format
& COMMAND_FORMAT_PRETTY
,
110 if (streq(vici_find_str(res
, "no", "success"), "yes"))
112 printf("initiate completed successfully\n");
116 fprintf(stderr
, "initiate failed: %s\n",
117 vici_find_str(res
, "", "errmsg"));
126 * Register the command.
128 static void __attribute__ ((constructor
))reg()
130 command_register((command_t
) {
131 initiate
, 'i', "initiate", "initiate a connection",
132 {"[--child <name>] [--ike <name>] [--timeout <s>] [--raw|--pretty]"},
134 {"help", 'h', 0, "show usage information"},
135 {"child", 'c', 1, "initiate a CHILD_SA configuration"},
136 {"ike", 'i', 1, "initiate an IKE_SA, or name of child's parent"},
137 {"timeout", 't', 1, "timeout in seconds before detaching"},
138 {"raw", 'r', 0, "dump raw response message"},
139 {"pretty", 'P', 0, "dump raw response message in pretty print"},
140 {"loglevel", 'l', 1, "verbosity of redirected log"},