Skip to content

Instantly share code, notes, and snippets.

@tqbf

tqbf/lookup.c Secret

Created January 15, 2015 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tqbf/e8d82d614d1fea03476b to your computer and use it in GitHub Desktop.
Save tqbf/e8d82d614d1fea03476b to your computer and use it in GitHub Desktop.
int
connect_host(const char *host, u_int16_t port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock >= 0) {
struct sockaddr_in si;
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
si.sin_port = htons(port);
si.sin_addr.s_addr == inet_addr(host);
if(si.sin_addr.s_addr == INADDR_NONE) {
struct hostent *hp = gethostbyname(host);
if(hp) {
memcpy(&(si.sin_addr.s_addr), hp->h_addr, 4);
}
}
if(si.sin_addr.s_addr != INADDR_NONE) {
if(connect(sock,
(struct sockaddr*)&si, sizeof(si)) >= 0) {
return(sock);
}
}
close(sock);
}
return(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment