how can i bind socket to ipv6 address? - c++

I am trying to port ipv4 applications to ipv6 but I can't bind the socket to the ipv6 address.
The problem is here:
err=bind(listening, (sockaddr*)&hint, sizeof(hint));
The err should be 0 but in this code it returns -1. What is going wrong ?
SOCKET listening = socket(AF_INET6, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
cerr << "Can't create a socket! Quitting" << endl;
return;
}
int err;
// Bind the ip address and port to a socket
sockaddr_in6 hint;
hint.sin6_family = AF_INET6;
hint.sin6_flowinfo = 0;
hint.sin6_port = htons(54000);
hint.sin6_addr = in6addr_any;
err=bind(listening, (sockaddr*)&hint, sizeof(hint)); //<======= here

Rather than populate the sockaddr_in6 manually, you can (and should) use getaddrinfo() instead and let it allocate a properly filled sockaddr_in6 for you, eg:
int err;
SOCKET listening = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (listening == INVALID_SOCKET)
{
err = WSAGetLastError(); // or errno on non-Windows platforms...
cerr << "Can't create a socket! Error " << err << ". Quitting" << endl;
return;
}
// Bind the ip address and port to a socket
addrinfo hint = {};
hint.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
hint.ai_family = AF_INET6;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
addrinfo *res;
err = getaddrinfo("::0", "54000", &hint, &res);
if (err != 0)
{
cerr << "Can't get address to bind the socket! Error " << err << ". Quitting" << endl;
closesocket(listening); // or close() on non-Windows platforms...
return;
}
err = bind(listening, res->ai_addr, res->ai_addrlen);
if (err == SOCKET_ERROR)
{
err = WSAGetLastError(); // or errno on non-Windows platforms...
cerr << "Can't bind the socket! Error " << err << ". Quitting" << endl;
freeaddrinfo(res);
closesocket(listening); // or close() on non-Windows platforms...
return;
}
freeaddrinfo(res);
...

This might depend on your platform, but on Linux since 2.4 the sockaddr_in6 structure also contains a sin6_scope_id member for defining the IPv6 scope, and since the variable hint is on the stack, it's got random data in it.
The IPv6 scope describes what kind of address it is: unicast, multicast, link local, and a few others, and I have only a drive-by knowledge of them. But if there's garbage in there, it could be a thing.
Recommend ruling this out as an issue by either hard-setting sin6_scope_id to zero, or (better) just zero out the entire sockaddr_in6 structure before assigning stuff to it; I've long done this with my sockaddr_in variables just to be sure I didn't end up with junk I didn't want.
memset(&hint, 0, sizeof hint);
And yes, the errno is really important.

Related

IPv6 client can't connect to IPv6 server

