1254219Scy/* 2254219Scy * Copyright (C) 2012 by Darren Reed. 3254219Scy * 4254219Scy * See the IPFILTER.LICENCE file for details on licencing. 5254219Scy * 6254219Scy * $Id: connecttcp.c,v 1.3.2.2 2012/07/22 08:04:24 darren_r Exp $ 7254219Scy */ 8254219Scy 9254219Scy#include "ipf.h" 10254219Scy#include <ctype.h> 11254219Scy 12254219Scy/* 13254219Scy * Format expected is one addres per line, at the start of each line. 14254219Scy */ 15254219Scyint 16254219Scyconnecttcp(char *server, int port) 17254219Scy{ 18254219Scy struct sockaddr_in sin; 19254219Scy struct hostent *host; 20254219Scy int fd; 21254219Scy 22254219Scy memset(&sin, 0, sizeof(sin)); 23254219Scy sin.sin_family = AF_INET; 24254219Scy sin.sin_port = htons(port & 65535); 25254219Scy 26254219Scy if (ISDIGIT(*server)) { 27254219Scy if (inet_aton(server, &sin.sin_addr) == -1) { 28254219Scy return -1; 29254219Scy } 30254219Scy } else { 31254219Scy host = gethostbyname(server); 32254219Scy if (host == NULL) 33254219Scy return -1; 34254219Scy memcpy(&sin.sin_addr, host->h_addr_list[0], 35254219Scy sizeof(sin.sin_addr)); 36254219Scy } 37254219Scy 38254219Scy fd = socket(AF_INET, SOCK_STREAM, 0); 39254219Scy if (fd == -1) 40254219Scy return -1; 41254219Scy 42254219Scy if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) { 43254219Scy close(fd); 44254219Scy return -1; 45254219Scy } 46254219Scy 47254219Scy return fd; 48254219Scy} 49