How to determine primary network adapter in results from PdhExpandWildCardPath? - c++

My goal is to measure the average Bytes received per second using the Windows performance counter API. My problem is that, when there are multiple network adapters, I don't know which one is the "primary" one (i.e. the one used for Internet and LAN traffic).
I can pass "\Network Interface(*)\Bytes Received/sec" to PdhExpandWildCardPath() and get a list back, but that list seems to be in alphabetical order, not routing order. And the names returned by PdhExpandWildCardPath() don't seem to be the same as the ones the WMI functions use -- Pdh seems to add "Packet Scheduler Miniport" -- so even if I went that route I'm not sure how to correlate the information from the different APIs.

Which network adapter is 'Primary' is a qualitative judgment. A computer might have both onboard wireless 802.11, and wired Ethernet adapters, either of which could be primary at any given time (or even at the same time).
You are probably going to have to use the IP Helper API to determine which you prefer and correlate that with the PerfMon ouput. or you could just pick the one with the most activity and call it primary. That's just the kind of value judgment that PerfMon cannot possibly make, as these stats are transparent to it.

Related

Akka Actor: Path lookup efficiency

I am creating actors that represents physical devices and their state. As devices come online I create them "on demand" by sending and Identify message to the actor's path and then if it does not exist yet I create one. Potentially, there could be several million of these devices.
My concern is that the Identify look-up will take a performance hit as the number of actors increases. Is this a valid concern?
I was considering using a router strategy to segment the actors, but then I found that searching on the path with a wild card for the router yielded ActorIdentities from each router. I assume that a ConsistentHashingRouter would suit this scenario, but before I go down that rabbit hole I just want to make sure I am not optimizing prematurely.
The entity which creates an actor is only its parent (there no other way), which means that that parent actor does not need to use Identify at all, just check context.child(name).isDefined. That is very efficient, although you might want to shard your devices across multiple parents if you really have a massive number of them.

Distributing state across many machines

I'm trying to write up a tool that requires knowledge of the state of other machines in a cluster (local LAN). This is for a network failover/high availability system similar to VRRP and corosync/openais, but I wish to contain more information (such as near real-time speed/performance characteristics) so devices can make more intelligent choices. This means using a protocol more complicated than a predetermine weight-based mechanism: by allowing all clustered machines to see the state of each other, they can communally agree on which is the most suitable to be the master device.
From my searches, I haven't found any (C, C++ or JavaME) libraries that offer a distributed state mechanism. Ideally, I'm looking for something that broadcasts/multicasts each individual machines state periodically so participating machines can build up a global state table and all can see who the master should be. State in this case is arbitrary key/value pairs.
I'd rather not re-invent any wheels so am curious to know if anyone here can point me in the right direction?
If I were you I'd investigate memcached (memcached.org) or one of the nosql variants.
It sounds like Apache ZooKeeper might be a good match. It's distributed, hierarchical key-value store. To quote their Overview page:
ZooKeeper was designed to store coordination data: status information, configuration, location information, etc.
Here's an example of a simple Leader Election recipie, although it would require adaptation to determine a leader by some weighted criterion.
I'm not sure if there is any application for your purpose or not.
But I know that you can write a simple program with MPI library and broadcast any information that you want.
all client's can send their state to root node, and the root node then broadcast the message.
functions that you need for this are:
MPI_Bcast
MPI_Send
MPI_Recv
there is lots of tutorial on C++/MPI on net, just google it!

GeoCoding providers for non-map use

I'm looking for a GeoCoding provider for two purposes:
Address parsing (convert a long String into address components)
Address validation (make sure the address really exists)
I need to support North America addresses first, but keep the door open for international addresses as well.
I won't be displaying this information on a map or in a webapp, which puts me in a bit of a bind because services like Google Maps and Yahoo Maps require you to display any information you look up on their services.
Wikipedia contains a nice list of available geocoding providers here. My question is:
Is there a reliable/easy way to parse an address into component? I'd prefer embedding this logic into my application instead of having to depend on a 3rd-party provider.
Eventually I'll need to add address validation (with a map but not in a webapp). At that point, what do you recommend I do?
Is there a reliable/easy way to parse an address into component? I'd
prefer embedding this logic into my application instead of having to
depend on a 3rd-party provider.
No. You can always try to do it, but it will eventually fail. There is no universal planetary standard for addresses and not every country uses English addresses which add to the complexity of the task. There are 311 millions peoples in the USA and nearly 7 billion people in the world, now think of the different addresses it can represent.
Eventually I'll need to add address validation (with a map but not in
a webapp). At that point, what do you recommend I do?
I would use Google Maps API V3 but since it's against the rules in your case, I would try one of the paid service available out there for address parsing/validation (there are even free ones but they are less reliable). I think it's the best you can do.
In your case the only way to be 100% sure if the address exists and is valid would be to check it manually and then go there physically ;)
Gili, good for you for heeding license restrictions and other important "fine print".
I know you would rather embed the logic/functionality into your application without using an external service, but if you can figure out how to do that without jumping through a bunch of USPS hoopla to do it, kudos.
I work for SmartyStreets where we do both of those things. There's a really easy API called LiveAddress which does what you need... and it performs such that it doesn't seem like you're using a third-party service. I might add also, that usually it is smart business practice to dissociate non-core operations from your internal system, leaving the "black box" aspect of other stuff up to experts in those fields.
Here's some more information about converting a string into address components using LiveAddress.

