| 1 | /* |
| 2 | Copyright (c) 2007, Trenton Schulz |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are met: |
| 6 | |
| 7 | 1. Redistributions of source code must retain the above copyright notice, |
| 8 | this list of conditions and the following disclaimer. |
| 9 | |
| 10 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | this list of conditions and the following disclaimer in the documentation |
| 12 | and/or other materials provided with the distribution. |
| 13 | |
| 14 | 3. The name of the author may not be used to endorse or promote products |
| 15 | derived from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 20 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "bonjourserviceregister.h" |
| 30 | |
| 31 | #include <QtCore/QSocketNotifier> |
| 32 | |
| 33 | BonjourServiceRegister::BonjourServiceRegister(QObject *parent) |
| 34 | : QObject(parent), dnssref(0), bonjourSocket(0) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | BonjourServiceRegister::~BonjourServiceRegister() |
| 39 | { |
| 40 | if (dnssref) { |
| 41 | DNSServiceRefDeallocate(dnssref); |
| 42 | dnssref = 0; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void BonjourServiceRegister::registerService(const BonjourRecord &record, quint16 servicePort) |
| 47 | { |
| 48 | if (dnssref) { |
| 49 | qWarning("Warning: Already registered a service for this object, aborting new register"); |
| 50 | return; |
| 51 | } |
| 52 | quint16 bigEndianPort = servicePort; |
| 53 | #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
| 54 | { |
| 55 | bigEndianPort = 0 | ((servicePort & 0x00ff) << 8) | ((servicePort & 0xff00) >> 8); |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | DNSServiceErrorType err = DNSServiceRegister(&dnssref, 0, 0, record.serviceName.toUtf8().constData(), |
| 60 | record.registeredType.toUtf8().constData(), |
| 61 | record.replyDomain.isEmpty() ? 0 |
| 62 | : record.replyDomain.toUtf8().constData(), 0, |
| 63 | bigEndianPort, 0, 0, bonjourRegisterService, this); |
| 64 | if (err != kDNSServiceErr_NoError) { |
| 65 | emit error(err); |
| 66 | } else { |
| 67 | int sockfd = DNSServiceRefSockFD(dnssref); |
| 68 | if (sockfd == -1) { |
| 69 | emit error(kDNSServiceErr_Invalid); |
| 70 | } else { |
| 71 | bonjourSocket = new QSocketNotifier(sockfd, QSocketNotifier::Read, this); |
| 72 | connect(bonjourSocket, SIGNAL(activated(int)), this, SLOT(bonjourSocketReadyRead())); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void BonjourServiceRegister::bonjourSocketReadyRead() |
| 79 | { |
| 80 | DNSServiceErrorType err = DNSServiceProcessResult(dnssref); |
| 81 | if (err != kDNSServiceErr_NoError) |
| 82 | emit error(err); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void BonjourServiceRegister::bonjourRegisterService(DNSServiceRef, DNSServiceFlags, |
| 87 | DNSServiceErrorType errorCode, const char *name, |
| 88 | const char *regtype, const char *domain, |
| 89 | void *data) |
| 90 | { |
| 91 | BonjourServiceRegister *serviceRegister = static_cast<BonjourServiceRegister *>(data); |
| 92 | if (errorCode != kDNSServiceErr_NoError) { |
| 93 | emit serviceRegister->error(errorCode); |
| 94 | } else { |
| 95 | serviceRegister->finalRecord = BonjourRecord(QString::fromUtf8(name), |
| 96 | QString::fromUtf8(regtype), |
| 97 | QString::fromUtf8(domain)); |
| 98 | emit serviceRegister->serviceRegistered(serviceRegister->finalRecord); |
| 99 | } |
| 100 | } |