Exemplo n.º 1
0
/*! Construct the hardware resource UpnpDiscovery with the given \a parent. */
UpnpDiscovery::UpnpDiscovery(QObject *parent) :
    QUdpSocket(parent)
{
    // bind udp socket and join multicast group
    m_port = 1900;
    m_host = QHostAddress("239.255.255.250");

    setSocketOption(QAbstractSocket::MulticastTtlOption,QVariant(1));
    setSocketOption(QAbstractSocket::MulticastLoopbackOption,QVariant(1));

    if(!bind(QHostAddress::AnyIPv4, m_port, QUdpSocket::ShareAddress)){
        qCWarning(dcHardware) << "UPnP discovery could not bind to port" << m_port;
        return;
    }

    if(!joinMulticastGroup(m_host)){
        qCWarning(dcHardware) << "UPnP discovery could not join multicast group" << m_host;
        return;
    }

    // network access manager for requesting device information
    m_networkAccessManager = new QNetworkAccessManager(this);
    connect(m_networkAccessManager, &QNetworkAccessManager::finished, this, &UpnpDiscovery::replyFinished);

    connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError)));
    connect(this, &UpnpDiscovery::readyRead, this, &UpnpDiscovery::readData);

    qCDebug(dcHardware) << "--> UPnP discovery created successfully.";
}
Exemplo n.º 2
0
bool UDPSocket::startIPMulticastListening(const QHostAddress &ipMulticastGroupAddress, quint16 ipMulticastGroupPort){

//    if(!ipMulticastSocket){
//        ipMulticastSocket = new IPMulticastSocket(this);

//    }else{
//        if((ipMulticastSocket->getMulticastGroupAddress() == ipMulticastGroupAddress) && (ipMulticastSocket->getPort() == ipMulticastGroupPort)){
//            qWarning("Server already started!");
//            return true;
//        }
//        ipMulticastSocket->leaveGroup();
//    }

//    close();
//    ipMulticastSocket->setUdpSocket(this);

//    bool bound = ipMulticastSocket->startIPMulticastListening(ipMulticastGroupAddress, ipMulticastGroupPort);

    close();
    listeningState = NotListening;

    //bool bound = bind(ipMulticastGroupPort, QUdpSocket::ShareAddress);
    bool bound = bind(QHostAddress::AnyIPv4, ipMulticastGroupPort, QUdpSocket::ShareAddress);
    if(!bound){
        return bound;
    }

    bound = joinMulticastGroup(ipMulticastGroupAddress);
    if(bound){
        listeningState = IPMulticastListening;
    }

    return bound;

}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
  int receivingSocket, sendingSocket;
  u_char ttl;
  struct sockaddr_in mcast_group;
  struct utsname name;

  if ((argc < 3) || (argc > 4)) {
    fprintf(stderr, "Usage: %s mcast_group port [ttl]\n", argv[0]);
    exit(1);
  }

  memset(&mcast_group, 0, sizeof(mcast_group));
  mcast_group.sin_family = AF_INET;
  mcast_group.sin_port = htons((unsigned short int)strtol(argv[2], NULL, 0));
  mcast_group.sin_addr.s_addr = inet_addr(argv[1]);

  allocateSocket(&sendingSocket);
  ttl = (argc == 4) ? strtol(argv[3], NULL, 0) : TTL;
  allocateSocket(&receivingSocket);
  enableSocketReuse(&receivingSocket);
  enableMulticastLoop(&receivingSocket);
  bindSocket(&receivingSocket, mcast_group);
  joinMulticastGroup(mcast_group.sin_addr, &receivingSocket);
  getSystemNodeName(&name);

  char buffer[MAXLEN + 1];
  FILE *fp;

  if (fork() == 0) {
    setsid();
    chdir("/");
    uname(0);
    openlog("mcshd", LOG_CONS, LOG_USER);

    for (;;) {
      memset(buffer, 0, sizeof(buffer));
      recvfrom(receivingSocket, buffer, MAXLEN, 0, NULL, NULL);
      
      if (strstr(buffer, "STARTOFCOMMAND: ") != NULL) {
        syslog(LOG_INFO, "Received command: %s", buffer);

        fp = popen(buffer+16, "r");
        
        if (fp != NULL) {
          while (fgets(buffer, MAXLEN, fp) != NULL) {
            sendto(sendingSocket, buffer, strlen(buffer)+1, 0, (const struct sockaddr *)&mcast_group, sizeof(mcast_group));
          }
          
          snprintf(buffer, MAXLEN, "ENDOFCOMMAND");
          sendto(sendingSocket, buffer, strlen(buffer)+1, 0, (const struct sockaddr *)&mcast_group, sizeof(mcast_group));
          pclose(fp);
        }
      }
    }
  }
  exit(0);
}
Exemplo n.º 4
0
void NetworkManager::init(const HWND hWnd)
{
    startWinsock();
    joinMulticastGroup(hWnd);
    setupPeerListener(hWnd);
}
Exemplo n.º 5
0
bool MulticastSocket::joinMulticastGroup(const QHostAddress &groupAddress)
{
    return joinMulticastGroup(groupAddress, QNetworkInterface());
}