]>
git.proxmox.com Git - libgit2.git/blob - src/netops.c
2 * Copyright (C) 2009-2011 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>
13 # define _WIN32_WINNT 0x0501
14 # include <winsock2.h>
15 # include <Ws2tcpip.h>
17 # pragma comment(lib, "Ws2_32.lib")
22 #include "git2/errors.h"
27 void gitno_buffer_setup(gitno_buffer
*buf
, char *data
, unsigned int len
, int fd
)
29 memset(buf
, 0x0, sizeof(gitno_buffer
));
30 memset(data
, 0x0, len
);
37 int gitno_recv(gitno_buffer
*buf
)
41 ret
= recv(buf
->fd
, buf
->data
+ buf
->offset
, buf
->len
- buf
->offset
, 0);
43 return git__throw(GIT_EOSERR
, "Failed to receive data: %s", strerror(errno
));
44 if (ret
== 0) /* Orderly shutdown, so exit */
52 /* Consume up to ptr and move the rest of the buffer to the beginning */
53 void gitno_consume(gitno_buffer
*buf
, const char *ptr
)
57 assert(ptr
- buf
->data
>= 0);
58 assert(ptr
- buf
->data
<= (int) buf
->len
);
60 consumed
= ptr
- buf
->data
;
62 memmove(buf
->data
, ptr
, buf
->offset
- consumed
);
63 memset(buf
->data
+ buf
->offset
, 0x0, buf
->len
- buf
->offset
);
64 buf
->offset
-= consumed
;
67 /* Consume const bytes and move the rest of the buffer to the beginning */
68 void gitno_consume_n(gitno_buffer
*buf
, size_t cons
)
70 memmove(buf
->data
, buf
->data
+ cons
, buf
->len
- buf
->offset
);
71 memset(buf
->data
+ cons
, 0x0, buf
->len
- buf
->offset
);
75 int gitno_connect(const char *host
, const char *port
)
77 struct addrinfo
*info
, *p
;
78 struct addrinfo hints
;
79 int ret
, error
= GIT_SUCCESS
;
82 memset(&hints
, 0x0, sizeof(struct addrinfo
));
83 hints
.ai_family
= AF_UNSPEC
;
84 hints
.ai_socktype
= SOCK_STREAM
;
86 ret
= getaddrinfo(host
, port
, &hints
, &info
);
93 for (p
= info
; p
!= NULL
; p
= p
->ai_next
) {
94 s
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
96 if (s
== INVALID_SOCKET
) {
104 ret
= connect(s
, p
->ai_addr
, p
->ai_addrlen
);
105 /* If we can't connect, try the next one */
110 /* Return the socket */
115 /* Oops, we couldn't connect to any address */
116 error
= git__throw(GIT_EOSERR
, "Failed to connect: %s", strerror(errno
));
123 int gitno_send(GIT_SOCKET s
, const char *msg
, size_t len
, int flags
)
131 ret
= send(s
, msg
+ off
, len
- off
, flags
);
133 return git__throw(GIT_EOSERR
, "Error sending data: %s", strerror(errno
));
142 int gitno_close(GIT_SOCKET s
)
144 return closesocket(s
) == SOCKET_ERROR
? -1 : 0;
147 int gitno_close(GIT_SOCKET s
)
153 int gitno_select_in(gitno_buffer
*buf
, long int sec
, long int usec
)
162 FD_SET(buf
->fd
, &fds
);
164 /* The select(2) interface is silly */
165 return select(buf
->fd
+ 1, &fds
, NULL
, NULL
, &tv
);
168 int gitno_extract_host_and_port(char **host
, char **port
, const char *url
, const char *default_port
)
170 char *colon
, *slash
, *delim
;
171 int error
= GIT_SUCCESS
;
173 colon
= strchr(url
, ':');
174 slash
= strchr(url
, '/');
177 return git__throw(GIT_EOBJCORRUPTED
, "Malformed URL: missing /");
180 *port
= git__strdup(default_port
);
182 *port
= git__strndup(colon
+ 1, slash
- colon
- 1);
188 delim
= colon
== NULL
? slash
: colon
;
189 *host
= git__strndup(url
, delim
- url
);