Click here to Skip to main content
15,064,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I'm currently writing a small server to check for updates of my programs etc.
This works nicely and now I'm trying to make it a little more secure. What I want to implement is a blacklisting of certain IP adresses.
Currently I'm doing it like this:

VB
Dim client As TcpClient = listener.AcceptTcpClient
blacklist = IO.File.ReadAllLines(Application.StartupPath & _
                                 "\Blacklist.txt")
If blacklist.Contains(GetIp(client).ToString) = False Then
     'IP not on blacklist, proceed with processing the client
     Dim t As New Thread(AddressOf HandleClient)
     Dim argus() As Object = {client, ClientIdentifier}
     t.Start(argus)
     threads.Add(t)
     clients.Add(client)
Else
    'Client is on blacklist, close the connection
    tb("Blacklisted IP tries to connect. Closing connection.")
    client.Close()
End If


Now this works OK, but what bothers me is, that even though the client is blacklisted the TCP connection has to be accepted first and the connection is established before being closed again.

My question now is: Is there any way to get information about the client that is about to be accepted before it is accepted? And can I refuse the connection attempt if the client's IP is blacklisted? I can't seem to find a way with the tools at hand.

Thank you in advance for any answers and if you have further questions about my problem, please feel free to ask.

Best wishes
Jens
Posted

1 solution

Unfortunately there is no way to do this without accepting the socket connection first. You could try getting network information about what sockets are in the SYN_SEND or SYN_RECEIVED state, but its impossible to know if that connection is the one you are in the process of accepting without initializing the socket structure (which the TcpClient is built on).
   
Comments
Keex0r 22-Jun-13 17:43pm
   
Ok, thank you very much for your answer. I'm not very experienced in network stuff anyways so if there is no easy solution I'm ok with that. I think it works with directly closing the connection again, too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900