diff options
author | Trumeet <yuuta@yuuta.moe> | 2021-02-26 13:36:37 -0800 |
---|---|---|
committer | Trumeet <yuuta@yuuta.moe> | 2021-02-26 13:36:37 -0800 |
commit | 3c80cd2b7744a366d39de684c6451285b94a4d1b (patch) | |
tree | 9b267dc4afb3cd2e7278167678c4badf946e0245 /net | |
download | minebridge-3c80cd2b7744a366d39de684c6451285b94a4d1b.tar minebridge-3c80cd2b7744a366d39de684c6451285b94a4d1b.tar.gz minebridge-3c80cd2b7744a366d39de684c6451285b94a4d1b.tar.bz2 minebridge-3c80cd2b7744a366d39de684c6451285b94a4d1b.zip |
First Commit
Diffstat (limited to 'net')
-rw-r--r-- | net/curlutils.c | 28 | ||||
-rw-r--r-- | net/curlutils.h | 14 | ||||
-rw-r--r-- | net/net.c | 60 | ||||
-rw-r--r-- | net/net.h | 12 |
4 files changed, 114 insertions, 0 deletions
diff --git a/net/curlutils.c b/net/curlutils.c new file mode 100644 index 0000000..69187cc --- /dev/null +++ b/net/curlutils.c @@ -0,0 +1,28 @@ +#include "../common.h" +#include "curlutils.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stddef.h> + +size_t curl_callback(void *ptr, size_t size, size_t nmemb, CURLBody *userp) +{ + size_t newLen = userp->len + size * nmemb; + userp->ptr = realloc(userp->ptr, newLen + 1); + if(userp->ptr == NULL) { + fprintf(stderr, _("Cannot allocate memory.\n")); + return 0; + } + memcpy(userp->ptr + userp->len, ptr, size * nmemb); + userp->ptr[newLen] = '\0'; + userp->len = newLen; + return size * nmemb; +} + +void curlbody_setup(CURLBody *body) +{ + body->len = 0; + body->ptr = calloc(1, sizeof(char)); + body->ptr[0] = '\0'; +} diff --git a/net/curlutils.h b/net/curlutils.h new file mode 100644 index 0000000..1d5a5a8 --- /dev/null +++ b/net/curlutils.h @@ -0,0 +1,14 @@ +#ifndef _CURLUTILS_H +#define _CURLUTILS_H + +#include <stddef.h> + +typedef struct curlbody { + char *ptr; + size_t len; +} CURLBody; + +size_t curl_callback(void *ptr, size_t size, size_t nmemb, CURLBody *userp); +void curlbody_setup(CURLBody *body); + +#endif // _CURLUTILS_H diff --git a/net/net.c b/net/net.c new file mode 100644 index 0000000..d999f8e --- /dev/null +++ b/net/net.c @@ -0,0 +1,60 @@ +/* + * Adopted from mcrcon, Copyright (c) 2012-2020, Tiiffi <tiiffi at gmail>. + * https://github.com/Tiiffi/mcrcon/tree/b02201d689b3032bc681b28f175fd3d83d167293 + */ + +#include "net.h" +#include "../common.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <sysexits.h> +#include <unistd.h> + +int net_connect(const char *host, const char *port, int *out) +{ + int sd; + struct addrinfo hints; + struct addrinfo *server_info, *p; + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + int ret = getaddrinfo(host, port, &hints, &server_info); + if(ret) + { + fprintf(stderr, _("Cannot resolve host %s: %s.\n"), host, strerror(ret)); + return EX_IOERR; + } + for (p = server_info; p != NULL; p = p->ai_next) + { + sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sd == -1) continue; + ret = connect(sd, p->ai_addr, p->ai_addrlen); + if(ret == -1) + { + net_close(sd); + continue; + } + break; + } + if(p == NULL) + { + fprintf(stderr, _("Cannot connect to %s:%s : %s.\n"), host, port, strerror(errno)); + freeaddrinfo(server_info); + return EX_IOERR; + } + freeaddrinfo(server_info); + *out = sd; + return 0; +} + +void net_close(int sd) +{ + close(sd); +} diff --git a/net/net.h b/net/net.h new file mode 100644 index 0000000..0bf5b62 --- /dev/null +++ b/net/net.h @@ -0,0 +1,12 @@ +/* + * Adopted from mcrcon, Copyright (c) 2012-2020, Tiiffi <tiiffi at gmail>. + * https://github.com/Tiiffi/mcrcon/tree/b02201d689b3032bc681b28f175fd3d83d167293 + */ + +#ifndef _NET_H +#define _NET_H + +int net_connect(const char *host, const char *port, int *out); +void net_close(int sd); + +#endif // _NET_H |