CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Block TCP Ports

  1. #1
    Join Date
    Jul 1999
    Posts
    97

    Using sockets to block ports

    I have a requirement for which I need a client application running on a windows machine to use up (occupy/block) a range of ports so that no other application can use these ports.

    This application is to be written using ATL,STL,Win32 SDK. So no MFC please.

    The question is what the most efficient way to do this?


    TIA

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882
    You need LSP (Layered Service provider) for Winsock to be able to block ports.

    See http://www.microsoft.com/msj/0599/La...edService.aspx
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    Get a firewall.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  5. #5
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    you may bind your sockets with these ports and block all icoming events ... hang on, i will give you code when i am at home, or wait for the gurus
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Merged threads]

  7. #7
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    LSP is good but it only blocks the calls from winsock. Others (TDI for instance) can bypass it. The easier option is filter APIs which is easy but works only in w2k+. Check PfCreateInterface() and co. The other (hard way) is writing NDIS low layer filter hook. This may help you started.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  8. #8
    Join Date
    Jul 1999
    Posts
    97

    block TCP ports

    I had written a small app that will attempt to bind my sockets within the range of TCP ports [1024-4995]. The problem is I cannot browse to certain web sites and some of the applciations are not working correctly. So, I am assuming these applications need to use one of the ports that my test app has blocked. How woul dI know not to block some essential ports? Is there a list somewhere that will provide me this info?

    Here is my test code

    Code:
    #include "stdafx.h"
    #include <winsock2.h>
    #include <list>
    
    using namespace std;
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	list<SOCKET>		m_lstSocket;
    
     	SOCKET		server;
    	WSADATA		wsaData;
    	sockaddr_in local;
    
    	int wsaret=WSAStartup(0x101,&wsaData);
    	if(wsaret!=0)
        {
            return 0;
        }
    
    	local.sin_family=AF_INET; //Address family
        local.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
    
    	for(int nCounter = 1024;nCounter < 4995;nCounter++)
    	{
    		local.sin_port=htons((u_short)nCounter); //port to use
    		server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    		bind(server,(sockaddr*)&local,sizeof(local));
    		m_lstSocket.push_back(server);
    	}
    
    	list<SOCKET>::iterator it;
    	for (it = m_lstSocket.begin(); it != m_lstSocket.end();it++)
    		closesocket((SOCKET)(*it));
    
    	WSACleanup();
    	return 0;
    }

  9. #9
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    This is the worst way to do it. Socket creation is expensive considering the time and resource used. Well, if you create about 5000 sockets that do nothing, it is a big waste of resource, and your browser may not display properly because the lack of the said. Moreover other applications may need the resource (mainly non-paged memory) that you use up. One of the biggest problem with using up the NP memory is that, your whole os could crash if some driver doesn't behave properly because of the lack NP memory. Also binding to a port doesn't mean that other applications cannot bind to it. Another question is, what if the port you already want to block is already bound by an application?
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  10. #10
    Join Date
    Jul 1999
    Posts
    97
    Thanks for the suggestion. I understand this is not the best way to work with sockets but that is reason I posted this query here. If I knew what the most efficient way to do what is required, I would have saved my time than type a query here.

    Coming back to the problem, I dont know if using sockets is the solution here. So let me start of with the actual problem and see if I can get some leads on how to solve this.

    Problem: An application we use, utilizes the first available free TCP port within the range 1024-5000. The problem is with the range. this range is to big and we have to reduce it to a /10 port range.

    The way I attempted to get around this is to write a wrapper around this application. The idea for this wrapper is to take up/block the other ports to force the actual app to use the smaller port range.

    Is this a good way of solving my problem? What are my other options?

    p.s: I cannot modify the original app, so please no suggestions on modifying the original app.

  11. #11
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    Originally posted by Avad
    Problem: An application we use, utilizes the first available free TCP port within the range 1024-5000. The problem is with the range. this range is to big and we have to reduce it to a /10 port range.
    I don't see any problem. You want to use the first available port. Why do you want to block then? Just loop the bind(). Start with port 1024 and break when you have a successfull bind. I still don't understand what you are trying to do.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  12. #12
    Join Date
    Jul 1999
    Posts
    97
    The problem is I have to force the main application to use ports 2000-2005 ONLY. So even if one of the ports in the range of 1024-2000 and 2005-5000 is free, I should not allow the main application to use them. Hope this helps.

  13. #13
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    So whats the problem? Loop from 2000 to 2005. Check the RFC 1700 that describes the ports of well known services to see if any sevice uses it. At binding if all of the 5 (6 ??) ports are blocked you have no choice but to inform the user. He has to shutdown the application that uses it. BTW are you using all of the ports in the range or any of the port?
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  14. #14
    Join Date
    Nov 2001
    Location
    Denmark
    Posts
    68
    You could make a Firewall


    Lasse.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured