Blame src/socket_stream.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "netops.h"
Packit Service 20376f
#include "stream.h"
Packit Service 20376f
#include "socket_stream.h"
Packit Service 20376f
Packit Service 20376f
#ifndef _WIN32
Packit Service 20376f
#	include <sys/types.h>
Packit Service 20376f
#	include <sys/socket.h>
Packit Service 20376f
#	include <sys/select.h>
Packit Service 20376f
#	include <sys/time.h>
Packit Service 20376f
#	include <netdb.h>
Packit Service 20376f
#	include <netinet/in.h>
Packit Service 20376f
#       include <arpa/inet.h>
Packit Service 20376f
#else
Packit Service 20376f
#	include <winsock2.h>
Packit Service 20376f
#	include <ws2tcpip.h>
Packit Service 20376f
#	ifdef _MSC_VER
Packit Service 20376f
#		pragma comment(lib, "ws2_32")
Packit Service 20376f
#	endif
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
static void net_set_error(const char *str)
Packit Service 20376f
{
Packit Service 20376f
	int error = WSAGetLastError();
Packit Service 20376f
	char * win32_error = git_win32_get_error_message(error);
Packit Service 20376f
Packit Service 20376f
	if (win32_error) {
Packit Service 20376f
		giterr_set(GITERR_NET, "%s: %s", str, win32_error);
Packit Service 20376f
		git__free(win32_error);
Packit Service 20376f
	} else {
Packit Service 20376f
		giterr_set(GITERR_NET, str);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
#else
Packit Service 20376f
static void net_set_error(const char *str)
Packit Service 20376f
{
Packit Service 20376f
	giterr_set(GITERR_NET, "%s: %s", str, strerror(errno));
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static int close_socket(GIT_SOCKET s)
Packit Service 20376f
{
Packit Service 20376f
	if (s == INVALID_SOCKET)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	if (SOCKET_ERROR == closesocket(s))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (0 != WSACleanup()) {
Packit Service 20376f
		giterr_set(GITERR_OS, "winsock cleanup failed");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
#else
Packit Service 20376f
	return close(s);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int socket_connect(git_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	struct addrinfo *info = NULL, *p;
Packit Service 20376f
	struct addrinfo hints;
Packit Service 20376f
	git_socket_stream *st = (git_socket_stream *) stream;
Packit Service 20376f
	GIT_SOCKET s = INVALID_SOCKET;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	/* on win32, the WSA context needs to be initialized
Packit Service 20376f
	 * before any socket calls can be performed */
Packit Service 20376f
	WSADATA wsd;
Packit Service 20376f
Packit Service 20376f
	if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "winsock init failed");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2) {
Packit Service 20376f
		WSACleanup();
Packit Service 20376f
		giterr_set(GITERR_OS, "winsock init failed");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	memset(&hints, 0x0, sizeof(struct addrinfo));
Packit Service 20376f
	hints.ai_socktype = SOCK_STREAM;
Packit Service 20376f
	hints.ai_family = AF_UNSPEC;
Packit Service 20376f
Packit Service 20376f
	if ((ret = p_getaddrinfo(st->host, st->port, &hints, &info)) != 0) {
Packit Service 20376f
		giterr_set(GITERR_NET,
Packit Service 20376f
			   "failed to resolve address for %s: %s", st->host, p_gai_strerror(ret));
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (p = info; p != NULL; p = p->ai_next) {
Packit Service 20376f
		s = socket(p->ai_family, p->ai_socktype | SOCK_CLOEXEC, p->ai_protocol);
Packit Service 20376f
Packit Service 20376f
		if (s == INVALID_SOCKET)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		/* If we can't connect, try the next one */
Packit Service 20376f
		close_socket(s);
Packit Service 20376f
		s = INVALID_SOCKET;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Oops, we couldn't connect to any address */
Packit Service 20376f
	if (s == INVALID_SOCKET && p == NULL) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to connect to %s", st->host);
Packit Service 20376f
		p_freeaddrinfo(info);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	st->s = s;
Packit Service 20376f
	p_freeaddrinfo(info);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
ssize_t socket_write(git_stream *stream, const char *data, size_t len, int flags)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t ret;
Packit Service 20376f
	size_t off = 0;
Packit Service 20376f
	git_socket_stream *st = (git_socket_stream *) stream;
Packit Service 20376f
Packit Service 20376f
	while (off < len) {
Packit Service 20376f
		errno = 0;
Packit Service 20376f
		ret = p_send(st->s, data + off, len - off, flags);
Packit Service 20376f
		if (ret < 0) {
Packit Service 20376f
			net_set_error("Error sending data");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		off += ret;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return off;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
ssize_t socket_read(git_stream *stream, void *data, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t ret;
Packit Service 20376f
	git_socket_stream *st = (git_socket_stream *) stream;
Packit Service 20376f
Packit Service 20376f
	if ((ret = p_recv(st->s, data, len, 0)) < 0)
Packit Service 20376f
		net_set_error("Error receiving socket data");
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int socket_close(git_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	git_socket_stream *st = (git_socket_stream *) stream;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	error = close_socket(st->s);
Packit Service 20376f
	st->s = INVALID_SOCKET;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void socket_free(git_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	git_socket_stream *st = (git_socket_stream *) stream;
Packit Service 20376f
Packit Service 20376f
	git__free(st->host);
Packit Service 20376f
	git__free(st->port);
Packit Service 20376f
	git__free(st);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_socket_stream_new(git_stream **out, const char *host, const char *port)
Packit Service 20376f
{
Packit Service 20376f
	git_socket_stream *st;
Packit Service 20376f
Packit Service 20376f
	assert(out && host);
Packit Service 20376f
Packit Service 20376f
	st = git__calloc(1, sizeof(git_socket_stream));
Packit Service 20376f
	GITERR_CHECK_ALLOC(st);
Packit Service 20376f
Packit Service 20376f
	st->host = git__strdup(host);
Packit Service 20376f
	GITERR_CHECK_ALLOC(st->host);
Packit Service 20376f
Packit Service 20376f
	if (port) {
Packit Service 20376f
		st->port = git__strdup(port);
Packit Service 20376f
		GITERR_CHECK_ALLOC(st->port);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	st->parent.version = GIT_STREAM_VERSION;
Packit Service 20376f
	st->parent.connect = socket_connect;
Packit Service 20376f
	st->parent.write = socket_write;
Packit Service 20376f
	st->parent.read = socket_read;
Packit Service 20376f
	st->parent.close = socket_close;
Packit Service 20376f
	st->parent.free = socket_free;
Packit Service 20376f
	st->s = INVALID_SOCKET;
Packit Service 20376f
Packit Service 20376f
	*out = (git_stream *) st;
Packit Service 20376f
	return 0;
Packit Service 20376f
}