ssl tunnel application

jebediah

New member
Joined
Oct 11, 2007
Messages
2
Programming Experience
5-10
Hi,

I need a bit of help. I am attempting to write a program that creates a listening socket. When a connection is made I create another connection to another computer and send whatever that is sent to the program through an ssl stream to the other computer. Now, the other computer has a program called STunnel installed on it which does the job of decrypting the SSL data and forwards the decrypted message to another port.

The problem I'm having is with the unencrypted listening socket. I'll use connecting to a MySQL database server as an example. If you telnet to port 3306, on a computer with a mysql server, as soon as you hit enter you will receive a string with the version number of the mysql server. What I'm doing is telneting to port 6969, which my application is listening on. As soon as I hit enter on the telnet session my application detects that a connection is made and then goes through the process of setting up a NetworkStream object. Unfortunately, when I get to calling the Read function of the NetworkStream it blocks. When I go to the telnet session and hit the enter key again, the Read function unblocks, it reads two bytes (CR + LF) sends the two bytes over the ssl stream and then I read back whatever comes back over the ssl stream and I write it back to the unencrypted NetworkStream object which then causes the telnet session to display the mysql server version.

I was first thinking that telnet is just funny and everything will work fine with a true mysql client so I tried the same thing with a program called MySQL Control Center. When I tried to connect it blocked again at the call to Read and I wasn't able to get to go further because I obviously can't get access to the sending socket to throw down another CRLF. Following is a snippet of code that shows pretty much what I'm doing when setting up the listener, it's pretty straight forward:

VB.NET:
Dim clientListener As New TcpListener(System.Net.IPAddress.Any, 6969)

            clientListener.Start()

            Dim c As TcpClient = clientListener.AcceptTcpClient()

            Dim buff(1024) As Byte
            Dim numbytes As Integer = 0

            Dim ns As NetworkStream = c.GetStream()

            ns.Read(buff, 0, buff.Length)

            Dim encoding As New System.Text.ASCIIEncoding()

            ns.Write(encoding.GetBytes("hello"), 0, 5)

            ns.Close()
            c.Close()
            clientListener.Stop()

Now, I did try at first to make this asynchronous driven but the problem was there as well, so I rewrote to make things simpler. Is it because the TCPListener object returns a client that has a Stream based Socket attached to it? Should I be using Raw Sockets instead, and if so how? Any help would be greatly appreciated
 
I don't understand why you expect to receive a CrLf after the connection is made, but if that is what you initially want to relay to other computer can't you just do that?
 
Well, the thing is that I don't want to expect a CRLF. I just discovered that typing an extra CRLF in the telnet session is what unblocks the Read function call.

Wait a minute, I was operating under the assumption that the client is supposed to be the first one to send data after establishing a connection. Maybe the MySQL server is the one to first send out data after the client makes a connection and then the sending/receiving continues until the connection is terminated.

I think you've helped me correct my view of the problem I'm having, I'm going to make a some changes, thank you.
 
Back
Top