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