How Windows routing works on application level? - windows

Imagine I have Windows TCP socket. And the application connects this socket once on startup. And then sends/receives TCP traffic for long time.
Windows has ability to route IP traffic. Imagine you have multiple network adaptors and you have to set static routing for your application that the traffic goes to a particular NIC.
The question is - will Windows waste CPU cycles to route TCP socket connection only or will it route every IP packet?
I am counting microseconds and I need to know precisely - will be there CPU overhead on sending / receiving the traffic or connection only ?

I assume by "routing" you mean the process of looking at the local routing table to decide where an outgoing packet should be sent. This is decided first by which router to use, and second which interface to use to get to that router.
If you have established a static route, "routing" must still occur for the system to see that route. This consists of a table lookup which takes just a few dozen machine instructions. It is absolutely negligible compared to the cost of copying the packet around.
Keep in mind that binding a socket to a network interface is not the same as entering static rules into the routing table, and that a network interface is not the same as a Network Interface Controller (NIC). This is important when considering overhead, because the effect of binding or routing to a particular network interface, may be that the packet gets copied extra times which will create substantial overhead.
It is possible to contrive a scenario in which a packet is transmitted on the LAN, re-read by the same computer that transmitted it, then transmitted again through a different NIC to the correct router. Most often, the best performance will be had by binding to INADDR_ANY (address 0.0.0.0) and letting the routing tables handle the optimization for you.
Binding to a particular network interface should only be done if you need to ensure that a particular IP address is used for sending and receiving. Static routing to a particular NIC seems unlikely to produce useful results unless the local routing is already broken in some way. Otherwise, interfering with the normal routing process just risks adding to the overhead.

Related

What do you understand by ip based telecommunications?

What do you understand by ip based telecommunications? Does it includes CDMA, UMTS, LTE, HSPA, IP telephony?
Thank you.
Well, there is a lot of theory and information behind this, but, in a few words, ip based communications are those who uses the IP (Internet Protocol). This means:
The connected devices must have an IP number
The connected devices send and receive data as datagrams (also known as packets)
They might rely on bigger network approaches like TCP/IP stack or OSI model
So, everything with an IP number/address uses an IP based communication, and, as far as I know, CDMA, UMTS, LTE and HSPA don't use IP's to send data. They are more likely to be the infrastructure of telecommunications (layer 1 and 2 in OSI model, and Layer 1 in TCP/IP), they care about how to physically link devices in a network, and IP cares about how to use those links to send data through it.
I'd recommend you to read about TCP and OSI model

Why is packet-sniffing possible?

