99 lines
1.8 KiB
C
99 lines
1.8 KiB
C
#pragma once
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#include <sys/clock.h>
|
|
#include <sys/param.h>
|
|
#include <sys/cpuset.h>
|
|
#include <errno.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
|
|
#define inline inline __attribute__((unused))
|
|
|
|
#define S2US (1000000)
|
|
|
|
static inline uint64_t
|
|
get_time_us()
|
|
{
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return ts.tv_sec * S2US + ts.tv_nsec / 1000;
|
|
}
|
|
|
|
|
|
static inline void
|
|
cpulist_to_cpuset(char *cpulist, cpuset_t *cpuset)
|
|
{
|
|
char *cpu = strtok(cpulist, ",");
|
|
CPU_ZERO(cpuset);
|
|
|
|
while (cpu != nullptr) {
|
|
CPU_SET(atoi(cpu), cpuset);
|
|
cpu = strtok(nullptr, ",");
|
|
}
|
|
}
|
|
|
|
static inline int
|
|
split_kvstr(char * str, const char * delim, char * key, int klen, char * val, int vlen)
|
|
{
|
|
char* token = strtok(str, delim);
|
|
|
|
if (token == NULL)
|
|
return -1;
|
|
strncpy(key, token, klen);
|
|
|
|
token = strtok(NULL, delim);
|
|
if (token == NULL)
|
|
return -1;
|
|
strncpy(val, token, vlen);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static inline int
|
|
nslookup(const char *hostname, char *out, int len)
|
|
{
|
|
int ret;
|
|
struct addrinfo *addrinfo;
|
|
struct addrinfo hints;
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_INET;
|
|
ret = getaddrinfo(hostname, nullptr, &hints, &addrinfo);
|
|
if (ret != 0) {
|
|
errno = ret;
|
|
return -1;
|
|
}
|
|
for (struct addrinfo *cur = addrinfo; cur != NULL; cur = cur->ai_next) {
|
|
if (cur->ai_family == AF_INET) {
|
|
if (inet_ntop(AF_INET, &((struct sockaddr_in *)addrinfo->ai_addr)->sin_addr, out, len) == NULL) {
|
|
return -1;
|
|
}
|
|
freeaddrinfo(addrinfo);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
freeaddrinfo(addrinfo);
|
|
errno = EADDRNOTAVAIL;
|
|
return -1;
|
|
}
|
|
|
|
#undef inline
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|