/* * is_group_invalid * * returns 1 if the `gid' of level `lvl' is invalid and * cannot be used in a regular IP. */ int is_group_invalid(int *gids, int gid, int lvl, int family) { if (family == AF_INET) { if (lvl == GET_LEVELS(family) - 1) { if (!gid) /* ZERONET */ return 1; if (gid >= 224 && gid <= 255) /* MULTICAST and BADCLASS */ return 1; if (gid == 127) /* LOOPBACK */ return 1; if (gid == 192 && gids[lvl - 1] == 168) /* 192.168.x.x is private, we cannot use IP of * that range */ return 1; if ((gid == 172 && (gids[lvl - 1] >= 16 || gids[lvl - 1] <= 31)) && !restricted_mode) /* We aren't in restricted mode, so * 172.16.0.0-172.31.255.255 is a private * class */ return 1; if (restricted_mode && ((restricted_class == RESTRICTED_10 && gid != 10) || (restricted_class == RESTRICTED_172 && !(gid == 172 && (gids[lvl - 1] >= 16 || gids[lvl - 1] <= 31)) ) ) ) /* We are in restricted mode, thus this IP is * invalid because it isn't in the 10.x.x.x or * 172.(16-31).x.x format */ return 1; } else if (lvl == GET_LEVELS(family) - 2) { if (gid == 168 && gids[lvl + 1] == 192) /* 192.168.x.x */ return 1; if (((gid >= 16 || gid <= 31) && gids[lvl + 1] == 172) && !restricted_mode) /* 172.16.0.0-172.31.255.255 */ return 1; } } else if (family == AF_INET6) { /* TODO: nothing ? */ return 0; } return 0; }
/* * 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; }
/* * 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]; } }
SEXP as_dummy(SEXP R_x) { int n, l, i, j; SEXP R_obj, R_tmp; n = LENGTH(R_x); l = LENGTH(GET_LEVELS(R_x)); if (l == 0) return R_NilValue; PROTECT(R_obj = NEW_LOGICAL(n*l)); for (i = 0; i < n*l; i++) /* this sucks! */ LOGICAL(R_obj)[i] = FALSE; for (i = 0; i < n; i++) { j = INTEGER(R_x)[i]; if (j == NA_INTEGER) continue; LOGICAL(R_obj)[i+(j-1)*n] = TRUE; } PROTECT(R_tmp = NEW_INTEGER(2)); INTEGER(R_tmp)[0] = n; INTEGER(R_tmp)[1] = l; SET_DIM(R_obj, R_tmp); UNPROTECT(1); SET_LEVELS(R_obj, duplicate(GET_LEVELS(R_x))); UNPROTECT(1); return R_obj; }
/* * iptogid * * ip to gnode id, of the specified `level', conversion function. * Note: this function cannot fail! So be sure to pass a valid `level'. */ int iptogid(inet_prefix * ip, int level) { u_char *h_ip = (u_char *) ip->data; int gid; /* * The formula is: * gid=(ip/MAXGROUPNODE^level) - (ip/MAXGROUPNODE^(level+1) * MAXGROUPNODE); * but since we have a MAXGROUPNODE equal to 2^8 we can just return * the `level'-th byte of ip.data. */ #if BYTE_ORDER == LITTLE_ENDIAN gid = (int) h_ip[level]; #else gid = (int) h_ip[GET_LEVELS(ip->family) - level - 1]; #endif return gid; }
/* * init_internet_gateway_search: * Initialization of the igs.c code. */ void init_internet_gateway_search(void) { inet_prefix new_gw; char new_gw_dev[IFNAMSIZ]; pthread_t ping_thread; pthread_attr_t t_attr; int i, ret, res, e; active_gws = 0; igw_multi_gw_disabled = 0; setzero(multigw_nh, sizeof(igw_nexthop) * MAX_MULTIPATH_ROUTES); /* * Just return if we aren't in restricted mode or if the user doesn't * want to use shared internet connections */ if (!restricted_mode || (!server_opt.use_shared_inet && !server_opt.share_internet)) return; loginfo("Activating the Internet Gateway Search engine"); init_igws(&me.igws, &me.igws_counter, GET_LEVELS(my_family)); init_tunnels_ifs(); /* delete all the old tunnels */ del_all_tunnel_ifs(0, 0, 0, NTK_TUNL_PREFIX); /* * Bring tunl0 up (just to test if the ipip module is loaded) */ loginfo("Checking if \"" DEFAULT_TUNL_IF "\" exists"); if (tunnel_change(0, 0, 0, DEFAULT_TUNL_PREFIX, DEFAULT_TUNL_NUMBER) < 0) { printf("Cannot read \"" DEFAULT_TUNL_IF "\". " "Is the \"ipip\" kernel module loaded?\n" " If you don't care about using the shared internet " "connections of the ntk nodes\n" " around you, disable the \"use_shared_inet\" option " "in netsukuku.conf"); del_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf"); exit(1); } ifs_del_all_name(me.cur_ifs, &me.cur_ifs_n, NTK_TUNL_PREFIX); ifs_del_all_name(me.cur_ifs, &me.cur_ifs_n, DEFAULT_TUNL_PREFIX); /* * Delete old routing rules */ reset_igw_rules(); /* * Init netfilter */ res = mark_init(server_opt.share_internet); if (res) { error(err_str); error("Cannot set the netfilter rules needed for the multi-igw. " "This feature will be disabled"); igw_multi_gw_disabled = 1; } /* * Check anomalies: from this point we initialize stuff only if we * have an Inet connection */ if (!server_opt.inet_connection) return; if (!server_opt.inet_hosts) fatal("You didn't specified any Internet hosts in the " "configuration file. What hosts should I ping?"); /* * If we are sharing our internet connection, activate the * masquerading. */ if (server_opt.share_internet) { igw_exec_masquerade_sh(server_opt.ip_masq_script, 0); if (!server_opt.ip_masq_script) fatal("No masquerading script was configured!"); }; /* * Get the default gateway route currently set in the kernel routing * table */ setzero(&new_gw, sizeof(inet_prefix)); ret = rt_get_default_gw(&new_gw, new_gw_dev); /* * If there is no IP set in the route, fetch it at least from the * device included in it. */ if (!new_gw.family && *new_gw_dev) { if (get_dev_ip(&new_gw, my_family, new_gw_dev) < 0) (*new_gw_dev) = 0; } if (ret < 0 || (!*new_gw_dev && !new_gw.family)) { /* Nothing useful has been found */ loginfo("The retrieval of the default gw from the kernel failed."); if (!server_opt.inet_gw.data[0]) fatal("The default gw isn't set in the kernel and you " "didn't specified it in netsukuku.conf. " "Cannot continue!"); } else if (!server_opt.inet_gw_dev || strncmp(new_gw_dev, server_opt.inet_gw_dev, IFNAMSIZ) || memcmp(new_gw.data, server_opt.inet_gw.data, MAX_IP_SZ)) { if (server_opt.inet_gw.data[0]) loginfo("Your specified Internet gateway doesn't match with " "the one currently stored in the kernel routing table." "I'm going to use the kernel gateway: %s dev %s", inet_to_str(new_gw), new_gw_dev); if (!server_opt.inet_gw_dev) server_opt.inet_gw_dev = xstrdup(new_gw_dev); else strncpy(server_opt.inet_gw_dev, new_gw_dev, IFNAMSIZ); memcpy(&server_opt.inet_gw, &new_gw, sizeof(inet_prefix)); /* Delete the default gw, we are replacing it */ rt_delete_def_gw(0); } loginfo("Using \"%s dev %s\" as your first Internet gateway.", inet_to_str(server_opt.inet_gw), server_opt.inet_gw_dev); if (rt_replace_def_gw(server_opt.inet_gw_dev, server_opt.inet_gw, 0)) fatal("Cannot set the default gw to %s %s", inet_to_str(server_opt.inet_gw), server_opt.inet_gw_dev); active_gws++; /* * Activate the anti-loop multi-igw shield */ if (server_opt.share_internet) { rule_add(0, 0, 0, 0, FWMARK_ALISHIELD, RTTABLE_ALISHIELD); if (rt_replace_def_gw(server_opt.inet_gw_dev, server_opt.inet_gw, RTTABLE_ALISHIELD)) { error("Cannot set the default route in the ALISHIELD table. " "Disabling the multi-inet_gw feature"); igw_multi_gw_disabled = 1; } } /* * Activate the traffic shaping for the `server_opt.inet_gw_dev' * device */ if (server_opt.shape_internet) igw_exec_tcshaper_sh(server_opt.tc_shaper_script, 0, server_opt.inet_gw_dev, server_opt.my_upload_bw, server_opt.my_dnload_bw); for (i = 0; i < me.cur_ifs_n; i++) if (!strcmp(me.cur_ifs[i].dev_name, server_opt.inet_gw_dev)) { for (e = 0; e < server_opt.ifs_n; e++) if (!strcmp(server_opt.ifs[i], server_opt.inet_gw_dev)) fatal("You specified the \"%s\" interface" " in the options, but this device is also" " part of the primary Internet gw route." " Don't include \"%s\" in the list of " "interfaces utilised by the daemon", server_opt.inet_gw_dev, server_opt.inet_gw_dev); loginfo("Deleting the \"%s\" interface from the device " "list since it is part of the primary Internet" " gw route.", me.cur_ifs[i].dev_name); ifs_del(me.cur_ifs, &me.cur_ifs_n, i); if (me.cur_ifs_n <= 0) fatal ("The deleted interface cannot be used by NetsukukuD because it is part\n" " of your primary Internet gw route. You have to specify another\n" " interface with the -i option or you won't be able share your" " Internet connection"); } loginfo("Launching the first ping to the Internet hosts"); if (!server_opt.disable_andna) internet_hosts_to_ip(); me.inet_connected = igw_check_inet_conn(); if (me.inet_connected) loginfo("The Internet connection is up & running"); else loginfo("The Internet connection appears to be down"); if (!me.inet_connected && server_opt.share_internet) fatal("We are not connected to the Internet, but you want to " "share your connection. Please check your options"); debug(DBG_SOFT, "Evoking the Internet ping daemon."); pthread_attr_init(&t_attr); pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED); pthread_create(&ping_thread, &t_attr, igw_check_inet_conn_t, 0); }
/* NOTE: R vectors of length 1 will yield a python list of length 1*/ int to_Pyobj_vector(SEXP robj, PyObject **obj, int mode) { PyObject *it, *tmp; SEXP names, dim; int len, *integers, i, type; const char *strings, *thislevel; double *reals; Rcomplex *complexes; #ifdef WITH_NUMERIC PyObject *array; #endif if (!robj) { // return -1; /* error */ // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "robj does not exist")); // PyObject_CallObject(my_callback, argslist); // } return 1; } if (robj == R_NilValue) { Py_INCREF(Py_None); *obj = Py_None; return 1; /* succeed */ } len = GET_LENGTH(robj); tmp = PyList_New(len); type = TYPEOF(robj); // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(si)", "robj length is ", len)); // PyObject_CallObject(my_callback, argslist); // } /// break for checking the R length and other aspects for (i=0; i<len; i++) { switch (type) { case LGLSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In LGLSXP")); // PyObject_CallObject(my_callback, argslist); // } integers = INTEGER(robj); if(integers[i]==NA_INTEGER) /* watch out for NA's */ { if (!(it = PyInt_FromLong(integers[i]))) //return -1; tmp = Py_BuildValue("s", "failed in the PyInt_FromLong"); // we are at least getting an robj *obj = tmp; return 1; //it = Py_None; } else if (!(it = PyBool_FromLong(integers[i]))) { tmp = Py_BuildValue("s", "failed in the PyBool_FromLong"); // we are at least getting an robj *obj = tmp; return 1; //return -1; } break; case INTSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In INTSXP")); // PyObject_CallObject(my_callback, argslist); // } integers = INTEGER(robj); if(isFactor(robj)) { /* Watch for NA's! */ if(integers[i]==NA_INTEGER) it = PyString_FromString(CHAR(NA_STRING)); else { thislevel = CHAR(STRING_ELT(GET_LEVELS(robj), integers[i]-1)); if (!(it = PyString_FromString(thislevel))) { tmp = Py_BuildValue("s", "failed in the PyString_FromString"); // we are at least getting an robj *obj = tmp; return 1; } //return -1; } } else { if (!(it = PyInt_FromLong(integers[i]))) { tmp = Py_BuildValue("s", "failed in the PyInt_FromLong"); // we are at least getting an robj *obj = tmp; return 1; //return -1; } } break; case REALSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In REALSXP")); // PyObject_CallObject(my_callback, argslist); // } reals = REAL(robj); if (!(it = PyFloat_FromDouble(reals[i]))) { // tmp = Py_BuildValue("s", "failed in the PyFloat_FromDouble"); // we are at least getting an robj // *obj = tmp; // return 1; return -1; } break; case CPLXSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In CPLXSXP")); // PyObject_CallObject(my_callback, argslist); // } complexes = COMPLEX(robj); if (!(it = PyComplex_FromDoubles(complexes[i].r, complexes[i].i))) { // tmp = Py_BuildValue("s", "failed in PyComplex_FromDoubles!!!"); // we are at least getting an robj // *obj = tmp; // return 1; return -1; } break; case STRSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In STRSXP")); // PyObject_CallObject(my_callback, argslist); // } if(STRING_ELT(robj, i)==R_NaString) it = PyString_FromString(CHAR(NA_STRING)); else { strings = CHAR(STRING_ELT(robj, i)); if (!(it = PyString_FromString(strings))) { // tmp = Py_BuildValue("s", "failed in PyString_FromString!!!"); // we are at least getting an robj // *obj = tmp; // return 1; return -1; } } break; case LISTSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In LISTSXP")); // PyObject_CallObject(my_callback, argslist); // } if (!(it = to_Pyobj_with_mode(elt(robj, i), mode))) { // tmp = Py_BuildValue("s", "failed in to_Pyobj_with_mode LISTSXP!!!"); // we are at least getting an robj // *obj = tmp; // return 1; return -1; } break; case VECSXP: // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "In VECSXP")); // PyObject_CallObject(my_callback, argslist); // } if (!(it = to_Pyobj_with_mode(VECTOR_ELT(robj, i), mode))) { return -1; } break; default: Py_DECREF(tmp); return 0; /* failed */ } if (PyList_SetItem(tmp, i, it) < 0) // there was a failure in setting the item { // tmp = Py_BuildValue("s", "failed in PyList_SetItem!!!"); // we are at least getting an robj // *obj = tmp; // return 1; return -1; } } dim = GET_DIM(robj); if (dim != R_NilValue) { // #ifdef WITH_NUMERIC // if(use_numeric) // { // array = to_PyNumericArray(tmp, dim); // if (array) { /* If the conversion to Numeric succeed.. */ // *obj = array; /* we are done */ // Py_DECREF(tmp); // return 1; // } // PyErr_Clear(); // } // #endif len = GET_LENGTH(dim); *obj = to_PyArray(tmp, INTEGER(dim), len); Py_DECREF(tmp); return 1; } // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(O)", tmp)); // PyObject_CallObject(my_callback, argslist); // } names = GET_NAMES(robj); if (names == R_NilValue) { *obj = tmp; // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "returning as list (of lists)")); // PyObject_CallObject(my_callback, argslist); // } } else { *obj = to_PyDict(tmp, names); // if(my_callback){ // argslist = Py_BuildValue("(O)", Py_BuildValue("(s)", "returning as dict")); // PyObject_CallObject(my_callback, argslist); // } Py_DECREF(tmp); } return 1; }
SEXP foreach_bed(SEXP bed, SEXP function, SEXP envir) { SEXP chroms, starts, ends, strands = R_NilValue; SEXP arg_idx, arg_chrom, arg_start, arg_end, arg_strand; SEXP fcall; int has_strand = 0; int i, N; PROTECT(bed); if(!isEnvironment(envir)) error("'envir' should be an environment"); chroms = VECTOR_ELT(bed, 0); if (!isFactor(chroms) && TYPEOF(chroms) != STRSXP) error("first column of bed file must be a factor or character vector"); PROTECT(starts = AS_INTEGER(VECTOR_ELT(bed, 1))); PROTECT(ends = AS_INTEGER(VECTOR_ELT(bed, 2))); if (length(bed) >= 6) { has_strand = 1; strands = VECTOR_ELT(bed, 5); if (!isFactor(strands) && TYPEOF(strands) != STRSXP) error("sixth column of bed file must be a factor or character vector"); } /* build function call */ PROTECT(arg_idx = NEW_INTEGER(1)); PROTECT(arg_chrom = allocVector(STRSXP, 1)); PROTECT(arg_start = NEW_INTEGER(1)); PROTECT(arg_end = NEW_INTEGER(1)); PROTECT(arg_strand = allocVector(STRSXP, 1)); if (has_strand == 0) SET_STRING_ELT(arg_strand, 0, NA_STRING); PROTECT(fcall = lang6(function, arg_idx, arg_chrom, arg_start, arg_end, arg_strand)); /* run loop */ N = length(chroms); for (i = 0; i < N; ++i) { INTEGER(arg_idx)[0] = i + 1; INTEGER(arg_start)[0] = INTEGER(starts)[i]; INTEGER(arg_end)[0] = INTEGER(ends)[i]; /* chrom */ if (isFactor(chroms)) { int idx = INTEGER(chroms)[i] - 1; SET_STRING_ELT(arg_chrom, 0, STRING_ELT(GET_LEVELS(chroms), idx)); } else SET_STRING_ELT(arg_chrom, 0, STRING_ELT(chroms, i)); /* strand */ if (has_strand == 1) { if (isFactor(strands)) { int idx = INTEGER(strands)[i] - 1; SET_STRING_ELT(arg_strand, 0, STRING_ELT(GET_LEVELS(strands), idx)); } else SET_STRING_ELT(arg_strand, 0, STRING_ELT(strands, i)); } /* eval and ignore result */ eval(fcall, envir); /* check for interrupts */ R_CheckUserInterrupt(); } /* clean up */ UNPROTECT(9); return R_NilValue; }