How can I block outbound connection with Winsock - windows

I want to create an app to block IP Address. I read Microsoft docs about Winsock then I know about setsockopt() function. This function has an attribute level that has an option SO_CONDITIONAL_ACCEPT. The Description of this option is can enable incoming connections are to be accepted or rejected by the application, not by the protocol stack (https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt)
But I don't know how to set it to auto block my Input that is an IP Address. Or do you know any function in Winsock that can block outbound connections please tell me. Thank all!

Related

How to get the IP address of a connected WebSocket-client?

I'm currently working on a ABAP Push Channel server to WebSocket client connection and I need the IP-address of the client in order to identify whether this client is the one I want to send the message to. In my scenario there could be multiple WebSocket connections.
Now there is the ssi_websocket_table table and the ssi_websocket_table_row row with the the field caller_ip, however this gives me the IP address of the DNS-Server of the network I'm connected to, and I expected the IP address of my local PC since the WebSocket-client is running on this machine.
Is there any other way to get the clients IP address from an active WebSocket connection in ABAP?
P.S. Looking at all the table entries, it shows the correct IP when using a different server configuration, as soon as I know why that's the case I will report back.
As pointed out by vwegert it makes no sense to use the IP to tell the WebSockets apart, I think it would probably be better to use an ID for each WebSocket connection instead.
You could get the IP from the WebSocket server context which gets the IP header apparently from the opening HTTP handshake for the connection:
DATA(lo_context) = i_context. " IF_APC_WSP_SERVER_CONTEXT type
DATA(lo_request) = lo_context->get_initial_request( ).
" initialize G_CONTEXT_ID_FIELD for PCP_SET_CONTEXT_FIELDS
DATA(lv_id) = lo_request->get_header_field( if_http_header_fields_sap=>remote_addr ).
the sample is taken from the SAP standard class CL_APC_WS_EXT_ABAP_ONLINE_COMM, ON_MESSAGE method.

ZMQError: Cannot assign requested address

I'm using zeromq to develop a dynamic application which operate like upnp protocol( autoconfiguration and auto-discovery of equipment on a living room).
To do it so, i use zeromq to distribute messages, my problem is that when i create a socket and binding on a adress "169.254.1.0 through 169.254.254.255", I receive a error messages like this: ZMQError: Cannot assign requested address.
I've tried to figure out why by changing the address by local adress (127.0.0.1),and it's works !!.
The problem is that i'm implementing autoip, it's means that i'm oblliged to use the adresse on range 169.254.1.0 through 169.254.254.255.
Thanks in advance for your help!
I got this error, too... and I realized I had the wrong idea of who was connecting to whom in the PUB/SUB model.
This was working: Host A as the ZMQ PUB, and could easily connect with Host A as the ZMQ SUB.
Then I tried to set up Host B as the SUB, and have Host A "send" to that... and I kept getting this error. After all don't you need to tell the PUB where to send the data?
No! Host A as the PUB should still bind to IP_ANY (0.0.0.0); it is Host B as the SUB who has to be configured with the address of Host A.
Once I got that straight in my code (and in my head), I was up and running.
After you bring the interface up, you need to get its IP address and then bind on that. Alternatively you may be able to bind on "*", meaning all interfaces.

Assign specific port to RPC server\client

I am writting a RPC client server application on windows. I have gone through RPC sample programs MS has given. But none of them mention port specifically. This probably because RPC uses dynamic port above 1024. But what if I wanted to assign specific port/port range to specific service (my server and client app for example). How can I do that? I can use RPCCFG to assign range but that range will be for all RPC programs (http://support.microsoft.com/kb/908472) right? How can I control a single program? I know it's possible because exchange seem to able to do it for Client Access Service?
Thanks in advance,
-Neel.
You can define ports in the code or use a config file which you read in the code.
status = RpcServerUseProtseqEp(
(char *)"ncacn_ip_tcp", // Use TCP/IP
RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP.
(char *)"4747", // TCP/IP port to use.
NULL); // No security.
Success. Jasper

Connected socket information

Hallo, how it is possible to get information form connected socket. I need to get address of server, the socket is connected to and port. Is this possible to get?
I need to figure it out in WSPSend, to block it or not. So i cant remember it form previous actions(like socket(),connect() etc..)
thx.
getpeername

Receiving datagrams using Udp connection

In order to receive datagrams through an UDP connection I have created an object of type UDPClient.
receivedNotificationSock = new UdpClient();
However once done and on using the receive method:
receivedHostNameBuffer=receivedNotificationSock.Receive(ref receivedNotificationIP);
I am getting an exception saying that I must call the bind method.
But there is no bind method in the UDPClient class.
Could You guys please provide me with the code if possible as to what should be done to overcome this exception.
You need I think to know some more about sockets.
All sockets possess a port number. First, you create a socket - which is almost useless on its own. It just floats there. But then you bind it - you assign it a port number. Now it's useful - now you can send and receive data on it.
Remember, all UDP communications are defined by the quad data set of the IP and port of the source and the IP and port of the destination. A freshly created socket doesn't have an IP address or port; binding gives it an IP address and port.
Unfortunately, I'm not a C# programmer, so I can't properly answer your question. But at least you know why it's important.
Pass the port number into the constructor of your UDP client.
receivedNotificationSock = new UdpClient(21000);
You may need to change firewall settings to allow the bind, though a popup window normally opens when you first run this on your dev machine.
For Socket proramming you need to know the sequence of syscalls you need to do on client side and on the server side.
If you are writting a client :
you open a socket with a socket call.
you then connect to the server port with a connect call
once connect is successful
then you send the request to the server using either a send or sendto or a write
which results in reception of data that you can read using a receive or read
On Server Side
you create a socket
bind it to a port
start listening on the socket for incoming connections from various clients using a listen.
There is a non blocking way of listening for connections as well with a select syscall.
Once the you establish a connection you can essentially read the request and start processing it.
Here's an example in C# that may be useful to you.
http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/

Resources