0

it works when iam trying to bind to my auto-config link local address. but when i try to bind to some manually assigned IPv6 address bind() fails with 10049 error. here is the code

bool StartServer()
{
  const char *Ip = " fe80::216:76ff:fe12:2661%4";
  SHORT nIPFamily = -1;
  int dSock;
  union 
 {
   struct sockaddr_in  saddr4;
   struct sockaddr_in6  saddr6;
 }ServerIP;

memset(&ServerIP,0,sizeof(ServerIP));

 if(strstr(Ip,":"))
 {
   UCHAR MyIp[16];
   USHORT ScopeId = 0;
   ServerIP.saddr6.sin6_family = nIPFamily = AF_INET6;
   ServerIP.saddr6.sin6_port = htons( nPort );
   StringToAddress( Ip, MyIp ); //common function to convert string to address (16byte)
   memcpy( ServerIP.saddr6.sin6_addr.u.Byte, MyIp,16 );
   sscanf(strstr(Ip,"%")+1,"%d",&ScopeId);
   ServerIP.saddr6.sin6_scope_id = ScopeId;
   nAddLen = sizeof(sockaddr_in6);
   psockaddr = &ServerIP.saddr6;
 }

 if ((dSock = socket(nIPFamily, SOCK_STREAM, 0)) < 0)
 {
   AfxMessageBox("Failed to create server socket");
   return -1;
 }
 if (bind (dSock, (sockaddr*)psockaddr, nAddLen) < 0)
 {
   char errmsg[128];
   sprintf(errmsg,"Bind Failed with error %d",WSAGetlastError());
   AfxMessageBox(errmsg);
   return -1;
 }

Please advice.I'm stuck with this for over 2 days now!!!

  • What do you change `fe80::216:76ff:fe12:2661%4` to when you try the manually assigned address? The code that deals with the scope ID should be different. (should probably go away, actually.) – mpontillo Jan 16 '12 at 09:47
  • Mike actually Iam setting some addresses like this '2000::3'. In this case bind fails!! but when i set address in the link-local add format starting with fe80 it works. But one of my colleagues had asigned some addresses other than in the format of link-local add. for two system connected back to back and he was able to send and recieve traffic with sockets. and this is worrying me!!! may be some problem with my IPv6 configuration!!! – user1151217 Jan 16 '12 at 13:53
  • Your `sscanf()` will fail trying to parse the scope id with that input. – mpontillo Jan 16 '12 at 19:14
  • What is the error returned by bind()? – user31986 Jul 08 '15 at 20:08

2 Answers2

0

Suggestions:

Q: does in6addr_any work?

Q: Do you happen to be running on an ipv6-aware PC (running Vista or Win7, for example)?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

only link-local addresses use scope id don't add it when using global unicast

edder
  • 41
  • 1