Jump to content

[SOLVED] How can I get the port a socket is using


soadlink

Recommended Posts

Hello,

 

If I create a udp socket, and send data on it like so:

<?php
$socket = fsockopen("udp://" . $ip, $port);
fwrite($socket, "hello");
?>

 

... is it possible to see the source port that the socket has bound itself to for the sending of data? I know I can packet scan or use something within the OS itself, but I'm looking for a way to store this source port as a string (within the script itself), and use it for whatever I need. I did some searching but wasn't able to find anything; however, it seems like it's probably very simple  :). Thanks!

Link to comment
Share on other sites

Great question. I was searching through the stream functions for you. Zippo. Alas, you might try a program execution function together with "netstat -ap". I'm not sure if apache is the process owner or what. This is probably unlikely, but perhaps a hint in the right direction. Also, you'd probably have to keep the socket open until netstat returned the information.

 

Good luck!

Link to comment
Share on other sites

I'm not sure if apache is the process owner or what. This is probably unlikely, but perhaps a hint in the right direction. Also, you'd probably have to keep the socket open until netstat returned the information.

 

Good luck!

 

I'm just using the CLI for this particular script, so no apache is involved. But I think you are correct in referring to using exec to call on netstat or something similar (and then just parse the info). I was hoping there was an easier way though :P.

 

I did a little more reading, and I see that I can create a socket (socket_create), bind it to an address/port (socket_bind), and then have set it send out the data on that socket. And I know the source port because I can specify it during socket_bind. A little more code, but I think it would work.

 

I'll try this, and also play with the netstat parsing, but if anyone has any other ideas feel free to post them. Thanks ;)

Link to comment
Share on other sites

You sure about that? Anyways, if it works, great. I'd love to know.

 

Yea, I just tested it and it worked. What you're referring to is the socket_connect function (that is where you specify the dest ip/port). The socket_bind function allows you to bind your particular socket to an address/port belonging to your machine before the connection is made. Here is working code:

 

<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, "192.168.1.2", "45456");
socket_connect($socket, $remoteip, $remoteport);
socket_write($socket, "Hello World");
?>

 

So 192.168.1.2 has to be an ip belonging to my machine (or you get the error: Cannot assign requested address) , and I would assume 45456 would have to be unused by any other processes (you would most likely get another error).

 

Going to mark this topic as solved, as socket_create seems to be a working solution (though not as easy as I assumed it would be  :P). Feel free to keep posting though if you guys wan't to continue the discussion, but I think I can manage to work this into my script from here. Thanks  ;D

Link to comment
Share on other sites

Interesting, might be a typo. I would agree that it sounds like they are talking about the destination port  ???. I'll make a bug report and let them know just in case, but the bugs site seems to be down at the moment.

 

Another good piece of info for socket_bind: use 0 in the source address field if you don't want to look up your machine's IP, and 0 in the port field for a random port.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.