#include #include #include #include #include using namespace std; const int BUFFER_SIZE{1000}; int main() { string hostname{"mirkwood.cs.edinboro.edu"}; string service{"ssh"}; char buffer[BUFFER_SIZE]; cout << "Enter the host name : "; cin >> hostname; cout << endl; cout << "Enter the service name : "; cin >> service; cout << endl; addrinfo *res; addrinfo hints; // Kerrisk or the man page reccomends this memset(&hints, 0, sizeof(hints)); // give it some clue what I am looking for. hints.ai_flags |= AI_CANONNAME; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; int errCode; errCode= getaddrinfo(hostname.c_str(), service.c_str(), &hints, &res); if (0 != errCode) { cout << "Get Address Error: " << gai_strerror(errCode) << endl; } else { addrinfo * next{res}; while (next != nullptr) { if (res->ai_canonname != nullptr) { cout << "\tName : " << res->ai_canonname << endl; } int port; cout << "\tFamily: " ; switch(res->ai_family) { case AF_INET6: // ya, I should rewrite this, but I am out of time so live with // it. I want different definitions of ptr and address but // but apparently the cases of the switch family are in the // same scope. { sockaddr_in6 * ptr; in6_addr *address; ptr = reinterpret_cast(next->ai_addr); address = &(ptr->sin6_addr); port = ntohs(ptr->sin6_port); cout << "\tIPv6" << endl; inet_ntop(res->ai_family, address, buffer, BUFFER_SIZE); cout << "\tIP : " << buffer << endl; cout << "\tPort: " << port << endl; } break; case AF_INET: { sockaddr_in * ptr; in_addr *address; cout << "\tIPv4" << endl; ptr = reinterpret_cast(next->ai_addr); address = &(ptr->sin_addr); port = ntohs(ptr->sin_port); inet_ntop(res->ai_family, address, buffer, BUFFER_SIZE); cout << "\tIP : " << buffer << endl; cout << "\tPort: " << port << endl; } break; default: cout << "Unknown" << endl; } next = next->ai_next; } cout << "\tSocket Type: "; switch (res->ai_socktype) { case SOCK_STREAM: cout << " stream " << endl; break; case SOCK_DGRAM: cout << " datagram " << endl; break; default: cout << "Unknown" << endl; } freeaddrinfo(res); } return 0; }