]> git.proxmox.com Git - libgit2.git/blob - src/netops.c
Update Copyright header
[libgit2.git] / src / netops.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
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.
6 */
7 #ifndef _WIN32
8 # include <sys/types.h>
9 # include <sys/socket.h>
10 # include <sys/select.h>
11 # include <sys/time.h>
12 # include <netdb.h>
13 #else
14 # define _WIN32_WINNT 0x0501
15 # include <winsock2.h>
16 # include <Ws2tcpip.h>
17 # ifdef _MSC_VER
18 # pragma comment(lib, "Ws2_32.lib")
19 # endif
20 #endif
21
22
23 #include "git2/errors.h"
24
25 #include "common.h"
26 #include "netops.h"
27 #include "posix.h"
28
29 void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, int fd)
30 {
31 memset(buf, 0x0, sizeof(gitno_buffer));
32 memset(data, 0x0, len);
33 buf->data = data;
34 buf->len = len;
35 buf->offset = 0;
36 buf->fd = fd;
37 }
38
39 int gitno_recv(gitno_buffer *buf)
40 {
41 int ret;
42
43 ret = recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
44 if (ret < 0)
45 return git__throw(GIT_EOSERR, "Failed to receive data: %s", strerror(errno));
46 if (ret == 0) /* Orderly shutdown, so exit */
47 return GIT_SUCCESS;
48
49 buf->offset += ret;
50
51 return ret;
52 }
53
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)
56 {
57 size_t consumed;
58
59 assert(ptr - buf->data >= 0);
60 assert(ptr - buf->data <= (int) buf->len);
61
62 consumed = ptr - buf->data;
63
64 memmove(buf->data, ptr, buf->offset - consumed);
65 memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
66 buf->offset -= consumed;
67 }
68
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)
71 {
72 memmove(buf->data, buf->data + cons, buf->len - buf->offset);
73 memset(buf->data + cons, 0x0, buf->len - buf->offset);
74 buf->offset -= cons;
75 }
76
77 int gitno_connect(const char *host, const char *port)
78 {
79 struct addrinfo *info, *p;
80 struct addrinfo hints;
81 int ret, error = GIT_SUCCESS;
82 GIT_SOCKET s;
83
84 memset(&hints, 0x0, sizeof(struct addrinfo));
85 hints.ai_family = AF_UNSPEC;
86 hints.ai_socktype = SOCK_STREAM;
87
88 ret = getaddrinfo(host, port, &hints, &info);
89 if (ret != 0) {
90 error = GIT_EOSERR;
91 info = NULL;
92 goto cleanup;
93 }
94
95 for (p = info; p != NULL; p = p->ai_next) {
96 s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
97 #ifdef GIT_WIN32
98 if (s == INVALID_SOCKET) {
99 #else
100 if (s < 0) {
101 #endif
102 error = GIT_EOSERR;
103 goto cleanup;
104 }
105
106 ret = connect(s, p->ai_addr, p->ai_addrlen);
107 /* If we can't connect, try the next one */
108 if (ret < 0) {
109 continue;
110 }
111
112 /* Return the socket */
113 error = s;
114 goto cleanup;
115 }
116
117 /* Oops, we couldn't connect to any address */
118 error = git__throw(GIT_EOSERR, "Failed to connect: %s", strerror(errno));
119
120 cleanup:
121 freeaddrinfo(info);
122 return error;
123 }
124
125 int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
126 {
127 int ret;
128 size_t off = 0;
129
130 while (off < len) {
131 errno = 0;
132
133 ret = send(s, msg + off, len - off, flags);
134 if (ret < 0)
135 return git__throw(GIT_EOSERR, "Error sending data: %s", strerror(errno));
136
137 off += ret;
138 }
139
140 return off;
141 }
142
143
144 #ifdef GIT_WIN32
145 int gitno_close(GIT_SOCKET s)
146 {
147 return closesocket(s) == SOCKET_ERROR ? -1 : 0;
148 }
149 #else
150 int gitno_close(GIT_SOCKET s)
151 {
152 return close(s);
153 }
154 #endif
155
156 int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
157 {
158 fd_set fds;
159 struct timeval tv;
160
161 tv.tv_sec = sec;
162 tv.tv_usec = usec;
163
164 FD_ZERO(&fds);
165 FD_SET(buf->fd, &fds);
166
167 /* The select(2) interface is silly */
168 return select(buf->fd + 1, &fds, NULL, NULL, &tv);
169 }
170
171 int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port)
172 {
173 char *colon, *slash, *delim;
174 int error = GIT_SUCCESS;
175
176 colon = strchr(url, ':');
177 slash = strchr(url, '/');
178
179 if (slash == NULL)
180 return git__throw(GIT_EOBJCORRUPTED, "Malformed URL: missing /");
181
182 if (colon == NULL) {
183 *port = git__strdup(default_port);
184 } else {
185 *port = git__strndup(colon + 1, slash - colon - 1);
186 }
187 if (*port == NULL)
188 return GIT_ENOMEM;;
189
190
191 delim = colon == NULL ? slash : colon;
192 *host = git__strndup(url, delim - url);
193 if (*host == NULL) {
194 git__free(*port);
195 error = GIT_ENOMEM;
196 }
197
198 return error;
199 }