Instantly share code, notes, and snippets.

@tqbf /lookup.c Secret
Created Jan 15, 2015

Embed
What would you like to do?
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