You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/lib/support.c

152 lines
2.8 KiB
C

/*
* Secure Network Transport Layer Library implementation.
* This is a proprietary software. See COPYING for further details.
*
* (c) 2013-2014 Copyright Askele, inc. <http://askele.com>
* (c) 2013-2014 Copyright Askele Ingria, inc. <http://askele-ingria.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
10 years ago
#ifdef WIN32
#include <Winsock2.h>
#include <windows.h>
#include <rpc.h>
#else
#include <sys/select.h>
10 years ago
#include <netdb.h>
#include <unistd.h>
10 years ago
#include <uuid/uuid.h>
#endif
#include <fcntl.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <tdata/usrtc.h>
#include <sexpr/sexp.h>
#include <sntl/connection.h>
10 years ago
#ifdef WIN32
#define UUID_T_LENGTH 16
#endif
#ifdef uuid_t
#undef uuid_t
#endif
#ifdef WIN32
10 years ago
typedef unsigned char uuid_t[16];
void uuid_generate_random(uuid_t out){
int i,a=0;
LARGE_INTEGER frequency,t1;
long int d;
for(i=0;i<UUID_T_LENGTH;i++){
a=rand();
*(out+i)=a;
}
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);
d=(long int)t1.QuadPart*1000/frequency.QuadPart;
memcpy(out,&d,sizeof(d));
}
#endif
/* this function is an ugly implementation to get C string with uuid */
char *__generate_uuid(void)
{
10 years ago
#ifdef WIN32
char *uuidc = NULL;
uuid_t uuid_t_m;
int len, i = 0,r=0;
len = 33;
if(!(uuidc = malloc(len))) return NULL;
uuid_generate_random(uuid_t_m);
for(i = 0; i < 16; i++)
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid_t_m[i]);
return uuidc;
#else
char *uuidc = NULL;
uuid_t uuid;
int len, i = 0;
len = sizeof(char)*(sizeof(uuid_t)*2) + sizeof(char);
if(!(uuidc = malloc(len))) return NULL;
uuid_generate_time_safe(uuid);
for(i = 0; i < sizeof(uuid_t); i++)
10 years ago
snprintf(uuidc+(2*i*sizeof(char)), len, "%02x", uuid[i]);
10 years ago
return uuidc;
#endif
}
/* networking helpers */
#ifndef WIN32
int __resolvehost(const char *hostname, char *buf, int buf_len,
struct hostent **rhp)
{
struct hostent *hostbuf ;//= malloc(sizeof(struct hostent));
struct hostent *hp = *rhp = NULL;
int herr = 0, hres = 0;
10 years ago
hostbuf = malloc(sizeof(struct hostent));
if(!hostbuf) return NO_ADDRESS;
hres = gethostbyname_r(hostname, hostbuf,
buf, buf_len, &hp, &herr);
10 years ago
if(hres) return NO_ADDRESS;
*rhp = hp;
10 years ago
return NETDB_SUCCESS;
}
#endif
/* sexp helpers */
int sexp_list_car(sexp_t *expr, sexp_t **sx)
{
if (!SEXP_IS_LIST(expr) || expr->list->ty != SEXP_VALUE) return 1;
*sx = expr->list;
return 0;
}
int sexp_list_cdr(sexp_t *expr, sexp_t **sx)
{
/* Dummy function. Can we do cdr properly? */
if (!SEXP_IS_LIST(expr) || expr->list->ty != SEXP_VALUE) return 1;
if (!expr->list->next) *sx = NULL;
else *sx = expr->list->next;
return 0;
}
10 years ago