Binding Zn servers on specific interfaces

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Binding Zn servers on specific interfaces

Sven Van Caekenberghe
Hi,

I recently added a feature to Zn servers to bind on specific interfaces. I got some questions about it, so here is a little writeup.

A TCP/IP server socket used by an HTTP server is normally started by specifying the port on which it has to listen. This is how Zn normally works. Behind the scenes, the server socket is actually bound to what is called 'any interface'. TCP/IP on your computer / operating system, can have multiple network interfaces. Most have at least 2: one is called the local interface (lo, 127.0.0.1) and another is the TCP/IP address you are using to connect to the network and by extension the internet. Servers can have multiple network connections.

By using the #interface: method, you can optionally control (or restrict) on which interfaces your Zn server should listen. In most cases this is not needed, but it can be an important security feature to only listen on the local interface (because that means the server can only be accessed from the machine itself, not over the network/internet).

The default case is to listen to any interface available to the machine / OS. The following three are equivalent:

(ZnServer defaultOn: 1701)
        logToTranscript;
        start.

(ZnServer defaultOn: 1701)
        interface: nil;
        logToTranscript;
        start.

(ZnServer defaultOn: 1701)
        interface: #[0 0 0 0];
        logToTranscript;
        start.

Next we bind only to the local interface:

(ZnServer defaultOn: 1701)
        interface: #[127 0 0 1];
        logToTranscript;
        start.

Now, only http://127.0.0.1:1701 and http://localhost:1701 will work.

On my machine (MacBook Pro, Mac OS X 10.7) I have the following network address: 192.168.1.6. This is how to listen to only that interface and not the local one.

(ZnServer defaultOn: 1701)
        interface: #[192 168 1 6];
        logToTranscript;
        start.

Now, only http://192.168.1.6:1701 or (in my case) http://voyager.local:1701 will work. I have only tested this on my development machine with a Pharo Cog VM, but it should work cross platform. Let me know if that is not the case.

Regards,


Sven


--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill





smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Binding Zn servers on specific interfaces

NorbertHartl
Sven,

this is pretty cool. A good feature to have less to think. In a normal environment there will be a front-end server that serves request to the external interface. So the zinc server is well put on the loopback interface.
I just would opt to use another name than interface. There are basically two things when it comes to network interfaces: an interface and a ip address. They belong to the same thing but they don't belong to each other. So to me it is really confusing to name it "interface" while meaning the address. So I think #bindTo: or #bindingAddress: would be better options.

my 2 cents,

Norbert

Am 11.04.2012 um 16:11 schrieb Sven Van Caekenberghe:

> Hi,
>
> I recently added a feature to Zn servers to bind on specific interfaces. I got some questions about it, so here is a little writeup.
>
> A TCP/IP server socket used by an HTTP server is normally started by specifying the port on which it has to listen. This is how Zn normally works. Behind the scenes, the server socket is actually bound to what is called 'any interface'. TCP/IP on your computer / operating system, can have multiple network interfaces. Most have at least 2: one is called the local interface (lo, 127.0.0.1) and another is the TCP/IP address you are using to connect to the network and by extension the internet. Servers can have multiple network connections.
>
> By using the #interface: method, you can optionally control (or restrict) on which interfaces your Zn server should listen. In most cases this is not needed, but it can be an important security feature to only listen on the local interface (because that means the server can only be accessed from the machine itself, not over the network/internet).
>
> The default case is to listen to any interface available to the machine / OS. The following three are equivalent:
>
> (ZnServer defaultOn: 1701)
> logToTranscript;
> start.
>
> (ZnServer defaultOn: 1701)
> interface: nil;
> logToTranscript;
> start.
>
> (ZnServer defaultOn: 1701)
> interface: #[0 0 0 0];
> logToTranscript;
> start.
>
> Next we bind only to the local interface:
>
> (ZnServer defaultOn: 1701)
> interface: #[127 0 0 1];
> logToTranscript;
> start.
>
> Now, only http://127.0.0.1:1701 and http://localhost:1701 will work.
>
> On my machine (MacBook Pro, Mac OS X 10.7) I have the following network address: 192.168.1.6. This is how to listen to only that interface and not the local one.
>
> (ZnServer defaultOn: 1701)
> interface: #[192 168 1 6];
> logToTranscript;
> start.
>
> Now, only http://192.168.1.6:1701 or (in my case) http://voyager.local:1701 will work. I have only tested this on my development machine with a Pharo Cog VM, but it should work cross platform. Let me know if that is not the case.
>
> Regards,
>
>
> Sven
>
>
> --
> Sven Van Caekenberghe
> http://stfx.eu
> Smalltalk is the Red Pill
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Binding Zn servers on specific interfaces

Sven Van Caekenberghe

On 11 Apr 2012, at 18:39, Norbert Hartl wrote:

> this is pretty cool. A good feature to have less to think. In a normal environment there will be a front-end server that serves request to the external interface. So the zinc server is well put on the loopback interface.
> I just would opt to use another name than interface. There are basically two things when it comes to network interfaces: an interface and a ip address. They belong to the same thing but they don't belong to each other. So to me it is really confusing to name it "interface" while meaning the address. So I think #bindTo: or #bindingAddress: would be better options.

Yeah, maybe that would be a good idea, Norbert.
I took the name from Socket>>listenOn:backlogSize:interface: without really thinking about it.
I like #bindingAddress: the most.
I'll sleep over it and then decide whether to change it or not, but I probably will.

Thx,

Sven


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Binding Zn servers on specific interfaces

Sven Van Caekenberghe

On 11 Apr 2012, at 19:22, Sven Van Caekenberghe wrote:

> On 11 Apr 2012, at 18:39, Norbert Hartl wrote:
>
>> this is pretty cool. A good feature to have less to think. In a normal environment there will be a front-end server that serves request to the external interface. So the zinc server is well put on the loopback interface.
>> I just would opt to use another name than interface. There are basically two things when it comes to network interfaces: an interface and a ip address. They belong to the same thing but they don't belong to each other. So to me it is really confusing to name it "interface" while meaning the address. So I think #bindTo: or #bindingAddress: would be better options.
>
> Yeah, maybe that would be a good idea, Norbert.
> I took the name from Socket>>listenOn:backlogSize:interface: without really thinking about it.
> I like #bindingAddress: the most.
> I'll sleep over it and then decide whether to change it or not, but I probably will.
>
> Thx,
>
> Sven
A new version of Zinc-HTTP was added to project Zinc HTTP Components:
http://www.squeaksource.com/ZincHTTPComponents/Zinc-HTTP-SvenVanCaekenberghe.256.mcz

==================== Summary ====================

Name: Zinc-HTTP-SvenVanCaekenberghe.256
Author: SvenVanCaekenberghe
Time: 13 April 2012, 1:20:15 pm
UUID: a3d6638c-d5c4-4c20-a6c1-566e00b752fb
Ancestors: Zinc-HTTP-SvenVanCaekenberghe.255

renamed ZnServer>>#interface[:] to ZnServer>>#bindingAddress[:] following a suggestion by Norbert Hartl, Thx!


--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill


smime.p7s (5K) Download Attachment