void ListenSocketIO::OnSocketCanAcceptWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(mCOSocketIO); RemoveWatchers(READ_WATCHER|WRITE_WATCHER); struct sockaddr_storage storage; socklen_t addressLength = sizeof(storage); int fd; nsresult rv = mConnector->AcceptStreamSocket( GetFd(), reinterpret_cast<struct sockaddr*>(&storage), &addressLength, fd); if (NS_FAILED(rv)) { FireSocketError(); return; } mCOSocketIO->Accept(fd, reinterpret_cast<struct sockaddr*>(&storage), addressLength); }
void ListenSocketIO::OnAccepted(int aFd, const sockaddr_any* aAddr, socklen_t aAddrLen) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(mCOSocketIO); RemoveWatchers(READ_WATCHER|WRITE_WATCHER); nsRefPtr<nsRunnable> runnable; if (NS_SUCCEEDED(mCOSocketIO->Accept(aFd, aAddr, aAddrLen))) { runnable = new SocketIOEventRunnable<ListenSocketIO>( this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_SUCCESS); return; } else { runnable = new SocketIOEventRunnable<ListenSocketIO>( this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_ERROR); } NS_DispatchToMainThread(runnable); }
void UnixSocketConsumerIO::OnAccepted(int aFd, const sockaddr_any* aAddr, socklen_t aAddrLen) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(aAddr); MOZ_ASSERT(aAddrLen <= sizeof(mAddr)); memcpy (&mAddr, aAddr, aAddrLen); mAddrSize = aAddrLen; if (!mConnector->SetUp(aFd)) { NS_WARNING("Could not set up socket!"); return; } RemoveWatchers(READ_WATCHER|WRITE_WATCHER); Close(); if (!SetSocketFlags(aFd)) { return; } SetSocket(aFd, SOCKET_IS_CONNECTED); nsRefPtr<nsRunnable> r = new SocketIOEventRunnable<UnixSocketConsumerIO>( this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS); NS_DispatchToMainThread(r); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void UnixSocketConsumerIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); if (!SetSocketFlags(GetFd())) { NS_WARNING("Cannot set socket flags!"); FireSocketError(); return; } if (!mConnector->SetUp(GetFd())) { NS_WARNING("Could not set up socket!"); FireSocketError(); return; } nsRefPtr<nsRunnable> r = new SocketIOEventRunnable<UnixSocketConsumerIO>( this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS); NS_DispatchToMainThread(r); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void Proceed(BluetoothStatus aStatus) override { DispatchBluetoothSocketHALResult( GetResultHandler(), &BluetoothSocketResultHandler::Connect, GetFd(), GetBdAddress(), GetConnectionStatus(), aStatus); MessageLoopForIO::current()->PostTask( FROM_HERE, new DeleteTask<ConnectWatcher>(this)); }
void UnixSocketConsumer::GetSocketAddr(nsAString& aAddrStr) { aAddrStr.Truncate(); if (!mIO || GetConnectionStatus() != SOCKET_CONNECTED) { NS_WARNING("No socket currently open!"); return; } mIO->GetSocketAddr(aAddrStr); }
void BluetoothDaemonConnectionIO::OnSocketCanSendWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); MOZ_ASSERT(!IsShutdownOnIOThread()); if (NS_WARN_IF(NS_FAILED(SendPendingData(GetFd())))) { RemoveWatchers(WRITE_WATCHER); } }
void UnixSocketConsumerIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 nsresult rv = ReceiveData(GetFd(), this); if (NS_FAILED(rv)) { RemoveWatchers(READ_WATCHER|WRITE_WATCHER); return; } }
void ListenSocketIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); AddWatchers(READ_WATCHER, true); /* We signal a successful 'connection' to a local address for listening. */ NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); }
void ListenSocketIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); AddWatchers(READ_WATCHER, true); /* We signal a successful 'connection' to a local address for listening. */ GetConsumerThread()->PostTask( FROM_HERE, new SocketEventTask(this, SocketEventTask::CONNECT_SUCCESS)); }
void Proceed(BluetoothStatus aStatus) override { if ((aStatus != STATUS_SUCCESS) && (GetClientFd() != -1)) { mozilla::ScopedClose(GetClientFd()); // Close received socket fd on error } DispatchBluetoothSocketHALResult( GetResultHandler(), &BluetoothSocketResultHandler::Accept, GetClientFd(), GetBdAddress(), GetConnectionStatus(), aStatus); MessageLoopForIO::current()->PostTask( FROM_HERE, new DeleteTask<AcceptWatcher>(this)); }
void UnixSocketConsumerIO::OnListening() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); if (!mConnector->SetUpListenSocket(GetFd())) { NS_WARNING("Could not set up listen socket!"); FireSocketError(); return; } AddWatchers(READ_WATCHER, true); }
void BluetoothDaemonConnectionIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); NS_DispatchToMainThread( new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ConnectionOrientedSocketIO::OnConnected() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); GetConsumerThread()->PostTask( MakeAndAddRef<SocketEventTask>(this, SocketEventTask::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void UnixSocketConsumerIO::OnSocketCanSendWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 nsresult rv = SendPendingData(GetFd(), this); if (NS_FAILED(rv)) { return; } if (HasPendingData()) { AddWatchers(WRITE_WATCHER, false); } }
void ConnectionOrientedSocketIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984 ssize_t res = ReceiveData(GetFd()); if (res < 0) { /* I/O error */ RemoveWatchers(READ_WATCHER|WRITE_WATCHER); } else if (!res) { /* EOF or peer shutdown */ RemoveWatchers(READ_WATCHER); } }
CConnection::~CConnection() { if (m_pSendBuffer) { delete [] m_pSendBuffer; m_pSendBuffer = NULL; } if (m_pRecvBuffer) { delete [] m_pRecvBuffer; m_pRecvBuffer = NULL; } if (GetConnectionStatus() != CONNECTION_STATUS_CLOSED_FROM_LOCAL) closesocket(m_socket); }
void BluetoothDaemonConnectionIO::OnSocketCanReceiveWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); MOZ_ASSERT(!IsShutdownOnIOThread()); ssize_t res = ReceiveData(GetFd()); if (res < 0) { /* I/O error */ RemoveWatchers(READ_WATCHER|WRITE_WATCHER); } else if (!res) { /* EOF or peer shutdown */ RemoveWatchers(READ_WATCHER); } }
nsresult ConnectionOrientedSocketIO::Accept(int aFd, const struct sockaddr* aPeerAddress, socklen_t aPeerAddressLength) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTING); SetSocket(aFd, SOCKET_IS_CONNECTED); // Address setup mPeerAddressLength = aPeerAddressLength; memcpy(&mPeerAddress, aPeerAddress, mPeerAddressLength); // Signal success and start data transfer OnConnected(); return NS_OK; }
void ListenSocketIO::OnSocketCanAcceptWithoutBlocking() { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING); MOZ_ASSERT(mCOSocketIO); struct sockaddr_storage addr; socklen_t addrLen = sizeof(addr); int fd = TEMP_FAILURE_RETRY(accept(GetFd(), reinterpret_cast<struct sockaddr*>(&addr), &addrLen)); if (fd < 0) { OnError("accept", errno); return; } RemoveWatchers(READ_WATCHER|WRITE_WATCHER); mCOSocketIO->Accept(fd, reinterpret_cast<union sockaddr_any*>(&addr), addrLen); }
nsresult BluetoothDaemonConnectionIO::Accept(int aFd, const union sockaddr_any* aAddr, socklen_t aAddrLen) { MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTING); // File-descriptor setup if (TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFL, O_NONBLOCK)) < 0) { OnError("fcntl", errno); ScopedClose cleanupFd(aFd); return NS_ERROR_FAILURE; } SetSocket(aFd, SOCKET_IS_CONNECTED); // Signal success OnConnected(); return NS_OK; }
/* sample upnp client program */ int main(int argc, char ** argv) { char command = 0; char ** commandargv = 0; int commandargc = 0; struct UPNPDev * devlist = 0; char lanaddr[64]; /* my ip address on the LAN */ int i; const char * rootdescurl = 0; const char * multicastif = 0; const char * minissdpdpath = 0; int retcode = 0; #ifdef WIN32 WSADATA wsaData; int nResult = WSAStartup(MAKEWORD(2,2), &wsaData); if(nResult != NO_ERROR) { fprintf(stderr, "WSAStartup() failed.\n"); return -1; } #endif printf("upnpc : miniupnpc library test client. (c) 2006-2010 Thomas Bernard\n"); printf("Go to http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/\n" "for more information.\n"); /* command line processing */ for(i=1; i<argc; i++) { if(argv[i][0] == '-') { if(argv[i][1] == 'u') rootdescurl = argv[++i]; else if(argv[i][1] == 'm') multicastif = argv[++i]; else if(argv[i][1] == 'p') minissdpdpath = argv[++i]; else { command = argv[i][1]; i++; commandargv = argv + i; commandargc = argc - i; break; } } else { fprintf(stderr, "option '%s' invalid\n", argv[i]); } } if(!command || (command == 'a' && commandargc<4) || (command == 'd' && argc<2) || (command == 'r' && argc<2)) { fprintf(stderr, "Usage :\t%s [options] -a ip port external_port protocol\n\t\tAdd port redirection\n", argv[0]); fprintf(stderr, " \t%s [options] -d external_port protocol [port2 protocol2] [...]\n\t\tDelete port redirection\n", argv[0]); fprintf(stderr, " \t%s [options] -s\n\t\tGet Connection status\n", argv[0]); fprintf(stderr, " \t%s [options] -l\n\t\tList redirections\n", argv[0]); fprintf(stderr, " \t%s [options] -r port1 protocol1 [port2 protocol2] [...]\n\t\tAdd all redirections to the current host\n", argv[0]); fprintf(stderr, "\nprotocol is UDP or TCP\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -u url : bypass discovery process by providing the XML root description url.\n"); fprintf(stderr, " -m address : provide ip address of the interface to use for sending SSDP multicast packets.\n"); fprintf(stderr, " -p path : use this path for MiniSSDPd socket.\n"); return 1; } if( rootdescurl || (devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0))) { struct UPNPDev * device; struct UPNPUrls urls; struct IGDdatas data; if(devlist) { printf("List of UPNP devices found on the network :\n"); for(device = devlist; device; device = device->pNext) { printf(" desc: %s\n st: %s\n\n", device->descURL, device->st); } } i = 1; if( (rootdescurl && UPNP_GetIGDFromUrl(rootdescurl, &urls, &data, lanaddr, sizeof(lanaddr))) || (i = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)))) { switch(i) { case 1: printf("Found valid IGD : %s\n", urls.controlURL); break; case 2: printf("Found a (not connected?) IGD : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); break; case 3: printf("UPnP device found. Is it an IGD ? : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); break; default: printf("Found device (igd ?) : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); } printf("Local LAN ip address : %s\n", lanaddr); #if 0 printf("getting \"%s\"\n", urls.ipcondescURL); descXML = miniwget(urls.ipcondescURL, &descXMLsize); if(descXML) { /*fwrite(descXML, 1, descXMLsize, stdout);*/ free(descXML); descXML = NULL; } #endif switch(command) { case 'l': DisplayInfos(&urls, &data); ListRedirections(&urls, &data); break; case 'a': SetRedirectAndTest(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargv[3]); break; case 'd': for(i=0; i<commandargc; i+=2) { RemoveRedirect(&urls, &data, commandargv[i], commandargv[i+1]); } break; case 's': GetConnectionStatus(&urls, &data); break; case 'r': for(i=0; i<commandargc; i+=2) { /*printf("port %s protocol %s\n", argv[i], argv[i+1]);*/ SetRedirectAndTest(&urls, &data, lanaddr, commandargv[i], commandargv[i], commandargv[i+1]); } break; default: fprintf(stderr, "Unknown switch -%c\n", command); retcode = 1; } FreeUPNPUrls(&urls); } else { fprintf(stderr, "No valid UPNP Internet Gateway Device found.\n"); retcode = 1; } freeUPNPDevlist(devlist); devlist = 0; } else { fprintf(stderr, "No IGD UPnP Device found on the network !\n"); retcode = 1; } return retcode; }
void CActivityView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { bool bInserting = false; POSITION pos; int nDocCount; CListCtrl & pList = GetListCtrl (); // if we don't want list updated right now, then exit if (m_bUpdateLockout) return; // TRACE ("Activity window being updated\n"); m_bUpdateLockout = TRUE; App.m_bUpdateActivity = FALSE; App.m_timeLastActivityUpdate = CTime::GetCurrentTime(); // if the list has the same number of worlds (and they are the same // worlds), then we can not bother deleting the list and re-adding it // first, count worlds pos = App.m_pWorldDocTemplate->GetFirstDocPosition(); for (nDocCount = 0; pos; nDocCount++) App.m_pWorldDocTemplate->GetNextDoc(pos); // if count is the same, check world is the same as in the list if (nDocCount == pList.GetItemCount ()) { pos = App.m_pWorldDocTemplate->GetFirstDocPosition(); while (pos) { CMUSHclientDoc* pDoc = (CMUSHclientDoc*) App.m_pWorldDocTemplate->GetNextDoc(pos); int nItem = pDoc->m_view_number - 1; if (nItem < 0) { bInserting = true; break; } if ((DWORD) pDoc != pList.GetItemData (nItem)) { bInserting = true; break; } } // end of looping through each world } // end of world count being same as list count else bInserting = true; // different counts, must re-do list if (bInserting) pList.DeleteAllItems (); // add all documents to the list pos = App.m_pWorldDocTemplate->GetFirstDocPosition(); for (int nItem = 0; pos != NULL; nItem++) { CMUSHclientDoc* pDoc = (CMUSHclientDoc*) App.m_pWorldDocTemplate->GetNextDoc(pos); if (bInserting) pDoc->m_view_number = nItem + 1; // so we can use Ctrl+1 etc. else nItem = pDoc->m_view_number - 1; // use existing item number CString strSeq; CString strLines; CString strNewLines; CString strStatus; CString strSince; CString strDuration; strSeq.Format ("%ld", pDoc->m_view_number); strNewLines.Format ("%ld", pDoc->m_new_lines); strLines.Format ("%ld", pDoc->m_total_lines); // work out world status strStatus = GetConnectionStatus (pDoc->m_iConnectPhase); // when they connected if (pDoc->m_iConnectPhase == eConnectConnectedToMud) strSince = pDoc->FormatTime (pDoc->m_tConnectTime, "%#I:%M %p, %d %b"); else strSince.Empty (); // work out world connection duration // first time spent in previous connections CTimeSpan ts = pDoc->m_tsConnectDuration; // now time spent connected in this session, if we are connected if (pDoc->m_iConnectPhase == eConnectConnectedToMud) ts += CTime::GetCurrentTime() - pDoc->m_tConnectTime; if (ts.GetDays () > 0) strDuration = ts.Format ("%Dd %Hh %Mm %Ss"); else if (ts.GetHours () > 0) strDuration = ts.Format ("%Hh %Mm %Ss"); else if (ts.GetMinutes () > 0) strDuration = ts.Format ("%Mm %Ss"); else strDuration = ts.Format ("%Ss"); // sequence if (bInserting) pList.InsertItem (nItem, strSeq); // eColumnSeq pList.SetItemText(nItem, eColumnMush, pDoc->m_mush_name); pList.SetItemText(nItem, eColumnNew, strNewLines); pList.SetItemText(nItem, eColumnLines, strLines); pList.SetItemText(nItem, eColumnStatus, strStatus); pList.SetItemText(nItem, eColumnSince, strSince); pList.SetItemText(nItem, eColumnDuration, strDuration); if (bInserting) pList.SetItemData(nItem, (DWORD) pDoc); LVITEM lvitem; memset (&lvitem, 0, sizeof lvitem); // update where tick goes lvitem.iImage = 0; if (pDoc->m_pActiveCommandView || pDoc->m_pActiveOutputView) lvitem.iImage = 1; // show the tick lvitem.mask = LVIF_IMAGE; lvitem.iItem = nItem; pList.SetItem (&lvitem); } // end of searching for all documents // make sure in same order that we left them pList.SortItems (CompareFunc, m_reverse << 8 | m_last_col); m_bUpdateLockout = FALSE; } // end of CActivityView::OnUpdate
int CALLBACK CActivityView::CompareFunc ( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { CMUSHclientDoc * item1 = (CMUSHclientDoc *) lParam1; CMUSHclientDoc * item2 = (CMUSHclientDoc *) lParam2; int iResult = 0; switch (lParamSort & 0xFF) // which sort key { case eColumnSeq: if (item1->m_view_number < item2->m_view_number) iResult = -1; else if (item1->m_view_number > item2->m_view_number) iResult = 1; break; case eColumnMush: iResult = item1->m_mush_name.CompareNoCase (item2->m_mush_name); break; case eColumnNew: if (item1->m_new_lines < item2->m_new_lines) iResult = -1; else if (item1->m_new_lines > item2->m_new_lines) iResult = 1; break; case eColumnLines: if (item1->m_total_lines < item2->m_total_lines) iResult = -1; else if (item1->m_total_lines > item2->m_total_lines) iResult = 1; break; case eColumnStatus: { CString strStatus1 = GetConnectionStatus (item1->m_iConnectPhase); CString strStatus2 = GetConnectionStatus (item2->m_iConnectPhase); iResult = strStatus1.CompareNoCase (strStatus2); break; } case eColumnSince: { if (item1->m_tConnectTime < item2->m_tConnectTime) iResult = -1; else if (item1->m_tConnectTime > item2->m_tConnectTime) iResult = 1; } break; case eColumnDuration: { CTimeSpan ts1 = CTime::GetCurrentTime() - item1->m_tConnectTime; CTimeSpan ts2 = CTime::GetCurrentTime() - item2->m_tConnectTime; if (ts1 < ts2) iResult = -1; else if (ts1 > ts2) iResult = 1; } break; default: return 0; } // end of switch // if reverse sort wanted, reverse sense of result if (lParamSort & 0xFF00) iResult *= -1; return iResult; } // end of CompareFunc
/* sample upnp client program */ int main(int argc, char ** argv) { char command = 0; char ** commandargv = 0; int commandargc = 0; struct UPNPDev * devlist = 0; char lanaddr[64]; /* my ip address on the LAN */ int i; const char * rootdescurl = 0; const char * multicastif = 0; const char * minissdpdpath = 0; int retcode = 0; int error = 0; int ipv6 = 0; const char * description = 0; #ifdef _WIN32 WSADATA wsaData; int nResult = WSAStartup(MAKEWORD(2,2), &wsaData); if(nResult != NO_ERROR) { fprintf(stderr, "WSAStartup() failed.\n"); return -1; } #endif printf("upnpc : miniupnpc library test client. (c) 2005-2014 Thomas Bernard\n"); printf("Go to http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/\n" "for more information.\n"); /* command line processing */ for(i=1; i<argc; i++) { if(0 == strcmp(argv[i], "--help") || 0 == strcmp(argv[i], "-h")) { command = 0; break; } if(argv[i][0] == '-') { if(argv[i][1] == 'u') rootdescurl = argv[++i]; else if(argv[i][1] == 'm') multicastif = argv[++i]; else if(argv[i][1] == 'p') minissdpdpath = argv[++i]; else if(argv[i][1] == '6') ipv6 = 1; else if(argv[i][1] == 'e') description = argv[++i]; else { command = argv[i][1]; i++; commandargv = argv + i; commandargc = argc - i; break; } } else { fprintf(stderr, "option '%s' invalid\n", argv[i]); } } if(!command || (command == 'a' && commandargc<4) || (command == 'd' && argc<2) || (command == 'r' && argc<2) || (command == 'A' && commandargc<6) || (command == 'U' && commandargc<2) || (command == 'D' && commandargc<1)) { fprintf(stderr, "Usage :\t%s [options] -a ip port external_port protocol [duration]\n\t\tAdd port redirection\n", argv[0]); fprintf(stderr, " \t%s [options] -d external_port protocol <remote host>\n\t\tDelete port redirection\n", argv[0]); fprintf(stderr, " \t%s [options] -s\n\t\tGet Connection status\n", argv[0]); fprintf(stderr, " \t%s [options] -l\n\t\tList redirections\n", argv[0]); fprintf(stderr, " \t%s [options] -L\n\t\tList redirections (using GetListOfPortMappings (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -n ip port external_port protocol [duration]\n\t\tAdd (any) port redirection allowing IGD to use alternative external_port (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -N external_port_start external_port_end protocol [manage]\n\t\tDelete range of port redirections (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -r port1 [external_port1] protocol1 [port2 [external_port2] protocol2] [...]\n\t\tAdd all redirections to the current host\n", argv[0]); fprintf(stderr, " \t%s [options] -A remote_ip remote_port internal_ip internal_port protocol lease_time\n\t\tAdd Pinhole (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -U uniqueID new_lease_time\n\t\tUpdate Pinhole (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -C uniqueID\n\t\tCheck if Pinhole is Working (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -K uniqueID\n\t\tGet Number of packets going through the rule (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -D uniqueID\n\t\tDelete Pinhole (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -S\n\t\tGet Firewall status (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -G remote_ip remote_port internal_ip internal_port protocol\n\t\tGet Outbound Pinhole Timeout (for IGD:2 only)\n", argv[0]); fprintf(stderr, " \t%s [options] -P\n\t\tGet Presentation url\n", argv[0]); fprintf(stderr, "\nprotocol is UDP or TCP\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -e description : set description for port mapping.\n"); fprintf(stderr, " -6 : use ip v6 instead of ip v4.\n"); fprintf(stderr, " -u url : bypass discovery process by providing the XML root description url.\n"); fprintf(stderr, " -m address/interface : provide ip address (ip v4) or interface name (ip v4 or v6) to use for sending SSDP multicast packets.\n"); fprintf(stderr, " -p path : use this path for MiniSSDPd socket.\n"); return 1; } if( rootdescurl || (devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0/*sameport*/, ipv6, &error))) { struct UPNPDev * device; struct UPNPUrls urls; struct IGDdatas data; if(devlist) { printf("List of UPNP devices found on the network :\n"); for(device = devlist; device; device = device->pNext) { printf(" desc: %s\n st: %s\n\n", device->descURL, device->st); } } else if(!rootdescurl) { printf("upnpDiscover() error code=%d\n", error); } i = 1; if( (rootdescurl && UPNP_GetIGDFromUrl(rootdescurl, &urls, &data, lanaddr, sizeof(lanaddr))) || (i = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)))) { switch(i) { case 1: printf("Found valid IGD : %s\n", urls.controlURL); break; case 2: printf("Found a (not connected?) IGD : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); break; case 3: printf("UPnP device found. Is it an IGD ? : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); break; default: printf("Found device (igd ?) : %s\n", urls.controlURL); printf("Trying to continue anyway\n"); } printf("Local LAN ip address : %s\n", lanaddr); #if 0 printf("getting \"%s\"\n", urls.ipcondescURL); descXML = miniwget(urls.ipcondescURL, &descXMLsize); if(descXML) { /*fwrite(descXML, 1, descXMLsize, stdout);*/ free(descXML); descXML = NULL; } #endif switch(command) { case 'l': DisplayInfos(&urls, &data); ListRedirections(&urls, &data); break; case 'L': NewListRedirections(&urls, &data); break; case 'a': SetRedirectAndTest(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargv[3], (commandargc > 4)?commandargv[4]:"0", description, 0); break; case 'd': RemoveRedirect(&urls, &data, commandargv[0], commandargv[1], commandargc > 2 ? commandargv[2] : NULL); break; case 'n': /* aNy */ SetRedirectAndTest(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargv[3], (commandargc > 4)?commandargv[4]:"0", description, 1); break; case 'N': if (commandargc < 3) fprintf(stderr, "too few arguments\n"); RemoveRedirectRange(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargc > 3 ? commandargv[3] : NULL); break; case 's': GetConnectionStatus(&urls, &data); break; case 'r': i=0; while(i<commandargc){ if(!is_int(commandargv[i+1])){ /* 2nd parameter not an integer, so format is '<port> <protocol>' */ /* Note: no 2nd parameter is also not-an-integer, and will lead to a "Wrong arguments" */ SetRedirectAndTest(&urls, &data, lanaddr, commandargv[i], commandargv[i], commandargv[i+1], "0", description, 0); i+=2; /* 2 parameters parsed */ } else { /* 2nd parameter is an integer, so format is '<port> <external_port> <protocol>' */ SetRedirectAndTest(&urls, &data, lanaddr, commandargv[i], commandargv[i+1], commandargv[i+2], "0", description, 0); i+=3; /* 3 parameters parsed */ } } break; case 'A': SetPinholeAndTest(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargv[3], commandargv[4], commandargv[5]); break; case 'U': GetPinholeAndUpdate(&urls, &data, commandargv[0], commandargv[1]); break; case 'C': for(i=0; i<commandargc; i++) { CheckPinhole(&urls, &data, commandargv[i]); } break; case 'K': for(i=0; i<commandargc; i++) { GetPinholePackets(&urls, &data, commandargv[i]); } break; case 'D': for(i=0; i<commandargc; i++) { RemovePinhole(&urls, &data, commandargv[i]); } break; case 'S': GetFirewallStatus(&urls, &data); break; case 'G': GetPinholeOutboundTimeout(&urls, &data, commandargv[0], commandargv[1], commandargv[2], commandargv[3], commandargv[4]); break; case 'P': printf("Presentation URL found:\n"); printf(" %s\n", data.presentationurl); break; default: fprintf(stderr, "Unknown switch -%c\n", command); retcode = 1; } FreeUPNPUrls(&urls); } else { fprintf(stderr, "No valid UPNP Internet Gateway Device found.\n"); retcode = 1; } freeUPNPDevlist(devlist); devlist = 0; } else { fprintf(stderr, "No IGD UPnP Device found on the network !\n"); retcode = 1; } #ifdef _WIN32 nResult = WSACleanup(); if(nResult != NO_ERROR) { fprintf(stderr, "WSACleanup() failed.\n"); } #endif /* _WIN32 */ return retcode; }