]>
Commit | Line | Data |
---|---|---|
1 | /* $KAME: kmpstat.c,v 1.33 2004/08/16 08:20:28 itojun Exp $ */ | |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include "config.h" | |
33 | ||
34 | #include <sys/types.h> | |
35 | #include <sys/param.h> | |
36 | #include <sys/socket.h> | |
37 | #include <sys/un.h> | |
38 | ||
39 | #include <netinet/in.h> | |
40 | #include <arpa/inet.h> | |
41 | ||
42 | #include <net/pfkeyv2.h> | |
43 | ||
44 | #include <stdlib.h> | |
45 | #include <stdio.h> | |
46 | #include <string.h> | |
47 | #include <errno.h> | |
48 | #if TIME_WITH_SYS_TIME | |
49 | # include <sys/time.h> | |
50 | # include <time.h> | |
51 | #else | |
52 | # if HAVE_SYS_TIME_H | |
53 | # include <sys/time.h> | |
54 | # else | |
55 | # include <time.h> | |
56 | # endif | |
57 | #endif | |
58 | #include <netdb.h> | |
59 | #ifdef HAVE_UNISTD_H | |
60 | #include <unistd.h> | |
61 | #endif | |
62 | #include <err.h> | |
63 | #include <sys/ioctl.h> | |
64 | ||
65 | #include "libpfkey.h" | |
66 | ||
67 | #include "var.h" | |
68 | #include "misc.h" | |
69 | #include "vmbuf.h" | |
70 | #include "plog.h" | |
71 | #include "debug.h" | |
72 | #include "sockmisc.h" | |
73 | ||
74 | #include "racoonctl.h" | |
75 | #include "schedule.h" | |
76 | #include "isakmp_var.h" | |
77 | #include "isakmp.h" | |
78 | #include "isakmp_xauth.h" | |
79 | #include "isakmp_var.h" | |
80 | #include "isakmp_cfg.h" | |
81 | #include "isakmp_unity.h" | |
82 | #include "oakley.h" | |
83 | #include "handler.h" | |
84 | #include "pfkey.h" | |
85 | #include "ipsec_doi.h" | |
86 | ||
87 | u_int32_t racoonctl_interface = RACOONCTL_INTERFACE; | |
88 | u_int32_t racoonctl_interface_major = RACOONCTL_INTERFACE_MAJOR; | |
89 | ||
90 | static int so; | |
91 | u_int32_t loglevel = 0; | |
92 | ||
93 | int | |
94 | com_init() | |
95 | { | |
96 | union { // Wcast-align fix - force alignment of sockaddr_un | |
97 | struct sockaddr_storage ss; | |
98 | struct sockaddr_un name; | |
99 | } u; | |
100 | ||
101 | memset(&u, 0, sizeof(struct sockaddr_un)); | |
102 | u.name.sun_family = AF_UNIX; | |
103 | snprintf(u.name.sun_path, sizeof(u.name.sun_path), | |
104 | "%s", adminsock_path); | |
105 | ||
106 | so = socket(AF_UNIX, SOCK_STREAM, 0); | |
107 | if (so < 0) | |
108 | return -1; | |
109 | ||
110 | if (connect(so, (struct sockaddr *)&u.ss, sizeof(struct sockaddr_un)) < 0) { | |
111 | (void)close(so); | |
112 | return -1; | |
113 | } | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | int | |
119 | com_send(combuf) | |
120 | vchar_t *combuf; | |
121 | { | |
122 | int len; | |
123 | ||
124 | if ((len = send(so, combuf->v, combuf->l, 0)) == -1) { | |
125 | perror("send"); | |
126 | (void)close(so); | |
127 | return -1; | |
128 | } | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
133 | int | |
134 | com_recv(combufp) | |
135 | vchar_t **combufp; | |
136 | { | |
137 | struct admin_com h; | |
138 | int len; | |
139 | int l = 0; | |
140 | caddr_t p; | |
141 | ||
142 | if (combufp == NULL) | |
143 | return -1; | |
144 | ||
145 | /* receive by PEEK */ | |
146 | if ((len = recv(so, &h, sizeof(h), MSG_PEEK)) == -1) | |
147 | goto bad1; | |
148 | ||
149 | /* sanity check */ | |
150 | if (len < sizeof(h)) | |
151 | goto bad1; | |
152 | ||
153 | if (h.ac_errno) { | |
154 | errno = h.ac_errno; | |
155 | goto bad1; | |
156 | } | |
157 | ||
158 | /* allocate buffer */ | |
159 | if ((*combufp = vmalloc(h.ac_len)) == NULL) | |
160 | goto bad1; | |
161 | ||
162 | /* read real message */ | |
163 | p = (*combufp)->v; | |
164 | while (l < len) { | |
165 | if ((len = recv(so, p, h.ac_len, 0)) < 0) { | |
166 | perror("recv"); | |
167 | goto bad2; | |
168 | } | |
169 | l += len; | |
170 | p += len; | |
171 | } | |
172 | ||
173 | return 0; | |
174 | ||
175 | bad2: | |
176 | vfree(*combufp); | |
177 | bad1: | |
178 | *combufp = NULL; | |
179 | return -1; | |
180 | } | |
181 | ||
182 | /* | |
183 | * Dumb plog functions (used by sockmisc.c) | |
184 | */ | |
185 | ||
186 | void | |
187 | plogdump_func(int pri, void *data, size_t len, const char *fmt, ...) | |
188 | { | |
189 | return; | |
190 | } | |
191 | ||
192 | struct sockaddr * | |
193 | get_sockaddr(family, name, port) | |
194 | int family; | |
195 | char *name, *port; | |
196 | { | |
197 | struct addrinfo hint, *ai; | |
198 | int error; | |
199 | ||
200 | memset(&hint, 0, sizeof(hint)); | |
201 | hint.ai_family = PF_UNSPEC; | |
202 | hint.ai_family = family; | |
203 | hint.ai_socktype = SOCK_STREAM; | |
204 | ||
205 | error = getaddrinfo(name, port, &hint, &ai); | |
206 | if (error != 0) { | |
207 | printf("%s: %s/%s\n", gai_strerror(error), name, port); | |
208 | return NULL; | |
209 | } | |
210 | ||
211 | return ai->ai_addr; | |
212 | } |