int scamper_addr2mac_add(int ifindex, scamper_addr_t *ip, scamper_addr_t *mac) { addr2mac_t *a2m = NULL; char ipstr[128], macstr[128]; if(scamper_addr2mac_whohas(ifindex, ip) != NULL) return 0; if((a2m = addr2mac_alloc(ifindex, ip, mac, 0)) == NULL) return -1; if(splaytree_insert(tree, a2m) == NULL) { printerror(errno, strerror, __func__, "could not add %s:%s to tree", scamper_addr_tostr(a2m->ip, ipstr, sizeof(ipstr)), scamper_addr_tostr(a2m->mac, macstr, sizeof(macstr))); addr2mac_free(a2m); return -1; } scamper_debug(__func__, "ifindex %d ip %s mac %s", ifindex, scamper_addr_tostr(a2m->ip, ipstr, sizeof(ipstr)), scamper_addr_tostr(a2m->mac, macstr, sizeof(macstr))); return 0; }
scamper_fd_t *scamper_fd_dl(int ifindex) { scamper_fd_t *fdn = NULL, findme; int fd = -1; findme.type = SCAMPER_FD_TYPE_DL; findme.fd_dl_ifindex = ifindex; if((fdn = fd_find(&findme)) != NULL) { scamper_fd_read_unpause(fdn); return fdn; } /* * open the file descriptor for the ifindex, and then allocate a scamper_fd * for the file descriptor */ if((fd = scamper_dl_open(ifindex)) == -1 || (fdn = fd_alloc(SCAMPER_FD_TYPE_DL, fd)) == NULL) { goto err; } /* * record the ifindex for the file descriptor, and then allocate the state * that is maintained with it */ fdn->fd_dl_ifindex = ifindex; /* * 1. add the file descriptor to the splay tree * 2. allocate state for the datalink file descriptor */ if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL || (fdn->fd_dl_dl = scamper_dl_state_alloc(fdn)) == NULL) { goto err; } /* set the file descriptor up for reading */ fdn->read.cb = scamper_dl_read_cb; fdn->read.param = fdn->fd_dl_dl; fdn->write.cb = NULL; fdn->write.param = NULL; scamper_fd_read_unpause(fdn); return fdn; err: if(fdn != NULL) free(fdn); if(fd != -1) scamper_dl_close(fd); return NULL; }
int scamper_task_sig_install(scamper_task_t *task) { scamper_task_sig_t *sig; scamper_task_t *tf; s2t_t *s2t; slist_node_t *n; if(slist_count(task->siglist) < 1) return -1; for(n=slist_head_node(task->siglist); n != NULL; n = slist_node_next(n)) { s2t = slist_node_item(n); sig = s2t->sig; /* check if another task has this signature already */ if((tf = scamper_task_find(sig)) != NULL) { if(tf != task) goto err; continue; } if(sig->sig_type == SCAMPER_TASK_SIG_TYPE_TX_IP) s2t->node = splaytree_insert(tx_ip, s2t); else if(sig->sig_type == SCAMPER_TASK_SIG_TYPE_TX_ND) s2t->node = splaytree_insert(tx_nd, s2t); else if(sig->sig_type == SCAMPER_TASK_SIG_TYPE_SNIFF) s2t->node = dlist_tail_push(sniff, s2t); if(s2t->node == NULL) { scamper_debug(__func__, "could not install sig"); goto err; } } return 0; err: scamper_task_sig_deinstall(task); return -1; }
static scamper_fd_t *fd_tcp(int type, void *addr, uint16_t sport) { scamper_fd_t *fdn, findme; size_t len = 0; int fd = -1; findme.type = type; findme.fd_tcp_addr = addr; findme.fd_tcp_sport = sport; if((fdn = fd_find(&findme)) != NULL) { return fdn; } if(type == SCAMPER_FD_TYPE_TCP4) { fd = scamper_tcp4_open(addr, sport); len = sizeof(struct in_addr); } else if(type == SCAMPER_FD_TYPE_TCP6) { fd = scamper_tcp6_open(addr, sport); len = sizeof(struct in6_addr); } if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (addr != NULL && (fdn->fd_tcp_addr = memdup(addr, len)) == NULL)) { goto err; } fdn->fd_tcp_sport = sport; if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_TCP4) scamper_tcp4_close(fd); else if(type == SCAMPER_FD_TYPE_TCP6) scamper_tcp6_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }
static int addr2mac_add(const int ifindex, const int type, const void *ipraw, const void *macraw, const time_t expire) { const int mt = SCAMPER_ADDR_TYPE_ETHERNET; scamper_addr_t *mac = NULL; scamper_addr_t *ip = NULL; addr2mac_t *addr2mac = NULL; char ipstr[128], macstr[128]; if((ip = scamper_addrcache_get(addrcache, type, ipraw)) == NULL) { printerror(errno, strerror, __func__, "could not get ip"); goto err; } if((mac = scamper_addrcache_get(addrcache, mt, macraw)) == NULL) { printerror(errno, strerror, __func__, "could not get mac"); goto err; } if((addr2mac = addr2mac_alloc(ifindex, ip, mac, expire)) == NULL) { goto err; } scamper_addr_free(ip); ip = NULL; scamper_addr_free(mac); mac = NULL; if(splaytree_insert(tree, addr2mac) == NULL) { printerror(errno, strerror, __func__, "could not add %s:%s to tree", scamper_addr_tostr(addr2mac->ip, ipstr, sizeof(ipstr)), scamper_addr_tostr(addr2mac->mac, macstr, sizeof(macstr))); goto err; } scamper_debug(__func__, "ifindex %d ip %s mac %s expire %d", ifindex, scamper_addr_tostr(addr2mac->ip, ipstr, sizeof(ipstr)), scamper_addr_tostr(addr2mac->mac, macstr, sizeof(macstr)), expire); return 0; err: if(addr2mac != NULL) addr2mac_free(addr2mac); if(mac != NULL) scamper_addr_free(mac); if(ip != NULL) scamper_addr_free(ip); return -1; }
static scamper_fd_t *fd_null(int type) { scamper_fd_t *fdn = NULL, findme; int fd = -1; /* first check if a sharable fd exists for this type */ findme.type = type; if((fdn = fd_find(&findme)) != NULL) { return fdn; } if(type == SCAMPER_FD_TYPE_RTSOCK) fd = scamper_rtsock_open(); else if(type == SCAMPER_FD_TYPE_IFSOCK) fd = socket(AF_INET, SOCK_DGRAM, 0); else if(type == SCAMPER_FD_TYPE_IP4) fd = scamper_ip4_openraw(); if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_RTSOCK) scamper_rtsock_close(fd); else if(type == SCAMPER_FD_TYPE_IFSOCK) close(fd); else if(type == SCAMPER_FD_TYPE_IP4) scamper_ip4_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }
static scamper_outfile_t *outfile_alloc(char *name, scamper_file_t *sf) { scamper_outfile_t *sof = NULL; if((sof = malloc_zero(sizeof(scamper_outfile_t))) == NULL) { printerror(errno, strerror, __func__, "could not malloc sof"); goto err; } sof->sf = sf; sof->refcnt = 1; if((sof->name = strdup(name)) == NULL) { printerror(errno, strerror, __func__, "could not strdup"); goto err; } if(splaytree_insert(outfiles, sof) == NULL) { printerror(errno, strerror, __func__, "could not insert"); goto err; } scamper_debug(__func__, "name %s fd %d", name, scamper_file_getfd(sf)); return sof; err: if(sof != NULL) { if(sof->name != NULL) free(sof->name); free(sof); } return NULL; }
handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_cache_entry **ret_sce) { #ifdef HAVE_FAM_H fam_dir_entry *fam_dir = NULL; int dir_ndx = -1; splay_tree *dir_node = NULL; #endif stat_cache_entry *sce = NULL; stat_cache *sc; struct stat st; size_t k; int fd; struct stat lst; #ifdef DEBUG_STAT_CACHE size_t i; #endif int file_ndx; splay_tree *file_node = NULL; *ret_sce = NULL; /* * check if the directory for this file has changed */ sc = srv->stat_cache; buffer_copy_buffer(sc->hash_key, name); buffer_append_int(sc->hash_key, con->conf.follow_symlink); file_ndx = hashme(sc->hash_key); sc->files = splaytree_splay(sc->files, file_ndx); #ifdef DEBUG_STAT_CACHE for (i = 0; i < ctrl.used; i++) { if (ctrl.ptr[i] == file_ndx) break; } #endif if (sc->files && (sc->files->key == file_ndx)) { #ifdef DEBUG_STAT_CACHE /* it was in the cache */ force_assert(i < ctrl.used); #endif /* we have seen this file already and * don't stat() it again in the same second */ file_node = sc->files; sce = file_node->data; /* check if the name is the same, we might have a collision */ if (buffer_is_equal(name, sce->name)) { if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_SIMPLE) { if (sce->stat_ts == srv->cur_ts) { *ret_sce = sce; return HANDLER_GO_ON; } } } else { /* oops, a collision, * * file_node is used by the FAM check below to see if we know this file * and if we can save a stat(). * * BUT, the sce is not reset here as the entry into the cache is ok, we * it is just not pointing to our requested file. * * */ file_node = NULL; } } else { #ifdef DEBUG_STAT_CACHE if (i != ctrl.used) { log_error_write(srv, __FILE__, __LINE__, "xSB", file_ndx, "was already inserted but not found in cache, ", name); } force_assert(i == ctrl.used); #endif } #ifdef HAVE_FAM_H /* dir-check */ if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) { if (0 != buffer_copy_dirname(sc->dir_name, name)) { log_error_write(srv, __FILE__, __LINE__, "sb", "no '/' found in filename:", name); return HANDLER_ERROR; } buffer_copy_buffer(sc->hash_key, sc->dir_name); buffer_append_int(sc->hash_key, con->conf.follow_symlink); dir_ndx = hashme(sc->hash_key); sc->dirs = splaytree_splay(sc->dirs, dir_ndx); if (sc->dirs && (sc->dirs->key == dir_ndx)) { dir_node = sc->dirs; } if (dir_node && file_node) { /* we found a file */ sce = file_node->data; fam_dir = dir_node->data; if (fam_dir->version == sce->dir_version) { /* the stat()-cache entry is still ok */ *ret_sce = sce; return HANDLER_GO_ON; } } } #endif /* * *lol* * - open() + fstat() on a named-pipe results in a (intended) hang. * - stat() if regular file + open() to see if we can read from it is better * * */ if (-1 == stat(name->ptr, &st)) { return HANDLER_ERROR; } if (S_ISREG(st.st_mode)) { /* fix broken stat/open for symlinks to reg files with appended slash on freebsd,osx */ if (name->ptr[buffer_string_length(name) - 1] == '/') { errno = ENOTDIR; return HANDLER_ERROR; } /* try to open the file to check if we can read it */ if (-1 == (fd = open(name->ptr, O_RDONLY))) { return HANDLER_ERROR; } close(fd); } if (NULL == sce) { #ifdef DEBUG_STAT_CACHE int osize = splaytree_size(sc->files); #endif sce = stat_cache_entry_init(); buffer_copy_buffer(sce->name, name); sc->files = splaytree_insert(sc->files, file_ndx, sce); #ifdef DEBUG_STAT_CACHE if (ctrl.size == 0) { ctrl.size = 16; ctrl.used = 0; ctrl.ptr = malloc(ctrl.size * sizeof(*ctrl.ptr)); } else if (ctrl.size == ctrl.used) { ctrl.size += 16; ctrl.ptr = realloc(ctrl.ptr, ctrl.size * sizeof(*ctrl.ptr)); } ctrl.ptr[ctrl.used++] = file_ndx; force_assert(sc->files); force_assert(sc->files->data == sce); force_assert(osize + 1 == splaytree_size(sc->files)); #endif } sce->st = st; sce->stat_ts = srv->cur_ts; /* catch the obvious symlinks * * this is not a secure check as we still have a race-condition between * the stat() and the open. We can only solve this by * 1. open() the file * 2. fstat() the fd * * and keeping the file open for the rest of the time. But this can * only be done at network level. * * per default it is not a symlink * */ #ifdef HAVE_LSTAT sce->is_symlink = 0; /* we want to only check for symlinks if we should block symlinks. */ if (!con->conf.follow_symlink) { if (stat_cache_lstat(srv, name, &lst) == 0) { #ifdef DEBUG_STAT_CACHE log_error_write(srv, __FILE__, __LINE__, "sb", "found symlink", name); #endif sce->is_symlink = 1; } /* * we assume "/" can not be symlink, so * skip the symlink stuff if our path is / **/ else if (buffer_string_length(name) > 1) { buffer *dname; char *s_cur; dname = buffer_init(); buffer_copy_buffer(dname, name); while ((s_cur = strrchr(dname->ptr, '/'))) { buffer_string_set_length(dname, s_cur - dname->ptr); if (dname->ptr == s_cur) { #ifdef DEBUG_STAT_CACHE log_error_write(srv, __FILE__, __LINE__, "s", "reached /"); #endif break; } #ifdef DEBUG_STAT_CACHE log_error_write(srv, __FILE__, __LINE__, "sbs", "checking if", dname, "is a symlink"); #endif if (stat_cache_lstat(srv, dname, &lst) == 0) { sce->is_symlink = 1; #ifdef DEBUG_STAT_CACHE log_error_write(srv, __FILE__, __LINE__, "sb", "found symlink", dname); #endif break; }; }; buffer_free(dname); }; }; #endif if (S_ISREG(st.st_mode)) { /* determine mimetype */ buffer_reset(sce->content_type); #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR) if (con->conf.use_xattr) { stat_cache_attr_get(sce->content_type, name->ptr); } #endif /* xattr did not set a content-type. ask the config */ if (buffer_string_is_empty(sce->content_type)) { size_t namelen = buffer_string_length(name); for (k = 0; k < con->conf.mimetypes->used; k++) { data_string *ds = (data_string *)con->conf.mimetypes->data[k]; buffer *type = ds->key; size_t typelen = buffer_string_length(type); if (buffer_is_empty(type)) continue; /* check if the right side is the same */ if (typelen > namelen) continue; if (0 == strncasecmp(name->ptr + namelen - typelen, type->ptr, typelen)) { buffer_copy_buffer(sce->content_type, ds->value); break; } } } etag_create(sce->etag, &(sce->st), con->etag_flags); } else if (S_ISDIR(st.st_mode)) { etag_create(sce->etag, &(sce->st), con->etag_flags); } #ifdef HAVE_FAM_H if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) { /* is this directory already registered ? */ if (!dir_node) { fam_dir = fam_dir_entry_init(); buffer_copy_buffer(fam_dir->name, sc->dir_name); fam_dir->version = 1; fam_dir->req = calloc(1, sizeof(FAMRequest)); if (0 != FAMMonitorDirectory(&sc->fam, fam_dir->name->ptr, fam_dir->req, fam_dir)) { log_error_write(srv, __FILE__, __LINE__, "sbsbs", "monitoring dir failed:", fam_dir->name, "file:", name, FamErrlist[FAMErrno]); fam_dir_entry_free(&sc->fam, fam_dir); fam_dir = NULL; } else { int osize = 0; if (sc->dirs) { osize = sc->dirs->size; } sc->dirs = splaytree_insert(sc->dirs, dir_ndx, fam_dir); force_assert(sc->dirs); force_assert(sc->dirs->data == fam_dir); force_assert(osize == (sc->dirs->size - 1)); } } else { fam_dir = dir_node->data; } /* bind the fam_fc to the stat() cache entry */ if (fam_dir) { sce->dir_version = fam_dir->version; } } #endif *ret_sce = sce; return HANDLER_GO_ON; }
int main(int argc, char *argv[]) { scamper_file_t *file[2]; scamper_file_filter_t *filter; scamper_trace_t *trace; tracepair_t *pair, fm; uint16_t type = SCAMPER_FILE_OBJ_TRACE; char buf[256]; int i, filec_open; #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); #endif if(check_options(argc, argv) != 0) goto err; if((filter = scamper_file_filter_alloc(&type, 1)) == NULL) { fprintf(stderr, "could not allocate filter\n"); goto err; } memset(file, 0, sizeof(file)); for(i=0; i<filec; i++) { if((file[i] = scamper_file_open(files[i], 'r', NULL)) == NULL) { fprintf(stderr, "could not open %s\n", files[i]); goto err; } } filec_open = filec; if((pairs = splaytree_alloc(tracepair_cmp)) == NULL) { fprintf(stderr, "could not alloc tracepair tree\n"); goto err; } splaytree_onremove(pairs, (splaytree_onremove_t)tracepair_onremove); while(filec_open != 0) { for(i=0; i<filec; i++) { if(file[i] == NULL) continue; if(scamper_file_read(file[i], filter, &type, (void *)&trace) != 0) { fprintf(stderr, "could not read from %s\n", files[i]); goto err; } if(trace == NULL) { filec_open--; scamper_file_close(file[i]); continue; } assert(type == SCAMPER_FILE_OBJ_TRACE); fm.tracec = 1; fm.traces[0] = trace; if((pair = splaytree_find(pairs, &fm)) == NULL) { if((pair = malloc_zero(sizeof(tracepair_t))) == NULL) goto err; pair->traces[i] = trace; pair->tracec = 1; if((pair->node = splaytree_insert(pairs, pair)) == NULL) goto err; } else { if(pair->traces[i] != NULL) { fprintf(stderr, "repeated trace for %s\n", scamper_addr_tostr(trace->dst, buf, sizeof(buf))); goto err; } pair->traces[i] = trace; pair->tracec++; } if(pair->tracec != filec) continue; splaytree_remove_node(pairs, pair->node); tracepair_process(pair); tracepair_free(pair); } } return 0; err: return -1; }
static scamper_fd_t *fd_udp(int type, void *addr, uint16_t sport) { scamper_fd_t *fdn, findme; size_t len = 0; int fd = -1; findme.type = type; findme.fd_udp_addr = addr; findme.fd_udp_sport = sport; if((fdn = fd_find(&findme)) != NULL) return fdn; if(type == SCAMPER_FD_TYPE_UDP4) { fd = scamper_udp4_openraw(addr); len = sizeof(struct in_addr); } else if(type == SCAMPER_FD_TYPE_UDP6) { fd = scamper_udp6_open(addr, sport); len = sizeof(struct in6_addr); } else if(type == SCAMPER_FD_TYPE_UDP4DG) { fd = scamper_udp4_opendgram(addr, sport); len = sizeof(struct in_addr); } if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (addr != NULL && (fdn->fd_udp_addr = memdup(addr, len)) == NULL)) { printerror(errno, strerror, __func__, "could not open socket"); goto err; } fdn->fd_udp_sport = sport; if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL) { printerror(errno, strerror, __func__, "could not add socket to tree"); goto err; } if((fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { printerror(errno, strerror, __func__, "could not add socket to list"); goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_UDP4 || type == SCAMPER_FD_TYPE_UDP4DG) scamper_udp4_close(fd); else if(type == SCAMPER_FD_TYPE_UDP6) scamper_udp6_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }
scamper_firewall_entry_t *scamper_firewall_entry_get(scamper_firewall_rule_t *sfw) { scamper_firewall_entry_t findme, *entry = NULL; int n, af, p, sp, dp; void *s, *d; /* sanity check the rule */ if((sfw->sfw_5tuple_proto != IPPROTO_TCP && sfw->sfw_5tuple_proto != IPPROTO_UDP) || sfw->sfw_5tuple_sport == 0 || sfw->sfw_5tuple_dport == 0 || (sfw->sfw_5tuple_dst == NULL || sfw->sfw_5tuple_src == NULL || sfw->sfw_5tuple_src->type != sfw->sfw_5tuple_dst->type)) { scamper_debug(__func__, "invalid 5tuple rule"); goto err; } if(sfw->sfw_5tuple_src->type == SCAMPER_ADDR_TYPE_IPV4) { af = AF_INET; if(have_ipv4 == 0) { scamper_debug(__func__, "IPv4 rule requested but no IPv4 firewall"); goto err; } } else if(sfw->sfw_5tuple_src->type == SCAMPER_ADDR_TYPE_IPV6) { af = AF_INET6; if(have_ipv6 == 0) { scamper_debug(__func__, "IPv6 rule requested but no IPv6 firewall"); goto err; } } else { scamper_debug(__func__, "invalid src type"); goto err; } findme.rule = sfw; if((entry = splaytree_find(entries, &findme)) != NULL) { entry->refcnt++; return entry; } if((entry = firewall_entry_get()) == NULL) goto err; entry->refcnt = 1; if((entry->rule = firewall_rule_dup(sfw)) == NULL || (entry->node = splaytree_insert(entries, entry)) == NULL) { goto err; } n = entry->slot; p = sfw->sfw_5tuple_proto; dp = sfw->sfw_5tuple_dport; sp = sfw->sfw_5tuple_sport; s = sfw->sfw_5tuple_src->addr; if(sfw->sfw_5tuple_dst == NULL) d = NULL; else d = sfw->sfw_5tuple_dst->addr; #if defined(HAVE_IPFW) #ifdef WITHOUT_PRIVSEP if(scamper_firewall_ipfw_add(n, af, p, s, d, sp, dp) != 0) goto err; #else if(scamper_privsep_ipfw_add(n, af, p, s, d, sp, dp) != 0) goto err; #endif #endif return entry; err: if(entry != NULL) { if(entry->rule != NULL) firewall_rule_free(entry->rule); free(entry); } return NULL; }