Socket Send and Receive, Same local port, Different Remote Port

J

jeffburgoon

I'm having a problem opening a socket and receiving a response from a
different IP/port than what I'm sending to. Here is what I'm trying to
do:

Open Socket with IP/Port (A,B)
Send to location with IP/Port (C,D)
Immediately receive response on IP/Port (A,B) from IP/Port (X,Y), or
any IP/Port other than (C,D)

My problem is that in order for me to send to (C,D) I have to create
the socket with peer address/port (C,D), so only responses from (C,D)
are allowed. I have tried setting up the socket to use (C,D), then
tearing it down and recreating it with no peer IP/port specified but I
think this is too slow, as the response comes back almost immediately
and is missed by the recv() function. Here is what I'm trying:

$socket=new IO::Socket::INET->new(PeerPort=>$dst_port_num
,Proto=>$'udp'
,PeerAddr=>$dst_addr_str
,LocalPort=>5060
,Reuse=>1);
$socket->send($packet_text);
close($socket);
$socket->recv($response,1024);
close($socket);

The above code works as long as the response comes back from ip/port
($dst_addr_str, $dst_port_num). If a packet is received from a
different IP or port, it is ignored as the socket is only listening on
that pair. I have tried adding in the code to recreate the socket but
it appears to be too slow as the response is never received. Here is
what I add in, directly above the recv() line in the above code:

$socket=new IO::Socket::INET->new(Proto=>$protocol_str
,LocalPort=>5060);


I'm thinking what I need is to either

A) Allocate a socket with no remote port/ip and instead specify port/ip
in send()
or
B) Change peer IP/port to nothing on the fly immediately after sending
packet.

Any help would be greatly appreciated.
 
T

Toni Erdmann

I'm having a problem opening a socket and receiving a response from a
different IP/port than what I'm sending to. Here is what I'm trying to
do:

Open Socket with IP/Port (A,B)
Send to location with IP/Port (C,D)
Immediately receive response on IP/Port (A,B) from IP/Port (X,Y), or
any IP/Port other than (C,D)

My problem is that in order for me to send to (C,D) I have to create
the socket with peer address/port (C,D), so only responses from (C,D)
are allowed. I have tried setting up the socket to use (C,D), then
tearing it down and recreating it with no peer IP/port specified but I
think this is too slow, as the response comes back almost immediately
and is missed by the recv() function. Here is what I'm trying:

$socket=new IO::Socket::INET->new(PeerPort=>$dst_port_num

leave PeerPort undef
,Proto=>$'udp'
^
does this realy work? ----------------------|
,PeerAddr=>$dst_addr_str

leave PeerAddr undef
,LocalPort=>5060
,Reuse=>1);
$socket->send($packet_text);

use $socket->send( BUF, FLAGS, TO );
close($socket);

why do you close the socket?
$socket->recv($response,1024);

Can you receive on a closed socket?
If your are using SIP, take at least 1500 (IP's MTU on Ethernet)
as buffer size.

On the other side 65535 is the max UDP packet size.
Why don't you use this value? Then a SIP messages arrives completely
in $response (you don't have to take care about fragmentation).

But then again SIP defines TCP to be used if the SIP messages is
longer than MTU-200. So with more then 1300 bytes, you should
use and be able to receive from TCP.

But, if your implementing a SIP client that uses a SIP proxy,
it should be OK to connect() this socket to communicate only
with the SIP proxy.

But, as a SIP client, you could use any LocalPort. 5060 is
recommended but not mandatory for SIP clients.

But, as a SIP proxy, you should use 5060 and be prepared to
receive SIP messages from any SIP client.
close($socket);

The above code works as long as the response comes back from ip/port
($dst_addr_str, $dst_port_num). If a packet is received from a

Yes, because by specifying 'PeerAddr'and 'PeerPort', even a UDP
socket performs a 'Socket::connect()' and therefor is dedicated to
communicate with (receive from) PeerAddr:peerPort only.
different IP or port, it is ignored as the socket is only listening on
that pair. I have tried adding in the code to recreate the socket but
it appears to be too slow as the response is never received. Here is
what I add in, directly above the recv() line in the above code:

$socket=new IO::Socket::INET->new(Proto=>$protocol_str
,LocalPort=>5060);


I'm thinking what I need is to either

A) Allocate a socket with no remote port/ip and instead specify port/ip
in send()
or
B) Change peer IP/port to nothing on the fly immediately after sending
packet.

Any help would be greatly appreciated.

Toni "use Socket;" User
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top