How to know through which network interface ip address is pinged? - c++

I am progrmmatically pininging ip address using c++ in windows.
I used the code from this link for pinging https://www.codeproject.com/Articles/647/Ping-for-Windows
I am receiving reply from this function
WSAAPI
recvfrom(
SOCKET s,
char FAR * buf,
int len,
int flags,
struct sockaddr FAR * from,
int FAR * fromlen
);
From this function, How to know through which local network interface I am receiving the reply or able to ping?
Thanks.

Related

How to use socket_t (socket reference) to get ip and port in Network Kernel Extension of Mac OS

I am writing the Socket filter in which most of call back functions for socket filtering(struct sflt_filter) have input parameter socket_t so as shown below:
errno_t ( *sf_bind_func)(
void *cookie,
socket_t so,
const struct sockaddr *to);
Need to get Port and IP from socket_t so.
Is anyone have the idea how to do this?
Got the solution in OSx and iOS Kernel programming Book.
function sock_getsockname() do the job. Find following code snippet:
unsigned char addrStr[256];
struct sockaddr_in addrLcl;
sock_getsockname(so, (struct sockaddr *)&addrLcl, sizeof(addrLcl));
inet_ntop(AF_INET, &addrLcl.sin_addr, (char *)addrStr, sizeof(addrStr));
printf("SFWebSecBind() : <%s> Hex:<%X>", addrStr, ntohs(addrLcl.sin_port));

How do I replicate a C++ cast of Windows SOCKADDR_BTH to a SOCKADDR in Rust?

I am writing a Bluetooth utility on Windows in Rust, using winsock2 (but happy to use any other libraries) and have hit a roadblock. The following C++ sample from the Windows Bluetooth Connection Sample is what I am using as a reference:.
SOCKADDR_BTH SockAddrBthServer;
# code to set SockAddrBthServer vals in here
connect(LocalSocket, (struct sockaddr *) &SockAddrBthServer, sizeof(SOCKADDR_BTH));
The structs are defined like so:
typedef struct _SOCKADDR_BTH
{
USHORT addressFamily; // Always AF_BTH
BTH_ADDR btAddr; // Bluetooth device address
GUID serviceClassId; // [OPTIONAL] system will query SDP for port
ULONG port; // RFCOMM channel or L2CAP PSM
} SOCKADDR_BTH, *PSOCKADDR_BTH;
typedef struct sockaddr {
#if (_WIN32_WINNT < 0x0600)
u_short sa_family;
#else
ADDRESS_FAMILY sa_family; // Address family.
#endif //(_WIN32_WINNT < 0x0600)
CHAR sa_data[14]; // Up to 14 bytes of direct address.
} SOCKADDR, *PSOCKADDR, FAR *LPSOCKADDR;
How do I replicate the (struct sockaddr *) &SockAddrBthServer cast from the connect line above in Rust? So far, I'm making use of the winapi, user32 and ws2_32 crates.
Here is the Rust version of the connect function from the ws2_32 crate.
pub unsafe extern "system" fn connect(s: SOCKET, name: *const SOCKADDR, namelen: c_int) -> c_int
You are overthinking the problem. If Windows says it's cool to cast a SOCKADDR_BTH pointer to a SOCKADDR pointer, then just do that. In Rust, you have to add one extra cast to leave the safe world of references and get to a raw pointer, then you can cast that to whatever you want:
use std::mem;
struct SomeErrorType;
fn example(LocalSocket: SOCKET) -> Result<SOCKADDR_BTH, SomeErrorType> {
unsafe {
let SockAddrBthServer: SOCKADDR_BTH = mem::uninitialized();
let retval = connect(
LocalSocket,
&SockAddrBthServer as *const SOCKADDR_BTH as *const SOCKADDR,
mem::size_of::<SOCKADDR_BTH>() as i32,
);
// PERFORM REAL ERROR CHECKING HERE
if retval == 42 {
Ok(SockAddrBthServer)
} else {
Err(SomeErrorType)
}
}
}
(Untested because I don't have a Windows machine handy)
Under the hood, this will only work as long as the initial members of SOCKADDR_BTH exactly match the members of SOCKADDR.

c++ Injected DLL vars getting corrupted

I'm currently trying to add some functionality to a basic server application by injecting a DLL and detouring several functions and I'm having a problem with a stored IP address getting corrupted in-between 2 calls.
First I detour 'accept' and parse some values then enter them into a connection class and add it to a list.
Accept detour function:
std::list<Connection*> ConnectionsList;
SOCKET WINAPI MyAccept(SOCKET s, sockaddr *addr, int *addrlen)
{
...
ConnectionsList.push_back(new Connection(ClientSocket, ipstr));
...
}
connection class:
SOCKET s;
char * ipAddress;
char * playerName;
Connection::Connection(SOCKET sock, char * address)
{
s = sock;
ipAddress = address;
}
I've also detoured 'closesocket' at which point I'd like to remove the socket from the list of connections. If I breakpoint on this function the IP address appears to be corrupted.
Does anyone know why this is happening?
ipAddress = address; will just copy the pointer. If something else changes what it points to, you will be in trouble.
Since this is C++ it might be safest to use a std::string.
std::string ipAdreess;
...
ipAddress = address;
Otherwise, stdcpy into a buffer big enough.
BTW, what deletes all the Connection* from the list?
try to protect your ConnectionList etc static/global variable with a lock.

How does nfq_get_payload structure its return data?

Primarily, I'm trying to get the source address and dest port from the payload of a Netfilter queue payload (The payload is retrieved using the nfq_get_payload function). The following question asks the same thing and gets a correct answer:
How to extract source and destination port number from packet in queue of iptables
Unfortunately, there's no explanation as to why adding 20 and 22 to the address puts you in the right spot to read the info. I assume this is because of the structure of the data (obviously), but if there's a defined structure, what is it?
The documentation doesn't explicitly explain how the data is formatted, only that 'type of data retrieved by this function will depend on the mode set with the nfq_set_mode() function', but then the docs for set_mode don't mention anything about data type and the source doesn't immediately reveal anything.
I feel like this must be something very central to common network programming structs that I'm missing or not understanding.
Notes: nfq_get_payload function: http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Parsing.html#gaf79628558c94630e25dbfcbde09f2933
I managed to figure this out and I'll leave this here for others to find.
The payload starts with an iphdr struct. The iphdr struct has a protocol field, for example tcp, if it is tcp then the data after the iphdr struct is a tcphdr struct, if it is udp, then there's another struct hdr for that, etc for icmp etc.
To access port, assume q_data is a pointer to a nfq_data struct:
unsigned char *data;
nfq_get_payload(q_data, (unsigned char**)&data);
struct iphdr * ip_info = (struct iphdr *)data;
if(ip_info->protocol == IPPROTO_TCP) {
struct tcphdr * tcp_info = (struct tcphdr*)(data + sizeof(*ip_info));
unsigned short dest_port = ntohs(tcp_info->dest);
} else if(ip_info->protocol == IPPROTO_UDP) {
//etc etc
}

Programmatically verify if a UDP port is bound in C/C++

Without attempting to bind it
This should do the trick...
int getsockname(int socket, struct sockaddr *restrict address,
socklen_t *restrict address_len);