1224006Shrs/*-
2224006Shrs * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
3224006Shrs * All rights reserved.
4224006Shrs *
5224006Shrs * Redistribution and use in source and binary forms, with or without
6224006Shrs * modification, are permitted provided that the following conditions
7224006Shrs * are met:
8224006Shrs * 1. Redistributions of source code must retain the above copyright
9224006Shrs *    notice, this list of conditions and the following disclaimer.
10224006Shrs * 2. Redistributions in binary form must reproduce the above copyright
11224006Shrs *    notice, this list of conditions and the following disclaimer in the
12224006Shrs *    documentation and/or other materials provided with the distribution.
13224006Shrs *
14224006Shrs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15224006Shrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16224006Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17224006Shrs * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS
18224006Shrs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19224006Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20224006Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21224006Shrs * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22224006Shrs * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23224006Shrs * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24224006Shrs * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25224006Shrs *
26224006Shrs * $FreeBSD$
27224006Shrs *
28224006Shrs */
29224006Shrs
30224006Shrs#include <sys/queue.h>
31224006Shrs#include <sys/types.h>
32224006Shrs#include <sys/socket.h>
33224006Shrs#include <sys/stat.h>
34224006Shrs#include <sys/un.h>
35224006Shrs#include <sys/uio.h>
36224006Shrs#include <net/if.h>
37224006Shrs#include <net/if_dl.h>
38224006Shrs#include <netinet/in.h>
39224006Shrs#include <netinet/icmp6.h>
40224006Shrs#include <fcntl.h>
41224006Shrs#include <errno.h>
42224006Shrs#include <netdb.h>
43224006Shrs#include <unistd.h>
44224006Shrs#include <signal.h>
45224006Shrs#include <string.h>
46224006Shrs#include <stdarg.h>
47224006Shrs#include <stdio.h>
48224006Shrs#include <stdlib.h>
49224006Shrs#include <syslog.h>
50224006Shrs
51224006Shrs#include "pathnames.h"
52224006Shrs#include "rtadvd.h"
53224006Shrs#include "if.h"
54224006Shrs#include "control.h"
55224006Shrs#include "control_client.h"
56224006Shrs
57224006Shrsint
58225519Shrscm_handler_client(int fd, int state, char *buf_orig)
59224006Shrs{
60224006Shrs	char buf[CM_MSG_MAXLEN];
61224006Shrs	struct ctrl_msg_hdr *cm;
62224006Shrs	struct ctrl_msg_hdr *cm_orig;
63224006Shrs	int error;
64224006Shrs	char *msg;
65224006Shrs	char *msg_orig;
66224006Shrs
67224006Shrs	syslog(LOG_DEBUG, "<%s> enter", __func__);
68224006Shrs
69224006Shrs	memset(buf, 0, sizeof(buf));
70224006Shrs	cm = (struct ctrl_msg_hdr *)buf;
71224006Shrs	cm_orig = (struct ctrl_msg_hdr *)buf_orig;
72224006Shrs	msg = (char *)buf + sizeof(*cm);
73224006Shrs	msg_orig = (char *)buf_orig + sizeof(*cm_orig);
74224006Shrs
75224006Shrs	if (cm_orig->cm_len > CM_MSG_MAXLEN) {
76224006Shrs		syslog(LOG_DEBUG, "<%s> msg too long", __func__);
77224006Shrs		close(fd);
78224006Shrs		return (-1);
79224006Shrs	}
80224006Shrs	cm->cm_type = cm_orig->cm_type;
81224006Shrs	if (cm_orig->cm_len > sizeof(*cm_orig)) {
82224006Shrs		memcpy(msg, msg_orig, cm_orig->cm_len - sizeof(*cm));
83224006Shrs		cm->cm_len = cm_orig->cm_len;
84224006Shrs	}
85224006Shrs	while (state != CM_STATE_EOM) {
86224006Shrs		syslog(LOG_DEBUG, "<%s> state = %d", __func__, state);
87224006Shrs
88224006Shrs		switch (state) {
89224006Shrs		case CM_STATE_INIT:
90224006Shrs			state = CM_STATE_EOM;
91224006Shrs			break;
92224006Shrs		case CM_STATE_MSG_DISPATCH:
93224006Shrs			cm->cm_version = CM_VERSION;
94225519Shrs			error = cm_send(fd, buf);
95224006Shrs			if (error)
96224006Shrs				syslog(LOG_WARNING,
97225519Shrs				    "<%s> cm_send()", __func__);
98224006Shrs			state = CM_STATE_ACK_WAIT;
99224006Shrs			break;
100224006Shrs		case CM_STATE_ACK_WAIT:
101225519Shrs			error = cm_recv(fd, buf);
102224006Shrs			if (error) {
103224006Shrs				syslog(LOG_ERR,
104225519Shrs				    "<%s> cm_recv()", __func__);
105224006Shrs				close(fd);
106224006Shrs				return (-1);
107224006Shrs			}
108224006Shrs			switch (cm->cm_type) {
109224006Shrs			case CM_TYPE_ACK:
110224006Shrs				syslog(LOG_DEBUG,
111224006Shrs				    "<%s> CM_TYPE_ACK", __func__);
112224006Shrs				break;
113224006Shrs			case CM_TYPE_ERR:
114224006Shrs				syslog(LOG_DEBUG,
115224006Shrs				    "<%s> CM_TYPE_ERR", __func__);
116224006Shrs				close(fd);
117224006Shrs				return (-1);
118224006Shrs			default:
119224006Shrs				syslog(LOG_DEBUG,
120224006Shrs				    "<%s> unknown status", __func__);
121224006Shrs				close(fd);
122224006Shrs				return (-1);
123224006Shrs			}
124224006Shrs			memcpy(buf_orig, buf, cm->cm_len);
125224006Shrs			state = CM_STATE_EOM;
126224006Shrs			break;
127224006Shrs		}
128224006Shrs	}
129224006Shrs	close(fd);
130224006Shrs	return (0);
131224006Shrs}
132