Initialise the winsock DLL Windows wants us to initialise the networking DLL before we're allowed to send data through a socket. Call WSASetup and WSACleanup if GIT_WIN32 is defined. Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
diff --git a/src/netops.c b/src/netops.c
index 54aaa46..c8fe376 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -77,7 +77,7 @@ int gitno_connect(const char *host, const char *port)
struct addrinfo *info, *p;
struct addrinfo hints;
int ret, error = GIT_SUCCESS;
- int s;
+ GIT_SOCKET s;
memset(&hints, 0x0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
@@ -92,7 +92,11 @@ int gitno_connect(const char *host, const char *port)
for (p = info; p != NULL; p = p->ai_next) {
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+#ifdef GIT_WIN32
+ if (s == INVALID_SOCKET) {
+#else
if (s < 0) {
+#endif
error = GIT_EOSERR;
goto cleanup;
}
@@ -109,19 +113,21 @@ int gitno_connect(const char *host, const char *port)
}
/* Oops, we couldn't connect to any address */
- error = GIT_EOSERR;
+ error = git__throw(GIT_EOSERR, "Failed to connect: %s", strerror(errno));
cleanup:
freeaddrinfo(info);
return error;
}
-int gitno_send(int s, const char *msg, size_t len, int flags)
+int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
{
int ret;
size_t off = 0;
while (off < len) {
+ errno = 0;
+
ret = send(s, msg + off, len - off, flags);
if (ret < 0)
return git__throw(GIT_EOSERR, "Error sending data: %s", strerror(errno));
diff --git a/src/netops.h b/src/netops.h
index 0d962ef..b0425ae 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -10,7 +10,7 @@
#ifndef GIT_WIN32
typedef int GIT_SOCKET;
#else
-typedef unsigned int GIT_SOCKET;
+typedef SOCKET GIT_SOCKET;
#endif
typedef struct gitno_buffer {
@@ -26,7 +26,7 @@ void gitno_consume(gitno_buffer *buf, const char *ptr);
void gitno_consume_n(gitno_buffer *buf, size_t cons);
int gitno_connect(const char *host, const char *port);
-int gitno_send(int s, const char *msg, size_t len, int flags);
+int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags);
int gitno_select_in(gitno_buffer *buf, long int sec, long int usec);
int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port);
diff --git a/src/transport-http.c b/src/transport-http.c
index 70086ad..3ee4025 100644
--- a/src/transport-http.c
+++ b/src/transport-http.c
@@ -52,6 +52,9 @@ typedef struct {
enum last_cb last_cb;
char *content_type;
char *service;
+#ifdef GIT_WIN32
+ WSADATA wsd;
+#endif
} transport_http;
static int gen_request(git_buf *buf, const char *url, const char *host, const char *service)
@@ -76,7 +79,8 @@ static int gen_request(git_buf *buf, const char *url, const char *host, const ch
static int do_connect(transport_http *t, const char *service)
{
git_buf request = GIT_BUF_INIT;
- int s = -1, error;
+ int error;
+ int s;
const char *url, *prefix;
char *host = NULL, *port = NULL;
@@ -362,6 +366,10 @@ static int http_close(git_transport *transport)
if (error < 0)
return git__throw(GIT_EOSERR, "Failed to close the socket: %s", strerror(errno));
+#ifdef GIT_WIN32
+ WSACleanup();
+#endif
+
return GIT_SUCCESS;
}
@@ -388,6 +396,9 @@ static void http_free(git_transport *transport)
int git_transport_http(git_transport **out)
{
transport_http *t;
+#ifdef GIT_WIN32
+ int ret;
+#endif
t = git__malloc(sizeof(transport_http));
if (t == NULL)
@@ -402,5 +413,13 @@ int git_transport_http(git_transport **out)
*out = (git_transport *) t;
+#ifdef GIT_WIN32
+ ret = WSAStartup(MAKEWORD(2,2), &t->wsd);
+ if (ret != 0) {
+ http_free(*out);
+ return git__throw(GIT_EOSERR, "Winsock init failed");
+ }
+#endif
+
return GIT_SUCCESS;
}
diff --git a/src/transport_git.c b/src/transport_git.c
index a1c54ca..42503e1 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -22,10 +22,13 @@
typedef struct {
git_transport parent;
- int socket;
+ GIT_SOCKET socket;
git_vector refs;
git_remote_head **heads;
git_transport_caps caps;
+#ifdef GIT_WIN32
+ WSADATA wsd;
+#endif
} transport_git;
/*
@@ -68,7 +71,7 @@ static int gen_proto(char **out, int *outlen, const char *cmd, const char *url)
return GIT_SUCCESS;
}
-static int send_request(int s, const char *cmd, const char *url)
+static int send_request(GIT_SOCKET s, const char *cmd, const char *url)
{
int error, len;
char *msg = NULL;
@@ -91,7 +94,7 @@ cleanup:
*/
static int do_connect(transport_git *t, const char *url)
{
- int s = -1;
+ GIT_SOCKET s;
char *host, *port;
const char prefix[] = "git://";
int error, connected = 0;
@@ -525,6 +528,10 @@ static void git_free(git_transport *transport)
git_pkt_free(p);
}
+#ifdef GIT_WIN32
+ WSACleanup();
+#endif
+
git_vector_free(refs);
free(t->heads);
free(t->parent.url);
@@ -534,6 +541,9 @@ static void git_free(git_transport *transport)
int git_transport_git(git_transport **out)
{
transport_git *t;
+#ifdef GIT_WIN32
+ int ret;
+#endif
t = git__malloc(sizeof(transport_git));
if (t == NULL)
@@ -554,5 +564,13 @@ int git_transport_git(git_transport **out)
*out = (git_transport *) t;
+#ifdef GIT_WIN32
+ ret = WSAStartup(MAKEWORD(2,2), &t->wsd);
+ if (ret != 0) {
+ git_free(*out);
+ return git__throw(GIT_EOSERR, "Winsock init failed");
+ }
+#endif
+
return GIT_SUCCESS;
}