BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

IdFTP (10.1.5) Where is my local port number?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Marc Wetzel
Guest





PostPosted: Tue May 15, 2007 10:09 pm    Post subject: IdFTP (10.1.5) Where is my local port number? Reply with quote



Hi,

In my software I need to know the local port number, after I connected to a
ftp-server (for the control and for the data connection), to configure a
tcp-sniffer, which is running in another thread.

Where can I find the numbers?

After several hours of debugging, I found a number, which is relatively near to
the number I'm looking for, but its always off by 2.


Currently I'm trying with the following code:

FTP := TIdFTP.Create(Self);
try
// [...] some more settings, e.g. username/pwd

FTP.IOHandler := IdIOHandler; // <- this is a IdIOHandlerStack

FTP.Connect;
Log(Format('Local port opened: Peer: %d, Port: %d',[
IdIOHandler.Binding.PeerPort, IdIOHandler.Binding.Port]));

finally
FTP.Disconnect;
FTP.Free;
end;


This is the Log of the above code:

15.05.2007 18:53:54.343 IOHandler Status Connecting to [IP-Adress ommitted].
15.05.2007 18:53:54.343 Connected.
15.05.2007 18:53:54.375 FTPBannerBeforeLogin 220 ProFTPD 1.2.10 Server
15.05.2007 18:53:54.437 FTPBannerAfterLogin 230 User test logged in.
15.05.2007 18:53:54.468 FTPAfterClientLogin
15.05.2007 18:53:54.484 Connection established
** 15.05.2007 18:53:54.484 Local port opened: Peer: 21, Port: 2220
15.05.2007 18:53:54.500 Disconnecting.
15.05.2007 18:53:54.500 FTPDisconnected
15.05.2007 18:53:54.500 Disconnected.

Wireshark shows me that in reality we were connected with
port-pair: 2222 -> 21

Transmission Control Protocol
Source port: 2222 (2222)
Destination port: ftp (21)
Sequence number: 4015518599



What is going wrong here?

By the way, I'm using the stock Indy V10 that comes with Delphi 2007.
I also tried with Indy9 (in Delphi 2006)

TIA
Marc
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed May 16, 2007 12:32 am    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote



"Marc Wetzel" <mwNOSPAMse (AT) gmx (DOT) de> wrote in message
news:4649e9d1$1 (AT) newsgroups (DOT) borland.com...

Quote:
In my software I need to know the local port number, after I
connected
to a ftp-server (for the control and for the data connection), to
configure
a tcp-sniffer, which is running in another thread.

Where can I find the numbers?

You already know the answer to that, because you are already using
them.

Quote:
After several hours of debugging, I found a number, which is
relatively near to the number I'm looking for, but its always off
by 2.

If you do not specify your own local port to bind to when connecting,
the OS picks one at random for you, so it could be anything. That is
exactly what your log is showing you.

Quote:
Wireshark shows me that in reality we were connected with
port-pair: 2222 -> 21

The Binding.Port property contains the actual port number as provided
by the socket itself. Indy does not calculate that value at all. If
the socket says you are connected using port 2220, then you really are
connected using port 2220.

Now, do keep in mind that your log shows you disconnecting. Because
you are binding to a random port each time, it very well could be that
you were connected using 2220 when you generated the log, and then
connected using 2222 later on when running Wireshark for a new
connection.


Gambit
Back to top
Marc Wetzel
Guest





PostPosted: Wed May 16, 2007 8:12 am    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote



Remy Lebeau (TeamB) schrieb:
Quote:
"Marc Wetzel" <mwNOSPAMse (AT) gmx (DOT) de> wrote in message
news:4649e9d1$1 (AT) newsgroups (DOT) borland.com...

In my software I need to know the local port number, after I
connected
to a ftp-server (for the control and for the data connection), to
configure
a tcp-sniffer, which is running in another thread.

Where can I find the numbers?

You already know the answer to that, because you are already using
them.

After several hours of debugging, I found a number, which is
relatively near to the number I'm looking for, but its always off
by 2.

If you do not specify your own local port to bind to when connecting,
the OS picks one at random for you, so it could be anything. That is
exactly what your log is showing you.

