Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. client-server

client-server

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ask_you
    wrote on last edited by
    #1

    hello, i m developed a simple client-server application. now i want that in my server i don't accept connections from a list of IP addresses. how do i accomplish it. After accept() call i tried to check the IP of the connecting client and then used closesocket() to close the connection. But the problem is that the client gets connected as the connect() call from the client side works. i don't want that to happen. i want that the server should not accept connection from the list of IPs rather than accepting and then breaking. how do i achieve it? thanx in advance

    M 1 Reply Last reply
    0
    • A ask_you

      hello, i m developed a simple client-server application. now i want that in my server i don't accept connections from a list of IP addresses. how do i accomplish it. After accept() call i tried to check the IP of the connecting client and then used closesocket() to close the connection. But the problem is that the client gets connected as the connect() call from the client side works. i don't want that to happen. i want that the server should not accept connection from the list of IPs rather than accepting and then breaking. how do i achieve it? thanx in advance

      M Offline
      M Offline
      munawar1968
      wrote on last edited by
      #2

      Not possible if you use socket api. What you have to do is to detect the first SYN packet from client IP's and filter on that. 1. Use a firewall (external to your prog). 2. Code raw sockets... i.e be prepared to write your own TCP protocol :(

      A 1 Reply Last reply
      0
      • M munawar1968

        Not possible if you use socket api. What you have to do is to detect the first SYN packet from client IP's and filter on that. 1. Use a firewall (external to your prog). 2. Code raw sockets... i.e be prepared to write your own TCP protocol :(

        A Offline
        A Offline
        ask_you
        wrote on last edited by
        #3

        can u provide me with some working example on raw socket? any links? thanks in advance

        M P 2 Replies Last reply
        0
        • A ask_you

          can u provide me with some working example on raw socket? any links? thanks in advance

          M Offline
          M Offline
          munawar1968
          wrote on last edited by
          #4

          Raw sockets: look at ping.c sample in MSDN.. Raw packet capture library: http://www.winpcap.org The above two will help you detect a SYN pkt from clients. But you'll be wasting your time trying to code a conventional client-server prog using the above two methods.. you'll have to write the the entire TCP protocol suite yourself at the server end. Why dont you simply use a packet filter firewall at the server?

          1 Reply Last reply
          0
          • A ask_you

            can u provide me with some working example on raw socket? any links? thanks in advance

            P Offline
            P Offline
            pierrekande
            wrote on last edited by
            #5

            I suppose that you have a good level on networking programming then do this if not just forget about, sorry Normaly with Socket API it is not possible, because while calling accept() the client get connect. For that you need to use some firewall in your program for packet filter here is am easy way to do that: INTERFACE_HANDLE hInterface; // interface PFFORWARD_ACTION defaultAction=PF_ACTION_DROP; // create the interface // I create the interface. Predefined acctions, forward all. DWORD errorCode = PfCreateInterface(0,defaultAction,defaultAction,FALSE,TRUE,&hInterface); if(errorCode != NO_ERROR) { return -1; } // Bind the Ip Address with the interface PBYTE lIp = (PBYTE)&ip; //the ip address of your card errorCode = PfBindInterfaceToIPAddress(hInterface, PF_IPV4, lIp); if(errorCode != NO_ERROR) { PfDeleteInterface(hInterface); hInterface = NULL; return -1; } ///////////////////////////////////////////////////// now here do with the code { DWORD result; PIP_ADAPTER_INFO pAdapterInfo = NULL, aux; IP_ADDR_STRING *localIp; unsigned long len = 0; GetAdaptersInfo(pAdapterInfo, &len); pAdapterInfo = (PIP_ADAPTER_INFO) malloc (len); result = GetAdaptersInfo(pAdapterInfo, &len); if(result != ERROR_SUCCESS) { AfxMessageBox("Error getting adapters info."); return; } // Fill the real filter struct PF_FILTER_DESCRIPTOR ipFlt; ipFlt.dwFilterFlags = FD_FLAGS_NOSYN; ipFlt.dwRule = 0; ipFlt.pfatType = PF_IPV4; ipFlt.dwProtocol = protocol; // value is : TCP =6;UDP=17 or ICMP=1 ipFlt.fLateBound = 0; ipFlt.wSrcPort = srcPort; // source port ipFlt.wSrcPortHighRange = srcPort; // source port range ipFlt.wDstPort = dstPort; // destination port ipFlt.wDstPortHighRange = dstPort; // destination port range unsigned long lIpSrc = CharToIp(srcIp); //chartoIP convert (*.*.*.*) to long unsigned long lIpDst = CharToIp(dstIp); unsigned long lMaskSrc = CharToIp(srcMask); unsigned long lMaskDst = CharToIp(dstMask); ipFlt.SrcAddr = (PBYTE) &lIpSrc; ipFlt.SrcMask = (PBYTE) &lMaskSrc; ipFlt.DstAddr = (PBYTE) &lIpDst; ipFlt.DstMask = (PBYTE) &lMaskDst; DWORD errorCode; // I add the filter if(direction == IN_DIRECTION || direction == ANY_DIRECTION) errorCode = PfAddFiltersToInterface(hInterface,1,&ipFlt,0,NULL,NULL); if(direction == OUT_DIRECTION || direction == ANY_DIRECTION) errorCode = PfAddFiltersToInterface(hInterface,0,NULL,1,&ipFlt,NULL); } Not that when stopping your

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups