]> git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/kmpstat.c
ipsec-93.15.tar.gz
[apple/ipsec.git] / ipsec-tools / racoon / kmpstat.c
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 #ifdef __APPLE__
43 #include <System/net/pfkeyv2.h>
44 #else
45 #include <net/pfkeyv2.h>
46 #endif
47
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <errno.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 # include <sys/time.h>
58 # else
59 # include <time.h>
60 # endif
61 #endif
62 #include <netdb.h>
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66 #include <err.h>
67 #include <sys/ioctl.h>
68 #include <resolv.h>
69
70 #include "libpfkey.h"
71
72 #include "var.h"
73 #include "misc.h"
74 #include "vmbuf.h"
75 #include "plog.h"
76 #include "debug.h"
77 #include "sockmisc.h"
78
79 #include "racoonctl.h"
80 #include "admin.h"
81 #include "schedule.h"
82 #include "isakmp_var.h"
83 #include "isakmp.h"
84 #include "isakmp_xauth.h"
85 #include "isakmp_var.h"
86 #include "isakmp_cfg.h"
87 #include "isakmp_unity.h"
88 #include "oakley.h"
89 #include "handler.h"
90 #include "pfkey.h"
91 #include "admin.h"
92 #include "evt.h"
93 #include "admin_var.h"
94 #include "ipsec_doi.h"
95
96 u_int32_t racoonctl_interface = RACOONCTL_INTERFACE;
97 u_int32_t racoonctl_interface_major = RACOONCTL_INTERFACE_MAJOR;
98
99 static int so;
100 u_int32_t loglevel = 0;
101
102 int
103 com_init()
104 {
105 struct sockaddr_un name;
106
107 memset(&name, 0, sizeof(name));
108 name.sun_family = AF_UNIX;
109 snprintf(name.sun_path, sizeof(name.sun_path),
110 "%s", adminsock_path);
111
112 so = socket(AF_UNIX, SOCK_STREAM, 0);
113 if (so < 0)
114 return -1;
115
116 if (connect(so, (struct sockaddr *)&name, sizeof(name)) < 0) {
117 (void)close(so);
118 return -1;
119 }
120
121 return 0;
122 }
123
124 int
125 com_send(combuf)
126 vchar_t *combuf;
127 {
128 int len;
129
130 if ((len = send(so, combuf->v, combuf->l, 0)) == -1) {
131 perror("send");
132 (void)close(so);
133 return -1;
134 }
135
136 return 0;
137 }
138
139 int
140 com_recv(combufp)
141 vchar_t **combufp;
142 {
143 struct admin_com h, *com;
144 caddr_t buf;
145 int len;
146 int l = 0;
147 caddr_t p;
148
149 if (combufp == NULL)
150 return -1;
151
152 /* receive by PEEK */
153 if ((len = recv(so, &h, sizeof(h), MSG_PEEK)) == -1)
154 goto bad1;
155
156 /* sanity check */
157 if (len < sizeof(h))
158 goto bad1;
159
160 if (h.ac_errno) {
161 errno = h.ac_errno;
162 goto bad1;
163 }
164
165 /* allocate buffer */
166 if ((*combufp = vmalloc(h.ac_len)) == NULL)
167 goto bad1;
168
169 /* read real message */
170 p = (*combufp)->v;
171 while (l < len) {
172 if ((len = recv(so, p, h.ac_len, 0)) < 0) {
173 perror("recv");
174 goto bad2;
175 }
176 l += len;
177 p += len;
178 }
179
180 return 0;
181
182 bad2:
183 vfree(*combufp);
184 bad1:
185 *combufp = NULL;
186 return -1;
187 }
188
189 /*
190 * Dumb plog functions (used by sockmisc.c)
191 */
192 void
193 plog(int pri, const char *func, struct sockaddr *sa, const char *fmt, ...)
194 {
195 va_list ap;
196
197 va_start(ap, fmt);
198 vprintf(fmt, ap);
199 va_end(ap);
200 }
201
202 void
203 plogdump(pri, data, len)
204 int pri;
205 void *data;
206 size_t len;
207 {
208 return;
209 }
210
211 struct sockaddr *
212 get_sockaddr(family, name, port)
213 int family;
214 char *name, *port;
215 {
216 struct addrinfo hint, *ai;
217 int error;
218
219 memset(&hint, 0, sizeof(hint));
220 hint.ai_family = PF_UNSPEC;
221 hint.ai_family = family;
222 hint.ai_socktype = SOCK_STREAM;
223
224 error = getaddrinfo(name, port, &hint, &ai);
225 if (error != 0) {
226 printf("%s: %s/%s\n", gai_strerror(error), name, port);
227 return NULL;
228 }
229
230 return ai->ai_addr;
231 }