Wireshark shows me that in reality we were connected with
port-pair: 2222 -> 21

The Binding.Port property contains the actual port number as provided
by the socket itself. Indy does not calculate that value at all. If
the socket says you are connected using port 2220, then you really are
connected using port 2220.


I rechecked this morning, and I am sorry to find out that you are wrong.
(By the way, I know what I am doing Smile I use wireshark and Indy for years)
The port is off by two. See the following log (I included timestamps this time)

Can you please recheck on your system using the code I provided in my first mail?

Quote:
Now, do keep in mind that your log shows you disconnecting. Because
you are binding to a random port each time, it very well could be that
you were connected using 2220 when you generated the log, and then
connected using 2222 later on when running Wireshark for a new
connection.


Any clues? I debugged through the Indy code and I could not find any issues...
But the port is off by two.

App.-Log:

16.05.2007 07:36:51.187 IOHandler Status Connecting to [IP Adress]
16.05.2007 07:36:51.187 Connected.
16.05.2007 07:36:51.218 FTPBannerBeforeLogin 220 ProFTPD 1.2.10 Server
16.05.2007 07:36:51.250 FTPBannerAfterLogin 230 User vodafone logged in.
16.05.2007 07:36:51.296 FTPAfterClientLogin
16.05.2007 07:36:51.312 Connection established
16.05.2007 07:36:51.312 Local port opened: Peer: 21, Port: 1181

TCPLOG:

No. Time Time2 Source Destination
Protocol Info
11 14.646819 2007-05-16 07:36:51.190271 192.168.178.23 [IP
Adress] TCP 1183 > ftp [SYN] Seq=59795085 Len=0 MSS=1360 WS=0 TSV=0
TSER=0

Frame 11 (78 bytes on wire, 78 bytes captured)
Ethernet II, Src: Msi_b3:5f:3b (00:16:17:b3:5f:3b), Dst: Avm_88:94:42
(00:15:0c:88:94:42)
Internet Protocol, Src: 192.168.178.23 (192.168.178.23), Dst: [IP Adress]
Transmission Control Protocol
Source port: 1183 (1183)
Destination port: ftp (21)
Sequence number: 59795085
Header length: 44 bytes
Flags: 0x02 (SYN)
Window size: 65535
Checksum: 0x9cf9 [validation disabled]
Options: (24 bytes)

No. Time Time2 Source Destination
Protocol Info
12 14.661633 2007-05-16 07:36:51.205085 [IP Adress]
192.168.178.23 TCP ftp > 1183 [SYN, ACK] Seq=683778349 Ack=59795086
Win=5840 Len=0 MSS=1452

Frame 12 (62 bytes on wire, 62 bytes captured)
Ethernet II, Src: Avm_88:94:42 (00:15:0c:88:94:42), Dst: Msi_b3:5f:3b
(00:16:17:b3:5f:3b)
Internet Protocol, Src: [IP Adress], Dst: 192.168.178.23 (192.168.178.23)
Transmission Control Protocol
Source port: ftp (21)
Destination port: 1183 (1183)
Sequence number: 683778349
Acknowledgement number: 59795086
Header length: 28 bytes
Flags: 0x12 (SYN, ACK)
Window size: 5840
Checksum: 0x08ec [validation disabled]
Options: (8 bytes)

No. Time Time2 Source Destination
Protocol Info
13 14.661654 2007-05-16 07:36:51.205106 192.168.178.23 [IP
Adress] TCP 1183 > ftp [ACK] Seq=59795086 Ack=683778350 Win=65535 Len=0

Frame 13 (54 bytes on wire, 54 bytes captured)
Ethernet II, Src: Msi_b3:5f:3b (00:16:17:b3:5f:3b), Dst: Avm_88:94:42
(00:15:0c:88:94:42)
Internet Protocol, Src: 192.168.178.23 (192.168.178.23), Dst: [IP Adress]
Transmission Control Protocol
Source port: 1183 (1183)
Destination port: ftp (21)
Sequence number: 59795086
Acknowledgement number: 683778350
Header length: 20 bytes
Flags: 0x10 (ACK)
Window size: 65535
Checksum: 0x4c78 [validation disabled]

