Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : */
9 : : #include <stdlib.h>
10 : : #include <algorithm>
11 : : #include <vector>
12 : :
13 : : #include <comphelper/processfactory.hxx>
14 : : #include <rtl/strbuf.hxx>
15 : : #include <string.h>
16 : :
17 : : #include "DiscoveryService.hxx"
18 : :
19 : : #ifdef WIN32
20 : : #include <winsock2.h>
21 : : #include <ws2tcpip.h>
22 : : typedef int socklen_t;
23 : : #else
24 : : #include <sys/types.h>
25 : : #include <sys/socket.h>
26 : : #include <netinet/in.h>
27 : : #include <arpa/inet.h>
28 : : #endif
29 : :
30 : : using namespace osl;
31 : : using namespace rtl;
32 : : using namespace sd;
33 : : using namespace std;
34 : :
35 : 0 : DiscoveryService::DiscoveryService()
36 : : :
37 : 0 : Thread( "sd::DiscoveryService" )
38 : : // mSocket()
39 : : {
40 : 0 : mSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
41 : :
42 : : sockaddr_in aAddr;
43 : 0 : aAddr.sin_family = AF_INET;
44 : 0 : aAddr.sin_addr.s_addr = htonl(INADDR_ANY);
45 : 0 : aAddr.sin_port = htons( PORT_DISCOVERY );
46 : :
47 : 0 : bind( mSocket, (sockaddr*) &aAddr, sizeof(sockaddr_in) );
48 : :
49 : : struct ip_mreq multicastRequest;
50 : :
51 : 0 : multicastRequest.imr_multiaddr.s_addr = inet_addr( "239.0.0.1" );
52 : 0 : multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
53 : :
54 : : setsockopt( mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
55 : : #ifdef WNT
56 : : (const char*)
57 : : #endif
58 : 0 : &multicastRequest, sizeof(multicastRequest));
59 : 0 : }
60 : :
61 : 0 : DiscoveryService::~DiscoveryService()
62 : : {
63 [ # # ]: 0 : }
64 : :
65 : 0 : void DiscoveryService::execute()
66 : : {
67 [ # # ]: 0 : fprintf( stderr, "Discovery service is listening\n" );;
68 : :
69 : : char aBuffer[BUFFER_SIZE];
70 : :
71 : 0 : while ( true )
72 : : {
73 : 0 : memset( aBuffer, 0, sizeof(char) * BUFFER_SIZE );
74 : : sockaddr_in aAddr;
75 : 0 : socklen_t aLen = sizeof( aAddr );
76 [ # # ]: 0 : fprintf( stderr, "DiscoveryService waiting for packet\n" );
77 [ # # ]: 0 : recvfrom( mSocket, aBuffer, BUFFER_SIZE, 0, (sockaddr*) &aAddr, &aLen );
78 [ # # ]: 0 : fprintf( stderr, "DiscoveryService received a packet.\n" );
79 : :
80 : 0 : OString aString( aBuffer, strlen( "LOREMOTE_SEARCH" ) );
81 [ # # ]: 0 : if ( aString.compareTo( "LOREMOTE_SEARCH" ) == 0 )
82 : : {
83 : 0 : OStringBuffer aStringBuffer("LOREMOTE_ADVERTISE\n");
84 : : aStringBuffer.append( OUStringToOString(
85 [ # # ][ # # ]: 0 : osl::SocketAddr::getLocalHostname(), RTL_TEXTENCODING_UTF8 ) )
[ # # ]
86 [ # # ]: 0 : .append( "\n\n" );
87 [ # # ]: 0 : if ( sendto( mSocket, aStringBuffer.getStr(),
88 : 0 : aStringBuffer.getLength(), 0, (sockaddr*) &aAddr,
89 [ # # ]: 0 : sizeof(aAddr) ) <= 0 )
90 : : {
91 : : // Read error or closed socket -- we are done.
92 : 0 : return;
93 [ # # ]: 0 : }
94 : : }
95 [ # # ]: 0 : }
96 : : }
97 : :
98 : : DiscoveryService *sd::DiscoveryService::spService = NULL;
99 : :
100 : 0 : void DiscoveryService::setup()
101 : : {
102 [ # # ]: 0 : if (spService)
103 : 0 : return;
104 : :
105 [ # # ]: 0 : spService = new DiscoveryService();
106 : 0 : spService->launch();
107 : : }
108 : :
109 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|