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

222 lines
8.6 KiB

9 years ago
/******************************************************************************
* Copyright © 2014-2016 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
#include "iguana777.h"
#include "../includes/tweetnacl.h"
#include "../crypto777/OS_portable.h"
#include "../includes/libgfshare.h"
#include "../includes/utlist.h"
#include "../includes/uthash.h"
#include "../includes/curve25519.h"
#include "../includes/cJSON.h"
9 years ago
bits256 calc_categoryhashes(bits256 *subhashp,char *category,char *subcategory)
{
bits256 categoryhash;
if ( category == 0 || category[0] == 0 || strcmp(category,"broadcast") == 0 )
categoryhash = GENESIS_PUBKEY;
else vcalc_sha256(0,categoryhash.bytes,(uint8_t *)category,(int32_t)strlen(category));
if ( subhashp != 0 )
{
if ( subcategory == 0 || subcategory[0] == 0 || strcmp(subcategory,"broadcast") == 0 )
*subhashp = GENESIS_PUBKEY;
else vcalc_sha256(0,subhashp->bytes,(uint8_t *)subcategory,(int32_t)strlen(subcategory));
}
return(categoryhash);
}
9 years ago
struct category_info *category_find(bits256 categoryhash,bits256 subhash)
9 years ago
{
9 years ago
struct category_info *cat=0,*sub = 0;
9 years ago
HASH_FIND(hh,Categories,categoryhash.bytes,sizeof(categoryhash),cat);
if ( cat != 0 )
{
if ( bits256_nonz(subhash) > 0 && memcmp(GENESIS_PUBKEY.bytes,subhash.bytes,sizeof(subhash)) != 0 )
{
HASH_FIND(hh,cat->sub,subhash.bytes,sizeof(subhash),sub);
9 years ago
if ( sub != 0 )
return(sub);
9 years ago
}
return(cat);
9 years ago
} //else printf("category_find.(%s) not found\n",bits256_str(str,categoryhash));//, getchar();
9 years ago
return(0);
}
9 years ago
queue_t *category_Q(struct category_info **catptrp,bits256 categoryhash,bits256 subhash)
9 years ago
{
struct category_info *cat;
9 years ago
*catptrp = 0;
9 years ago
if ( (cat= category_find(categoryhash,subhash)) != 0 )
9 years ago
{
*catptrp = cat;
9 years ago
return(&cat->Q);
9 years ago
}
9 years ago
else return(0);
}
void *category_info(bits256 categoryhash,bits256 subhash)
{
struct category_info *cat;
if ( (cat= category_find(categoryhash,subhash)) != 0 )
return(cat->info);
else return(0);
}
void *category_infoset(bits256 categoryhash,bits256 subhash,void *info)
{
struct category_info *cat;
if ( (cat= category_find(categoryhash,subhash)) != 0 )
{
cat->info = info;
return(info);
9 years ago
}
9 years ago
return(0);
9 years ago
}
9 years ago
struct category_info *category_processfunc(bits256 categoryhash,bits256 subhash,char *(*process_func)(struct supernet_info *myinfo,struct category_info *cat,void *data,int32_t datalen,char *remoteaddr))
9 years ago
{
struct category_info *cat;
9 years ago
if ( (cat= category_find(categoryhash,subhash)) != 0 )
9 years ago
{
9 years ago
cat->processfunc = process_func;
9 years ago
return(cat);
}
return(0);
}
9 years ago
struct category_msg *category_gethexmsg(struct supernet_info *myinfo,struct category_info **catptrp,bits256 categoryhash,bits256 subhash)
9 years ago
{
queue_t *Q;
9 years ago
//char str[65]; printf("getmsg.(%s) %llx\n",bits256_str(str,categoryhash),(long long)subhash.txid);
9 years ago
if ( (Q= category_Q(catptrp,categoryhash,subhash)) != 0 )
9 years ago
return(queue_dequeue(Q,0));
else return(0);
}
9 years ago
void category_posthexmsg(struct supernet_info *myinfo,bits256 categoryhash,bits256 subhash,char *hexmsg,struct tai now,char *remoteaddr)
9 years ago
{
9 years ago
int32_t len; struct category_msg *m; queue_t *Q = 0; struct category_info *cat;
if ( (Q= category_Q(&cat,categoryhash,subhash)) != 0 )
9 years ago
{
len = (int32_t)strlen(hexmsg) >> 1;
m = calloc(1,sizeof(*m) + len);
m->t = now, m->len = len;
9 years ago
if ( remoteaddr != 0 && remoteaddr[0] != 0 )
m->remoteipbits = calc_ipbits(remoteaddr);
9 years ago
decode_hex(m->msg,m->len,hexmsg);
queue_enqueue("categoryQ",Q,&m->DL,0);
9 years ago
char str[65]; printf("POST HEXMSG.(%s) -> %s.%llx len.%d\n",hexmsg,bits256_str(str,categoryhash),(long long)subhash.txid,m->len);
9 years ago
return;
}
9 years ago
// char str[65]; printf("no subscription for category.(%s) %llx\n",bits256_str(str,categoryhash),(long long)subhash.txid);
9 years ago
}
9 years ago
void *category_subscribe(struct supernet_info *myinfo,bits256 categoryhash,bits256 subhash)
9 years ago
{
struct category_info *cat,*sub; bits256 hash;
HASH_FIND(hh,Categories,categoryhash.bytes,sizeof(categoryhash),cat);
if ( cat == 0 )
{
cat = mycalloc('c',1,sizeof(*cat));
cat->hash = hash = categoryhash;
9 years ago
char str[65]; printf("ADD cat.(%s)\n",bits256_str(str,categoryhash));
9 years ago
HASH_ADD(hh,Categories,hash,sizeof(hash),cat);
}
if ( bits256_nonz(subhash) > 0 && memcmp(GENESIS_PUBKEY.bytes,subhash.bytes,sizeof(subhash)) != 0 && cat != 0 )
{
HASH_FIND(hh,cat->sub,subhash.bytes,sizeof(subhash),sub);
if ( sub == 0 )
{
sub = mycalloc('c',1,sizeof(*sub));
sub->hash = hash = subhash;
9 years ago
char str[65],str2[65]; printf("subadd.(%s) -> (%s)\n",bits256_str(str,hash),bits256_str(str2,categoryhash));
9 years ago
HASH_ADD(hh,cat->sub,hash,sizeof(hash),sub);
}
}
return(cat);
}
int32_t category_peer(struct supernet_info *myinfo,struct iguana_peer *addr,bits256 category,bits256 subhash)
9 years ago
{
return(1);
9 years ago
}
int32_t category_plaintext(struct supernet_info *myinfo,bits256 category,bits256 subhash,int32_t plaintext)
9 years ago
{
return(plaintext);
}
int32_t category_maxdelay(struct supernet_info *myinfo,bits256 category,bits256 subhash,int32_t maxdelay)
9 years ago
{
return(maxdelay);
}
9 years ago
int32_t category_broadcastflag(struct supernet_info *myinfo,bits256 category,bits256 subhash,int32_t broadcastflag)
9 years ago
{
9 years ago
if ( broadcastflag < 1 )
broadcastflag = 1;
else if ( broadcastflag > SUPERNET_MAXHOPS )
broadcastflag = SUPERNET_MAXHOPS;
return(broadcastflag);
}
9 years ago
char *SuperNET_categorymulticast(struct supernet_info *myinfo,int32_t surveyflag,bits256 categoryhash,bits256 subhash,char *message,int32_t maxdelay,int32_t broadcastflag,int32_t plaintext,cJSON *argjson,char *remoteaddr)
9 years ago
{
9 years ago
char *hexmsg,*retstr; int32_t len; cJSON *retjson = cJSON_CreateObject();
9 years ago
len = (int32_t)strlen(message);
9 years ago
//char str[65]; printf("multicast.(%s)\n",bits256_str(str,categoryhash));
9 years ago
if ( is_hexstr(message,len) == 0 )
{
9 years ago
hexmsg = malloc(((len+1) << 1) + 1);
9 years ago
init_hexbytes_noT(hexmsg,(uint8_t *)message,len+1);
} else hexmsg = message;
9 years ago
plaintext = category_plaintext(myinfo,categoryhash,subhash,plaintext);
broadcastflag = category_broadcastflag(myinfo,categoryhash,subhash,broadcastflag);
maxdelay = category_maxdelay(myinfo,categoryhash,subhash,maxdelay);
retstr = SuperNET_DHTsend(myinfo,0,categoryhash,subhash,hexmsg,maxdelay,broadcastflag,plaintext);
9 years ago
//if ( 0 && argjson != 0 )
// SuperNET_hexmsgprocess(myinfo,retjson,argjson,hexmsg,remoteaddr);
9 years ago
if ( hexmsg != message)
free(hexmsg);
9 years ago
if ( retjson != 0 )
{
if ( retstr != 0 )
jaddstr(retjson,"result",retstr);
retstr = jprint(retjson,1);
}
9 years ago
return(retstr);
}
void category_init(struct supernet_info *myinfo)
{
bits256 pangeahash,instantdexhash;
category_subscribe(myinfo,GENESIS_PUBKEY,GENESIS_PUBKEY);
pangeahash = calc_categoryhashes(0,"pangea",0);
myinfo->pangea_category = pangeahash;
category_subscribe(myinfo,pangeahash,GENESIS_PUBKEY);
9 years ago
category_processfunc(pangeahash,GENESIS_PUBKEY,pangea_hexmsg);
9 years ago
category_chain_functions(myinfo,pangeahash,GENESIS_PUBKEY,sizeof(bits256),sizeof(bits256),0,0,0,0,0,0);
instantdexhash = calc_categoryhashes(0,"InstantDEX",0);
myinfo->instantdex_category = instantdexhash;
category_subscribe(myinfo,instantdexhash,GENESIS_PUBKEY);
category_processfunc(instantdexhash,GENESIS_PUBKEY,InstantDEX_hexmsg);
category_processfunc(instantdexhash,myinfo->myaddr.persistent,InstantDEX_hexmsg);
//exchanges777_init(myinfo,0,0);
}