From 177a38d9b0c0b456b32d71f6aae358dd6f898f29 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 2 Jul 2009 01:46:28 -0300 Subject: [PATCH] Added library to do a TCP connection. --- tcp_connect.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ tcp_connect.h | 24 +++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tcp_connect.c create mode 100644 tcp_connect.h diff --git a/tcp_connect.c b/tcp_connect.c new file mode 100644 index 0000000..1f62c97 --- /dev/null +++ b/tcp_connect.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2008-2009 Thadeu Lima de Souza Cascardo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include +#include + +int +tcp_connect (struct addrinfo *ai) +{ + int fd; + fd = socket (ai->ai_family, SOCK_STREAM, 0); + if (fd < 0) + return -1; + if (connect (fd, ai->ai_addr, ai->ai_addrlen) < 0) + { + close (fd); + return -1; + } + return fd; +} + +int +tcp_connect_list (struct addrinfo *ai) +{ + int fd = -1; + for (; ai; ai = ai->ai_next) + { + fd = tcp_connect (ai); + if (fd >= 0) + { + return fd; + } + } + return fd; +} + +int +hc_tcp_connect (char *server, char *service) +{ + struct addrinfo *ai = NULL; + int fd; + if (getaddrinfo (server, service, NULL, &ai) < 0) + return -1; + fd = tcp_connect_list (ai); + freeaddrinfo (ai); + return fd; +} + +#ifdef TEST +int +main (int argc, char **argv) +{ + char *server; + char *service; + int fd; + server = (argc >= 2) ? argv[1] : "localhost"; + service = (argc >= 3) ? argv[2] : "110"; + fd = hc_tcp_connect (server, service); + if (fd > 0) + close (fd); + return 0; +} +#endif diff --git a/tcp_connect.h b/tcp_connect.h new file mode 100644 index 0000000..cb323d9 --- /dev/null +++ b/tcp_connect.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2008 Thadeu Lima de Souza Cascardo + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef HC_TCP_CONNECT_H +#define HC_TCP_CONNECT_H + +int hc_tcp_connect (char *, char*); + +#endif -- 2.20.1