]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - Source/stroke/stroke.c
9ecda04137f91826a52b153aa6040a905f2b08b8
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/fcntl.h>
25 #include <linux/stddef.h>
29 static char* push_string(stroke_msg_t
**strm
, char *string
)
31 stroke_msg_t
*stroke_msg
;
39 string_length
= strlen(string
) + 1;
40 stroke_msg
->length
+= string_length
;
42 stroke_msg
= realloc(stroke_msg
, stroke_msg
->length
);
43 strcpy((char*)stroke_msg
+ stroke_msg
->length
- string_length
, string
);
46 return (char*)(u_int
)stroke_msg
->length
- string_length
;
49 static int send_stroke_msg (stroke_msg_t
*msg
)
51 struct sockaddr_un ctl_addr
= { AF_UNIX
, STROKE_SOCKET
};
56 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
59 fprintf(stderr
, "Opening unix socket %s: %s\n", STROKE_SOCKET
, strerror(errno
));
62 if (connect(sock
, (struct sockaddr
*)&ctl_addr
,
63 offsetof(struct sockaddr_un
, sun_path
) + strlen(ctl_addr
.sun_path
)) < 0)
65 fprintf(stderr
, "Connect to socket failed: %s\n", strerror(errno
));
71 if (write(sock
, msg
, msg
->length
) != msg
->length
)
73 fprintf(stderr
, "writing to socket failed: %s\n", strerror(errno
));
78 while ((byte_count
= read(sock
, buffer
, sizeof(buffer
)-1)) > 0)
80 buffer
[byte_count
] = '\0';
85 fprintf(stderr
, "reading from socket failed: %s\n", strerror(errno
));
92 static int add_connection(char *name
,
93 char *my_id
, char *other_id
,
94 char *my_addr
, char *other_addr
,
95 char *my_net
, char *other_net
,
96 u_int my_netmask
, u_int other_netmask
)
98 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
101 msg
->length
= sizeof(stroke_msg_t
);
102 msg
->type
= STR_ADD_CONN
;
104 msg
->add_conn
.name
= push_string(&msg
, name
);
106 msg
->add_conn
.me
.id
= push_string(&msg
, my_id
);
107 msg
->add_conn
.me
.address
= push_string(&msg
, my_addr
);
108 msg
->add_conn
.me
.subnet
= push_string(&msg
, my_net
);
109 msg
->add_conn
.me
.subnet_mask
= my_netmask
;
110 msg
->add_conn
.me
.cert
= NULL
;
112 msg
->add_conn
.other
.id
= push_string(&msg
, other_id
);
113 msg
->add_conn
.other
.address
= push_string(&msg
, other_addr
);
114 msg
->add_conn
.other
.subnet
= push_string(&msg
, other_net
);
115 msg
->add_conn
.other
.subnet_mask
= other_netmask
;
116 msg
->add_conn
.other
.cert
= NULL
;
118 res
= send_stroke_msg(msg
);
123 static int initiate_connection(char *name
)
125 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
128 msg
->length
= sizeof(stroke_msg_t
);
129 msg
->type
= STR_INITIATE
;
130 msg
->initiate
.name
= push_string(&msg
, name
);
131 res
= send_stroke_msg(msg
);
136 static int terminate_connection(char *name
)
138 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
141 msg
->length
= sizeof(stroke_msg_t
);
142 msg
->type
= STR_TERMINATE
;
143 msg
->initiate
.name
= push_string(&msg
, name
);
144 res
= send_stroke_msg(msg
);
149 static int show_status(char *mode
, char *connection
)
151 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
154 msg
->length
= sizeof(stroke_msg_t
);
155 if (strcmp(mode
, "statusall") == 0)
157 msg
->type
= STR_STATUS_ALL
;
161 msg
->type
= STR_STATUS
;
163 msg
->status
.name
= push_string(&msg
, connection
);
164 res
= send_stroke_msg(msg
);
169 static int set_logtype(char *context
, char *type
, int enable
)
171 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
174 msg
->length
= sizeof(stroke_msg_t
);
175 msg
->type
= STR_LOGTYPE
;
176 msg
->logtype
.context
= push_string(&msg
, context
);
177 msg
->logtype
.type
= push_string(&msg
, type
);
178 msg
->logtype
.enable
= enable
;
179 res
= send_stroke_msg(msg
);
184 static int set_loglevel(char *context
, u_int level
)
186 stroke_msg_t
*msg
= malloc(sizeof(stroke_msg_t
));
189 msg
->length
= sizeof(stroke_msg_t
);
190 msg
->type
= STR_LOGLEVEL
;
191 msg
->loglevel
.context
= push_string(&msg
, context
);
192 msg
->loglevel
.level
= level
;
193 res
= send_stroke_msg(msg
);
198 static void exit_error(char *error
)
202 fprintf(stderr
, "%s\n", error
);
207 static void exit_usage(char *error
)
210 printf(" Add a connection:\n");
211 printf(" stroke add NAME MY_ID OTHER_ID MY_ADDR OTHER_ADDR\\\n");
212 printf(" MY_NET OTHER_NET MY_NETBITS OTHER_NETBITS\n");
213 printf(" where: ID is any IKEv2 ID \n");
214 printf(" ADDR is a IPv4 address\n");
215 printf(" NET is a IPv4 address of the subnet to tunnel\n");
216 printf(" NETBITS is the size of the subnet, as the \"24\" in 192.168.0.0/24\n");
217 printf(" Initiate a connection:\n");
218 printf(" stroke up NAME\n");
219 printf(" where: NAME is a connection name added with \"stroke add\"\n");
220 printf(" Terminate a connection:\n");
221 printf(" stroke down NAME\n");
222 printf(" where: NAME is a connection name added with \"stroke add\"\n");
223 printf(" Set logtype for a logging context:\n");
224 printf(" stroke logtype CONTEXT TYPE ENABLE\n");
225 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
226 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
227 printf(" TYPE is CONTROL|ERROR|AUDIT|RAW|PRIVATE\n");
228 printf(" ENABLE is 0|1\n");
229 printf(" Set loglevel for a logging context:\n");
230 printf(" stroke loglevel CONTEXT LEVEL\n");
231 printf(" where: CONTEXT is PARSR|GNRAT|IKESA|SAMGR|CHDSA|MESSG|TPOOL|WORKR|SCHED|\n");
232 printf(" SENDR|RECVR|SOCKT|TESTR|DAEMN|CONFG|ENCPL|PAYLD\n");
233 printf(" LEVEL is 0|1|2|3\n");
234 printf(" Show connection status:\n");
235 printf(" stroke status\n");
239 int main(int argc
, char *argv
[])
248 if (strcmp(argv
[1], "status") == 0 ||
249 strcmp(argv
[1], "statusall") == 0)
251 res
= show_status(argv
[1], argc
> 2 ? argv
[2] : NULL
);
254 else if (strcmp(argv
[1], "up") == 0)
258 exit_usage("\"up\" needs a connection name");
260 res
= initiate_connection(argv
[2]);
262 else if (strcmp(argv
[1], "down") == 0)
266 exit_usage("\"down\" needs a connection name");
268 res
= terminate_connection(argv
[2]);
270 else if (strcmp(argv
[1], "add") == 0)
274 exit_usage("\"add\" needs more parameters...");
276 res
= add_connection(argv
[2],
280 atoi(argv
[9]), atoi(argv
[10]));
282 else if (strcmp(argv
[1], "logtype") == 0)
286 exit_usage("\"logtype\" needs more parameters...");
288 res
= set_logtype(argv
[2], argv
[3], atoi(argv
[4]));
290 else if (strcmp(argv
[1], "loglevel") == 0)
294 exit_usage("\"logtype\" needs more parameters...");
296 res
= set_loglevel(argv
[2], atoi(argv
[3]));