← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3185: socket accept returns the remote port

 

------------------------------------------------------------
revno: 3185
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Thu 2013-01-24 21:18:18 +0100
message:
  socket accept returns the remote port
modified:
  dcpp/BufferedSocket.cpp
  dcpp/BufferedSocket.h
  dcpp/SSLSocket.cpp
  dcpp/SSLSocket.h
  dcpp/Socket.cpp
  dcpp/Socket.h
  dcpp/UserConnection.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/BufferedSocket.cpp'
--- dcpp/BufferedSocket.cpp	2013-01-18 21:28:38 +0000
+++ dcpp/BufferedSocket.cpp	2013-01-24 20:18:18 +0000
@@ -87,18 +87,20 @@
 		sock->setSocketOpt(SO_SNDBUF, SETTING(SOCKET_OUT_BUFFER));
 }
 
-void BufferedSocket::accept(const Socket& srv, bool secure, bool allowUntrusted) {
+uint16_t BufferedSocket::accept(const Socket& srv, bool secure, bool allowUntrusted) {
 	dcdebug("BufferedSocket::accept() %p\n", (void*)this);
 
 	unique_ptr<Socket> s(secure ? CryptoManager::getInstance()->getServerSocket(allowUntrusted) : new Socket(Socket::TYPE_TCP));
 
-	s->accept(srv);
+	auto ret = s->accept(srv);
 
 	setSocket(move(s));
 	setOptions();
 
 	Lock l(cs);
 	addTask(ACCEPTED, 0);
+
+	return ret;
 }
 
 void BufferedSocket::connect(const string& aAddress, const string& aPort, bool secure, bool allowUntrusted, bool proxy) {

=== modified file 'dcpp/BufferedSocket.h'
--- dcpp/BufferedSocket.h	2013-01-18 21:28:38 +0000
+++ dcpp/BufferedSocket.h	2013-01-24 20:18:18 +0000
@@ -73,7 +73,7 @@
 			Thread::sleep(100);
 	}
 
-	void accept(const Socket& srv, bool secure, bool allowUntrusted);
+	uint16_t accept(const Socket& srv, bool secure, bool allowUntrusted);
 	void connect(const string& aAddress, const string& aPort, bool secure, bool allowUntrusted, bool proxy);
 	void connect(const string& aAddress, const string& aPort, const string& localPort, NatRoles natRole, bool secure, bool allowUntrusted, bool proxy);
 

=== modified file 'dcpp/SSLSocket.cpp'
--- dcpp/SSLSocket.cpp	2013-01-23 23:33:50 +0000
+++ dcpp/SSLSocket.cpp	2013-01-24 20:18:18 +0000
@@ -65,10 +65,12 @@
 	}
 }
 
-void SSLSocket::accept(const Socket& listeningSocket) {
-	Socket::accept(listeningSocket);
+uint16_t SSLSocket::accept(const Socket& listeningSocket) {
+	auto ret = Socket::accept(listeningSocket);
 
 	waitAccepted(0);
+
+	return ret;
 }
 
 bool SSLSocket::waitAccepted(uint32_t millis) {

=== modified file 'dcpp/SSLSocket.h'
--- dcpp/SSLSocket.h	2013-01-21 16:09:46 +0000
+++ dcpp/SSLSocket.h	2013-01-24 20:18:18 +0000
@@ -44,7 +44,7 @@
 public:
 	virtual ~SSLSocket() { }
 
-	virtual void accept(const Socket& listeningSocket);
+	virtual uint16_t accept(const Socket& listeningSocket);
 	virtual void connect(const string& aIp, const string& aPort);
 	virtual int read(void* aBuffer, int aBufLen);
 	virtual int write(const void* aBuffer, int aLen);

=== modified file 'dcpp/Socket.cpp'
--- dcpp/Socket.cpp	2013-01-23 23:33:50 +0000
+++ dcpp/Socket.cpp	2013-01-24 20:18:18 +0000
@@ -254,7 +254,7 @@
 	return setSock(check([&] { return ::socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol); }), ai.ai_family);
 }
 
-void Socket::accept(const Socket& listeningSocket) {
+uint16_t Socket::accept(const Socket& listeningSocket) {
 	disconnect();
 
 	addr sock_addr = { { 0 } };
@@ -268,7 +268,17 @@
 	::WSAAsyncSelect(sock, NULL, 0, 0);
 #endif
 
+	// remote IP
 	setIp(resolveName(&sock_addr.sa, sz));
+
+	// return the remote port
+	if(sock_addr.sa.sa_family == AF_INET) {
+		return ntohs(sock_addr.sai.sin_port);
+	}
+	if(sock_addr.sa.sa_family == AF_INET6) {
+		return ntohs(sock_addr.sai6.sin6_port);
+	}
+	return 0;
 }
 
 string Socket::listen(const string& port) {

=== modified file 'dcpp/Socket.h'
--- dcpp/Socket.h	2013-01-23 23:33:50 +0000
+++ dcpp/Socket.h	2013-01-24 20:18:18 +0000
@@ -167,7 +167,9 @@
 
 	/** Binds a socket to a certain local port and possibly IP. */
 	virtual string listen(const string& port);
-	virtual void accept(const Socket& listeningSocket);
+	/** Accept a socket.
+	@return remote port */
+	virtual uint16_t accept(const Socket& listeningSocket);
 
 	int getSocketOptInt(int option);
 	void setSocketOpt(int option, int value);

=== modified file 'dcpp/UserConnection.cpp'
--- dcpp/UserConnection.cpp	2013-01-18 21:28:38 +0000
+++ dcpp/UserConnection.cpp	2013-01-24 20:18:18 +0000
@@ -152,7 +152,7 @@
 	dcassert(!socket);
 	socket = BufferedSocket::getSocket(0);
 	socket->addListener(this);
-	socket->accept(aServer, secure, SETTING(ALLOW_UNTRUSTED_CLIENTS));
+	setPort(Util::toString(socket->accept(aServer, secure, SETTING(ALLOW_UNTRUSTED_CLIENTS))));
 }
 
 void UserConnection::inf(bool withToken) {