void qspn_reset(u_char levels) { setzero(qspn_b, sizeof(struct qspn_buffer *)*levels); setzero(qspn_send_mutex, sizeof(int)*levels); setzero(me.cur_qspn_id, sizeof(int)*levels); qspn_reset_counters(levels); }
/* * ifs_del: removes from the `ifs' array the device which is at the * `if_pos'th position. `*ifs_n' is then decremented. */ void ifs_del(interface *ifs, int *ifs_n, int if_pos) { if(if_pos == (*ifs_n)-1) setzero(&ifs[if_pos], sizeof(interface)); else { memcpy(&ifs[if_pos], &ifs[(*ifs_n)-1], sizeof(interface)); setzero(&ifs[(*ifs_n)-1], sizeof(interface)); } (*ifs_n)--; }
void init_radar(void) { hook_retry = 0; my_echo_id = 0; total_radar_scans = 0; setzero(radar_scans, sizeof(radar_scans)); radar_scan_mutex = 0; radar_q = (struct radar_queue *) clist_init(&radar_q_counter); setzero(send_qspn_now, sizeof(u_char) * MAX_LEVELS); }
/* * str_to_inet_gw: * The syntax of `str' is IP:devname, i.e. 192.168.1.1:eth0. * str_to_inet_gw() stores the IP in `gw'. * In `*dev' is returned the pointer to a newly allocated string containing * the device name. * On error -1 is returned. */ int str_to_inet_gw(char *str, inet_prefix * gw, char **dev) { char *buf; setzero(dev, IFNAMSIZ); /* Copy :devname in `dev' */ if (!(buf = rindex(str, ':'))) return -1; *buf = 0; buf++; if (!*buf) /* No device was specified */ return -1; if (strlen(buf) >= IFNAMSIZ) /* It is too long, truncate it */ buf[IFNAMSIZ - 1] = 0; *dev = xstrndup(buf, IFNAMSIZ); /* Extract the IP from the first part of `str' */ if (str_to_inet(str, gw)) return -1; return 0; }
void route_reset_filter() { setzero(&filter, sizeof(filter)); filter.mdst.bits = -1; filter.msrc.bits = -1; }
/* * gidtoipstart * * It sets in `*ip' the ipstart of the gnode using the `gid[x]' for * each level x. * `total_levels' is the total number of levels and the `gid' array * has `total_levels' elements. * `levels' is the number of array elements considered, gidtoipstart() will use * only the elements going from gid[total_levels-levels] to gid[total_levels-1]. * `family' is used to fill the inet_prefix of ipstart. */ void gidtoipstart(int *gid, u_char total_levels, u_char levels, int family, inet_prefix * ip) { int i, h_ip[MAX_IP_INT]; u_char *ipstart; setzero(h_ip, MAX_IP_SZ); ipstart = (u_char *) h_ip; for (i = total_levels - ZERO_LEVEL; i >= total_levels - levels; i--) { /* The formula is: * ipstart += MAXGROUPNODE^i * gid[i]; * but since MAXGROUPNODE is equal to 2^8 we just set each * single byte of ipstart. */ #if BYTE_ORDER == LITTLE_ENDIAN ipstart[i] = (u_char) gid[i]; #else ipstart[GET_LEVELS(family) - i - 1] = (u_char) gid[i]; #endif } memcpy(ip->data, h_ip, MAX_IP_SZ); ip->family = family; ip->len = (family == AF_INET) ? 4 : 16; ip->bits = ip->len * 8; }
/* * radar_update_bmap * * updates the bnode map of the given `level' the root_node bnode in the bmap * will also point to the gnode of level `gnode_level'+1 that is * `rq'->quadg.gnode[_EL(gnode_level+1)]. */ void radar_update_bmap(struct radar_queue *rq, int level, int gnode_level) { map_gnode *gnode; map_node *root_node; map_rnode *rnode, rn; int bm, rnode_pos, root_node_pos; void *void_map; if (level == me.cur_quadg.levels - 1) return; qspn_set_map_vars(level, 0, &root_node, &root_node_pos, 0); void_map = me.ext_map; gnode = rq->quadg.gnode[_EL(gnode_level + 1)]; bm = map_find_bnode(me.bnode_map[level], me.bmap_nodes[level], root_node_pos); if (bm == -1) { bm = map_add_bnode(&me.bnode_map[level], &me.bmap_nodes[level], root_node_pos, 0); rnode_pos = -1; } else rnode_pos = rnode_find(&me.bnode_map[level][bm], &gnode->g); if (rnode_pos == -1) { setzero(&rn, sizeof(map_rnode)); rn.r_node = (int *) &gnode->g; rnode_add(&me.bnode_map[level][bm], &rn); rnode_pos = 0; } rnode = &me.bnode_map[level][bm].r_node[rnode_pos]; rnode->trtt = MILLISEC(rq->final_rtt); }
/* * final_radar_queue * * analyses the received ECHO_REPLY pkt and write the * average rtt of each found node in the radar_queue. */ void final_radar_queue(void) { struct radar_queue *rq; int e; struct timeval sum; u_int f_rtt; setzero(&sum, sizeof(struct timeval)); rq = radar_q; list_for(rq) { if (!rq->node) continue; /* Sum the rtt of all the received pongs */ for (e = 0; e < rq->pongs; e++) timeradd(&rq->rtt[e], &sum, &sum); /* Add penality rtt for each pong lost */ for (; e < MAX_RADAR_SCANS; e++) timeradd(&rq->rtt[e - rq->pongs], &sum, &sum); f_rtt = MILLISEC(sum) / MAX_RADAR_SCANS; MILLISEC_TO_TV(f_rtt, rq->final_rtt); } my_echo_id = 0; }
/* * iptoquadg * * Using the given `ip' it fills the `qg' quadro_group struct. The `flags' * given specify what element fill in the struct (the flags are in gmap.h). */ void iptoquadg(inet_prefix ip, map_gnode ** ext_map, quadro_group * qg, char flags) { int i; u_char levels; int gid[MAX_LEVELS]; setzero(qg, sizeof(quadro_group)); levels = GET_LEVELS(ip.family); qg->levels = levels; if (flags & QUADG_GID) { iptogids(&ip, qg->gid, levels); memcpy(gid, qg->gid, sizeof(gid)); if (flags & QUADG_IPSTART) for (i = 0; i < levels; i++) gidtoipstart(gid, levels, levels - i, ip.family, &qg->ipstart[i]); } if (flags & QUADG_GNODE) { for (i = 0; i < levels - ZERO_LEVEL; i++) qg->gnode[i] = gnode_from_pos(qg->gid[i + 1], ext_map[i]); qg->gnode[levels - ZERO_LEVEL] = &ext_map[i][0]; } }
int main(int argc, char **argv) { s_dir *d; static s_arg *a; s_err *e; e = malloc(sizeof(s_err)); a = malloc(sizeof(s_arg)); d = malloc(sizeof(s_dir)); a = setzero(a); e = seterrzero(e); e = all_check(d, a, e, argv); if (argc > 2) argv = sort_arg(argv); //ft_putnbr(count_arg(argv)); //ft_putnbr(ft_strcmp("b.out","a")); option_error(e); //DEBUG //a = argcheck(a, argv); //remettre quand jaurais fait le tri des arguments if(!argv[a->arc]) argv[a->arc] = "."; ft_dirread(d, a, argv); //ft_check(d, argv); return(0); }
/* gmap_node_del: It deletes a `gnode' from the `gmap'. Really it sets the * gnode's flags to GMAP_VOID.*/ void gmap_node_del(map_gnode * gnode) { map_node_del(&gnode->g); setzero(gnode, sizeof(map_gnode)); gnode->flags |= GMAP_VOID; gnode->g.flags |= MAP_VOID; }
/* * prepare_listen_socket: * It creates a new socket of the desired `family' and binds it to the * specified `port'. It sets also the reuseaddr and NONBLOCK * socket options, because this new socket shall be used to listen() and * accept(). * If `dev' is not null, the socket will be binded to the device named * `dev'->dev_name with the SO_BINDTODEVICE socket option. * The created socket is returned. */ int prepare_listen_socket(int family, int socktype, u_short port, interface * dev) { struct addrinfo hints, *ai, *aitop; char strport[NI_MAXSERV]; int err, s; setzero(&hints, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = socktype; hints.ai_flags = AI_PASSIVE; snprintf(strport, NI_MAXSERV, "%u", port); err = getaddrinfo(NULL, strport, &hints, &aitop); if (err) { error("Getaddrinfo error: %s", gai_strerror(err)); return -1; } for (ai = aitop; ai; ai = ai->ai_next) { if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) continue; s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (s == -1) /* Maybe we can use another socket... */ continue; /* Bind the created socket to the device named dev->dev_name */ if (dev && (set_bindtodevice_sk(s, dev->dev_name) < 0)) { inet_close(&s); continue; } if (set_reuseaddr_sk(s) < 0) { inet_close(&s); continue; } /* Let's bind it! */ if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) { error("Cannot bind the port %d: %s. Trying another " "socket...", port, strerror(errno)); inet_close(&s); continue; } freeaddrinfo(aitop); return s; } error("Cannot open inbound socket on port %d: %s", port, strerror(errno)); freeaddrinfo(aitop); return -1; }
/* * lcl_new_keyring * * It generates a new keyring. */ void lcl_new_keyring(lcl_cache_keyring * keyring) { setzero(keyring, sizeof(lcl_cache_keyring)); loginfo("Generating a new ANDNA keyring"); /* Generate the new key pair for the first time */ keyring->priv_rsa = genrsa(ANDNA_PRIVKEY_BITS, &keyring->pubkey, &keyring->pkey_len, &keyring->privkey, &keyring->skey_len); }
void critfunc(double *grad, int *nPar, int *nD, double *probs, int *M, double *design, double *n2, double *nold, double *A, double *tol, double *MEDgrad, int *type, int *stand, double *res){ // grad - contains gradient vectors (4 cells reserved for each model) // nPar - number of parameters (dim A) // nD - number of dose-levels // design - design // type - 1: MED, 2: Dopt, 3: MED&Dopt int m,incgrad=0,incb=0; double resM=0,resD=0,fracp=0; // variables for SVD decomposition, initialize to max possible dimension double work[30]={0.0}; double s[4]={0.0}; double VT[16]={0.0}; double U[16]={0.0}; *res = 0.0; // calculate weight vector getAllocs(design, n2, nold, nD); for(m=0;m<*M;m++){ if(m > 0){ incgrad+=*nD*nPar[m-1]; incb+=nPar[m-1]; } setzero(A, 16);resM = 0.0; // calulate matrix calcMat(grad, &nPar[m], design, nD, A, &incgrad); // calculate det and/or MP-Inverse calcDetGinv(A, &nPar[m], work, s, VT, U, tol, type, &resD); if(*type == 1){ // calculate quadratic form (for MED designs) calcQuadform(MEDgrad, A, &nPar[m], &resM, &incb); *res += probs[m]*log(resM); } if(*type == 2){ if(*stand == 1){ fracp = (double) nPar[m]; *res += probs[m]*(-log(resD)/fracp); } else { *res += probs[m]*(-log(resD)); } } if(*type == 3){ // calculate quadratic form (for MED designs) calcQuadform(MEDgrad, A, &nPar[m], &resM, &incb); if(*stand == 1){ fracp = (double) nPar[m]; *res += probs[m]*(-0.5*log(resD)/fracp+0.5*log(resM)); } else { *res += probs[m]*(-0.5*log(resD)+0.5*log(resM)); } } } }
void andna_caches_init(int family) { net_family = family; setzero(&lcl_keyring, sizeof(lcl_keyring)); andna_lcl = (lcl_cache *) clist_init(&lcl_counter); andna_c = (andna_cache *) clist_init(&andna_c_counter); andna_counter_c = (counter_c *) clist_init(&cc_counter); andna_rhc = (rh_cache *) clist_init(&rhc_counter); }
/* * lcl_destroy_keyring * * destroys accurately the keyring ^_^ */ void lcl_destroy_keyring(lcl_cache_keyring * keyring) { if (keyring->priv_rsa) RSA_free(keyring->priv_rsa); if (keyring->pubkey) xfree(keyring->pubkey); if (keyring->privkey) xfree(keyring->privkey); setzero(keyring, sizeof(lcl_cache_keyring)); }
void selectclust(int *n, int *merge, int *senode, int *seleaf) { int i,j,k,temp,nn; int *sn, *tleaf, *tnode; nn=(*n)+1; sn=(int *)malloc((nn-1)*sizeof(int)); tleaf=(int *)malloc(nn*sizeof(int)); tnode=(int *)malloc(nn*sizeof(int)); setzero(&nn, tleaf); setzero(n, tnode); for(i=0;i<(*n);i++) { *(sn+i)=*(senode+i); } maxnonzero(n, sn, &temp); k=1; while(temp>0) { subtree(n, merge, &temp, tnode, tleaf); for(i=0;i<((*n)+1);i++) { if(*(tleaf+i)==1) *(seleaf+i)=k; } for(i=0;i<(*n);i++) { if(*(tnode+i)==1) *(sn+i)=0; } k++; setzero(&nn, tleaf); setzero(n, tnode); maxnonzero(n, sn, &temp); } }
/* * load_config_file: loads from `file' all the options that are written in it * and stores them in the environment. See parse_config_line() above. * If `file' cannot be opened -1 is returned, but if it is read and * parse_config_line() detects a corrupted line, fatal() is directly called. * On success 0 is returned. */ int load_config_file(char *file) { FILE *fd; char buf[PATH_MAX+1], *p, *str; size_t slen; int i=0, e=0; if(!(fd=fopen(file, "r"))) { fatal("Cannot load the configuration file from %s: %s\n" " Maybe you want to use the -c option ?", file, strerror(errno)); return -1; } while(!feof(fd) && i < CONF_MAX_LINES) { setzero(buf, PATH_MAX+1); fgets(buf, PATH_MAX, fd); e++; if(feof(fd)) break; str=buf; while(isspace(*str)) str++; if(*str=='#' || !*str) { /* Strip off any comment or null lines */ continue; } else { /* Remove the last part of the string where a side * comment starts, #a comment like this. */ if((p=strrchr(str, '#'))) *p='\0'; /* Don't include the newline and spaces of the end of * the string */ slen=strlen(str); for(p=&str[slen-1]; isspace(*p); p--) *p='\0'; parse_config_line(file, e, str); i++; } } fclose(fd); return 0; }
map_node *init_map(size_t len) { int i; map_node *map; if(!len) len=sizeof(map_node)*MAXGROUPNODE; map=(map_node *)xmalloc(len); setzero(map, len); for(i=0; i<MAXGROUPNODE; i++) map[i].flags|=MAP_VOID; return map; }
/* * rnodetoip * * converts the node `maprnode', which is a rnode of the root_node, * to the relative ip. */ void rnodetoip(u_int mapstart, u_int maprnode, inet_prefix ipstart, inet_prefix * ret) { ext_rnode *e_rnode; map_node *rnode = (map_node *) maprnode; setzero(ret, sizeof(inet_prefix)); if (rnode->flags & MAP_ERNODE) { e_rnode = (ext_rnode *) rnode; inet_copy(ret, &e_rnode->quadg.ipstart[0]); } else maptoip(mapstart, maprnode, ipstart, ret); }
map_gnode * init_gmap(int groups) { map_gnode *gmap; size_t len; if (!groups) groups = MAXGROUPNODE; len = sizeof(map_gnode) * groups; gmap = (map_gnode *) xmalloc(len); setzero(gmap, len); reset_gmap(gmap, groups); return gmap; }
/* * inet_setip_raw: fills the `ip' inet_prefix struct with `data' and `family'. */ int inet_setip_raw(inet_prefix *ip, u_int *data, int family) { ip->family=family; setzero(ip->data, sizeof(ip->data)); if(family==AF_INET) { ip->data[0]=data[0]; ip->len=4; } else if(family==AF_INET6) { memcpy(ip->data, data, sizeof(ip->data)); ip->len=16; } else fatal(ERROR_MSG "family not supported", ERROR_POS); ip->bits=ip->len<<3; /* bits=len*8 */ return 0; }
void free_map(map_node *map, size_t count) { int i, len; if(!count) count=MAXGROUPNODE; len=sizeof(map_node)*count; for(i=0; i<count; i++) { if(map[i].links) { if(map[i].r_node) xfree(map[i].r_node); } } setzero(map, len); xfree(map); }
/* * get_dev_ip: fetches the ip currently assigned to the interface named `dev' * and stores it to `ip'. * On success 0 is returned, -1 otherwise. */ int get_dev_ip(inet_prefix *ip, int family, char *dev) { int s=-1; int ret=0; setzero(ip, sizeof(inet_prefix)); if((s=new_socket(family)) < 0) { error("Error while setting \"%s\" ip: Cannot open socket", dev); return -1; } if(family == AF_INET) { struct ifreq req; strncpy(req.ifr_name, dev, IFNAMSIZ); req.ifr_addr.sa_family = family; if(ioctl(s, SIOCGIFADDR, &req)) ERROR_FINISH(ret, -1, finish); sockaddr_to_inet(&req.ifr_addr, ip, 0); } else if(family == AF_INET6) { struct in6_ifreq req6; /* * XXX: NOT TESTED */ req6.ifr6_ifindex=ll_name_to_index(dev); req6.ifr6_prefixlen=0; if(ioctl(s, SIOCGIFADDR, &req6)) ERROR_FINISH(ret, -1, finish); inet_setip(ip, (u_int *)&req6.ifr6_addr, family); } finish: if(s != -1) close(s); return ret; }
/* * new_rnode_allowed * * add a new allowed rnode in the `alr' llist which has * already `*alr_counter' members. `gid', `min_lvl', and `tot_lvl' are the * respective field of the new allowed_rnode struct. */ void new_rnode_allowed(struct allowed_rnode **alr, int *alr_counter, int *gid, int min_lvl, int tot_lvl) { struct allowed_rnode *new_alr; new_alr = xmalloc(sizeof(struct allowed_rnode)); new_alr->min_level = min_lvl; new_alr->tot_level = tot_lvl; setzero(new_alr->gid, sizeof(int) * MAX_LEVELS); memcpy(&new_alr->gid[min_lvl], &gid[min_lvl], sizeof(int) * (tot_lvl - min_lvl)); debug(DBG_SOFT, "new_rnode_allowed: %d, %d, %d, %d. min_lvl: %d, tot_lvl: %d", gid[0], gid[1], gid[2], gid[3], min_lvl, tot_lvl); clist_add(alr, alr_counter, new_alr); }
/* * fill_default_options: fills the default values in the server_opt struct */ void fill_default_options(void) { setzero(&server_opt, sizeof(server_opt)); server_opt.family = AF_INET; server_opt.config_file = NTK_CONFIG_FILE; server_opt.pid_file = NTK_PID_FILE; server_opt.int_map_file = INT_MAP_FILE; server_opt.ext_map_file = EXT_MAP_FILE; server_opt.bnode_map_file = BNODE_MAP_FILE; server_opt.andna_hnames_file = ANDNA_HNAMES_FILE; server_opt.snsd_nodes_file = SNSD_NODES_FILE; server_opt.andna_cache_file = ANDNA_CACHE_FILE; server_opt.lclkey_file = LCLKEY_FILE; server_opt.lcl_file = LCL_FILE; server_opt.rhc_file = RHC_FILE; server_opt.counter_c_file = COUNTER_C_FILE; server_opt.daemon = 1; server_opt.dbg_lvl = 0; server_opt.disable_andna = 0; server_opt.disable_resolvconf = 0; server_opt.restricted = 0; server_opt.restricted_class = 0; server_opt.use_shared_inet = 1; server_opt.ip_masq_script = IPMASQ_SCRIPT_FILE; server_opt.tc_shaper_script = TCSHAPER_SCRIPT_FILE; server_opt.max_connections = MAX_CONNECTIONS; server_opt.max_accepts_per_host = MAX_ACCEPTS; server_opt.max_accepts_per_host_time = FREE_ACCEPT_TIME; }
/* * route_get_exact_prefix: it dumps the routing table and search for a route * which has the prefix equal to `prefix', if it is found its destination * address is stored in `dst' and its interface name in `dev_name' (which must * be IFNAMSIZ big). */ int route_get_exact_prefix_dst(inet_prefix prefix, inet_prefix * dst, char *dev_name) { int do_ipv6 = AF_UNSPEC; struct rtnl_handle rth; char dst_data[sizeof(inet_prefix) + IFNAMSIZ]; route_reset_filter(); filter.tb = RT_TABLE_MAIN; filter.mdst = prefix; filter.rdst = filter.mdst; if (do_ipv6 == AF_UNSPEC && filter.tb) do_ipv6 = AF_INET; if (rtnl_open(&rth, 0) < 0) return -1; ll_init_map(&rth); if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { error(ERROR_MSG "Cannot send dump request" ERROR_POS); return -1; } setzero(dst_data, sizeof(dst_data)); if (rtnl_dump_filter(&rth, route_get_gw, dst_data, NULL, NULL) < 0) { debug(DBG_NORMAL, ERROR_MSG "Dump terminated" ERROR_POS); return -1; } inet_copy(dst, (inet_prefix *) dst_data); memcpy(dev_name, dst_data + sizeof(inet_prefix), IFNAMSIZ); rtnl_close(&rth); return 0; }
void serial_mainloop(void) { unsigned char buf; int countbyte = 0, started = 0; datarecord data; // used to collect datas from plc datarecord data_response; // used to send responses to plc setzero(&data, sizeof(data)); first_loop = 1; while (1) { int buflen = 0; buflen = read(portfd, &buf, 1); if (buflen == -1) { printFatal(strerror(errno)); } if (!started && buf == SOH && !countbyte) { started = 1; countbyte++; data.start = buf; continue; } if (started) { switch (countbyte) { case 1: /* 0x18 == 24 */ /* if byte 1 isn't 0x18 retry... */ if (buf != 0x18) { started = 0; countbyte = 0; continue; } data.length = buf; break; /* if byte 2 isn't 0x04 retry... */ case 2: if (buf != 0x04) { started = 0; countbyte = 0; continue; } data.cmd = buf; break; } /* put bytes from 3 to 27 into the struct field */ if (countbyte >= 3 && countbyte < data.length + 3) { data.databytes[countbyte - 3] = buf; } /* the byte 27 is the checksum given by the plc */ if (countbyte == 27) { data.checksum = buf; if (cfg.verbose) { // TODO: improve logging and printf debug behaviour // TODO: take the log util functions from netsukuku src printf ("catch BCC checksum value: %#x\n", data.checksum); printf ("calculated BCC checksum value: %#x\n", (unsigned char) calculateBCC(data)); } } /* the byte 28 is the last byte of the record and it must be ETX */ if (countbyte == 28 && buf == ETX) { data.end = buf; started = 0; countbyte = 0; int tmpcurr = 0; float tmpvolt = 0; // MAX_VOLT : MAX_HEX_VALUE = x : bytes tmpvolt = (unsigned int) FETCH2BYTES(5, 4, data.databytes); if (tmpvolt > MAX_HEX_VALUE) data.voltage = 0; else data.voltage = tmpvolt * (float) MAX_VOLT / (float) MAX_HEX_VALUE *emb_info.k1; tmpcurr = (unsigned int) FETCH2BYTES(7, 6, data.databytes); if (tmpcurr > MAX_HEX_VALUE) data.current = 0; else data.current = (int) ((float) tmpcurr * (float) MAX_VOLT / (float) MAX_HEX_VALUE * emb_info.k2); data.encoder[0] = (unsigned long int) FETCH4BYTES(data. databytes, 8) / emb_info.array_island_k1[0]; data.encoder[1] = (unsigned long int) FETCH4BYTES(data. databytes, 12) / emb_info.array_island_k1[1]; data.encoder[2] = (unsigned long int) FETCH4BYTES(data. databytes, 16) / emb_info.array_island_k1[2]; data.encoder[3] = (unsigned long int) FETCH4BYTES(data. databytes, 20) / emb_info.array_island_k1[3]; if (cfg.verbose) { // TODO: improve logging and printf debug behaviour printf ("contacolpi1: %f, contacolpi2: %f, ", data.encoder[0], data.encoder[1]); printf ("contacolpi3: %f, contacolpi4: %f, ", data.encoder[2], data.encoder[3]); printf ("voltage value: %f, current value: %d\n", data.voltage, data.current); } if (cfg.verbose) { int i; // TODO: take the log util functions from netsukuku src // TODO: improve logging and printf debug behaviour // printf("catch start code: %#x\n", data.start); // printf("catch len code: %#x\n", data.length); // printf("catch cmd code: %#x\n", data.cmd); for (i = 0; i < 24; i++) { printf("byte[%d]: %#x\n", i, data.databytes[i]); } } /* if the checksum given by the plc is the same we have * calculated send ack */ if ((unsigned char) calculateBCC(data) == (unsigned char) data.checksum) { /* compile the response data record with the ACK byte * the response is the same record except for the first * data byte that is ACK */ memcpy(&data_response, &data, sizeof(data)); data_response.databytes[0] = ACK; connect_db(cfg.dsn, cfg.dbuser, cfg.dbpass); if (cfg.polling) get_embedded_infos(); /* TODO: do all the processing SQL things */ do_sql_things(data); disconnect_db(); if (cfg.verbose) printf("ok, sending ACK...\n"); /* send response */ send_response(portfd, data_response); } else { memcpy(&data_response, &data, sizeof(data)); data_response.databytes[0] = NACK; //if(cfg.polling) get_embedded_infos(); //do_sql_things(data); if (cfg.verbose) printf("ok, sending NACK...\n"); /* send response */ send_response(portfd, data_response); } setzero(&data, sizeof(data)); setzero(&data_response, sizeof(data_response)); continue; } countbyte++; } } close(portfd); }
/* * The main flow shall never be stopped, and the sand of time will be * revealed. */ int main(int argc, char **argv) { struct udp_daemon_argv ud_argv; u_short *port; pthread_t daemon_tcp_thread, daemon_udp_thread, andna_thread; pthread_t ping_igw_thread; pthread_attr_t t_attr; log_init(argv[0], 0, 1); /* Options loading... */ fill_default_options(); parse_options(argc, argv); /* reinit the logs using the new `dbg_lvl' value */ log_init(argv[0], server_opt.dbg_lvl, 1); log_to_file(0); /* Load the option from the config file */ load_config_file(server_opt.config_file); fill_loaded_cfg_options(); /* If a same option was specified in the config file and in the * command line, give priority to the latter */ parse_options(argc, argv); check_conflicting_options(); /* Initialize the whole netsukuku source code */ init_netsukuku(argv); signal(SIGALRM, sigalrm_handler); signal(SIGHUP, sighup_handler); signal(SIGINT, sigterm_handler); signal(SIGTERM, sigterm_handler); signal(SIGQUIT, sigterm_handler); /* Angelic foreground or Daemonic background ? */ if (server_opt.daemon) { loginfo("Forking to background"); log_init(argv[0], server_opt.dbg_lvl, 0); if (daemon(0, 0) == -1) error("Impossible to daemonize: %s.", strerror(errno)); } pthread_attr_init(&t_attr); pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED); setzero(&ud_argv, sizeof(struct udp_daemon_argv)); port = xmalloc(sizeof(u_short)); /* * These are the daemons, the main threads that keeps NetsukukuD * up & running. */ debug(DBG_NORMAL, "Activating all daemons"); pthread_mutex_init(&udp_daemon_lock, 0); pthread_mutex_init(&tcp_daemon_lock, 0); debug(DBG_SOFT, "Evoking the netsukuku udp radar daemon."); ud_argv.port = ntk_udp_radar_port; pthread_mutex_lock(&udp_daemon_lock); pthread_create(&daemon_udp_thread, &t_attr, udp_daemon, (void *) &ud_argv); pthread_mutex_lock(&udp_daemon_lock); pthread_mutex_unlock(&udp_daemon_lock); debug(DBG_SOFT, "Evoking the netsukuku tcp daemon."); *port = ntk_tcp_port; pthread_mutex_lock(&tcp_daemon_lock); pthread_create(&daemon_tcp_thread, &t_attr, tcp_daemon, (void *) port); pthread_mutex_lock(&tcp_daemon_lock); pthread_mutex_unlock(&tcp_daemon_lock); /* Now we hook in Netsukuku */ netsukuku_hook(0, 0); /* * If not disabled, start the ANDNA daemon */ if (!server_opt.disable_andna) pthread_create(&andna_thread, &t_attr, andna_main, 0); xfree(port); if (restricted_mode && (server_opt.share_internet || server_opt.use_shared_inet)) { debug(DBG_SOFT, "Evoking the Internet Gateway Pinger daemon"); pthread_create(&ping_igw_thread, &t_attr, igw_monitor_igws_t, 0); } /* We use this same process for the radar_daemon. */ debug(DBG_SOFT, "Evoking radar daemon."); radar_daemon(0); /* Not reached, hahaha */ loginfo("Cya m8"); pthread_attr_destroy(&t_attr); destroy_netsukuku(); exit(0); }
void init_netsukuku(char **argv) { xsrand(); if (geteuid()) fatal("Need root privileges"); destroy_netsukuku_mutex = pid_saved = 0; sigterm_timestamp = sighup_timestamp = sigalrm_timestamp = 0; setzero(&me, sizeof(struct current_globals)); if (is_ntkd_already_running()) fatal("ntkd is already running. If it is not, remove \"%s\"", server_opt.pid_file); else save_pid(); my_family = server_opt.family; restricted_mode = server_opt.restricted; restricted_class = server_opt.restricted_class ? RESTRICTED_172 : RESTRICTED_10; /* Check if the DATA_DIR exists, if not create it */ if (check_and_create_dir(DATA_DIR)) fatal("Cannot access to the %s directory. Exiting.", DATA_DIR); /* * Device initialization */ if (if_init_all(server_opt.ifs, server_opt.ifs_n, me.cur_ifs, &me.cur_ifs_n) < 0) fatal("Cannot initialize any network interfaces"); /* * ANDNA init */ if (!server_opt.disable_andna) andna_init(); /* * Initialize the Internet gateway stuff */ if (server_opt.my_upload_bw && server_opt.my_dnload_bw) me.my_bandwidth = bandwidth_in_8bit((server_opt.my_upload_bw + server_opt.my_dnload_bw) / 2); init_internet_gateway_search(); pkts_init(me.cur_ifs, me.cur_ifs_n, 0); qspn_init(FAMILY_LVLS); me.cur_erc = e_rnode_init(&me.cur_erc_counter); /* Radar init */ rq_wait_idx_init(rq_wait_idx); first_init_radar(); total_radars = 0; ntk_load_maps(); #if 0 /* TODO: activate and test it !! */ debug(DBG_NORMAL, "ACPT: Initializing the accept_tbl: \n" " max_connections: %d,\n" " max_accepts_per_host: %d,\n" " max_accept_per_host_time: %d", server_opt.max_connections, server_opt.max_accepts_per_host, server_opt.max_accepts_per_host_time); init_accept_tbl(server_opt.max_connections, server_opt.max_accepts_per_host, server_opt.max_accepts_per_host_time); #endif if (restricted_mode) loginfo("NetsukukuD is in restricted mode. " "Restricted class: %s", server_opt. restricted_class ? RESTRICTED_172_STR : RESTRICTED_10_STR); hook_init(); rehook_init(); me.uptime = time(0); }