My program is client server IPv6. the client can't make connection to the server? The client and server must use loop-back address
the problem in this code its can't connect to the server
SOCKET sock = socket(AF_INET6, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
sockaddr_in6 hint;
hint.sin6_family = AF_INET6;
hint.sin6_port = htons(port);
hint.sin6_addr = in6addr_any;
// Connect to server
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR)
{
cerr << "Can't connect to server, Err #" << WSAGetLastError()
<< endl;
closesocket(sock);
WSACleanup();
return;
}
A client TCP socket cannot connect() to in6addr_any. A TCP server can bind() to in6addr_any so it can then listen() on all available local IPv6 network interfaces with a single SOCKET. But the client must connect() to a real IPv6 address that the server is actually listening on (such as in6addr_loopback if the client is running on the same machine as the server. Your server can use GetAdaptersInfo() or GetAdaptersAddresses() to discover what its local IP addresses actually are that are valid for the client to connect() to).
Also, you need to zero out the sockaddr_in6 struct completely. sockaddr_in6 has sin6_flowinfo and sin6_scope_id fields that you are not populating, so they will have random values from the stack. sin6_scope_id in particular will affect connect()'s ability to use the correct network interface to connect to the server.
SOCKET sock = socket(AF_INET6, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
sockaddr_in6 hint = {};
hint.sin6_family = AF_INET6;
hint.sin6_port = htons(port);
inet_pton(AF_INET6, "server IPv6 address here", &(hint.sin6_addr));
// Connect to server
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
...
Consider using getaddrinfo() instead. Let the OS allocate a properly-populated sockaddr_in6 for you, which you can then pass as-is to connect() (similarly to how I explained to you in your previous question for bind()).
addrinfo hint = {};
hint.ai_family = AF_INET6;
hint.ai.socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
addrinfo *res;
err = getaddrinfo("server hostname or IPv6 address here", "server port here", &hint, &res);
if (err != 0)
{
cerr << "Can't get address to connect, Err #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
SOCKET sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == INVALID_SOCKET)
{
cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
freeaddrinfo(res);
WSACleanup();
return;
}
// Connect to server
int connResult = connect(sock, res->ai_addr, res->ai_addrlen);
if (connResult == SOCKET_ERROR)
{
cerr << "Can't connect to server, Err #" << WSAGetLastError() << endl;
closesocket(sock);
freeaddrinfo(res);
WSACleanup();
return;
}
freeaddrinfo(res);
...

Socket connect error c socket

I made a simple client program who connect to server via port 80;
int v=connect(mysocket,(struct sockaddr *)&server,sizeof(server));
if(v==SOCKET_ERROR){
cout<<"error connecting to server";
}
if (v==0) cout<<"connected"<<endl;
its says connect return 0 if success.
but i get the error;
can you please tell me when i must use htonl or htons i used only server.sin_port=htons(80);
should i use server.sin_addr.s_addr=inet_addr("someip_ignorethis"); or i must use
server.sin_addr.s_addr=htonl(inet_addr("someip_ignorethis"));
what is the problem WHY AND WHEN i need to use host to network conversation,how does it make my program portable???.
what socket i must use? socket version 2,2?
THNAKS FOR THE HELP!
I get 10038 ERROR HELP FIX MY CODE pastebin.com/4pdqsGqW
If you had bothered to use your debugger and debug the code yourself, you would have found that your mySocket variable is always 0, because you are not initializing it correctly.
This line:
if (mysocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==INVALID_SOCKET){
Is effectively the same as this:
if (mysocket=(socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)==INVALID_SOCKET)){
If socket() succeeds, ==INVALID_SOCKET evaluates as false, so 0 is assigned to mysocket. Read up on Operator Precedence. The == operator has a higher precedence than the = operator.
To fix it, change that line to this instead:
if ((mysocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){
Or better, get out of the habit of assigning and comparing a variable in the same statement:
mysocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (mysocket==INVALID_SOCKET){
Also, if you had bothered to pay attention to your compiler's output messages, you would have seen that your "CONNECTED!" message is code that is never reached, because it is inside the curly braces for when connect() fails, but there is a return before you print the message.
Try this code instead:
#include <winsock2.h>
#include <iostream.h>
#include <windows.h>
//#define portnumber 80
using namespace std;
//Winsock Library
int main(int argc, char* argv[])
{
WSADATA ws = {0};
int v = WSAStartup(MAKEWORD(2,2), &ws);
if (v != 0)
{
cout << "error initialising winsock: " << v << endl;
getchar();
return 1;
}
cout << "winsock started" << endl;
SOCKET mysocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (mysocket == INVALID_SOCKET)
{
cout << "error creating socket: " << WSAGetLastError() << endl;
getchar();
return 1;
}
cout << "socket created" << endl;
struct sockaddr_in server = {0};
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("84.95.234.174");
//cout << inet_ntoa(server.sin_addr) << endl;
server.sin_port = htons(80);
if (connect(mysocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "error connecting to server: " << WSAGetLastError() << endl;
getchar();
return 1;
}
cout << "CONNECTED!" << endl;
getchar();
closesocket(mysocket);
return 0;
}
10038 is WSAENOTSOCK. Clearly the value of socket is invalid.
You should have found all that out for yourself before you posted, and looked up what the error meant as well.
Personally, I use this and it never fails me..
struct addrinfo *it = nullptr, *result = nullptr;
getaddrinfo(Address.c_str(), nullptr, nullptr, &result);
for (it = result; it != nullptr; it = it->ai_next)
{
sockaddr_ipv4 = reinterpret_cast<sockaddr_in*>(it->ai_addr);
Address = inet_ntoa(sockaddr_ipv4->sin_addr);
if (Address != "0.0.0.0") break;
}
freeaddrinfo(result);
And I use:
struct sockaddr_in SockAddr;
memset(&SockAddr, 0, sizeof(SockAddr));
SockAddr.sin_port = htons(Port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = Address == "INADDR_ANY" ? htonl(INADDR_ANY) : inet_addr(Address.c_str());
if (connect(socket, reinterpret_cast<SOCKADDR*>(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
//print error.. clean up..
}
What it does is it checks the address. If it is INADDR_ANY, then it will use htonl. If not, it uses inet_addr to convert the address into an IP. Htonl on the other hand just converts the address into network byte order.

C++ Server Connection Issue

Firstly, the code I will share is the basis of my code(found it in another site, open source), I added functions and threads later to improve.
In the office, we have local network and another 3 computer cannot connect to my server. Especially have a look at that line. 26010 is random port number that I want to listen. According to data I found in the other topics, NULL and 127.0.0.1 are the localhost ip.I tried my own ip number instead of NULL, but it didn't work. I can send data from my client code to other computers, but can't get any connections.
Code is listening connections properly, and can get info if I open another 3 terminal and try to connect it from my computer through my client code. How to fix that?
Thanks in advance.
status = getaddrinfo(NULL, "26010", &host_info, &host_info_list);
int main()
{
int status;
struct addrinfo host_info; // The struct that getaddrinfo() fills up with data.
struct addrinfo *host_info_list; // Pointer to the to the linked list of host_info's.
memset(&host_info, 0, sizeof host_info);
std::cout << "Setting up the structs..." << std::endl;
host_info.ai_family = AF_UNSPEC; // IP version not specified. Can be both.
host_info.ai_socktype = SOCK_STREAM; // Use SOCK_STREAM for TCP or SOCK_DGRAM for UDP.
host_info.ai_flags = AI_PASSIVE;
**status = getaddrinfo(NULL, "26010", &host_info, &host_info_list);**
// getaddrinfo returns 0 on succes, or some other value when an error occured.
// (translated into human readable text by the gai_gai_strerror function).
if (status != 0) std::cout << "getaddrinfo error" << gai_strerror(status) ;
std::cout << "Creating a socket..." << std::endl;
int socketfd ; // The socket descripter
socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
host_info_list->ai_protocol);
if (socketfd == -1) std::cout << "socket error " ;
std::cout << "Binding socket..." << std::endl;
int yes = 1;
status = setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
status = bind(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen);
if (status == -1) std::cout << "bind error" << std::endl ;
std::cout << "Listen()ing for connections..." << std::endl;
status = listen(socketfd, 5);
if (status == -1) std::cout << "listen error" << std::endl ;
int new_sd;
struct sockaddr_storage their_addr;
socklen_t addr_size = sizeof(their_addr);
new_sd = accept(socketfd, (struct sockaddr *)&their_addr, &addr_size);
if (new_sd == -1)
{
std::cout << "listen error" << std::endl ;
}
else
{
std::cout << "Connection accepted. Using new socketfd : " << new_sd << std::endl;
}
std::cout << "Waiting to recieve data..." << std::endl;
ssize_t bytes_recieved;
char incomming_data_buffer[1000];
bytes_recieved = recv(new_sd, incomming_data_buffer,1000, 0);
// If no data arrives, the program will just wait here until some data arrives.
...
std::cout << "send()ing back a message..." << std::endl;
return 0 ;
}
Your problem is your call to getaddrinfo(). This function returns information in the host_info_list member. What you're reading is the hint (host_info) which is not changed by getaddrinfo(). You would need to use host_info_list to read the what's returned by getaddrinfo. You never use it and you never free it (by calling freeaddrinfo).
I am not sure why you'd want to use getaddrinfo() to listen for connection. You can just build the sockaddr yourself. It's easy:
struct sockaddr_in listenAddr;
memset(&listenAddr, 0, sizeof(listenAddr));
/* IPv4 address */
listenAddr.sin_family = AF_INET;
/* your port number */
listenAddr.sin_port = htons(26010);
/* listen on all interfaces */
listenAddr.sin_addr.s_addr = htonl(INADDR_ANY);
/* TCP socket */
socketfd = socket(PF_INET, SOCK_STREAM, 0);
/* your error code, omitted here */
status = bind(SocketFD,(struct sockaddr *) &listenAddr, sizeof(listenAddr))

C++ Sockets recv() syscall returning -1

I'm currently having a problem passing messages between a server and client.
As far as I know, I am properly following best practices for socket programming outlined by Beej's Socket Programming Tutorial.
When I run the two processes, the recv() syscall returns -1 (an error), rather than the number of bytes received. Also when trying to output the buf, there are a bunch of gobbledygook characters. Which makes sense, because of the error.
I'm wondering if someone could steer me in the right direction as to why I am having issues with recv()? The following are relevant code snippets.
Server:
struct sockaddr_storage their_addr;
socklen_t addr_size;
int sockfd, newfd, byte_count, status;
char buf[512];
struct addrinfo hints, *res;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints);
hints.ai_family = PF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// get address info, print stuff if error
if((status = getaddrinfo("nunki.usc.edu", "21957", &hints, &res)) !=0){
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// make a socket:
if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
cout << "socket fail" << endl;
}
// bind the socket to the port
bind(sockfd, res->ai_addr, res->ai_addrlen);
// required output
cout << "Phase1: Login server has TCP port number " << "21957 "
<< "and IP address " << getIPfromHost("nunki.usc.edu") << endl;
// listen for incoming connections
listen(sockfd, 10);
cout << "after listen" << endl;
// halt until receipt
addr_size = sizeof(their_addr);
newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
cout << "after accept" << endl;
// Now that we're connected, we can receive some data
byte_count = recv(sockfd, buf, sizeof buf, 0);
printf("recv()'d %d bytes of data in buf\n", byte_count);
printf("Msg is %s\n", buf);
Client:
struct addrinfo hints, *res;
int sockfd;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("nunki.usc.edu", "21957", &hints, &res);
// make a socket:
if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
cout << "socket fail" << endl;
}
// attempt connection to port
if(connect(sockfd, res->ai_addr, res->ai_addrlen) == -1){
cout << "connect fail" << endl;
}
// send message to server
cout << "sockfd " << sockfd << endl;
int byte_count = send(sockfd, "Hello", 5, 0);
cout << byte_count << endl;
The following is the output for Server:
Phase1: Login server has TCP port number 21957 and IP address 68.181.201.3
after listen
after accept
recv()'d -1 bytes of data in buf
Msg is ÿhÿ?sÈ
Glæ
The following is the output for Client:
sockfd 4
5
You are calling recv on the wrong socket. You need to recv on newfd:
byte_count = recv(newfd, buf, sizeof buf, 0); /* newfd instead of sockfd. */
Now that that's out of the way,
As far as I know, I am properly following best practices for socket
programming
I completely disagree.
You are not checking return statuses for listen, bind, getaddrinfo etc
There's not strerror or perror in your program
You want to recv() using the socket returned from accept()
byte_count = recv(newfd, buf, sizeof buf, 0);
Maybe I shouldn't write this as an answer, but as a comment. Nevertheless, IMHO your use of getaddrinfo() seems wrong to me.
On the client side, it is supposed to be called and then iterated through the results until a connection can be established.
so
struct addrinfo * r2
sockfd = -1;
for (r2=res; r2; r2=r2->ai_next) {
// make a socket:
if((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
continue; // next result
}
// attempt connection to port
if(connect(sockfd, res->ai_addr, res->ai_addrlen) == -1){
close(sockfd);
sockfd = -1;
continue;
}
}
if (sockfd == -1) {
// do error handling
}
In this way, you can check all possible connections.
On the server side, it is rather unusual to use getaddrinfo(). Normally, you would create an IPv6 socket and enable it to listen for IPv4 as well by using setsockopt() to unset the IPV6_V6ONLY flag. In this way, the socket listens to both IPv6 and IPv4. (Alas, not on WIndows XP AFAIK.)
The sockfd is just useed for listening the clients, newfd is used for data transmitting.

getpeername sa_data

I want to get the IP address of the socket that's connected to my server socket. I've included my code.
The sa_data doesn't return char[14] of the type "xxx.xxx.xxx.xxx". Instead, it returns [11][1][xxx][xxx][xxx][xxx][][][][][][][]...[]. Can anyone help?
int InitialResult;
char SendMessage[1024];
char ReceiveMessage[1024];
WSADATA WSAData;
addrinfo Hints;
addrinfo *Result;
SOCKET ListenSocket;
SOCKET ClientSocket;
ZeroMemory(&Hints, sizeof(Hints));
Hints.ai_family = AF_INET;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_protocol = IPPROTO_TCP;
Hints.ai_flags = AI_PASSIVE;
while(1)
{
InitialResult = WSAStartup(MAKEWORD(2,2), &WSAData);
if (InitialResult != 0)
{
cout << "WSA start up failed.";
}
InitialResult = getaddrinfo(NULL, portnumber, &Hints, &Result);
if (InitialResult != 0)
{
cout << "Get address information failed.";
WSACleanup();
}
ListenSocket = socket(Result->ai_family, Result->ai_socktype, Result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
cout << "Socket initialization failed.";
WSACleanup();
}
InitialResult = bind(ListenSocket, Result->ai_addr, (int)Result->ai_addrlen);
if (InitialResult == SOCKET_ERROR)
{
cout << "Bind failed." << portnumber;
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
WSACleanup();
}
InitialResult = listen(ListenSocket, SOMAXCONN);
if (InitialResult == SOCKET_ERROR)
{
cout << "Listen failed." << portnumber;
closesocket(ListenSocket);
ListenSocket = INVALID_SOCKET;
WSACleanup();
}
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Socket accept failed." << portnumber;
WSACleanup();
}
sockaddr test;
int a = sizeof(test);
cout << "getpeername() return value : " << getpeername(ClientSocket, &test, &a) << endl;
cout << "test.sa_data : " << test.sa_data;
StartReceive(ReceiveMessage, ClientSocket);
strcpy(SendMessage,"Congratulations!! You have successfully transfered some data to G DA.");
StartSend(SendMessage, ClientSocket);
StartSend(portnumber, ClientSocket);
InitialResult = shutdown(ClientSocket, SD_SEND);
if (InitialResult == SOCKET_ERROR)
{
cout << "Shut down failed." << portnumber;
closesocket(ClientSocket);
WSACleanup();
}
closesocket(ClientSocket);
closesocket(ListenSocket);
WSACleanup();
}
Second and third arguments of accept(2) allow you to get that information without extra call to getpeername(2). Then you need to convert binary address to string representation with something like inet_ntoa(3).
Why do you think WSADATA should be a char [14]? It's a struct as shown e.g. in these docs.
And BTW, in the various error cases you check, you should terminate the whole function (e.g. with a return), while what you're doing appears to be just to continue, after some message, with structures &c known to be invalid...
Edit: oh I see, when you say sa_data you mean test.sa_data, and another answer explained why it's not what you expect. Leaving this answer in anyway since at least the second paragraph is pretty important (and still relevant;-).
The sa_data doesn't return char[14] of
the type "xxx.xxx.xxx.xxx"
It isn't specified to do that, so no wonder it doesn't.
You need to look into sockaddr_in and sockaddr_in6.