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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <comphelper/processfactory.hxx>
#include <rtl/strbuf.hxx>
#include "DiscoveryService.hxx"
#ifdef WIN32
// LO vs WinAPI conflict
#undef WB_LEFT
#undef WB_RIGHT
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
using namespace osl;
using namespace rtl;
using namespace sd;
DiscoveryService::DiscoveryService() :
mSocket(0)
{
int rc;
mSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
sockaddr_in aAddr;
aAddr.sin_family = AF_INET;
aAddr.sin_addr.s_addr = htonl(INADDR_ANY);
aAddr.sin_port = htons( PORT_DISCOVERY );
rc = bind( mSocket, (sockaddr*) &aAddr, sizeof(sockaddr_in) );
if (rc)
{
SAL_WARN("sd", "DiscoveryService: bind failed: " << errno);
return; // would be better to throw, but unsure if caller handles that
}
struct ip_mreq multicastRequest;
multicastRequest.imr_multiaddr.s_addr = inet_addr( "239.0.0.1" );
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
rc = setsockopt( mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
#ifdef WNT
(const char*)
#endif
&multicastRequest, sizeof(multicastRequest));
if (rc)
{
SAL_WARN("sd", "DiscoveryService: setsockopt failed: " << errno);
return; // would be better to throw, but unsure if caller handles that
}
}
DiscoveryService::~DiscoveryService()
{
}
void SAL_CALL DiscoveryService::run()
{
char aBuffer[BUFFER_SIZE];
while ( true )
{
memset( aBuffer, 0, sizeof(char) * BUFFER_SIZE );
sockaddr_in aAddr;
socklen_t aLen = sizeof( aAddr );
recvfrom( mSocket, aBuffer, BUFFER_SIZE, 0, (sockaddr*) &aAddr, &aLen );
OString aString( aBuffer, strlen( "LOREMOTE_SEARCH" ) );
if ( aString.compareTo( "LOREMOTE_SEARCH" ) == 0 )
{
OStringBuffer aStringBuffer("LOREMOTE_ADVERTISE\n");
aStringBuffer.append( OUStringToOString(
osl::SocketAddr::getLocalHostname(), RTL_TEXTENCODING_UTF8 ) )
.append( "\n\n" );
if ( sendto( mSocket, aStringBuffer.getStr(),
aStringBuffer.getLength(), 0, (sockaddr*) &aAddr,
sizeof(aAddr) ) <= 0 )
{
// Read error or closed socket -- we are done.
return;
}
}
}
}
DiscoveryService *sd::DiscoveryService::spService = NULL;
void DiscoveryService::setup()
{
if (spService)
return;
spService = new DiscoveryService();
spService->create();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|