summaryrefslogtreecommitdiffstats
path: root/sd/source/ui/remotecontrol/DiscoveryService.cxx
blob: bdd0b51c80020467a680104fb75d8ae82e3b8c43 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- 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 <string.h>
#include <iostream>

#include <osl/socket.hxx>
#include <config_features.h>
#include <sal/log.hxx>

#include "DiscoveryService.hxx"
#include "ZeroconfService.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/socket.h>
  #include <netinet/in.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 sd;

DiscoveryService::DiscoveryService()
    : mSocket(-1)
    , zService(nullptr)
{
}

DiscoveryService::~DiscoveryService()
{
    if (mSocket != -1)
    {
#ifdef _WIN32
        closesocket( mSocket );
#else
        close( mSocket );
#endif
    }

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

void DiscoveryService::setupSockets()
{

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

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

    zService = new AvahiNetworkService(hostname);
    zService->setup();
#endif

#ifdef _WIN32
    zService = new WINNetworkService();
    zService->setup();
#endif

    // Old implementation for backward compatibility matter
    mSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    if (mSocket == -1)
    {
        SAL_WARN("sd", "DiscoveryService: socket failed: " << errno);
        return; // would be better to throw, but unsure if caller handles that
    }

    sockaddr_in aAddr = {};
    aAddr.sin_family = AF_INET;
    aAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    aAddr.sin_port = htons( PORT_DISCOVERY );

    int rc = bind( mSocket, reinterpret_cast<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 = htonl((239U << 24) | 1U); // 239.0.0.1
    multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);

    rc = setsockopt( mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
    #ifdef _WIN32
        reinterpret_cast<const char*>(&multicastRequest),
    #else
        &multicastRequest,
    #endif
        sizeof(multicastRequest));

    if (rc)
    {
        SAL_WARN("sd", "DiscoveryService: setsockopt failed: " << errno);
        return; // would be better to throw, but unsure if caller handles that
    }
}

void SAL_CALL DiscoveryService::run()
{
    osl::Thread::setName("DiscoveryService");

    setupSockets();

    // Kept for backward compatibility
    while ( true )
    {
        char aBuffer[BUFFER_SIZE] = {};
        sockaddr_in aAddr;
        socklen_t aLen = sizeof( aAddr );
        if(recvfrom( mSocket, aBuffer, BUFFER_SIZE, 0, reinterpret_cast<sockaddr*>(&aAddr), &aLen ) > 0)
        {
            OString aString( aBuffer, strlen( "LOREMOTE_SEARCH" ) );
            if ( aString == "LOREMOTE_SEARCH" )
            {
                OString aStringBuffer = "LOREMOTE_ADVERTISE\n" +
                    OUStringToOString(osl::SocketAddr::getLocalHostname(), RTL_TEXTENCODING_UTF8 ) +
                    "\n\n";
                if ( sendto( mSocket, aStringBuffer.getStr(),
                             aStringBuffer.getLength(), 0, reinterpret_cast<sockaddr*>(&aAddr),
                             sizeof(aAddr) ) <= 0 )
                {
                    // Write error or closed socket -- we are done.
                    return;
                }
            }
        }
        else
        {
            // Read error or closed socket -- we are done.
            return;
        }
    }
}

DiscoveryService *sd::DiscoveryService::spService = nullptr;

void DiscoveryService::setup()
{
  if (spService)
    return;

  spService = new DiscoveryService();
  spService->create();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */