This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-tfe/common/src/tfe_utils.cpp

337 lines
7.8 KiB
C++
Raw Normal View History

#include <MESA/MESA_htable.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tfe_utils.h>
#include <time.h>
#include <assert.h>
2023-04-18 16:03:57 +08:00
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
//functioned as strdup, for dictator compatible.
char* tfe_strdup(const char* s)
{
char*d=NULL;
if(s==NULL)
{
return NULL;
}
d=(char*)malloc(strlen(s)+1);
memcpy(d,s,strlen(s)+1);
return d;
}
char *tfe_thread_safe_ctime(const time_t *tp, char *buf, int len)
{
struct tm local_time;
time_t now;
static unsigned char weekday_str[7][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
time(&now);
if(NULL == (localtime_r(&now, &local_time)))
return NULL;
len = snprintf(buf, len, "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday],
month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900);
return buf;
}
//replacement of glibc scandir, to adapt dictator malloc wrap
int tfe_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const void *, const void *))
{
DIR * od;
int n = 0;
int ENLARGE_STEP=1024;
int DIR_ENT_SIZE=ENLARGE_STEP;
struct dirent ** list = NULL;
struct dirent * p;
struct dirent entry,*result;
if((dir == NULL) || (namelist == NULL))
return -1;
od = opendir(dir);
if(od == NULL)
return -1;
list = (struct dirent **)malloc(DIR_ENT_SIZE*sizeof(struct dirent *));
while(0==readdir_r(od,&entry,&result))
{
if(result==NULL)
{
break;
}
if( filter && !filter(&entry))
continue;
p = (struct dirent *)malloc(sizeof(struct dirent));
memcpy((void *)p,(void *)(&entry),sizeof(struct dirent));
list[n] = p;
n++;
if(n >= DIR_ENT_SIZE)
{
DIR_ENT_SIZE+=ENLARGE_STEP;
list=(struct dirent **)realloc((void*)list,DIR_ENT_SIZE*sizeof(struct dirent *));
}
}
closedir(od);
*namelist = list;
if(compar)
qsort((void *)*namelist,n,sizeof(struct dirent *),compar);
return n;
}
char *tfe_read_file(const char *filename, size_t *filelen)
{
FILE *file = NULL;
long length = 0;
char *content = NULL;
size_t read_chars = 0;
file = fopen(filename, "rb");
if (file == NULL)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_END) != 0)
{
goto cleanup;
}
length = ftell(file);
if (length < 0)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_SET) != 0)
{
goto cleanup;
}
/* allocate content buffer */
content = (char*)malloc((size_t)length + sizeof(""));
if (content == NULL)
{
goto cleanup;
}
/* read the file into memory */
read_chars = fread(content, sizeof(char), (size_t)length, file);
if ((long)read_chars != length)
{
free(content);
content = NULL;
goto cleanup;
}
*filelen = read_chars;
content[read_chars] = '\0';
cleanup:
if (file != NULL)
{
fclose(file);
}
return content;
}
static int tfe_decode_base64_internal(u_char *dst, u_char *src, const u_char *basis)
{
size_t len;
u_char *d, *s;
for (len = 0; len < strlen((char *)src); len++)
{
if (src[len] == '=')
{
break;
}
if (basis[src[len]] == 77)
{
return 0;
}
}
if (len % 4 == 1)
{
return 0;
}
s = src;
d = dst;
while (len > 3)
{
*d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
*d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
*d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]);
s += 4;
len -= 4;
}
if (len > 1)
{
*d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
}
if (len > 2)
{
*d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
}
return d - dst;
}
int tfe_decode_base64url(u_char *dst, u_char *src)
{
static u_char basis64[] = {
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 63,
77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
};
return tfe_decode_base64_internal(dst, src, basis64);
2023-04-18 16:03:57 +08:00
}
/******************************************************************************
* sids
******************************************************************************/
void sids_write_once(struct sids *dst, struct sids *src)
{
if (dst && src)
{
if (dst->num == 0 && src->num > 0)
{
sids_copy(dst, src);
}
}
}
void sids_copy(struct sids *dst, struct sids *src)
{
if (dst && src)
{
dst->num = src->num;
memcpy(dst->elems, src->elems, sizeof(dst->elems[0]) * dst->num);
}
}
/******************************************************************************
* route_ctx
******************************************************************************/
int route_ctx_is_empty(struct route_ctx *ctx)
{
if (ctx->len == 0)
{
return 1;
}
else
{
return 0;
}
}
void route_ctx_copy(struct route_ctx *dst, struct route_ctx *src)
{
memcpy(dst->data, src->data, src->len);
dst->len = src->len;
}
/******************************************************************************
* protocol
******************************************************************************/
#define CHECKSUM_CARRY(x) (x = (x >> 16) + (x & 0xffff), (~(x + (x >> 16)) & 0xffff))
__attribute__((unused)) static int checksum(u_int16_t *addr, int len)
2023-04-18 16:03:57 +08:00
{
int sum = 0;
int nleft = len;
u_int16_t ans = 0;
u_int16_t *w = addr;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(char *)(&ans) = *(char *)w;
sum += ans;
}
return sum;
}
int str_to_mac(const char *str, char *mac_buff)
{
if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac_buff[0]), &(mac_buff[1]), &(mac_buff[2]), &(mac_buff[3]), &(mac_buff[4]), &(mac_buff[5])) == 6)
{
return 0;
}
else
{
return -1;
}
}
int get_mac_by_device_name(const char *dev_name, char *mac_buff)
{
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd == -1)
{
return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
strcpy(ifr.ifr_name, dev_name);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
{
close(fd);
return -1;
}
unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
2023-04-24 10:48:40 +08:00
memcpy(mac_buff, mac, 6);
2023-04-18 16:03:57 +08:00
close(fd);
return 0;
}