What is the best way to sync computer time with an internet time server?

I need to get the current time from one of internet time server in my desktop application. I suppose I need something like a request string and a regular expression to get time from any site that user wants (may be with several predefined sites).
Or may be there are some free libraries exist?
Thanks.
This is what the Network Time Protocol was built for. But it's probably something best left to your operating system, lest you end up with duelling applications using different, not-quite-synchronised servers.
See the headings in the link above for UNIX and Windows implementations.
There are free libraries and specifications for how to retrieve time, and the format in which you receive it (so REs are generally unnecessary). You choice depends on the level of precision/accuracy you want.
RFC 868 gives time to the second, which is entirely adequate for a lot of people's purposes. If it's good enough, it's a lot simpler to implement than the others listed below.
RFC 5905 defines the Network Time Protocol. As long as you only want to get the time, not provide it for anybody else, NTP is probably overkill though.
RFC 4330 defines SNTP (Simple NTP), which is a simplified version of NTP for computers that act as "leaf nodes" -- i.e., they retrieve time from elsewhere, but nothing else retrieves the time from them.
The NTP project has free NTP libraries for a number of systems.
This feels like something the OS should do on its own...
There is a protocol on the internet called NTP that returns time from timeservers.
You might want to try looking for a library/class that can use NTP to retrieve time for you.
Or you could try looking at the source code for ntpclient.
Usually, most operating systems embeds such a functionality.
Both Windows and Linux can sync with NTP servers.
If really want to let the user change its timezone, you should look for OS specific API's instead of communicating directly with the time servers then changing the system time. This would be way nicer.
Maybe you can take a look at this code and get some inspiration:
http://web.abnormal.com/~thogard/ntp/ntpdate.c

Windows network packet modification

I'm looking to write a small program which will intercept network packets (on the local machine) and modify them before they go out on the network. I need to be able to modify the headers as well, not just the data.
I've already looked through several possibilities but am unsure which one is best to pursue. There are open source packet filters out there, but filtering only seems to be able to either allow or reject packets, not much else.
The other solution would be to write an NDIS intermediate driver, but writing drivers is a beyond me. Even the simple pass-thru example in the WinDDK is thousands of lines. I'm also not looking forward to having to constantly reinstall a driver and reboot to test my code.
I'd ideally like the program to be self contained, and not rely on the installation of 3rd party drivers/software/whatever.
So if you people could point me in the right direction, throw some helpful links my way, whatever, I'd appreciate it.
Depends what kind of packets do you want to filter/modify.
If you're after application-level filtering, and want to get your hands on HTTP or similar packets, your best bet would probably be an LSP. Note however, following this path has certain disadvantages. First MS seems to be trying to get rid of this technology, and IIRC a part of Windows 7 logo requirements is "no LSP in your product", they seem to be promoting the Windows Filtering Platform. Second, you'd be very surprised with how much trouble you're getting into in terms of 3rd party LSP compatibility. Third, a very dummy LSP is still around 2 KLOC :)
If you're after an IP level packet filtering you'd need to go for a driver.
Windows Filtering Platform provides you with functionality needed in either case. However, it's only available on Windows Vista and later products, so no XP there. Another thing to take into consideration, WFP was only capable of allow/reject packets in user-land, and if you need to modify them, you'd need to go kernel-mode. (At least that what the situation was at the time it appeared, maybe they've improved something by now).
IMHO, If you want to modify packets you'll need something to talk to the hardware, a driver of some kind. If you do not want to use your own, you should get a 3rd party driver to inter-operate with.
For filtering there's libraries like: winpcap or libpcap.
Also have a look here: http://www.ntkernel.com/w&p.php?id=7
Another link: http://bittwist.sourceforge.net/
Hope this helps!
winpcap is only able to filter packets with precompiled conditions. What you need is to write LSP-level network driver. You won't need to reboot every time you reinstall it, but it can really modify packets before they go out to the network.
More info here: http://blogs.msdn.com/wndp/archive/2006/02/09/529031.aspx or here: http://www.microsoft.com/msj/0599/LayeredService/LayeredService.aspx
I'm no expert but I'm looking to do something similar on my LAN. I want to intercept packets form one single fixed IP and modify them before they go to my router then out onto the internet. I also want to capture and modify the returning packets prior to allowing them through to my host. The method I had envisaged was something like this...
ARP poison the host and router so my sniffing machine was having all packets passed through it.
Analyse the packets that I will want to modify in future and look for unique characteristics to those packets so I can catch just them.
Write a macro/script that looked for said characteristic in real-time and then modified it on the fly before sending it on its' way.
I know Cain&Abel for Windows is able (haha) to ARP poison but I'm not sure if it can provide raw dump of packet contents. Wireshark is able to dump all but not sure if it can ARP poison so as just to get what I'm after, if not then I can easily connect the host I want to intercept to my sniffer machine via ethernet and then share the internet via the sniffer so that all packets will go through the sniffer machine anyway.
So step 1 can be accomplished, I don't know if said programs have the ability to filter based on specifics yet but I'm guessing they do.
That's as far as I am with it. Hope this is of help to someone and maybe someone else can take this further?