Select throwing error WSAENOTSOCK - c++

ISSUE THAT I AM FACING:-
Select(..) API is throwing error when called with FD_SET set containing IPv6 and IPv4 sockets on Win7 Home premium but the same application is running fine on other win7 machine. But if i use only IPv6 or IPv4 socket in FD_SET then it runs fine. So i think, system support both IPv6 and IPv4.
I tried using the server from the link below :-
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738639%28v=vs.85%29.aspx
Even this sample is same error from select call.
Anyone can please help me in getting the reason behind this error and solving this issue.
Thanks!!!

Unfortunately, Windows does not permit mixing sockets from different providers in the same call to select. This is because each provider is permitted to implement its own select handler.
The sockets contained within the fd_set structures must be associated with a single service provider. -- select function
Windows will pick a provider to send the select to (I believe the provider of the first socket in the set), and that provider will not recognize the other provider's sockets. Honestly, you shouldn't be using select anyway -- it's just provided for compatibility with the BSD sockets interface. It's definitely not the "Windows way".

Related

How do applications handle ports if they are not open?

I am trying to write an application in c++ using winsock.
I need to handle a case where default port used by the app is not open to use then what is fallback mechanism?
Server or client?
In the server, if the requested port is already taken, you log an error and stop. This is why ports are often stored in configuration.
For the client, if you cannot connect to the specified port, you log an error too. Its not much different from not being able to connect to any other network resource, like a URL in your browser.
That completely depends on you.
There is a mechanism called port knocking: The application just tries a range of ports until it finds one it can bind to. Obviously server AND client have to do this to find each other if the default port did not work out.
You could also just display an error message and let the user decide on how to deal with this.
Or you specify a fallback port in a config file...
Just to give you an idea.

Boost exception while trying to ping

I am using the sample of given link for ping the given list of IP
http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/example/icmp/ping.cpp
when ever i try ping a IP i am getting this exception at the initialization time itself
Exception open: An attempt was made to access a socket in a way forbidden by its access permissions
What causing this problem?
i am running it on windows 7 64 bit
For security reasons, some systems limit the use of raw sockets to users with administrator permissions. Boost.Asio's ICMP socket implementation uses raw sockets, and thus requires the application to run with administrator permissions. The Windows documentation states:
Raw sockets offer the capability to manipulate the underlying transport, so they can be used for malicious purposes that pose a security threat. Therefore, only members of the Administrators group can create sockets of type SOCK_RAW on Windows 2000 and later.
Windows provides an IcmpSendEcho() family of functions that can be used to send and handle ICMP echo request/response without requiring administrative permissions, as the raw sockets are not exposed.

Listing all Network Connections, then managing one

I'm developing an application in C++ for Windows 7, that sets up a TFTP server to communicate to an embedded device. The device is supposed to be connected directly to the Ethernet port, and it expects a TFTP server on 192.168.1.19 static address.
To simplify user experience, I want the following scenario:
present a list of all network connections registered in the system to the user (same list as in "Control Panel\Network and Internet\Network Connections")
allow to choose one connection
memorize its IPv4 configuration
set IPv4 configuration to 192.168.1.19 and 255.255.255.0
after TFTP job done, restore original IPv4 configuration
The problem I encountered is a mix of the following:
when no Ethernet cable plugged, neither of GetAdaptersInfo, GetInterfaceInfo, GetAdaptersAddresses list the connection I'm interested in;
even if I get that connection, I'm not sure which API to use to set DHCP on/off, static IP, network mask.
I tried using netsh, and it works fine, but I don't want to run it from my app, because I don't want to deal with non-English characters in the connection names.
Suggestions welcome,
Thanks!
===== edit #1
Tried WMI query "Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'" -- no IP address in the feedback, although I can see it in the Control Panel connection properties.
Queries involving Win32_NetworkConnection don't return anything at all.
I ended up using:
"Select * From Win32_NetworkAdapter Where PhysicalAdapter='True' AND AdapterTypeID=0" query to list adapters/connection names/guid - works pretty good
registry to read configuration via guid
netsh to do configuration via connection name. Yes, I had much fun with locales.
I frankly didn't find a more reliable way to work with disconnected connections. Hope this helps. If anyone has better solutions - I'd be glad to see them!

Special routing for application via WinSocks or any another API?

I want to route packets sended from my application to another gateway but i don't know what options i must change via setsockopt.
I can't just add entries to routing table for specific hosts because second application also must connect to these hosts but via default gateway. Gateways have different public IPs.
I found topic about set "source routing" options via IP_OPTIONS socket option (though without format), but as i understand this option only for routers not for my network adapter.
Also i cannot add second adapter and connect it to same LAN.
Any suggestions how i can send packets on per application basis? May be routing API
P.S.: Windows versions are 32-bit XP SP3, 32- and 64 bit Windows 7 and one 64-bit Windows Server 2008 R2.
Short answer : this is really hard, are you sure you want to do it ? It is not possible directly via set sock opt. Most probably there are easier choices by adding routes manually with the command line utility "route". That will the best choice if your final destination is a known IP or network and that normal traffic can be routed through the other gateway as well.
Longer answer : if you are a code ninja, you have the following two options, good luck, I have no idea whether that will work in practice:
use raw sockets and IP_HDRINCL to do source routing by hand (ie add an option in the IP header with the address of your proxy). See here for an intro.
you may be able to code a Winsock LSP (introduction here) that you can use to intercept some packets and re route them.

Windows Peer to Peer Global_ Group without third party ipv6 tunnel

I have been trying to develop a peer to peer application that uses Micosoft's Peer to Peer Group library. Basing my work on the Creating a Group Chat Application acrticle on msdn. This works fine for local groups and will also work for global groups if I have a thrid party tunnel adapter installed such as the gogo6 client. However from a few things I have read it seems like I should be able to get things working through the Teredo tunnel adapter that comes built into Windows.
I have tried various things and can now access ipv6 only sites (eg ipv6.google.com) without the gogo6 tunnel running, but I can't seem to find any other peers in my global group through this method.
I have added a rule allowing trafic (including edge traversal) for the application in the Windows Firewall and also opened the following ports to incoming and outgoing trafic.
tcp 3587
udp 3540, 1900
From the samples I have read it seems like it should just work, but it doesn't. I did read that to use teredo in an application you had to specificaly enable it. The only way I have found to do this is when opening the socket, but the group api does all of that for you so I have no known way of controlling that.
Some Teredo clients are unreachable due to symmetric router problem. Teredo can work only behind 90% of routers. Gogo6 uses TSP which tunnels the packet to gogo6 infrastructure from where it reaches ipv6 internet.
I don't think Teredo supports IPv6 multicast. If the Peer to Peer Group library uses multicast under the hood, I think that's the problem. I could never find any confirmation that multicast is unsupported by Teredo; but in my own testing setsockopt(ADD_GROUP_MEMBERSHIP) would always fail when the interface ID was a Teredo interface.