2018-03-21 18:50:58 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-03-31 18:40:45 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 18:50:58 +01:00
|
|
|
#include "format.h"
|
|
|
|
#include "cw.h"
|
2022-07-31 17:15:32 +02:00
|
|
|
#include "val.h"
|
2018-03-21 18:50:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "sock.h"
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static void del ( struct cw_Val * data )
|
2018-03-21 18:50:58 +01:00
|
|
|
{
|
|
|
|
free ( data->val.ptr );
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static struct cw_Val *get ( struct cw_Val * data, const uint8_t * src, int len )
|
2018-03-21 18:50:58 +01:00
|
|
|
{
|
|
|
|
uint8_t * s;
|
|
|
|
s = bstr_create ( src, len );
|
|
|
|
|
|
|
|
if ( !s )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data->type = &cw_type_ipaddress;
|
|
|
|
data->val.ptr = s;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int put ( const struct cw_Val *data, uint8_t * dst )
|
2018-03-21 18:50:58 +01:00
|
|
|
{
|
|
|
|
return cw_put_bstr ( dst, data->val.ptr );
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int to_str ( const struct cw_Val *data, char *dst, int max_len )
|
2018-03-21 18:50:58 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
int l;
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
|
|
|
|
l = bstr_len(data->val.ptr);
|
|
|
|
if (l==4){
|
|
|
|
addr.ss_family = AF_INET;
|
|
|
|
memcpy(&(((struct sockaddr_in*)&addr)->sin_addr),bstr_data(data->val.ptr),4);
|
|
|
|
sock_addr2str(&addr,dst);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (l==16){
|
|
|
|
addr.ss_family = AF_INET6;
|
|
|
|
memcpy(&(((struct sockaddr_in6*)&addr)->sin6_addr),bstr_data(data->val.ptr),16);
|
|
|
|
sock_addr2str(&addr,dst);
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst=0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static struct cw_Val *from_str ( struct cw_Val * data, const char *src )
|
2018-03-21 18:50:58 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
uint8_t * s, * addrptr;
|
|
|
|
int rc,l;
|
|
|
|
|
|
|
|
rc = sock_strtoaddr(src,(struct sockaddr*)&addr);
|
|
|
|
if (!rc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
switch(((struct sockaddr*)&addr)->sa_family){
|
|
|
|
case AF_INET:
|
|
|
|
addrptr = (uint8_t*)&(((struct sockaddr_in*)(&addr))->sin_addr);
|
|
|
|
l = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
addrptr = (uint8_t*)&(((struct sockaddr_in6*)(&addr))->sin6_addr);
|
|
|
|
l = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s = bstr_create(addrptr,l);
|
|
|
|
if ( s == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
data->type = &cw_type_ipaddress;
|
|
|
|
data->val.ptr = s;
|
|
|
|
return data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static int len ( struct cw_Val * data ){
|
2018-03-24 07:56:05 +01:00
|
|
|
return bstr_len(data->val.ptr);
|
2018-03-21 18:50:58 +01:00
|
|
|
}
|
|
|
|
|
2022-07-31 17:15:32 +02:00
|
|
|
static void * data(cw_Val_t * data)
|
2018-04-07 19:28:00 +02:00
|
|
|
{
|
|
|
|
return bstr_data(data->val.ptr);
|
|
|
|
}
|
2022-07-31 17:15:32 +02:00
|
|
|
static const char * get_type_name(cw_Val_t *data)
|
2018-04-25 10:43:27 +02:00
|
|
|
{
|
|
|
|
return CW_TYPE_IPADDRESS->name;
|
|
|
|
}
|
2018-03-21 18:50:58 +01:00
|
|
|
|
2022-08-13 09:47:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int bread(cw_Cfg_t *cfg, const char * key, const uint8_t *src, int len, const void *param)
|
|
|
|
{
|
|
|
|
char str[128];
|
|
|
|
int rc;
|
|
|
|
cw_Val_t val;
|
|
|
|
memset(&val,0,sizeof(cw_Val_t));
|
|
|
|
get(&val,src,len);
|
|
|
|
to_str(&val,str,128);
|
|
|
|
cw_cfg_set(cfg,key,str);
|
|
|
|
rc = val.type->len(&val);
|
|
|
|
del(&val);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-21 18:50:58 +01:00
|
|
|
const struct cw_Type cw_type_ipaddress = {
|
|
|
|
"IPAddress", /* name */
|
2018-03-25 10:07:39 +02:00
|
|
|
del, /* del */
|
2018-03-21 18:50:58 +01:00
|
|
|
put, /* put */
|
|
|
|
get, /* get */
|
|
|
|
to_str, /* to_str */
|
|
|
|
from_str, /* from_str */
|
2018-04-07 19:28:00 +02:00
|
|
|
len, /* len */
|
2018-04-25 10:43:27 +02:00
|
|
|
data, /* data */
|
2022-08-13 09:47:12 +02:00
|
|
|
get_type_name,
|
|
|
|
NULL,
|
|
|
|
bread,
|
|
|
|
|
2018-03-21 18:50:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|