NetTalk Central

Author Topic: NetSimple Client - multiple IP address on local machine  (Read 18465 times)

Matthew

  • Full Member
  • ***
  • Posts: 137
    • View Profile
    • Email
NetSimple Client - multiple IP address on local machine
« on: January 16, 2013, 02:24:11 AM »
Hello

I have the following problem:

My machine has more than one IP address. I run on that machine NetSimple Client to send some informaction to some server which only accpet trafic from one specyfic IP address.

For example:
I have four IP address (x.x.x.1, x.x.x.2, x.x.x.3, x.x.x.4)
Server only accept packet from IP x.x.x.3

My question is:
How to send packet to that server from that specyfic IP address. In other words how to bind specyfic IP address to NetSimple Client.

From NetSimple documentaion
Quote
Server & UDP Use:
This method sets up a listening server Port on the specified Port parameters.

1a) If you leave the Server string set to '' (default behaviour) then the listening port will listen on all the IP addresses on that computer.

 Example:
MyServer.Open ('', 80) ! Listen on all IPs

1b) If your machine has more than one IP address or you would rather bind to 127.0.0.1 (which will only be available from the local machine), then set the Server string set to the desired IP address. (Please note if the machines TCP/IP protocol (Control Panel - Networks) has not been configured to support the IP you specify, NetSimple will default to using all available IP addresses).

 Example:
MyServer.Open ('127.0.0.1', 80)
or
MyServer.Open ('192.168.2.1', 80)

But this is for Server. What about Client?

Regards,
Matthew

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: NetSimple Client - multiple IP address on local machine
« Reply #1 on: January 18, 2013, 12:00:04 AM »
Hi Matthew,

Ok, so what you are talking about is "limiting the IP address of the client to a specific value, or range". In other words, you have say a network, but only 1 ip address is allowed to connect to the server. All other IP addresses are explicitly rejected. Right?

To do this, add some code to the .Process method in the WebServer procedure. This goes before the parent call.

  self._Wait()
  case self.Packet.PacketType
  ! --------------------------
  of NET:SimpleNewConnection
    if not IPOK(self.packet.FromIP)
        self.CloseServerConnection(self.packet.OnSocket, self.packet.SockID)     
    end
  end
  self._Release()


In the above code the IPOK function is something you create to decide if the IP address is acceptable or not. Obviously that could be as simple as an IF statement, or longer, depending on your method.

BIG WARNING - as you can see this happens inside a critical section (the Wait, and Release). So absolutely no Windows here, or Messages, or anything else of that nature. And make the code as fast as possible.

If you would prefer to send back something more constructive - not just kill the connection, then you can expand it to something like this;

  self._Wait()
  case self.Packet.PacketType
  ! --------------------------
  of NET:SimpleNewConnection
    if not IPOK(self.packet.FromIP)
          self.packet.binData = self._MakeErrorPacket(401, 'Unauthorized', 'You are Unauthorized to use this resource.')
          self.packet.binDataLen = len (clip(self.packet.binData))
          self.packet.ToIP = self.packet.FromIP
          self.Send()
        self.CloseServerConnection(self.packet.OnSocket, self.packet.SockID)     
    end
  end
  self._Release()


cheers
Bruce

Matthew

  • Full Member
  • ***
  • Posts: 137
    • View Profile
    • Email
Re: NetSimple Client - multiple IP address on local machine
« Reply #2 on: January 18, 2013, 06:01:06 AM »
Thank You Bruce for Your reply, but I have something different in mind.

I have few network card in my machines, so I have few IP address.

I don't have Web Server, but only NetSimple Client and I'm trying to connect to the server which has my telephone operator to send some SMS. Their server accept only one specyfic IP address of my machine. So, how can I bind NetSimple Client to one specific IP address of my machine or how can I change source IP address in TCP/IP packet before send.

Regards,
Matthew

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: NetSimple Client - multiple IP address on local machine
« Reply #3 on: January 18, 2013, 11:41:41 PM »
Hi Matthew,

just to be clear (so I answer the question right this time)

>> I have few network card in my machines, so I have few IP address.

your cards are on the same subnet? for example 192.168.2.50 and 192.168.2.51 ?

Cheers
Bruce

Matthew

  • Full Member
  • ***
  • Posts: 137
    • View Profile
    • Email
Re: NetSimple Client - multiple IP address on local machine
« Reply #4 on: January 20, 2013, 10:41:39 PM »
No Bruce, these IP addresses are not on a private network or on the same network.

For example:
First is: 213.x.x.19
Second: 91.x.x.53
Third: 215.x.x26
Fourth: 92.x.x.10

Regards
Matthew

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: NetSimple Client - multiple IP address on local machine
« Reply #5 on: January 21, 2013, 04:38:03 AM »
what is the ip of the machine you are talking to?
Is that on one of the LAN's?

cheers
Bruce

Matthew

  • Full Member
  • ***
  • Posts: 137
    • View Profile
    • Email
Re: NetSimple Client - multiple IP address on local machine
« Reply #6 on: January 21, 2013, 05:02:37 AM »
No Bruce, the IP address which I am trying to talk. is from completly different LAN.

For example: 203.x.x.72

So, any advice or conclusion?

Regards,
Matthew

Matthew

  • Full Member
  • ***
  • Posts: 137
    • View Profile
    • Email
Re: NetSimple Client - multiple IP address on local machine
« Reply #7 on: January 23, 2013, 12:37:18 AM »
Hello Bruce,

Do You have a solution on my question?

Regards,
Matthew

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: NetSimple Client - multiple IP address on local machine
« Reply #8 on: January 23, 2013, 05:31:57 AM »
Hi Matthew,

hmm - no I don't think so. If you have multiple IP addresses (in different subnets) and those subnets have different gateways, then I'm not sure you have control over the route the packet will take to get to the server. I haven't looked that low in the code, but I don't think you get to choose, at the application level, where the packet goes.

cheers
Bruce