File: ssmping.cpp

package info (click to toggle)
dbeacon 0.3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 260 kB
  • ctags: 444
  • sloc: cpp: 2,246; perl: 1,548; makefile: 79; sh: 58
file content (86 lines) | stat: -rw-r--r-- 1,992 bytes parent folder | download
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
/*
 * Copyright (C) 2005-7, Hugo Santos <hugo@fivebits.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * $Id: ssmping.cpp 397 2007-06-09 11:35:57Z hugo $
 */

#include "dbeacon.h"
#include "address.h"
#include "msocket.h"

#include <stdio.h>
#include <unistd.h>

enum {
	SSMPING_REQUEST = 'Q',
	SSMPING_ANSWER = 'A'
};

static const int maxSSMPingMessage = 1000;

static const char *SSMPingV6ResponseChannel = "ff3e::4321:1234";
static const char *SSMPingV4ResponseChannel = "232.43.211.234";

static int ssmPingSocket = -1;

int SetupSSMPing() {
	address addr;

	if (!addr.set_family(beaconUnicastAddr.family()))
		return -1;

	if (!addr.set_port(4321))
		return -1;

	ssmPingSocket = SetupSocket(addr, true, false);
	if (ssmPingSocket < 0)
		return -1;

	if (!SetHops(ssmPingSocket, addr, 64)) {
		close(ssmPingSocket);
		return -1;
	}

	if (!RequireToAddress(ssmPingSocket, addr)) {
		close(ssmPingSocket);
		return -1;
	}

	ListenTo(SSMPING, ssmPingSocket);

	return 0;
}

void handle_ssmping(int s, address &from, const address &to, uint8_t *buffer,
					int len, uint64_t ts) {
	if (buffer[0] != SSMPING_REQUEST || len > maxSSMPingMessage)
		return;

	if (verbose > 1) {
		char tmp[64];
		from.print(tmp, sizeof(tmp));
		info("Got SSM Ping Request from %s", tmp);
	}

	buffer[0] = SSMPING_ANSWER;

	if (SendTo(s, buffer, len, to, from) < 0)
		return;

	/* reuse address structure */
	if (!from.set_addr(from.family() == AF_INET6 ? SSMPingV6ResponseChannel :
							SSMPingV4ResponseChannel))
		return;

	SendTo(s, buffer, len, to, from);
}