Skip to content
Snippets Groups Projects
Commit d743e6ea authored by Wesley W. Terpstra's avatar Wesley W. Terpstra
Browse files

Windows seems to think standards are more suggestions than actual rules.

POSIX says return -1 on error. Windows returns whatever it feels like.
  => listen/accept/getaddrinfo all now compare for equality to 0
Windows also needs a call to WSAStartup before networking
parent 4913ce99
No related merge requests found
...@@ -37,6 +37,10 @@ ...@@ -37,6 +37,10 @@
#include "../memory/memory.h" #include "../memory/memory.h"
#include "../format/format.h" #include "../format/format.h"
#ifdef __WIN32
#include <winsock2.h>
#endif
const char* eb_status(eb_status_t code) { const char* eb_status(eb_status_t code) {
switch (code) { switch (code) {
case EB_OK: return "success"; case EB_OK: return "success";
...@@ -62,6 +66,10 @@ eb_status_t eb_socket_open(uint16_t abi_code, const char* port, eb_width_t suppo ...@@ -62,6 +66,10 @@ eb_status_t eb_socket_open(uint16_t abi_code, const char* port, eb_width_t suppo
struct eb_socket_aux* aux; struct eb_socket_aux* aux;
eb_status_t status; eb_status_t status;
uint8_t link_type; uint8_t link_type;
#ifdef __WIN32
WORD wVersionRequested;
WSADATA wsaData;
#endif
/* Does the library support the application? */ /* Does the library support the application? */
if (abi_code != EB_ABI_CODE) if (abi_code != EB_ABI_CODE)
...@@ -94,6 +102,15 @@ eb_status_t eb_socket_open(uint16_t abi_code, const char* port, eb_width_t suppo ...@@ -94,6 +102,15 @@ eb_status_t eb_socket_open(uint16_t abi_code, const char* port, eb_width_t suppo
return EB_OOM; return EB_OOM;
} }
#ifdef __WIN32
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
eb_free_socket(socketp);
eb_free_socket_aux(auxp);
return EB_FAIL;
}
#endif
/* Allocate the transports */ /* Allocate the transports */
status = EB_OK; status = EB_OK;
first_transport = EB_NULL; first_transport = EB_NULL;
...@@ -205,6 +222,10 @@ eb_status_t eb_socket_close(eb_socket_t socketp) { ...@@ -205,6 +222,10 @@ eb_status_t eb_socket_close(eb_socket_t socketp) {
eb_free_transport(transportp); eb_free_transport(transportp);
} }
#ifdef __WIN32
WSACleanup();
#endif
eb_free_socket(socketp); eb_free_socket(socketp);
eb_free_socket_aux(auxp); eb_free_socket_aux(auxp);
return EB_OK; return EB_OK;
......
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __WIN32
#include <winsock2.h>
#endif
struct eb_client { struct eb_client {
struct eb_transport udp_transport; struct eb_transport udp_transport;
struct eb_link udp_slave; struct eb_link udp_slave;
...@@ -87,12 +91,24 @@ int main(int argc, const char** argv) { ...@@ -87,12 +91,24 @@ int main(int argc, const char** argv) {
uint8_t buffer[16384]; uint8_t buffer[16384];
uint8_t len_buf[2]; uint8_t len_buf[2];
fd_set rfds; fd_set rfds;
#ifdef __WIN32
WORD wVersionRequested;
WSADATA wsaData;
#endif
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Syntax: %s <proxy-port>\n", argv[0]); fprintf(stderr, "Syntax: %s <proxy-port>\n", argv[0]);
return 1; return 1;
} }
#ifdef __WIN32
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
perror("Cannot initialize winsock");
return 1;
}
#endif
if ((err = eb_posix_tcp_open(&tcp_transport, argv[1])) != EB_OK) { if ((err = eb_posix_tcp_open(&tcp_transport, argv[1])) != EB_OK) {
perror("Cannot open TCP port"); perror("Cannot open TCP port");
return 1; return 1;
......
...@@ -62,7 +62,7 @@ eb_posix_sock_t eb_posix_ip_open(int type, const char* port) { ...@@ -62,7 +62,7 @@ eb_posix_sock_t eb_posix_ip_open(int type, const char* port) {
hints.ai_protocol = protocol; /* TCP/UDP over IP to exclude non IPv* protocols */ hints.ai_protocol = protocol; /* TCP/UDP over IP to exclude non IPv* protocols */
hints.ai_flags = AI_PASSIVE; /* Suitable for binding a socket */ hints.ai_flags = AI_PASSIVE; /* Suitable for binding a socket */
if (getaddrinfo(0, port?port:"0", &hints, &match) < 0) if (getaddrinfo(0, port?port:"0", &hints, &match) != 0)
return -1; return -1;
for (i = match; i; i = i->ai_next) { for (i = match; i; i = i->ai_next) {
...@@ -118,7 +118,7 @@ socklen_t eb_posix_ip_resolve(const char* prefix, const char* address, int type, ...@@ -118,7 +118,7 @@ socklen_t eb_posix_ip_resolve(const char* prefix, const char* address, int type,
hints.ai_protocol = protocol; /* TCP/UDP over IP to exclude non IPv* protocols */ hints.ai_protocol = protocol; /* TCP/UDP over IP to exclude non IPv* protocols */
hints.ai_flags = 0; hints.ai_flags = 0;
if (getaddrinfo(host, port, &hints, &match) < 0) if (getaddrinfo(host, port, &hints, &match) != 0)
return -1; return -1;
memcpy(out, match->ai_addr, match->ai_addrlen); memcpy(out, match->ai_addr, match->ai_addrlen);
......
...@@ -41,7 +41,7 @@ eb_status_t eb_posix_tcp_open(struct eb_transport* transportp, const char* port) ...@@ -41,7 +41,7 @@ eb_status_t eb_posix_tcp_open(struct eb_transport* transportp, const char* port)
sock = eb_posix_ip_open(SOCK_STREAM, port); sock = eb_posix_ip_open(SOCK_STREAM, port);
if (sock == -1) return EB_BUSY; if (sock == -1) return EB_BUSY;
if (listen(sock, 5) < 0) { if (listen(sock, 5) != 0) {
eb_posix_ip_close(sock); eb_posix_ip_close(sock);
return EB_ADDRESS; return EB_ADDRESS;
} }
...@@ -75,7 +75,7 @@ eb_status_t eb_posix_tcp_connect(struct eb_transport* transportp, struct eb_link ...@@ -75,7 +75,7 @@ eb_status_t eb_posix_tcp_connect(struct eb_transport* transportp, struct eb_link
sock = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP); sock = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) return EB_FAIL; if (sock == -1) return EB_FAIL;
if (connect(sock, (struct sockaddr*)&sa, len) < 0) { if (connect(sock, (struct sockaddr*)&sa, len) != 0) {
eb_posix_ip_close(sock); eb_posix_ip_close(sock);
return EB_FAIL; return EB_FAIL;
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment