summaryrefslogtreecommitdiff
path: root/sd/source/ui/remotecontrol/DiscoveryService.cxx
blob: f72c3b861339b2c137b8c7ad3484a05ea2d55a76 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- 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 <iostream>

#include <comphelper/processfactory.hxx>
#include <rtl/strbuf.hxx>
#include <config_features.h>

#include "DiscoveryService.hxx"

#ifdef WIN32
  // LO vs WinAPI conflict
  #undef WB_LEFT
  #undef WB_RIGHT

  #include <winsock2.h>
  #include <ws2tcpip.h>

  #include "WINNetworkService.hxx"
  typedef int socklen_t;
#else
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
#endif

#ifdef MACOSX
  #include <osl/conditn.hxx>
  #include <premac.h>
  #import <CoreFoundation/CoreFoundation.h>
  #include <postmac.h>
  #import "OSXNetworkService.hxx"
#endif

#if HAVE_FEATURE_AVAHI
  #include "AvahiNetworkService.hxx"
#endif

using namespace osl;
using namespace rtl;
using namespace sd;

DiscoveryService::DiscoveryService()
{
    zService = NULL;

#ifdef MACOSX
    // Bonjour for OSX
    zService = new OSXNetworkService();
#endif

#if HAVE_FEATURE_AVAHI
    // Avahi for Linux
    char hostname[1024];
    hostname[1023] = '\0';
    gethostname(hostname, 1023);

    zService = new AvahiNetworkService(hostname);
#endif

#ifdef WIN32
    zService = new WINNetworkService();
#endif

    if (zService)
        zService->setup();

    // Old implementation for backward compatibility matter
    mSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

    sockaddr_in aAddr;
    memset(&aAddr, 0, sizeof(aAddr));
    aAddr.sin_family = AF_INET;
    aAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    aAddr.sin_port = htons( PORT_DISCOVERY );

    int 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()
{
  #ifdef WNT
    closesocket( mSocket );
  #else
    close( mSocket );
  #endif

     if (zService)
         zService->clear();
}

void SAL_CALL DiscoveryService::run()
{
  // Kept for backwrad compatibility
    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 == "LOREMOTE_SEARCH" )
        {
            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: */