I can't wrap my head around how packet-sniffers can be used by anyone on the network.
I know very little about how networks work, but let me put it this way: suppose the mailman comes around delivering a package to my doorstep. Why is is that I'm able to rifle though all his other packages and look around? Shouldn't the mailman only hand me packages that are mine?
To quote William Pursell's comment, which he should have made an answer and should have expanded:
The mailman does not deliver the letter your doorstep. Instead, he opens your mail and shouts out: "this letter is for <name>. No-one else should listen" and then proceeds to read the letter out loud. –
In the original Ethernet network, there was a shared cable to which all hosts were attached; if a host wanted to send a message to another host, it would transmit the packet on the shared cable, with an Ethernet header with the destination Ethernet address of the other host. All hosts on the cable could, in theory, see the packet. (This was in an era where security was less of a concern; for cases where security was a concern, the packets were encrypted in a fashion that the other host could fairly easily decrypt but that other hosts would have to decrypt in some other more difficult fashion.)
In addition, a packet can be sent to the "broadcast" Ethernet address (all 1's) or a "multicast" Ethernet address (which several hosts are configured to handle); broadcast packets are intended for all hosts on the Ethernet to see, and multicast packets are intended for all hosts in the address's "multicast group" to see.
Normally, an Ethernet adapter would ignore packets that aren't sent to its Ethernet address, to the broadcast Ethernet address, or to a multicast address for which it's configured to receive packets. Most can, however, be put into "promiscuous" mode, where they pass all packets to the host; that mode is used for packet sniffers.
Most current Ethernets are "switched"; instead of a shared cable, there's an Ethernet switch, and hosts plug into the switch with a cable. Packets sent to a particular host's Ethernet address will only be sent out the switch port for that host (unless somebody's configured the hosts to have a "mirror port" on which all traffic is sent, or unless the switch hasn't yet determined which port is the port for that Ethernet address). Broadcast packets are sent to all ports, and multicast packets may be sent to all ports or, if the switch can determine that, to those ports that have adapters configured for the multicast address in question.
Wi-Fi networks are similar, but they're usually protected with encryption, as it's easier for somebody to bring in a laptop and put it into "monitor mode" to sniff on a given channel than it is for somebody to bring in a laptop, configure a switch to have a mirror port (or use some other mechanism to get access to the traffic), and plug the laptop into the appropriate port on the switch.
Generally speaking, with switches you are correct. However the person who owns the switch can intercept your traffic at will (in your example that would be the mail service). Also, sometimes the switch can be fooled into rerouting traffic (someone accepts the package on your behalf and then goes through it).
Furthermore, certain kinds of packets need to be broadcast. For instance ARP packets (where one computer is asking for the ethernet address of another computer specified by IP) get broadcast to all ethernet addresses and therefore can get sniffed.
Generally speaking man-in-the-middle requires someone in the connection chain to be compromised. For instance, your ISP or the company they buy transit from could create a man in the middle attack. (This is also why security in countries oppressive regimes is so difficult, they control the internet and therefore can sniff/man-in-the-middle attack whatever they please). This can also be done by compromising the DNS server you use to point you to a different site that can grab your data and forward your data (or a modified version thereof) on to the true site.
In the good 'ol days hubs were quite common (or even older, everyone shared a piece of coax). In this case it's more like the package gets dropped on the first door, the occupant looks to see if it's theirs, if not, passes it on, if so, copies what's inside and passes the package along. In other words, packet sniffing is actually quite easy.
Yes or more simple way packet sniffing not is good, while you login on the web page you normal use a secret password for verify this is you.
But in case we has a packet sniffer she/he can also see and read what you password is.
And laiter login in the web page as you.
Or in she/he can also modify you data on the road to do something other.
And in the case of internet, the normal way is more the one computer is use to
send a message from in this case Alice to Alice bank.
And in each of this computer ( right side of image ) is this possible
for the use to edit the message if the use want as in this image.
Eva is use for deliver the message to Alice Bank, but she can
can read the message/order and in some case edit this to get the bank
to think Alice want to transfer money to Eva instead of Bob.
In short for protect us against eva to modify the message we can use
hash-algorithm or cryptographer algorithm.

tcp packet arrivat at the application?

In the client-server environment, when client sends a packet (with source ip / dest ip / ports ... etc) requesting "GET /index.php ... etc",
at the server application (daemon) arrives the whole packet (the whole bits of data) including mac, IPs, ports, tcp flags, payload ? Or just the payload ?
Because I don;t understand how the scripts can read remote address (like echo $_SERVER['REMOTE_ADDR']; )
The server machine gets the whole packet. Its kernel and TCP/IP stack receives and processes it. The application server is using a socket to talk to the kernel, which is a higher-layer interface than raw packets; therefore it has a different view. Assuming we are talking about TCP, you will find among other things:
Information from the physical or datalink layer (such as source and destination MAC addresses) is not available on the socket (unless you do very fancy and probably non-portable things).
Some information from the IP & TCP layer is made available so the application can retrieve it using special system calls such as getsockname() and getpeername(). This includes the IP addresses and ports.
The application is not concerned with most of the rest of the information from the IP & TCP layers and it is not made available on the socket. For example, options, window size, checksum, fragment offset.
The application sends and receives data on the socket as though it was a continuous stream of bytes. It does not know or care how the datastream is broken up into small packets each containing a piece of the data.
for the specific case of $_SERVER['REMOTE_ADDR']; which you highlight, this information comes from the aforementioned getpeername() system call. PHP calls this for you and makes the information available.

Win32 sockets - Forcing ip packets to leave physical interfaces when sending to other local interfaces

Summary: I'm trying to create sockets to pass data between two physical interfaces that exist on the same machine, and Win32 sockets always forwards the traffic directly in the kernel instead of pushing through the physical interfaces. Is there any way to disable this behavior, perhaps through device settings, registry tweaks, routing table shenanigans, or socket options? We're using Windows XP SP3.
Some background. I'm attempting to build some completely automated IP tests to exercise our custom IPv4 equipment. We have a large lab of Windows XP machines, and individual physical ethernet interfaces for each device we're connecting to. Our devices are effectively ethernet routers each with their own IPs.
We need to send data out our lab machines, through our devices, then back into the same computer. We will be sending Unicast and Multicast UDP, TCP, and broadcast IP traffic through the devices.
We want (and likely need) the traffic to originate on the same machine it is destined to.
To do this, we configure two separate NICs each with their own IP on their own subnet, for instance NIC #1 with 10.0.0.1/24 and NIC #2 with 10.0.1.1/24. Our devices then act like simple passthrough routers, and have two interfaces, one on the 10.0.0.0/24 subnet, one on the 10.0.1.0/24 subnet, which they just forward packets back and forth from.
To generate our data, we'd like to be able to use Win32 sockets, since it is well-understood, well-supported, what our customers are using, and would probably be the most rapid approach. Packet injection is probably feasible for UDP and broadcast IP, but very likely not so for TCP. I'd entertain ideas that used packet injection, but would strongly prefer standard Win32 sockets.
As stated in the summary, the packets never leave the machine. I've googled like a madman and I've not found much. Any ideas?
Use Windows' command-line ROUTE utility. You can configure it so any IP packet sent to a specific IP address on a specific Subnet gets sent to another IP/device. For example:
route ADD <NIC_1_IP> MASK <NIC_1_SUBNET> <DEVICE_IP_CONNECTED_TO_NIC_2> METRIC 1
route ADD <NIC_2_IP> MASK <NIC_2_SUBNET> <DEVICE_IP_CONNECTED_TO_NIC_1> METRIC 1
Alternatively, if you know the index numbers of the NIC interfaces, you can specify them instead:
route ADD <NIC_1_IP> MASK <NIC_1_SUBNET> METRIC 1 IF <NIC_2_INTF>
route ADD <NIC_2_IP> MASK <NIC_2_SUBNET> METRIC 1 IF <NIC_1_INTF>
This way, whenever a packet is sent to NIC #1's IP, the packet goes to the device connected to NIC #2, which will then pass it on to NIC #1. And vice versa for packets sent to NIC #2's IP.
For instance, this is a useful technique for allowing WireShark to capture local IP traffic if the PC is connected to a network with a router. Packets from one local IP/Port to another local IP/Port can be bounced off the router back to the PC so they travel through physical interfaces that WireShark can monitor (WireShark will see duplicate copies of each local packet - one outbound and one inbound - but you can filter out the duplicates).
Winsock is always going to bring the packet data up into the kernel space and deal with it there. Thats the whole point to a generic API is that any device is dealt with at the same "layer". If you want to stick with Winsock, I don't believe you can (or would want to) work around this behavior.
You can remove some of the buffer copying with TransmitPackets or TransmitFile, but not between two device interfaces.
That being said, are you having a performance issue with the additional buffer coping that Winsock performs? Security concerns?
How about running the endpoints of your tester inside of distinct virtual machines? Then you need only a single piece of hardware, but you'll have separate TCP/IP stacks that don't know each other are local (and most VM solutions pass the packet straight through the host unchanged, I don't think the host is going to grab the packet and send it straight to another VM unless you configure bridging between VMs... but you'll bind each VM to a different physical network adapter).

Simulating a network down to particular process

I am trying to simulate a scenario where connection to the server of one process is down while the connection to another server is up. Just pulling the network cable won't work in my case since I need another process connection to stay up.
Is there any tool for this kind of job? I am on Windows. Thanks!
There's a few layers which you can simulate this at. The easiest would be if your two servers listen on two distinct TCP ports. In that case, you could run two tcp proxies, and stop/pause one when you want to simulate a failure. For Windows I would suggest using tcpTrace to do this.
Another option would be to have the two servers bound to two virtual NICs, which are bridged to the physical NIC. Of course if you have two physical NICs, you could bind each server process to a different physical NIC.
At a lower level, you can ran a WAN simulator. Most simulators allow you to impair specific types of traffic or specific ports. One such simulator is Packetstorm.
One other method which I would suggest is attaching a debugger to one process, and halting all threads on the process with the debugger. Often, a process doesn't die, but gets stuck in garbage collection, or in a loop. As the sockets don't close, many 'high availability' solutions won't automatically failover.
One approach would be to mock the relevant network connection code for the purposes of testing. In this case you would probably want to mock it returning whatever it usually would if the connection was down.
A poor man's approach if you can use sleep/hibernate mode on your machine :
Set an Outbound rule in the Windows Firewall to disallow connection for a particular Program.
Already connected sockets stay connected: put the machine in sleep/hibernate mode for a brief moment to force those sockets to disconnect.
When the system is restored, the program cannot establish new connections.
New connections are made possible as soon as you disable the firewall rule.
Note that it does not simulate network outage because each connection fails immediately with an permission error. But it prevents a process to establish connections.

Resources