]>
git.proxmox.com Git - libgit2.git/blob - src/netops.c
2 * Copyright (C) 2009-2012 the libgit2 contributors
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 # include <sys/types.h>
9 # include <sys/socket.h>
10 # include <sys/select.h>
11 # include <sys/time.h>
14 # define _WIN32_WINNT 0x0501
15 # include <winsock2.h>
16 # include <Ws2tcpip.h>
18 # pragma comment(lib, "Ws2_32.lib")
23 #include "git2/errors.h"
29 void gitno_buffer_setup(gitno_buffer
*buf
, char *data
, unsigned int len
, int fd
)
31 memset(buf
, 0x0, sizeof(gitno_buffer
));
32 memset(data
, 0x0, len
);
39 int gitno_recv(gitno_buffer
*buf
)
43 ret
= recv(buf
->fd
, buf
->data
+ buf
->offset
, buf
->len
- buf
->offset
, 0);
45 return git__throw(GIT_EOSERR
, "Failed to receive data: %s", strerror(errno
));
46 if (ret
== 0) /* Orderly shutdown, so exit */
54 /* Consume up to ptr and move the rest of the buffer to the beginning */
55 void gitno_consume(gitno_buffer
*buf
, const char *ptr
)
59 assert(ptr
- buf
->data
>= 0);
60 assert(ptr
- buf
->data
<= (int) buf
->len
);
62 consumed
= ptr
- buf
->data
;
64 memmove(buf
->data
, ptr
, buf
->offset
- consumed
);
65 memset(buf
->data
+ buf
->offset
, 0x0, buf
->len
- buf
->offset
);
66 buf
->offset
-= consumed
;
69 /* Consume const bytes and move the rest of the buffer to the beginning */
70 void gitno_consume_n(gitno_buffer
*buf
, size_t cons
)
72 memmove(buf
->data
, buf
->data
+ cons
, buf
->len
- buf
->offset
);
73 memset(buf
->data
+ cons
, 0x0, buf
->len
- buf
->offset
);
77 int gitno_connect(const char *host
, const char *port
)
79 struct addrinfo
*info
, *p
;
80 struct addrinfo hints
;
81 int ret
, error
= GIT_SUCCESS
;
84 memset(&hints
, 0x0, sizeof(struct addrinfo
));
85 hints
.ai_family
= AF_UNSPEC
;
86 hints
.ai_socktype
= SOCK_STREAM
;
88 ret
= getaddrinfo(host
, port
, &hints
, &info
);
95 for (p
= info
; p
!= NULL
; p
= p
->ai_next
) {
96 s
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
98 if (s
== INVALID_SOCKET
) {
106 ret
= connect(s
, p
->ai_addr
, p
->ai_addrlen
);
107 /* If we can't connect, try the next one */
112 /* Return the socket */
117 /* Oops, we couldn't connect to any address */
118 error
= git__throw(GIT_EOSERR
, "Failed to connect: %s", strerror(errno
));
125 int gitno_send(GIT_SOCKET s
, const char *msg
, size_t len
, int flags
)
133 ret
= send(s
, msg
+ off
, len
- off
, flags
);
135 return git__throw(GIT_EOSERR
, "Error sending data: %s", strerror(errno
));
145 int gitno_close(GIT_SOCKET s
)
147 return closesocket(s
) == SOCKET_ERROR
? -1 : 0;
150 int gitno_close(GIT_SOCKET s
)
156 int gitno_select_in(gitno_buffer
*buf
, long int sec
, long int usec
)
165 FD_SET(buf
->fd
, &fds
);
167 /* The select(2) interface is silly */
168 return select(buf
->fd
+ 1, &fds
, NULL
, NULL
, &tv
);
171 int gitno_extract_host_and_port(char **host
, char **port
, const char *url
, const char *default_port
)
173 char *colon
, *slash
, *delim
;
174 int error
= GIT_SUCCESS
;
176 colon
= strchr(url
, ':');
177 slash
= strchr(url
, '/');
180 return git__throw(GIT_EOBJCORRUPTED
, "Malformed URL: missing /");
183 *port
= git__strdup(default_port
);
185 *port
= git__strndup(colon
+ 1, slash
- colon
- 1);
191 delim
= colon
== NULL
? slash
: colon
;
192 *host
= git__strndup(url
, delim
- url
);