diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-12 17:12:52 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-12 17:12:52 +0200 |
| commit | cd2faf64b2e995dee42e7e3911325d372c6604a1 (patch) | |
| tree | e0ab608cdd7005e759b570a5a4ec2031191bafb5 /src/socket.c | |
| parent | 54f78d73ec378fe1d62a696edeb2ddc5a59e2669 (diff) | |
| download | mqtt-cd2faf64b2e995dee42e7e3911325d372c6604a1.tar.gz mqtt-cd2faf64b2e995dee42e7e3911325d372c6604a1.zip | |
Add all the stuff
Diffstat (limited to 'src/socket.c')
| -rw-r--r-- | src/socket.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c new file mode 100644 index 0000000..91999b2 --- /dev/null +++ b/src/socket.c @@ -0,0 +1,92 @@ +#include "socket.h" + +#include <string.h> +#include <stdio.h> +#include <assert.h> + +#if defined(_WIN32) +#error not implemented yet +#define WIN32_MEAN_AND_LEAN 1 +#include <windows.h> +#else +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <unistd.h> +#include <arpa/inet.h> +#endif + +int SocketConnect(const char *host, short port) +{ + struct addrinfo hints, *servinfo, *p; + int rv; + char portstr[6]; + int sock; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + assert(snprintf(portstr, sizeof(portstr), "%hu", port) < (int) sizeof(portstr)); + + if ((rv = getaddrinfo(host, portstr, &hints, &servinfo)) != 0) + { + return -1; + } + + for (p = servinfo; p != NULL; p = p->ai_next) + { + if ((sock = socket(p->ai_family, p->ai_socktype, + p->ai_protocol)) == -1) + { + continue; + } + + if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) + { + close(sock); + continue; + } + + break; + } + + freeaddrinfo(servinfo); + + if (p == NULL) + { + return -1; + } + + return sock; +} + +int SocketDisconnect(int sock) +{ +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} + +int SocketSendAll(int sock, const char *buf, size_t *len) +{ + size_t total = 0; + int rv; + size_t remaining = *len; + + while (remaining > 0) + { + if ((rv = send(sock, buf+total, remaining, 0)) == -1) + { + break; + } + total += rv; + remaining -= rv; + } + + *len = total; + + return rv == -1 ? -1 : 0; +} |
