-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.cpp
More file actions
93 lines (76 loc) · 2.16 KB
/
receive.cpp
File metadata and controls
93 lines (76 loc) · 2.16 KB
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
/* IPv6 multicast example - ipv6_multicast_recv.c
2012 - Bjorn Lindgren <nr@c64.org>
https://github.com/bjornl/ipv6_multicast_example
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include "UdpTools.h"
void ReceiveCallback(std::string datagram, sockaddr& sourceAddr)
{
std::string toCompare = "AreYouThere?";
if(datagram.compare(toCompare) != 0)
return;
//Send correct code, send our hostname back
char hostname[1024];
printf("Now a response with hostname would be sent back!\n");
gethostname(&hostname[0], 1024);
std::string hostnameAsString(hostname);
UdpTools sendBack;
std::string targetAddress = sendBack.DecodeSockAddr(sourceAddr);
printf("%s to %s\n", &hostname[0], targetAddress.c_str());
sendBack.SendUDPDatagram(
false,
targetAddress,
"54321",
hostnameAsString.c_str(),
hostnameAsString.length());
}
int main(int argc, char ** argv)
{
if(argc != 3 && argc != 4)
{
printf("Usage: listen address port. Example receive ff02::5:6 12345\n");
printf("Usage (need be root): listen iface-name address port. Example sudo receive eth0 ff02::5:6 12345\n");
return -1;
}
std::string listenAddress = std::string(argv[1]);
std::string port = std::string(argv[2]);
std::string ifaceName;
UdpTools* p_updReceiver;
if(argc == 3)
{
if(argv[1] == NULL || argv[2] == NULL)
{
printf("Usage: listen address port. Example receive ff02::5:6 12345\n");
return -1;
}
listenAddress = std::string(argv[1]);
port = std::string(argv[2]);
p_updReceiver = new UdpTools();
}
if(argc == 4)
{
if(argv[1] == NULL || argv[2] == NULL || argv[3] == NULL)
{
printf("Usage: listen iface-name address port. Example receive eth0 ff02::5:6 12345\n");
return -1;
}
ifaceName = std::string(argv[1]);
listenAddress = std::string(argv[2]);
port = std::string(argv[3]);
p_updReceiver = new UdpTools(ifaceName);
}
receiveCallbackFcn fcn = ReceiveCallback;
p_updReceiver->ReceiveUDPDatagram(listenAddress, port, fcn);
}