Transfer socket connection between programs

J

JamesHoward

Does anyone know any method to have one program, acting as a server
transfer a socket connection to another program? I looked into
transferring the connection via xml rpc to no avail. It seems to be a
problem of getting access to a programs private memory space and
giving another program access to that space.

Thanks in advance,
James Howard
 
G

Grant Edwards

Does anyone know any method to have one program, acting as a
server transfer a socket connection to another program?

The only way I know of is to use fork. When you fork a
process, all open file-descriptors (including network
connections) are inherited by the child.
I looked into transferring the connection via xml rpc to no
avail.

I've no idea how that could work (even in theory) on any OS
with which I'm familiar.
It seems to be a problem of getting access to a programs
private memory space and giving another program access to that
space.

Private memory has nothing to do with it. The connection is a
data structure that lives in kernel space, not in user space.
Even if you could grant another process access to your "private
memory space", it wouldn't help you "transfer a socket
connection", since that connection is something the OSes
manages.
 
P

Paul Rubin

JamesHoward said:
Does anyone know any method to have one program, acting as a server
transfer a socket connection to another program? I looked into
transferring the connection via xml rpc to no avail.

You have to use an out of band communication mechanism. On Linux this
is the SCM_RIGHTS ancillary message over Unix domain sockets. There
are similar things for Solaris and Windows. Unfortunately, Python
doesn't support any of them at the moment, but there's was once an RFE
waiting for a patch:

http://mail.python.org/pipermail/python-bugs-list/2003-September/020431.html

Actually I'm having trouble finding the RFE mentioned any more, but it
it looks like Heiko Wundram did a patch in a different item:

http://bugs.python.org/issue1194378

See also:

http://bugs.python.org/issue1492240
 
J

JamesHoward

The only way I know of is to use fork. When you fork a
process, all open file-descriptors (including network
connections) are inherited by the child.


I've no idea how that could work (even in theory) on any OS
with which I'm familiar.


Private memory has nothing to do with it. The connection is a
data structure that lives in kernel space, not in user space.
Even if you could grant another process access to your "private
memory space", it wouldn't help you "transfer a socket
connection", since that connection is something the OSes
manages.

Thanks Grant,

Does this mean that there is some way to transfer a pointer to that
kernel memory space from one program to another and have it be valid,
or is that kernel memory space protected and unusable from other
processes?
 
G

Grant Edwards

Does this mean that there is some way to transfer a pointer to that
kernel memory space from one program to another and have it be valid,
No.

or is that kernel memory space protected and unusable from other
processes?

On Linux (I'm not sure how it works on Windows), there is a
table of file descriptors that the kernel keeps for each
process. A table entry can be an open file on disk, a serial
port, a network connection, etc.

A file descriptor is just an index into that table. That table
can't be accessed by any other processes. When you fork a
process the new process inherits a copy of that table (and
therefore inherits any open network connections).
 
L

Laszlo Nagy

JamesHoward said:
Does anyone know any method to have one program, acting as a server
transfer a socket connection to another program? I looked into
transferring the connection via xml rpc to no avail. It seems to be a
problem of getting access to a programs private memory space and
giving another program access to that space.
If you are using UNIX, you can create a pipe in the filesystem, and
connect I/O on both ends. Of course this is not the same, because - for
example - you cannot use setsockopt. But it works with already existing
processes very well, and usually you can setup socket options in your
server.

Under windows, I have no clue. It may be possible to use pipes under
Windows but I don't know how. You can also use other nasty techniques,
like loading a DLL into your "another program", then make a code hook
into your server. Brrrrrrr. :) (Unstable, not portable even between
Windows versions...)

Regards,

Laszlo
 
M

marek.rocki

JamesHoward napisa (a):
Does anyone know any method to have one program, acting as a server
transfer a socket connection to another program? I looked into
transferring the connection via xml rpc to no avail. It seems to be a
problem of getting access to a programs private memory space and
giving another program access to that space.

Thanks in advance,
James Howard

Under Windows you may want to use WSADuplicateSocket WinAPI function +
a named pipe (for example) to transfer the obtained socket data from
one process to the other.

Hope that helped,
Marek
 
D

davisn90210

Does anyone know any method to have one program, acting as a server
transfer a socket connection to another program? I looked into
transferring the connection via xml rpc to no avail. It seems to be a
problem of getting access to a programs private memory space and
giving another program access to that space.

Thanks in advance,
James Howard

On Unix, you can use Unix Domain Sockets to pass file descriptors
between processes. You need to create a Unix Socket, then use sendmsg
and recvmsg to pass the file descriptor between processes.

Linux Application Development includes an example of how to do this in
section 16.4.6. The book is available through Amazon.com at
http://www.amazon.com/Linux-Application-Development-Michael-Johnson/dp/0321219147

I'm sure you can find other examples on the internet to follow.

Hope this points you in the right direction,

--Nathan Davis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top