No. Time Time2 Source Destination
Protocol Info
14 14.679341 2007-05-16 07:36:51.222793 [IP Adress] 192.168.178.23
FTP Response: 220 ProFTPD 1.2.10 Server

Frame 14 (112 bytes on wire, 112 bytes captured)
Ethernet II, Src: Avm_88:94:42 (00:15:0c:88:94:42), Dst: Msi_b3:5f:3b
(00:16:17:b3:5f:3b)
Internet Protocol, Src: [IP Adress], Dst: 192.168.178.23 (192.168.178.23)
Transmission Control Protocol
Source port: ftp (21)
Destination port: 1183 (1183)
Sequence number: 683778350
[Next sequence number: 683778408]
Acknowledgement number: 59795086
Header length: 20 bytes
Flags: 0x18 (PSH, ACK)
Window size: 5840
Checksum: 0x1075 [validation disabled]
File Transfer Protocol (FTP)
Back to top
Marc Wetzel
Guest





PostPosted: Wed May 16, 2007 8:12 am    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote

Hi Remy,

let me rephrase my question:

How can I find out the port number that will be used (in the future connect, or
while connecting)?
If my Connect() times out and is unsuccessful, I will never get access to the
socket anymore (I think it will be completely closed after the connect-timeout)

Is there some callback? Which one should I use to get informed after the
half-open socket is created?

Greetings
Marc
Back to top
Marc Wetzel
Guest





PostPosted: Wed May 16, 2007 8:12 am    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote

Puh - I solved it.

First I thought that some spyware could have infiltrated my computer, but
nothing.

But by using a localhost-sniffer I found out that instead of making a direct
connection, some tool was hijacking the tcpip stack and inserted a
local-connection in it :)

So instead of
[My process: localhost: port 1233] <-> [ftp-server: 21]

I had the following connection scheme:
[My process: localhost:port 1233] <-> [some other process: localhost: port 1235
<-> [ftp-server: 21]

Strange? No. This happens, if you use the WinXP Firewall.
The process who hijacked my connection was "alg.exe"

But I always thought the windows firewall is hijacking in a transparent way...

Other tools I know of that do the same thing are some local proxies (e.g. to
optimize you internet connection over a mobile phone, e.g. Bytemobile)

Remy, if you read this, do you have an answer where I get the port number before
bind() is called?

Best regards
Marc
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed May 16, 2007 10:27 pm    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote

"Marc Wetzel" <mwNOSPAMse (AT) gmx (DOT) de> wrote in message
news:464aa721$1 (AT) newsgroups (DOT) borland.com...

Quote:
do you have an answer where I get the port number
before bind() is called?

There isn't any Port assigned to a socket before a bind(), because
bind() is what sets the local port to begin with. And there is no way
to know the port used by an intermediary hijacker, either.


Gambit
Back to top
Marc Wetzel
Guest





PostPosted: Fri May 18, 2007 8:12 am    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote

Remy Lebeau (TeamB) schrieb:
Quote:
"Marc Wetzel" <mwNOSPAMse (AT) gmx (DOT) de> wrote in message
news:464aa721$1 (AT) newsgroups (DOT) borland.com...

do you have an answer where I get the port number
before bind() is called?

There isn't any Port assigned to a socket before a bind(), because
bind() is what sets the local port to begin with. And there is no way
to know the port used by an intermediary hijacker, either.



OK, sure - you are right.
What I meant was, where do I get the port number used in the 3-way-handshake.
So, where do I get my port number before the SYN packet is sent?

TIA
Marc
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri May 18, 2007 9:22 pm    Post subject: Re: IdFTP (10.1.5) Where is my local port number? Reply with quote

"Marc Wetzel" <mwNOSPAMse (AT) gmx (DOT) de> wrote in message
news:464d49e3$1 (AT) newsgroups (DOT) borland.com...

Quote:
where do I get the port number used in the 3-way-handshake.

So, where do I get my port number before the SYN packet is sent?

Again, the local port is not determined until bind() is called. The
handshake does not occur until connect() is called, which has to be
after bind() is called. You must call bind() first, and then you can
use getsockname() to get the port that was actually assigned.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.