summaryrefslogtreecommitdiff
path: root/usr.sbin/hoststated/check_send_expect.c
blob: 16b14eeddd70167421dfb29e6c55efbe8d6c4d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*	$OpenBSD: check_send_expect.c,v 1.2 2007/01/08 20:46:18 reyk Exp $ */
/*
 * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <net/if.h>
#include <limits.h>
#include <event.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <fnmatch.h>

#include "hostated.h"

void	se_validate(struct ctl_tcp_event *);
void	se_read(int, short, void *);

void
se_validate(struct ctl_tcp_event *cte)
{
	u_char	*b;

	/*
	 * ensure string is nul-terminated.
	 */
	b = buf_reserve(cte->buf, 1);
	if (b == NULL)
		fatal("out of memory");
	*b = '\0';
	if (fnmatch(cte->table->exbuf, cte->buf->buf, 0) == 0)
		cte->host->up = HOST_UP;
	else
		cte->host->up = HOST_DOWN;

	/*
	 * go back to original position.
	 */
	cte->buf->wpos--;
}

void
se_read(int s, short event, void *arg)
{
	ssize_t			 br;
	char			 rbuf[SMALL_READ_BUF_SIZE];
	struct timeval		 tv;
	struct timeval		 tv_now;
	struct ctl_tcp_event	*cte = arg;

	if (event == EV_TIMEOUT) {
		cte->host->up = HOST_DOWN;
		buf_free(cte->buf);
		hce_notify_done(cte->host, "se_read: timeout");
		return;
	}
	br = read(s, rbuf, sizeof(rbuf));
	log_debug("se_read: %d bytes read", br);
	if (br == 0) {
		cte->host->up = HOST_DOWN;
		se_validate(cte);
		buf_free(cte->buf);
		hce_notify_done(cte->host, "se_read: connection closed");
	} else if (br == -1) {
		cte->host->up = HOST_DOWN;
		buf_free(cte->buf);
		hce_notify_done(cte->host, "se_read: read failed");
	} else {
		buf_add(cte->buf, rbuf, br);
		bcopy(&cte->table->timeout, &tv, sizeof(tv));
		if (gettimeofday(&tv_now, NULL))
			fatal("se_read: gettimeofday");
		timersub(&tv_now, &cte->tv_start, &tv_now);
		timersub(&tv, &tv_now, &tv);
		se_validate(cte);
		if (cte->host->up == HOST_UP) {
			buf_free(cte->buf);
			hce_notify_done(cte->host, NULL);
		} else
			event_once(s, EV_READ|EV_TIMEOUT, se_read, cte, &tv);
	}
}

void
start_send_expect(struct ctl_tcp_event *cte)
{
	int		 bs;
	int		 pos;
	int		 len;
	char		*req;
	struct timeval	 tv;
	struct timeval	 tv_now;

	req = cte->table->sendbuf;
	pos = 0;
	len = strlen(req);
	if (len) {
		do {
			bs = write(cte->s, req + pos, len);
			if (bs <= 0) {
				log_warnx("send_se_data: cannot send");
				cte->host->up = HOST_DOWN;
				hce_notify_done(cte->host,
				    "start_send_expect: write");
				return;
			}
			pos += bs;
			len -= bs;
		} while (len > 0);
	}

	if ((cte->buf = buf_dynamic(SMALL_READ_BUF_SIZE, UINT_MAX)) == NULL)
		fatalx("send_se_data: cannot create dynamic buffer");

	log_debug("start_send_expect: reading");

	bcopy(&cte->table->timeout, &tv, sizeof(tv));
	if (gettimeofday(&tv_now, NULL))
		fatal("start_send_expect: gettimeofday");
	timersub(&tv_now, &cte->tv_start, &tv_now);
	timersub(&tv, &tv_now, &tv);
	event_once(cte->s, EV_READ|EV_TIMEOUT, se_read, cte, &tv);
}