Last active
December 26, 2015 06:49
-
-
Save nqn/7110345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <netdb.h> | |
int main( int argc, char *argv[] ) | |
{ | |
if (argc != 2) { | |
fprintf(stderr, "Usage validate-localname <hostname>\n"); | |
return EXIT_FAILURE; | |
} | |
char *hostname = argv[1]; | |
struct addrinfo *res; | |
struct addrinfo hints; | |
const char* probePort = "5051"; // We can perhaps pick a port from the port-range in resources? | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
if (getaddrinfo(hostname, probePort, &hints, &res) != 0) { | |
fprintf(stderr, "Unknown hostname '%s'\n", hostname); | |
return EXIT_FAILURE; | |
} | |
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
if (sockfd < 0) { | |
fprintf(stderr, "Cannot open socket\n"); | |
return EXIT_FAILURE; | |
} | |
if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0) { | |
fprintf(stderr, "Cannot bind on hostname '%s'\n", hostname); | |
return EXIT_FAILURE; | |
} | |
printf("Hostname '%s' is valid!\n", hostname); | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment