- changed build order to fix build error after distclean
[strongswan.git] / src / starter / starterstroke.c
1 /* Stroke for charon is the counterpart to whack from pluto
2 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
3 *
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>.
8 *
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
12 * for more details.
13 *
14 * RCSID $Id: starterstroke.c $
15 */
16
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <linux/stddef.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26
27 #include <freeswan.h>
28
29 #include <constants.h>
30 #include <defs.h>
31 #include <log.h>
32
33 #include <stroke.h>
34
35 #include "starterstroke.h"
36 #include "confread.h"
37 #include "files.h"
38
39 static char*
40 push_string(stroke_msg_t **strm, char *string)
41 {
42 stroke_msg_t *stroke_msg;
43 size_t string_length;
44
45 if (string == NULL)
46 {
47 return NULL;
48 }
49 stroke_msg = *strm;
50 string_length = strlen(string) + 1;
51 stroke_msg->length += string_length;
52
53 stroke_msg = realloc(stroke_msg, stroke_msg->length);
54 strcpy((char*)stroke_msg + stroke_msg->length - string_length, string);
55
56 *strm = stroke_msg;
57 return (char*)(u_int)stroke_msg->length - string_length;
58 }
59
60 static int
61 send_stroke_msg (stroke_msg_t *msg)
62 {
63 struct sockaddr_un ctl_addr = { AF_UNIX, CHARON_CTL_FILE };
64 int sock;
65 int byte_count;
66 char buffer[64];
67
68 sock = socket(AF_UNIX, SOCK_STREAM, 0);
69 if (sock < 0)
70 {
71 plog("socket() failed: %s", strerror(errno));
72 return -1;
73 }
74 if (connect(sock, (struct sockaddr *)&ctl_addr,
75 offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
76 {
77 plog("connect(charon_ctl) failed: %s", strerror(errno));
78 close(sock);
79 return -1;
80 }
81
82 /* send message */
83 if (write(sock, msg, msg->length) != msg->length)
84 {
85 plog("write(charon_ctl) failed: %s", strerror(errno));
86 close(sock);
87 return -1;
88 }
89 while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0)
90 {
91 buffer[byte_count] = '\0';
92 plog("%s", buffer);
93 }
94 if (byte_count < 0)
95 {
96 plog("read() failed: %s", strerror(errno));
97 }
98
99 close(sock);
100 return 0;
101 }
102
103 static char*
104 connection_name(starter_conn_t *conn)
105 {
106 /* if connection name is '%auto', create a new name like conn_xxxxx */
107 static char buf[32];
108
109 if (streq(conn->name, "%auto"))
110 {
111 sprintf(buf, "conn_%ld", conn->id);
112 return buf;
113 }
114 return conn->name;
115 }
116
117
118 int
119 starter_stroke_add_conn(starter_conn_t *conn)
120 {
121 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
122 int res;
123
124 msg->length = sizeof(stroke_msg_t);
125 msg->type = STR_ADD_CONN;
126
127 msg->add_conn.name = push_string(&msg, connection_name(conn));
128
129 msg->add_conn.me.id = push_string(&msg, conn->left.id);
130 msg->add_conn.me.cert = push_string(&msg, conn->left.cert);
131 msg->add_conn.me.address = push_string(&msg, inet_ntoa(conn->left.addr.u.v4.sin_addr));
132 msg->add_conn.me.subnet = push_string(&msg, inet_ntoa(conn->left.subnet.addr.u.v4.sin_addr));
133 msg->add_conn.me.subnet_mask = conn->left.subnet.maskbits;
134
135 msg->add_conn.other.id = push_string(&msg, conn->right.id);
136 msg->add_conn.other.cert = push_string(&msg, conn->right.cert);
137 msg->add_conn.other.address = push_string(&msg, inet_ntoa(conn->right.addr.u.v4.sin_addr));
138 msg->add_conn.other.subnet = push_string(&msg, inet_ntoa(conn->right.subnet.addr.u.v4.sin_addr));
139 msg->add_conn.other.subnet_mask = conn->right.subnet.maskbits;
140
141 res = send_stroke_msg(msg);
142 free(msg);
143 return res;
144 }
145
146 int
147 starter_stroke_del_conn(starter_conn_t *conn)
148 {
149 return 0;
150 }
151
152 int
153 starter_stroke_route_conn(starter_conn_t *conn)
154 {
155 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
156 int res;
157
158 msg->length = sizeof(stroke_msg_t);
159 msg->type = STR_INSTALL;
160 msg->install.name = push_string(&msg, connection_name(conn));
161 res = send_stroke_msg(msg);
162 free(msg);
163 return res;
164 }
165
166 int
167 starter_stroke_initiate_conn(starter_conn_t *conn)
168 {
169 stroke_msg_t *msg = malloc(sizeof(stroke_msg_t));
170 int res;
171
172 msg->length = sizeof(stroke_msg_t);
173 msg->type = STR_INITIATE;
174 msg->initiate.name = push_string(&msg, connection_name(conn));
175 res = send_stroke_msg(msg);
176 free(msg);
177 return res;
178 }