How to check which process in bound to a particular port? - sockets

I am getting some error in an application like:
Failed to bind to socket 192.168.122.1:87 : Address already in use
so I want to see which process is using this socket.
Is there any way to do this via netstat?

Related

socket connection using service name

We are using sockets in our project to transfer data from one system to another system. In socket connect call, we are using Ipaddress and the port number to establish socket connection to the other system where our service is running.
Would it be possible to use the service name also in the connect call along with or without port number?
or can this be done in any other way?
Thanks in advance.

socket.py not creating listener on server

I set variables host and port instead of setting the 'address' variable tuple in socket.py. I was unable to get 'address' as a tuple to work. I do not believe this is the issue, but I thought I should state this up front.
FYI, my goal is an integrations project, and I believe I isolated socket.py as the problematic code. socket.py is not creating a listener on the remote server. I run the python script on my client, and my server address is 192.168.1.130 port 7879.
I think socket.py is the problem, because I do not receive the expected print statements back through the console that socket.py is attempting to create a socket. In addition, I can RDC to the server, disable ufw (yes I know this is a bad idea), create a tcp listener, push data through the client socket to the server socket, and verify this with netcat.
Am I mistaken that I should be able to parameterize socket.py with nothing more than a host and port and be able to create a socket connection? I am happy to provide more detail from logs, but I thought I should start with a very high level overview.
Answer: More investigation needed. I think socket.py does not create the remote connection with socket(),bind(),listen() statements; instead simply looks for a listener on the remote server with a connect() statement. This is entirely my misunderstanding given I did not dive into the details of the socket.py code. I figured this out because the service running on the remote server creates the listener, but the service itself on the remote server is what is not properly starting.

Obtaining the source IP and port of an INADDR_ANY client socket before the TCP three-way handshake?

I'm on Windows 7, using bind before connect with SO_REUSEADDR, and setting the local address structure to IP address INADDR _ANY and port 0 (zero), in order to let the operating system select the source details for a client socket.
Firstly, I've read that it's not possible to get the source IP before connecting to the server, since it's being chosen at this point and several addresses can be valid. But the port is selected before the connection, so is there a way to get it? (getsockname() looks like to not work).
Secondly, about the source IP, is there a way to get it before a packet is sent to the server? I need the specific time between the moment the OS selected the source IP and the moment it starts the three-way handshake. The connect() function dominates the both.
I'm on Windows 7, using bind before connect with SO_REUSEADDR
Why are you using SO_REUSEADDR in this situation? You don't need it, and it makes no sense for what you are attempting. SO_REUSEADDR should typically only be used for a listening socket, not a connecting socket.
setting the local address structure to IP address INADDR _ANY and port 0 (zero), in order to let the operating system select the source details for a client socket.
It is meaningless to bind() a client socket to INADDR_ANY:0. You can (and should) omit such a bind() completely and leave the socket unbound until connect() is called. The only time you should ever bind() a client socket is if you want to bind it to a specific local IP and/or Port. But you are not doing that in this situation, so get rid of it.
Firstly, I've read that it's not possible to get the source IP before connecting to the server, since it's being chosen at this point and several addresses can be valid.
Correct, unless you bind() to a specific source IP.
But the port is selected before the connection
Both source IP and source port are selected by connect() if the socket is unbound, or bound to source IP INADDR_ANY and/or source port 0. So you have no opportunity to query either value before connect() has selected them.
so is there a way to get it? (getsockname() looks like to not work).
getsockname() is exactly what you need. Just make sure you are not calling it until connect() has successfully connected to the server first. This is stated in the getsockname() documentation:
This call is especially useful when a connect call has been made without doing a bind first; the getsockname function provides the only way to determine the local association that has been set by the system.
...
The getsockname function does not always return information about the host address when the socket has been bound to an unspecified address, unless the socket has been connected with connect or accept (for example, using ADDR_ANY). A Windows Sockets application must not assume that the address will be specified unless the socket is connected. The address that will be used for the socket is unknown unless the socket is connected when used in a multihomed host. If the socket is using a connectionless protocol, the address may not be available until I/O occurs on the socket.
Secondly, about the source IP, is there a way to get it before a packet is sent to the server?
For TCP, you can retrieve the selected source IP using getsockname() immediately after connect() has successfully connected to the server. Not before.
I need the specific time between the moment the OS selected the source IP and the moment it starts the three-way handshake.
It is not possible to determine that detail. No socket application should ever need that detail. Why do you need it?
I'm on Windows 7, using bind before connect with SO_REUSEADDR, and setting the local address structure to IP address INADDR _ANY and port 0 (zero), in order to let the operating system select the source details for a client socket.
Why? That's exactly what happens if you don't call bind() at all, during connect(). Binding a client socket to INADDR_ANY isn't correct in any case. Setting SO_REUSEADDR doesn't make sense either without specifying a non-zero port number. Just remove all this.
Firstly, I've read that it's not possible to get the source IP before connecting to the server, since it's being chosen at this point and several addresses can be valid.
Correct.
But the port is selected before the connection, so is there a way to get it?
Yes. getsockname().
(getsockname() looks like to not work)
Doesn't work how?
Secondly, about the source IP, is there a way to get it before a packet is sent to the server?
You can get it with getsockname() as soon as connect() has succeeded, but this involves sending packets to the server.
I need the specific time between the moment the OS selected the source IP and the moment it starts the three-way handshake. The connect() function dominates the both.
Bad luck.

Using Sockets with NSXPCConnection

Running into an issue when using sockets with an NSXPCConnection.
Basically, there is a main process and a helper process running, established via NSXPCConnection. That helper process needs to act as a server and listen to a particular port (say 111), which receives outside connections.
The helper process opens a listening socket using the TCPServer helper class (wrapper around CFSocket) which is provided by Apple. Code found here:
https://code.google.com/p/iphone-remotepad/source/browse/trunk/RemotePad/TCPServer.h?r=238
The socket is opened successfully in - (BOOL)start:(NSError **)error.
The outer clients can establish with the 111 port. (test in terminal via telnet localhost 111).
However, the helper process never receives the TCPServer callback TCPServerAcceptCallBack.
The helper process has com.apple.security.network.client entitlement enabled.
Also, when I run the TCPServer in the main app instead of the helper process, set up the server on port 111, and try to connect to port 111, I do get the callback.
Any ideas of why the helper process does not receive socket call back? An XPC related issue?
Ok figured out the issue.
An xpc service provides you with a default run loop of type dispatch_main.
You want to substitute that with an NSRunLoop - done by changing the xpc service info plist:
https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html
Once that is done, you want to manually create a run loop inside your xpc service, along lines of:
do {
#autoreleasepool {
[[NSRunLoop currentRunLoop]run];
}
} while (YES);
With that in place, the TCPServer (which needs an active runloop) will return the callback and you'll be able to get the incoming data.

Java Socket Issue

I'm gonna use Socket() constructor to create a dummy like socket.
what is the difference of using bind() and connect() to this created socket?
if i'm gonna use bind() how can i establish connection or does it directly establish connection?
With a socket you build a connection between two endpoints. One of these endpoints (the local one) is on your machine, the other endpoint (the remote one) may be anywhere.
With bind you set the address of the local endpoint. Often you don't need to do that, since your operating system will take care of the details.
With connect you set the address of the remote endpoint and start a connection.