From 9daac507d45ccf97752d9c6fa304a770ece74585 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 6 Jan 2013 14:09:15 +0000 Subject: [PATCH] Rewritten vomp discovery protocol --- vconnect.cc | 6 ++++-- vdr.cc | 60 +++++++++++++++++++++++++++++++++-------------------- vdr.h | 4 ++++ 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/vconnect.cc b/vconnect.cc index 9666156..c771aa5 100644 --- a/vconnect.cc +++ b/vconnect.cc @@ -90,6 +90,7 @@ void VConnect::threadMethod() strcpy(vdrserver.ip, server); vdrserver.name = new char[1]; vdrserver.name[0] = '\0'; + vdrserver.port = 3024; // FIXME servers.push_back(vdrserver); } else @@ -139,10 +140,11 @@ void VConnect::threadMethod() return; } - logger->log("VConnect", Log::NOTICE, "Connecting to server at %s", servers[selectedServer].ip); + logger->log("VConnect", Log::NOTICE, "Connecting to server at %s %u", servers[selectedServer].ip, servers[selectedServer].port); Wol::getInstance()->setWakeUpIP(servers[selectedServer].ip); vdr->setServerIP(servers[selectedServer].ip); - + vdr->setServerPort(servers[selectedServer].port); + // Clear the serverIPs vector for(UINT k = 0; k < servers.size(); k++) { diff --git a/vdr.cc b/vdr.cc index 3073697..724b558 100644 --- a/vdr.cc +++ b/vdr.cc @@ -152,9 +152,12 @@ void VDR::findServers(vector& servers) { Wol* wol = Wol::getInstance(); findingServer = 1; - char* message = "VOMP"; + char message[15]; + memset(message, 0, 15); + strcpy(message, "VDP-0001"); + /*tcp->getMAC(&message[9]); put mac here when TCP modified to do this*/ - DatagramSocket ds(port); + DatagramSocket ds(0); int haveAtLeastOne = 0; int retval; int waitType = 1; @@ -166,36 +169,40 @@ void VDR::findServers(vector& servers) ds.shutdown(); ds.init(); logger->log("VDR", Log::NOTICE, "Broadcasting for server"); - ds.send("255.255.255.255", 3024, message, strlen(message)); + ds.send("255.255.255.255", 51051, message, 15); + ds.send("255.255.255.255", 51052, message, 15); + ds.send("255.255.255.255", 51053, message, 15); + ds.send("255.255.255.255", 51054, message, 15); + ds.send("255.255.255.255", 51055, message, 15); if(!firstloop) wol->doWakeUp(); } retval = ds.waitforMessage(waitType); if (retval == 2) // we got a reply { - if (!strcmp(ds.getData(), "VOMP")) // echo..... - { - waitType = 2; - } - else + waitType = 2; + + char* vdpreply = ds.getData(); + if ((ds.getDataLength() >= 24) && !strncmp(vdpreply, "VDP-0002", 8)) { VDRServer newServer; + // FIXME upgrade this to look for a return IP in the reply packet newServer.ip = new char[16]; strcpy(newServer.ip, ds.getFromIPA()); - if (ds.getDataLength() == 0) - { - newServer.name = new char[1]; - newServer.name[0] = '\0'; - } - else - { - newServer.name = new char[strlen(ds.getData())+1]; - strcpy(newServer.name, ds.getData()); - } + USHORT newServerPort; + memcpy(&newServerPort, &vdpreply[26], 2); + newServer.port = ntohs(newServerPort); + + ULONG newServerVersion; + memcpy(&newServerVersion, &vdpreply[28], 4); + newServer.version = ntohl(newServerVersion); + + int newServerNameLength = ds.getDataLength() - 32; + newServer.name = new char[newServerNameLength]; + strcpy(newServer.name, &vdpreply[32]); servers.push_back(newServer); - waitType = 2; haveAtLeastOne = 1; } } @@ -219,6 +226,11 @@ void VDR::setServerIP(char* newIP) strcpy(serverIP, newIP); } +void VDR::setServerPort(USHORT newPort) +{ + serverPort = newPort; +} + int VDR::connect() { maxChannelNumber = 0; @@ -226,7 +238,7 @@ int VDR::connect() if (tcp) delete tcp; tcp = new TCP(); - if (tcp->connectTo(serverIP, 3024)) + if (tcp->connectTo(serverIP, serverPort)) { connected = true; threadStart(); @@ -524,6 +536,7 @@ VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp) logger->log("VDR", Log::DEBUG, "RR unsleep"); // Woken because a response packet has arrived, mutex will be locked + logger->log("VDR", Log::DEBUG, "Packet delivered to me, requestID: %lu", vdrpr.save_vresp->getRequestID()); edUnlock(); return vdrpr.save_vresp; @@ -634,9 +647,10 @@ bool VDR::networkLog(const char* logString) *(ULONG*)&buffer[4] = htonl(stringLength); strcpy(&buffer[8], logString); - if ((ULONG)tcp->sendData(buffer, packetLength) != packetLength) { - delete [] buffer; - return false; + if (tcp->sendData(buffer, packetLength) != packetLength) + { + delete [] buffer; + return false; } delete [] buffer; return true; diff --git a/vdr.h b/vdr.h index c3ff0f1..ab8541a 100644 --- a/vdr.h +++ b/vdr.h @@ -62,6 +62,8 @@ struct VDRServer { char* ip; char* name; + USHORT port; + ULONG version; }; struct RecTimerSorter // : public binary_function @@ -130,6 +132,7 @@ class VDR : public Thread_TYPE, public EventDispatcher, public MediaProvider void findServers(vector& servers); void cancelFindingServer(); void setServerIP(char*); + void setServerPort(USHORT); void setReceiveWindow(size_t size); int connect(); void disconnect(); @@ -220,6 +223,7 @@ class VDR : public Thread_TYPE, public EventDispatcher, public MediaProvider TCP* tcp; int port; char serverIP[16]; + USHORT serverPort; bool connected; ULONG maxChannelNumber; ULONG channelNumberWidth; -- 2.39.5