torrentd

simple torrent daemon
git clone git://git.2f30.org/torrentd
Log | Files | Refs | LICENSE

commit b13eb6063df000d202ae7568db7f2c0d6c826a77
parent 29b8cb7c38ed9c8cdbf1dddfdde3f2cfa25f763f
Author: sin <sin@2f30.org>
Date:   Thu, 17 Dec 2015 16:29:28 +0000

Add sock.c

Diffstat:
MMakefile | 1+
Asock.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -10,6 +10,7 @@ OBJ =\ ben.o\ sbtd.o\ sha1.o\ + sock.o\ tracker.o\ util.o diff --git a/sock.c b/sock.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 Dimitris Papastamos <sin@2f30.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/socket.h> + +#include <netdb.h> + +#include <err.h> +#include <string.h> +#include <unistd.h> + +int +dial(char *host, char *port) +{ + struct addrinfo hints, *res, *r; + int sock; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + if (getaddrinfo(host, port, &hints, &res)) + errx(1, "can't resolve %s", host); + for (r = res; r; r = r->ai_next) { + sock = socket(r->ai_family, r->ai_socktype, 0); + if (sock < 0) + continue; + if (!connect(sock, r->ai_addr, r->ai_addrlen)) + break; + close(sock); + } + freeaddrinfo(res); + if (!r) + errx(1, "cannot connect to %s", host); + return sock; +}