aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/zeroconf/BonjourService.java
blob: 8258da7ed38c87cb82b01231ccee7b3f854815b5 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.zeroconf;

import java.io.*;
import java.net.*;
import java.util.*;

import net.java.sip.communicator.impl.protocol.zeroconf.jmdns.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

/**
 * Class dealing with JmDNS and treating all the
 * incoming connections on the bonjour port
 * @author Christian Vincenot
 */
public class BonjourService extends Thread
    implements  ServiceListener,
                DNSListener
{
    private static final Logger logger =
        Logger.getLogger(BonjourService.class);

    private int port = 5298;
    private ServerSocket sock = null;
    private String id = null;
    private JmDNS jmdns=null;
    private final Map<String, Object> props = new Hashtable<String, Object>();
    private ServiceInfo service = null;
    private boolean dead = false;

    private final List<ContactZeroconfImpl> contacts
        = new Vector<ContactZeroconfImpl>();

    private ProtocolProviderServiceZeroconfImpl pps;
    OperationSetPersistentPresenceZeroconfImpl opSetPersPresence;

    private ZeroconfAccountID acc;

    /* Should maybe better get the status directly from OperationSetPresence */
    private PresenceStatus status = ZeroconfStatusEnum.OFFLINE;

    /**
     * Returns the corresponding ProtocolProviderService
     * @return corresponding ProtocolProviderService
     */
    public ProtocolProviderServiceZeroconfImpl getPPS()
    {
        return pps;
    }

    /**
     * Returns the id of this service.
     * @return returns the id of this service.
     */
    String getID()
    {
        return id;
    }

    /**
     * Creates a new instance of the Bonjour service thread
     * @param port TCP Port number on which to try to start the Bonjour service
     * @param pps ProtocolProviderService instance
     *      which is creating this BonjourService
     */
    public BonjourService(int port,
                          ProtocolProviderServiceZeroconfImpl pps)
    {
        this.acc = (ZeroconfAccountID) pps.getAccountID();
        this.port = port;
        this.id = acc.getUserID();
        this.pps = pps;

        opSetPersPresence =
            (OperationSetPersistentPresenceZeroconfImpl) pps
                .getOperationSet(OperationSetPersistentPresence.class);

        // Gaim
        props.put("1st", (acc.getFirst() == null)? "":acc.getFirst());
        props.put("email", (acc.getMail() == null)? "":acc.getMail());
        props.put("jid", this.id);
        props.put("last", (acc.getLast() == null)?"":acc.getLast());
        props.put("msg", opSetPersPresence.getCurrentStatusMessage());
        props.put("status", "avail");

        //iChat
        props.put("phsh","000");
        //props.put("status","avail");
        //props.put("port.p2pj", "5298");
        props.put("vc", "C!");
        //props.put("1st", "John");
        props.put("txtvers","1");

        //XEP-0174 (Final paper)
        props.put("ext","");
        props.put("nick", (acc.getFirst() == null)? this.id:acc.getFirst());
        props.put("ver", "1");
        props.put("node", "SIP Communicator");

        //Ours
        props.put("client", "SIP Communicator");

        changeStatus(opSetPersPresence.getPresenceStatus());

        sock = createSocket(port);
        if (sock == null)
            return;

        port = sock.getLocalPort();

        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: ServerSocket bound to port "+port);

        props.put("port.p2pj", Integer.toString(port));
        this.setDaemon(true);
        this.start();
    }

    /* TODO: Better exception checking to avoid sudden exit and bonjour
     * service shutdown */

    /**
     * Walk?
     */
    @Override
    public void run()
    {
        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: Bonjour Service Thread up and running!");

        /* Put jmDNS in DEBUD Mode :
         * Following verbosity levels can be chosen :
         * "INFO" , "WARNING", "SEVERE", "ALL", "FINE", "FINER", "FINEST", etc
         */
        //System.setProperty("jmdns.debug", "0");

        while (dead == false)
        {
            if (sock == null || sock.isClosed())
            {
                sock = createSocket(port);
                /* What should we do now? TEMPORARY: shutdown()*/
                if (sock == null) shutdown();
                port = sock.getLocalPort();
                props.put("port.p2pj", Integer.toString(port));
                //TODO: update JmDNS in case the port had to be changed!
            }
            try
            {
                Socket connection = sock.accept();
                ContactZeroconfImpl contact = getContact(null,
                                                    connection.getInetAddress());
                /*if (status.equals(ZeroconfStatusEnum.OFFLINE)
                || status.equals(ZeroconfStatusEnum.INVISIBLE) */
                if (dead == true) break;

                if  ((contact == null)
                  || (contact.getClientThread() != null))
                {
                    if (contact == null)
                        logger.error("ZEROCONF: Connexion from "
                                + "unknown contact ["
                                + connection.getInetAddress()
                                +"]. REJECTING!");
                    else if (contact.getClientThread() == null)
                        logger.error("ZEROCONF: Redundant chat "
                                + "channel ["
                                + contact
                                +"]. REJECTING!");
                    connection.close();
                }
                else new ClientThread(connection, this);
            }
            catch(Exception e)
            {
                logger.error(e);
            }
        }

        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: Going Offline - "
                          +"BonjourService Thread exiting!");
    }

    /**
     * Might be used for shutdown...
     */
    public void shutdown()
    {
        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: Shutdown!");

        dead = true;
        try
        {   sock.close();   }
        catch (Exception ex)
        {   logger.error(ex);  }

        changeStatus(ZeroconfStatusEnum.OFFLINE);
        if(jmdns != null)
            jmdns.close();
    }

    private ServerSocket createSocket(int port)
    {
        ServerSocket sock=null;
        try
        {
            sock = new ServerSocket(port);
        }
        catch(Exception e)
        {
            logger.error("ZEROCONF: Couldn't bind socket to port "
                               +port+"! Switching to an other port...");
            try
            {
                sock = new ServerSocket(0);
            }
            catch (IOException ex)
            {
                logger.error("ZEROCONF: FATAL ERROR => "
                                  +"Couldn't bind to a port!!", ex);
            }
        }

        return sock;
    }

    /**
     * Changes the status of the local user.
     * @param stat New presence status
     */
    public void changeStatus(PresenceStatus stat)
    {
        /* [old_status == new_status ?] => NOP */
        if (stat.equals(status))
            return;

        /* [new_status == OFFLINE ?] => clean up everything */
        if (stat.equals(ZeroconfStatusEnum.OFFLINE))
        {
            if (logger.isDebugEnabled())
                logger.debug("ZEROCONF: Going OFFLINE");
            //jmdns.unregisterAllServices();
            jmdns.removeServiceListener("_presence._tcp.local.", this);
            jmdns.close();
            jmdns=null;
            //dead = true;

            // Erase all contacts by putting them OFFLINE
            opSetPersPresence.changePresenceStatusForAllContacts(
                    opSetPersPresence.getServerStoredContactListRoot(), stat);

            try
            {
                sleep(1000);
            } catch (InterruptedException ex)
            {
                logger.error(ex);
            }
        }

        /* [old_status == OFFLINE ?] => register service */
        else if (status.equals(ZeroconfStatusEnum.OFFLINE))
        {
            if (logger.isDebugEnabled())
                logger.debug("ZEROCONF: Getting out of OFFLINE state");
            props.put("status", stat.getStatusName());
            service = new ServiceInfo("_presence._tcp.local.", id,
                                      port, 0, 0, props);

            try
            {
                jmdns = new JmDNS();
                jmdns.registerServiceType("_presence._tcp.local.");
                jmdns.addServiceListener("_presence._tcp.local.", this);
                jmdns.registerService(service);

                /* In case the ID had to be changed */
                id = service.getName();
            }
            catch (Exception ex)
            {   logger.error(ex);   }

            //dead = false;

            /* Normal status change */
        }
        else
        {
            if (logger.isDebugEnabled())
                logger.debug("ZEROCONF : Changing status");

            props.put("status", stat.getStatusName());

            /* FIXME: Not totally race condition free since the 3 calls aren't
             * atomic, but that's not really critical since there's little
             * change chance of concurrent local contact change, and this
             * wouldn't have big consequences.
             */
            ServiceInfo info =
               jmdns.getLocalService(id.toLowerCase()+"._presence._tcp.local.");
            if (info == null)
               logger.error("ZEROCONF/JMDNS: PROBLEM GETTING "
                                 +"LOCAL SERVICEINFO !!");

            byte[] old = info.getTextBytes();
            info.setProps(props);
            jmdns.updateInfos(info, old);
        }

        status = stat;
    }

    private class AddThread extends Thread
    {
        private String type, name;
        public AddThread(String type, String name)
        {
            this.setDaemon(true);
            this.type = type;
            this.name = name;
            this.start();
        }

        @Override
        public void run()
        {
            ServiceInfo service = null;
            while ((service == null) && (dead == false)
            && !status.equals(ZeroconfStatusEnum.OFFLINE))
            {
                service = jmdns.getServiceInfo(type, name, 10000);
                if (service == null)
                    logger.error("BONJOUR: ERROR - Service Info of "
                                      + name +" not found in cache!!");
                try
                {
                    sleep(2);
                }
                catch (InterruptedException ex)
                {
                    logger.error(ex);
                }
            }
            if ((dead == false) && !status.equals(ZeroconfStatusEnum.OFFLINE))
                jmdns.requestServiceInfo(type, name);
            //} else handleResolvedService(name, type, service);
        }
    }

    /* Service Listener Implementation */

    /**
     * A service has been added.
     *
     * @param event The ServiceEvent providing the name and fully qualified type
     *              of the service.
     */
    public void serviceAdded(ServiceEvent event)
    {
        /* WARNING: DONT PUT ANY BLOCKING CALLS OR FLAWED LOOPS IN THIS METHOD.
         * JmDNS calls this method without creating a new thread, so if this
         * method doesn't return, jmDNS will hang !!
         */

        String name = event.getName();
        String type = event.getType();

        if (name.equals(id))
            return;

        if (logger.isDebugEnabled())
            logger.debug("BONJOUR: "+name
                          +"["+type+"] detected! Trying to get information...");
        try
        {
            sleep(2);
        }
        catch (InterruptedException ex)
        {
            logger.error(ex);
        }

        jmdns.printServices();

        new AddThread(type, name);
    }



    /**
     * A service has been removed.
     *
     * @param event The ServiceEvent providing the name and fully qualified type
     *              of the service.
     */
    public void serviceRemoved(ServiceEvent event)
    {
        String name = event.getName();
        if (name.equals(id))
            return;

        ContactZeroconfImpl contact = getContact(name, null);

        if(contact == null)
            return;

        opSetPersPresence.changePresenceStatusForContact(contact,
                                            ZeroconfStatusEnum.OFFLINE);
        if (logger.isDebugEnabled())
            logger.debug("BONJOUR: Received announcement that "
                          +name+" went offline!");

    }

    /**
     * A service has been resolved. Its details are now available in the
     * ServiceInfo record.
     *
     * @param event The ServiceEvent providing the name, the fully qualified
     *              type of the service, and the service info record,
     *              or null if the service could not be resolved.
     */
    public void serviceResolved(ServiceEvent event)
    {
        String contactID = event.getName();
        String type = event.getType();
        ServiceInfo info = event.getInfo();

        if (logger.isDebugEnabled())
            logger.debug("BONJOUR:    Information about "
                          +contactID+" discovered");

        handleResolvedService(contactID, type, info);
    }

    private void handleResolvedService(String contactID,
                                       String type,
                                       ServiceInfo info)
    {
        if (contactID.equals(id))
            return;

        if (info.getAddress().toString().length() > 15)
        {
              if (logger.isDebugEnabled())
                  logger.debug("ZEROCONF: Temporarily ignoring IPv6 addresses!");
              return;
        }

        ContactZeroconfImpl newFriend;

        synchronized(this)
        {
            if (getContact(contactID, info.getAddress()) != null)
            {
                if (logger.isDebugEnabled())
                    logger.debug("Contact "
                              +contactID+" already in contact list! Skipping.");
                return;
            };
            if (logger.isDebugEnabled())
                logger.debug("ZEROCNF: ContactID " + contactID +
                " Address " + info.getAddress());

            if (logger.isDebugEnabled())
                logger.debug("            Address=>"+info.getAddress()
                          +":"+info.getPort());

            for (Iterator<String> names = info.getPropertyNames();
                    names.hasNext();)
            {
                String prop = names.next();
                if (logger.isDebugEnabled())
                    logger.debug("            "+prop+"=>"
                              +info.getPropertyString(prop));
            }

            /* Creating the contact */
            String name = info.getPropertyString("1st");
            if (info.getPropertyString("last") != null)
                name += " "+ info.getPropertyString("last");

            int port = Integer.valueOf(
                info.getPropertyString("port.p2pj")).intValue();

            if (port < 1)
            {
                logger.error("ZEROCONF: Flawed contact announced himself"
                              +"without necessary parameters : "+contactID);
                return;
            }

            if (logger.isDebugEnabled())
                logger.debug("ZEROCONF: Detected client "+name);

            newFriend =
                    opSetPersPresence.createVolatileContact(
                        contactID, this, name,
                        info.getAddress(), port);
        }
        /* Try to detect which client type it is */
        int clientType = ContactZeroconfImpl.XMPP;
        if (info.getPropertyString("client") != null
                && info.getPropertyString("client").
                compareToIgnoreCase("SIP Communicator") == 0)
                    clientType = ContactZeroconfImpl.SIPCOM;

        else if ((info.getPropertyString("jid") != null)
              && (info.getPropertyString("node") == null))
                    clientType = ContactZeroconfImpl.GAIM;

        else if (info.getPropertyString("jid") == null)
            clientType = ContactZeroconfImpl.ICHAT;

        newFriend.setClientType(clientType);
        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: CLIENT TYPE "+clientType);

        ZeroconfStatusEnum status =
                ZeroconfStatusEnum.statusOf(info.getPropertyString("status"));
        opSetPersPresence.
                changePresenceStatusForContact(newFriend,
                               status == null?ZeroconfStatusEnum.ONLINE:status);

        // Listening for changes
        jmdns.addListener(this, new DNSQuestion(info.getQualifiedName(),
                                                DNSConstants.TYPE_SRV,
                                                DNSConstants.CLASS_UNIQUE));
    }

    /**
     * Callback called by JmDNS to inform the
     * BonjourService of a potential status change of some contacts.
     * @param jmdns JmDNS instance responsible for this
     * @param now Timestamp
     * @param record DNSRecord which changed
     */
    public synchronized void updateRecord(  JmDNS jmdns,
                                            long now,
                                            DNSRecord record)
    {
        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF/JMDNS: Received record update for "+record);

        int clazz = record.getClazz();
        int type = record.getType();

        /* Check the info returned by JmDNS since we can't really trust its
         * filtering. */
        if (!(((type & DNSConstants.TYPE_TXT) != 0) &&
                ((clazz & DNSConstants.CLASS_IN) != 0) &&
                record.isUnique() &&
                record.getName().endsWith("_presence._tcp.local.")))
            return;

        String name = record.getName().replaceAll("._presence._tcp.local.","");
        ContactZeroconfImpl contact;

        synchronized(this)
        {
            contact = getContact(name, null);

            if (contact == null) { //return;
                logger.error("ZEROCONF: BUG in jmDNS => Received update without "
                        +"previous contact annoucement. Trying to add contact");
                new AddThread("_presence._tcp.local.", name);
                return;
            }
        }

        if (logger.isDebugEnabled())
            logger.debug("ZEROCONF: "+ name
                         + " changed status. Requesting fresh data!");

        /* Since a record was updated, we can be sure that we can do a blocking
         * getServiceInfo without risk. (Still, we use the method with timeout
         * to avoid bad surprises). If some problems of status change refresh
         * appear, we'll have to fall back on the method with callback as we've
         * done for "ServiceAdded".
         */

        ServiceInfo info = jmdns.getServiceInfo("_presence._tcp.local.", name,
                                                1000);
        if (info == null)
        {
                logger.error("ZEROCONF/JMDNS: Problem!! The service "
                              +"information was not in cache. See comment in "
                              +"BonjourService.java:updateRecord !!");
                return;
        }

        /* Let's change what we can : status, message, etc */
        ZeroconfStatusEnum status =
                ZeroconfStatusEnum.statusOf(info.getPropertyString("status"));

        opSetPersPresence.
                changePresenceStatusForContact(contact,
                           status == null ? ZeroconfStatusEnum.ONLINE:status);

    }

    /**
     * Returns an Iterator over all contacts.
     *
     * @return a java.util.Iterator over all contacts
     */
    public Iterator<ContactZeroconfImpl> contacts()
    {
        return contacts.iterator();
    }

    /**
     * Adds a contact to the locally stored list of contacts
     * @param contact Zeroconf Contact to add to the local list
     */
    public void addContact(ContactZeroconfImpl contact)
    {
        if (contact == null)
            throw new IllegalArgumentException("contact");

        synchronized(contacts)
        {
            contacts.add(contact);
        }
    }
    /**
     * Returns the <tt>Contact</tt> with the specified identifier or IP address.
     *
     * @param id the identifier of the <tt>Contact</tt> we are
     *   looking for.
     * @param ip the IP address of the <tt>Contact</tt> we are looking for.
     * @return the <tt>Contact</tt> with the specified id or address.
     */
    public ContactZeroconfImpl getContact(String id, InetAddress ip)
    {
        if (id == null && ip == null) return null;

        synchronized(contacts)
        {
            Iterator<ContactZeroconfImpl> contactsIter = contacts();

            while (contactsIter.hasNext())
            {
                ContactZeroconfImpl contact = contactsIter.next();
                //System.out.println("ZEROCNF: Comparing "+id+ " "+ip+
                //" with "+ contact.getAddress()+ " " + contact.getIpAddress());
                if (((contact.getAddress().equals(id)) || (id == null))
                 && ((contact.getIpAddress().equals(ip)) || (ip == null)))
                    return contact;

            }
        }
        //System.out.println("ZEROCNF: ERROR - " +
        //"Couldn't find contact to get ["+id+" / "+ip+"]");
        return null;
    }

    /**
     * Removes the <tt>Contact</tt> with the specified identifier or IP address.
     *
     *
     * @param id the identifier of the <tt>Contact</tt> we are
     *   looking for.
     * @param ip the IP address of the <tt>Contact</tt> we are looking for.
     */
    public void removeContact(String id, InetAddress ip)
    {
        synchronized(contacts)
        {
            Iterator<ContactZeroconfImpl> contactsIter = contacts();
            while (contactsIter.hasNext())
            {
                ContactZeroconfImpl contact = contactsIter.next();
                if (((contact.getAddress().equals(id)) || (id == null))
                  &&((contact.getIpAddress().equals(ip)) || (ip == null)))
                {
                     if (contact.getClientThread() != null)
                         contact.getClientThread().cleanThread();
                     contacts.remove(contact);
                     return;
                 }
            };
        }
        logger.error(
            "ZEROCONF: ERROR - Couldn't find contact to delete ["+id+" / "+ip+"]");
     }
}