aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs
diff options
context:
space:
mode:
authornkozlovskiy <nmk@ydb.tech>2023-10-12 12:36:15 +0300
committernkozlovskiy <nmk@ydb.tech>2023-10-12 13:05:35 +0300
commitcb22bc788fb52ee6aee5ac0e397e9e2dc6bd670a (patch)
tree23250bbabd243c4880c460d67a8b3cc6533ca2f8 /contrib/libs
parent80508570636c9339ea697ef7f5738543398953df (diff)
downloadydb-cb22bc788fb52ee6aee5ac0e397e9e2dc6bd670a.tar.gz
add contrib/libs/libc_compat/getservbyname for memory sanitizer
Diffstat (limited to 'contrib/libs')
-rw-r--r--contrib/libs/libc_compat/getservbyname/getservbyname.c11
-rw-r--r--contrib/libs/libc_compat/getservbyname/getservbyname_r.c55
-rw-r--r--contrib/libs/libc_compat/getservbyname/lookup.h57
-rw-r--r--contrib/libs/libc_compat/getservbyname/lookup_serv.c113
4 files changed, 236 insertions, 0 deletions
diff --git a/contrib/libs/libc_compat/getservbyname/getservbyname.c b/contrib/libs/libc_compat/getservbyname/getservbyname.c
new file mode 100644
index 0000000000..1fff91d005
--- /dev/null
+++ b/contrib/libs/libc_compat/getservbyname/getservbyname.c
@@ -0,0 +1,11 @@
+#include <netdb.h>
+
+struct servent *getservbyname(const char *name, const char *prots)
+{
+ static struct servent se;
+ static char *buf[2];
+ struct servent *res;
+ if (getservbyname_r(name, prots, &se, (void *)buf, sizeof buf, &res))
+ return 0;
+ return &se;
+}
diff --git a/contrib/libs/libc_compat/getservbyname/getservbyname_r.c b/contrib/libs/libc_compat/getservbyname/getservbyname_r.c
new file mode 100644
index 0000000000..cad6317ab8
--- /dev/null
+++ b/contrib/libs/libc_compat/getservbyname/getservbyname_r.c
@@ -0,0 +1,55 @@
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include "lookup.h"
+
+#define ALIGN (sizeof(struct { char a; char *b; }) - sizeof(char *))
+
+int getservbyname_r(const char *name, const char *prots,
+ struct servent *se, char *buf, size_t buflen, struct servent **res)
+{
+ struct service servs[MAXSERVS];
+ int cnt, proto, align;
+
+ *res = 0;
+
+ /* Don't treat numeric port number strings as service records. */
+ char *end = "";
+ strtoul(name, &end, 10);
+ if (!*end) return ENOENT;
+
+ /* Align buffer */
+ align = -(uintptr_t)buf & ALIGN-1;
+ if (buflen < 2*sizeof(char *)+align)
+ return ERANGE;
+ buf += align;
+
+ if (!prots) proto = 0;
+ else if (!strcmp(prots, "tcp")) proto = IPPROTO_TCP;
+ else if (!strcmp(prots, "udp")) proto = IPPROTO_UDP;
+ else return EINVAL;
+
+ cnt = __lookup_serv(servs, name, proto, 0, 0);
+ if (cnt<0) switch (cnt) {
+ case EAI_MEMORY:
+ case EAI_SYSTEM:
+ return ENOMEM;
+ default:
+ return ENOENT;
+ }
+
+ se->s_name = (char *)name;
+ se->s_aliases = (void *)buf;
+ se->s_aliases[0] = se->s_name;
+ se->s_aliases[1] = 0;
+ se->s_port = htons(servs[0].port);
+ se->s_proto = servs[0].proto == IPPROTO_TCP ? "tcp" : "udp";
+
+ *res = se;
+ return 0;
+}
diff --git a/contrib/libs/libc_compat/getservbyname/lookup.h b/contrib/libs/libc_compat/getservbyname/lookup.h
new file mode 100644
index 0000000000..7dc1fb8109
--- /dev/null
+++ b/contrib/libs/libc_compat/getservbyname/lookup.h
@@ -0,0 +1,57 @@
+#ifndef LOOKUP_H
+#define LOOKUP_H
+
+#include <stdint.h>
+#include <stddef.h>
+#include <features.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#define hidden __attribute__((__visibility__("hidden")))
+
+struct aibuf {
+ struct addrinfo ai;
+ union sa {
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+ } sa;
+ volatile int lock[1];
+ short slot, ref;
+};
+
+struct address {
+ int family;
+ unsigned scopeid;
+ uint8_t addr[16];
+ int sortkey;
+};
+
+struct service {
+ uint16_t port;
+ unsigned char proto, socktype;
+};
+
+#define MAXNS 3
+
+struct resolvconf {
+ struct address ns[MAXNS];
+ unsigned nns, attempts, ndots;
+ unsigned timeout;
+};
+
+/* The limit of 48 results is a non-sharp bound on the number of addresses
+ * that can fit in one 512-byte DNS packet full of v4 results and a second
+ * packet full of v6 results. Due to headers, the actual limit is lower. */
+#define MAXADDRS 48
+#define MAXSERVS 2
+
+hidden int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags);
+hidden int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags);
+hidden int __lookup_ipliteral(struct address buf[static 1], const char *name, int family);
+
+hidden int __get_resolv_conf(struct resolvconf *, char *, size_t);
+hidden int __res_msend_rc(int, const unsigned char *const *, const int *, unsigned char *const *, int *, int, const struct resolvconf *);
+
+hidden int __dns_parse(const unsigned char *, int, int (*)(void *, int, const void *, int, const void *), void *);
+
+#endif
diff --git a/contrib/libs/libc_compat/getservbyname/lookup_serv.c b/contrib/libs/libc_compat/getservbyname/lookup_serv.c
new file mode 100644
index 0000000000..31d407cf28
--- /dev/null
+++ b/contrib/libs/libc_compat/getservbyname/lookup_serv.c
@@ -0,0 +1,113 @@
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+#include "lookup.h"
+
+int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int proto, int socktype, int flags)
+{
+ char line[128];
+ int cnt = 0;
+ char *p, *z = "";
+ unsigned long port = 0;
+
+ switch (socktype) {
+ case SOCK_STREAM:
+ switch (proto) {
+ case 0:
+ proto = IPPROTO_TCP;
+ case IPPROTO_TCP:
+ break;
+ default:
+ return EAI_SERVICE;
+ }
+ break;
+ case SOCK_DGRAM:
+ switch (proto) {
+ case 0:
+ proto = IPPROTO_UDP;
+ case IPPROTO_UDP:
+ break;
+ default:
+ return EAI_SERVICE;
+ }
+ case 0:
+ break;
+ default:
+ if (name) return EAI_SERVICE;
+ buf[0].port = 0;
+ buf[0].proto = proto;
+ buf[0].socktype = socktype;
+ return 1;
+ }
+
+ if (name) {
+ if (!*name) return EAI_SERVICE;
+ port = strtoul(name, &z, 10);
+ }
+ if (!*z) {
+ if (port > 65535) return EAI_SERVICE;
+ if (proto != IPPROTO_UDP) {
+ buf[cnt].port = port;
+ buf[cnt].socktype = SOCK_STREAM;
+ buf[cnt++].proto = IPPROTO_TCP;
+ }
+ if (proto != IPPROTO_TCP) {
+ buf[cnt].port = port;
+ buf[cnt].socktype = SOCK_DGRAM;
+ buf[cnt++].proto = IPPROTO_UDP;
+ }
+ return cnt;
+ }
+
+ if (flags & AI_NUMERICSERV) return EAI_NONAME;
+
+ size_t l = strlen(name);
+
+ FILE *f = fopen("/etc/services", "rb");
+ if (!f) switch (errno) {
+ case ENOENT:
+ case ENOTDIR:
+ case EACCES:
+ return EAI_SERVICE;
+ default:
+ return EAI_SYSTEM;
+ }
+
+ while (fgets(line, sizeof line, f) && cnt < MAXSERVS) {
+ if ((p=strchr(line, '#'))) *p++='\n', *p=0;
+
+ /* Find service name */
+ for(p=line; (p=strstr(p, name)); p++) {
+ if (p>line && !isspace(p[-1])) continue;
+ if (p[l] && !isspace(p[l])) continue;
+ break;
+ }
+ if (!p) continue;
+
+ /* Skip past canonical name at beginning of line */
+ for (p=line; *p && !isspace(*p); p++);
+
+ port = strtoul(p, &z, 10);
+ if (port > 65535 || z==p) continue;
+ if (!strncmp(z, "/udp", 4)) {
+ if (proto == IPPROTO_TCP) continue;
+ buf[cnt].port = port;
+ buf[cnt].socktype = SOCK_DGRAM;
+ buf[cnt++].proto = IPPROTO_UDP;
+ }
+ if (!strncmp(z, "/tcp", 4)) {
+ if (proto == IPPROTO_UDP) continue;
+ buf[cnt].port = port;
+ buf[cnt].socktype = SOCK_STREAM;
+ buf[cnt++].proto = IPPROTO_TCP;
+ }
+ }
+ fclose(f);
+ return cnt > 0 ? cnt : EAI_SERVICE;
+}