kernel/vinum: One more -Wundef fix.
[dragonfly.git] / usr.sbin / rtadvd / control_client.c
blobd6e487be48a9482037538fae5a12ede04c323b5a
1 /*-
2 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: stable/10/usr.sbin/rtadvd/control_client.c 225519 2011-09-12 23:52:55Z hrs $
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/un.h>
35 #include <sys/uio.h>
36 #include <net/if.h>
37 #include <net/if_dl.h>
38 #include <netinet/in.h>
39 #include <netinet/icmp6.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <netdb.h>
43 #include <unistd.h>
44 #include <signal.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <syslog.h>
51 #include "pathnames.h"
52 #include "rtadvd.h"
53 #include "if.h"
54 #include "control.h"
55 #include "control_client.h"
57 int
58 cm_handler_client(int fd, int state, char *buf_orig)
60 char buf[CM_MSG_MAXLEN];
61 struct ctrl_msg_hdr *cm;
62 struct ctrl_msg_hdr *cm_orig;
63 int error;
64 char *msg;
65 char *msg_orig;
67 syslog(LOG_DEBUG, "<%s> enter", __func__);
69 memset(buf, 0, sizeof(buf));
70 cm = (struct ctrl_msg_hdr *)buf;
71 cm_orig = (struct ctrl_msg_hdr *)buf_orig;
72 msg = (char *)buf + sizeof(*cm);
73 msg_orig = (char *)buf_orig + sizeof(*cm_orig);
75 if (cm_orig->cm_len > CM_MSG_MAXLEN) {
76 syslog(LOG_DEBUG, "<%s> msg too long", __func__);
77 close(fd);
78 return (-1);
80 cm->cm_type = cm_orig->cm_type;
81 if (cm_orig->cm_len > sizeof(*cm_orig)) {
82 memcpy(msg, msg_orig, cm_orig->cm_len - sizeof(*cm));
83 cm->cm_len = cm_orig->cm_len;
85 while (state != CM_STATE_EOM) {
86 syslog(LOG_DEBUG, "<%s> state = %d", __func__, state);
88 switch (state) {
89 case CM_STATE_INIT:
90 state = CM_STATE_EOM;
91 break;
92 case CM_STATE_MSG_DISPATCH:
93 cm->cm_version = CM_VERSION;
94 error = cm_send(fd, buf);
95 if (error)
96 syslog(LOG_WARNING,
97 "<%s> cm_send()", __func__);
98 state = CM_STATE_ACK_WAIT;
99 break;
100 case CM_STATE_ACK_WAIT:
101 error = cm_recv(fd, buf);
102 if (error) {
103 syslog(LOG_ERR,
104 "<%s> cm_recv()", __func__);
105 close(fd);
106 return (-1);
108 switch (cm->cm_type) {
109 case CM_TYPE_ACK:
110 syslog(LOG_DEBUG,
111 "<%s> CM_TYPE_ACK", __func__);
112 break;
113 case CM_TYPE_ERR:
114 syslog(LOG_DEBUG,
115 "<%s> CM_TYPE_ERR", __func__);
116 close(fd);
117 return (-1);
118 default:
119 syslog(LOG_DEBUG,
120 "<%s> unknown status", __func__);
121 close(fd);
122 return (-1);
124 memcpy(buf_orig, buf, cm->cm_len);
125 state = CM_STATE_EOM;
126 break;
129 close(fd);
130 return (0);