void update_tree_row(int row, GtkTreeIter *iter) { ip_t *addr; char str[256]="???", *name=str; addr = net_addr(row); if (addrcmp( (void *) addr, (void *) &unspec_addr, af)) { if ((name = dns_lookup(addr))) { if (show_ips) { snprintf(str, sizeof(str), "%s (%s)", name, strlongip(addr)); name = str; } } else name = strlongip(addr); } gtk_list_store_set(ReportStore, iter, COL_HOSTNAME, name, COL_LOSS, (float)(net_loss(row)/1000.0), COL_RCV, net_returned(row), COL_SNT, net_xmit(row), COL_LAST, net_last(row)/1000, COL_BEST, net_best(row)/1000, COL_AVG, net_avg(row)/1000, COL_WORST, net_worst(row)/1000, COL_STDEV, (float)(net_stdev(row)/1000.0), COL_COLOR, net_up(row) ? "black" : "red", -1); }
void split_redraw(void) { int max; int at; ip_t *addr; char newLine[MAX_LINE_SIZE]; int i; #if DEBUG fprintf(stderr, "split_redraw()\n"); #endif /* * If there is less lines than last time, we delete them * TEST THIS PLEASE */ max = net_max(); for (i=LineCount; i>max; i--) { printf("-%d\n", i); LineCount--; } /* * For each line, we compute the new one and we compare it to the old one */ for(at = 0; at < max; at++) { addr = net_addr(at); if(addrcmp((void*)addr, (void*)&unspec_addr, af)) { char str[256], *name; if (!(name = dns_lookup(addr))) name = strlongip(addr); if (show_ips) { snprintf(str, sizeof(str), "%s %s", name, strlongip(addr)); name = str; } /* May be we should test name's length */ snprintf(newLine, sizeof(newLine), "%s %d %d %d %d %d %d", name, net_loss(at), net_returned(at), net_xmit(at), net_best(at) /1000, net_avg(at)/1000, net_worst(at)/1000); } else { sprintf(newLine, "???"); } if (strcmp(newLine, Lines[at]) == 0) { /* The same, so do nothing */ #if DEBUG printf("SAME LINE\n"); #endif } else { printf("%d %s\n", at+1, newLine); fflush(stdout); strcpy(Lines[at], newLine); if (LineCount < (at+1)) { LineCount = at+1; } } } }
static ssize_t get(struct store *st, void *buf, size_t len, struct addr *at) { idx_t i; struct idxent ie; struct fstore *fst; struct logent le; struct addr v; char tmpbuf[STORE_MAXBLSZ]; fst = st->pdata; if((i = lookup(fst, at, NULL)) == -1) { errno = ENOENT; return(-1); } assert(!getidx(fst, i, &ie)); if(readall(fst->logfd, &le, sizeof(le), ie.off)) { flog(LOG_CRIT, "could not read log entry: %s", strerror(errno)); errno = EIO; return(-1); } if(memcmp(le.magic, LOGENTMAGIC, 4)) { flog(LOG_CRIT, "invalid magic in log"); errno = EIO; return(-1); } if(addrcmp(&le.name, at)) { flog(LOG_CRIT, "did not receive correct block from log"); errno = EIO; return(-1); } if(readall(fst->logfd, tmpbuf, le.len, ie.off + sizeof(le))) { flog(LOG_CRIT, "could not read log data: %s", strerror(errno)); errno = EIO; return(-1); } hash(tmpbuf, le.len, &v); if(addrcmp(&v, &le.name)) { flog(LOG_CRIT, "log data did not verify against hash"); errno = EIO; return(-1); } if(buf != NULL) memcpy(buf, tmpbuf, min(len, le.len)); return(le.len); }
static int get_acl_callback(const char *w, int f, void *dummy) { struct get_acl *ga=(struct get_acl *)dummy; if (addrcmp(w, ga->who) == 0) ga->flags=f; return (0); }
static size_t snprint_addr(char *dst, size_t dst_len, ip_t *addr) { if(addrcmp((void *) addr, (void *) &unspec_addr, af)) { struct hostent *host = dns ? addr2host((void *) addr, af) : NULL; if (!host) return snprintf(dst, dst_len, "%s", strlongip(addr)); else if (dns && show_ips) return snprintf(dst, dst_len, "%s (%s)", host->h_name, strlongip(addr)); else return snprintf(dst, dst_len, "%s", host->h_name); } else return snprintf(dst, dst_len, "%s", "???"); }
static int addrcmp(const char *a, const char *b) { char *aa=NULL; const char *h=auth_myhostname(); int rc; if (!h) return (1); if (strchr(a, '@') == NULL) { aa=malloc(strlen(a)+strlen(h)+2); if (!aa) { syslog(LOG_NOTICE, "malloc: out of memory."); return (1); } strcat(strcat(strcpy(aa, a), "@"), h); rc=addrcmp(aa, b); free(aa); return (rc); } if (strchr(b, '@') == NULL) { aa=malloc(strlen(b)+strlen(h)+2); if (!aa) { syslog(LOG_NOTICE, "malloc: out of memory."); return (1); } strcat(strcat(strcpy(aa, b), "@"), h); rc=addrcmp(a, aa); free(aa); return (rc); } rc=strcasecmp(a, b); return (rc); }
static int put(struct store *st, const void *buf, size_t len, struct addr *at) { struct fstore *fst; struct addr pa; idx_t i, pi; struct idxent ie; loff_t leoff; int c; struct logent le; if(len > STORE_MAXBLSZ) { errno = E2BIG; return(-1); } fst = st->pdata; hash(buf, len, &pa); if(at != NULL) memcpy(at->hash, pa.hash, 32); if(lookup(fst, &pa, &pi) != -1) return(0); memcpy(le.magic, LOGENTMAGIC, 4); le.name = pa; le.len = len; le.fl = 0; /* XXX: Thread safety { */ leoff = fst->logsize; fst->logsize += sizeof(le) + len; /* } */ /* XXX: Handle data with embedded LOGENTMAGIC */ writeall(fst->logfd, &le, sizeof(le), leoff); writeall(fst->logfd, buf, len, leoff + sizeof(le)); i = newindex(fst); assert(!getidx(fst, i, &ie)); ie.addr = pa; ie.off = leoff; assert(!putidx(fst, i, &ie)); if(pi != -1) { assert(!getidx(fst, pi, &ie)); c = addrcmp(&pa, &ie.addr); if(c < 0) ie.l = i; else ie.r = i; assert(!putidx(fst, pi, &ie)); } return(0); }
/* * This gets called if the number of connections grows * past "max_connections". * * We kill the newest connection from a duplicate IP. */ static void kill_some_child(void) { const struct child *blanket, *next; if (!(blanket = firstborn)) return; for (; (next = blanket->next); blanket = next) if (!addrcmp(&blanket->address, &next->address)) { kill(blanket->cld.pid, SIGTERM); break; } }
static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_t addrlen) { struct child *newborn, **cradle; newborn = xcalloc(1, sizeof(*newborn)); live_children++; memcpy(&newborn->cld, cld, sizeof(*cld)); memcpy(&newborn->address, addr, addrlen); for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next) if (!addrcmp(&(*cradle)->address, &newborn->address)) break; newborn->next = *cradle; *cradle = newborn; }
static void add_child(pid_t pid, struct sockaddr *addr, int addrlen) { struct child *newborn, **cradle; newborn = xcalloc(1, sizeof(*newborn)); live_children++; newborn->pid = pid; memcpy(&newborn->address, addr, addrlen); for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next) if (!addrcmp(&(*cradle)->address, &newborn->address)) break; newborn->next = *cradle; *cradle = newborn; }
struct near_node *get_near_by_addr(const struct sockaddr_in *addr) { struct near_node * iterator = near_list_head; while(iterator != NULL){ if (addrcmp(&iterator->addr, addr)) { break; } iterator = iterator->next; } return iterator; }
/* * Funzione che ritorna il nodo contenente l'indirizzo passato per parametro. * Ritorna un puntatore al nodo che contiene l'indirizzo passato per parametro, * oppure NULL se l'indirizzo non è presente in lista. */ struct node *get_node_peer(const struct sockaddr_in *peer_addr) { struct node *tmp_node; tmp_node = peer_list_head; while (tmp_node != NULL) { //ret = (struct spaddr_node *)tmp_node->data; if (addrcmp(&((struct peer_node *)tmp_node->data)->peer_addr, peer_addr)) { return tmp_node; } tmp_node = tmp_node->next; } return NULL; }
void mtr_curses_graph(int startstat, int cols) { int max, at, y; ip_t * addr; char* name; max = net_max(); for (at = display_offset; at < max; at++) { printw("%2d. ", at+1); addr = net_addr(at); if (!addr) { printw("???\n"); continue; } if (! net_up(at)) attron(A_BOLD); if (addrcmp((void *) addr, (void *) &unspec_addr, af)) { #ifdef IPINFO if (is_printii()) printw(fmt_ipinfo(addr)); #endif name = dns_lookup(addr); printw("%s", name?name:strlongip(addr)); } else printw("???"); attroff(A_BOLD); getyx(stdscr, y, __unused_int); move(y, startstat); printw(" "); mtr_fill_graph(at, cols); printw("\n"); } }
static struct proxy_list *proxy(const char *proxy_userid, char **errmsg) { struct proxy_list *p; if (errmsg) *errmsg=0; for (p=proxy_list; p; p=p->next) { if (addrcmp(proxy_userid, p->userid) == 0) return (p); } p=malloc(sizeof(struct proxy_list)); if (!p) return (NULL); memset(p, 0, sizeof(*p)); if ((p->userid=strdup(proxy_userid)) == NULL) { free(p); return (NULL); } if ((p->proxy=pcp_find_proxy(proxy_userid, NULL, errmsg)) == NULL || pcp_set_proxy(p->proxy, userid)) { if (p->proxy) pcp_close(p->proxy); free(p->userid); free(p); return (NULL); } p->next=proxy_list; proxy_list=p; return (p); }
/* Returns NULL if you have to send an update to everyone, * or the mapping to send it to. :) */ struct tMapping *dopacket(char *buffer,int length,struct sockaddr_in address) { struct tMapping *entry = findMapping(buffer); int update = 0; if (!entry) { update = 1; entry = malloc(sizeof(struct tMapping)); entry->next = userList; userList=entry; entry->mac=strndup(buffer,6*2+5); } if (addrcmp(entry->address,address)!=0) { entry->address = address; update = 1; } entry->lastseen = time(NULL); entry->version = 1; syslog(LOG_DEBUG,"Recieved update from '%s' (%s)", entry->mac, inet_ntoa(entry->address.sin_addr) ); return (!update) ? entry : NULL; }
void mtr_curses_hosts(int startstat) { int max; int at; struct mplslen *mpls, *mplss; ip_t *addr, *addrs; int y; char *name; int i, j, k; int hd_len; char buf[1024]; max = net_max(); for(at = net_min () + display_offset; at < max; at++) { printw("%2d. ", at + 1); addr = net_addr(at); mpls = net_mpls(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { name = dns_lookup(addr); if (! net_up(at)) attron(A_BOLD); #ifdef IPINFO if (is_printii()) printw(fmt_ipinfo(addr)); #endif if(name != NULL) { if (show_ips) printw("%s (%s)", name, strlongip(addr)); else printw("%s", name); } else { printw("%s", strlongip( addr ) ); } attroff(A_BOLD); getyx(stdscr, y, __unused_int); move(y, startstat); /* net_xxx returns times in usecs. Just display millisecs */ hd_len = 0; for( i=0; i<MAXFLD; i++ ) { /* Ignore options that don't exist */ /* On the other hand, we now check the input side. Shouldn't happen, can't be careful enough. */ j = fld_index[fld_active[i]]; if (j == -1) continue; /* temporay hack for stats usec to ms... */ if( index( data_fields[j].format, 'f' ) ) { sprintf(buf + hd_len, data_fields[j].format, data_fields[j].net_xxx(at) /1000.0 ); } else { sprintf(buf + hd_len, data_fields[j].format, data_fields[j].net_xxx(at) ); } hd_len += data_fields[j].length; } buf[hd_len] = 0; printw("%s", buf); for (k=0; k < mpls->labels && enablempls; k++) { if((k+1 < mpls->labels) || (mpls->labels == 1)) { /* if we have more labels */ printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } else { /* bottom label */ printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } } /* Multi path */ for (i=0; i < MAXPATH; i++ ) { addrs = net_addrs(at, i); mplss = net_mplss(at, i); if ( addrcmp( (void *) addrs, (void *) addr, af ) == 0 ) continue; if ( addrcmp( (void *) addrs, (void *) &unspec_addr, af ) == 0 ) break; name = dns_lookup(addrs); if (! net_up(at)) attron(A_BOLD); printw("\n "); #ifdef IPINFO if (is_printii()) printw(fmt_ipinfo(addrs)); #endif if (name != NULL) { if (show_ips) printw("%s (%s)", name, strlongip(addrs)); else printw("%s", name); } else { printw("%s", strlongip( addrs ) ); } for (k=0; k < mplss->labels && enablempls; k++) { printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mplss->label[k], mplss->exp[k], mplss->s[k], mplss->ttl[k]); } attroff(A_BOLD); } } else { printw("???"); } printw("\n"); } move(2, 0); }
int pkb_check_kites_dns(struct pk_manager* pkm) { int i, j, rv; int in_dns = 0; int recently_in_dns = 0; time_t ddns_window; struct pk_tunnel* fe; struct pk_tunnel* dns_fe; struct pk_pagekite* kite; struct addrinfo hints; struct addrinfo *result, *rp; char buffer[128]; int cleared_flags = 0; PK_TRACE_FUNCTION; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* Walk through kite list, look each up in DNS and update the * tunnel flags as appropriate. */ for (i = 0, kite = pkm->kites; i < pkm->kite_max; i++, kite++) { rv = getaddrinfo(kite->public_domain, NULL, &hints, &result); if (rv == 0) { if (!cleared_flags) { /* Clear DNS flag everywhere, once we know DNS is responding. */ for (j = 0, fe = pkm->tunnels; j < pkm->tunnel_max; j++, fe++) { fe->conn.status &= ~FE_STATUS_IN_DNS; } cleared_flags = 1; } for (rp = result; rp != NULL; rp = rp->ai_next) { for (j = 0, fe = pkm->tunnels; j < pkm->tunnel_max; j++, fe++) { if (fe->ai.ai_addr && fe->fe_hostname) { if (0 == addrcmp(fe->ai.ai_addr, rp->ai_addr)) { pk_log(PK_LOG_MANAGER_DEBUG, "In DNS for %s: %s", kite->public_domain, in_ipaddr_to_str(fe->ai.ai_addr, buffer, 128)); fe->conn.status |= FE_STATUS_IN_DNS; fe->last_ddnsup = time(0); in_dns++; } } } } freeaddrinfo(result); } } /* * If flags weren't cleared, then the network is probably down: bail out! */ if (!cleared_flags) return 1; /* FIXME: We should really get this from the TTL of the DNS record itself, * not from a hard coded magic number. */ ddns_window = time(0) - PK_DDNS_UPDATE_INTERVAL_MIN; /* Walk through the list of tunnels and rewnew the FE_STATUS_IN_DNS * if they were either last updated within our window. */ dns_fe = NULL; for (j = 0, fe = pkm->tunnels; j < pkm->tunnel_max; j++, fe++) { if (fe->ai.ai_addr && fe->fe_hostname) { if (fe->last_ddnsup > ddns_window) { fe->conn.status |= FE_STATUS_IN_DNS; in_dns++; } /* Figure out which FE was most recently seen in DNS, for use below */ if (fe->last_ddnsup > recently_in_dns) { recently_in_dns = fe->last_ddnsup; dns_fe = fe; } } } /* If nothing was found in DNS, but we know there was stuff in DNS * before, then DNS is probably broken for us and the data in DNS is * unchanged. Keep the most recent one active! This is incomplete if * we are using many tunnels at once, but still better than nothing. */ if (in_dns < 1 && dns_fe) { dns_fe->conn.status |= FE_STATUS_IN_DNS; } PK_CHECK_MEMORY_CANARIES; return 0; }
CLIENT * get_client(struct sockaddr *host_addr, rpcvers_t vers) { CLIENT *client; struct timeval retry_time, time_now; int error, i; const char *netid; struct netconfig *nconf; char host[NI_MAXHOST]; uid_t old_euid; int clnt_fd; gettimeofday(&time_now, NULL); /* * Search for the given client in the cache, zapping any expired * entries that we happen to notice in passing. */ for (i = 0; i < CLIENT_CACHE_SIZE; i++) { client = clnt_cache_ptr[i]; if (client && ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME) < time_now.tv_sec)) { /* Cache entry has expired. */ if (debug_level > 3) syslog(LOG_DEBUG, "Expired CLIENT* in cache"); clnt_cache_time[i] = 0L; clnt_destroy(client); clnt_cache_ptr[i] = NULL; client = NULL; } if (client && !addrcmp((struct sockaddr *)&clnt_cache_addr[i], host_addr) && clnt_cache_vers[i] == vers) { /* Found it! */ if (debug_level > 3) syslog(LOG_DEBUG, "Found CLIENT* in cache"); return (client); } } if (debug_level > 3) syslog(LOG_DEBUG, "CLIENT* not found in cache, creating"); /* Not found in cache. Free the next entry if it is in use. */ if (clnt_cache_ptr[clnt_cache_next_to_use]) { clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]); clnt_cache_ptr[clnt_cache_next_to_use] = NULL; } /* * Need a host string for clnt_tp_create. Use NI_NUMERICHOST * to avoid DNS lookups. */ error = getnameinfo(host_addr, host_addr->sa_len, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error != 0) { syslog(LOG_ERR, "unable to get name string for caller: %s", gai_strerror(error)); return NULL; } #if 1 if (host_addr->sa_family == AF_INET6) netid = "udp6"; else netid = "udp"; #else if (host_addr->sa_family == AF_INET6) netid = "tcp6"; else netid = "tcp"; #endif nconf = getnetconfigent(netid); if (nconf == NULL) { syslog(LOG_ERR, "could not get netconfig info for '%s': " "no /etc/netconfig file?", netid); return NULL; } client = clnt_tp_create(host, NLM_PROG, vers, nconf); freenetconfigent(nconf); if (!client) { syslog(LOG_ERR, "%s", clnt_spcreateerror("clntudp_create")); syslog(LOG_ERR, "Unable to return result to %s", host); return NULL; } /* Get the FD of the client, for bindresvport. */ clnt_control(client, CLGET_FD, &clnt_fd); /* Regain root privileges, for bindresvport. */ old_euid = geteuid(); seteuid(0); /* * Bind the client FD to a reserved port. * Some NFS servers reject any NLM request from a non-reserved port. */ bindresvport(clnt_fd, NULL); /* Drop root privileges again. */ seteuid(old_euid); /* Success - update the cache entry */ clnt_cache_ptr[clnt_cache_next_to_use] = client; memcpy(&clnt_cache_addr[clnt_cache_next_to_use], host_addr, host_addr->sa_len); clnt_cache_vers[clnt_cache_next_to_use] = vers; clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec; if (++clnt_cache_next_to_use >= CLIENT_CACHE_SIZE) clnt_cache_next_to_use = 0; /* * Disable the default timeout, so we can specify our own in calls * to clnt_call(). (Note that the timeout is a different concept * from the retry period set in clnt_udp_create() above.) */ retry_time.tv_sec = -1; retry_time.tv_usec = -1; clnt_control(client, CLSET_TIMEOUT, (char *)&retry_time); if (debug_level > 3) syslog(LOG_DEBUG, "Created CLIENT* for %s", host); return client; }
void split_redraw(void) { int max; int at; ip_t *addr, *addrs; char newLine[MAX_LINE_SIZE]; int i; #if DEBUG SPLIT_PRINT(("split_redraw()")); #endif /* * If there is less lines than last time, we delete them * TEST THIS PLEASE */ max = net_max(); for (i=LineCount; i>max; i--) { SPLIT_PRINT(("-%d", i)); LineCount--; } /* * For each line, we compute the new one and we compare it to the old one */ for(at = 0; at < max; at++) { addr = net_addr(at); if(addrcmp((void*)addr, (void*)&unspec_addr, af)) { char str[256], *name; if (!(name = dns_lookup(addr))) name = strlongip(addr); if (show_ips) { snprintf(str, sizeof(str), "%s\t%s", name, strlongip(addr)); name = str; } /* May be we should test name's length */ snprintf(newLine, sizeof(newLine), "%s\t%s\t%.1f\t%d\t%d\t%.1f\t%.1f\t%.1f\t%.1f", name, fmt_ipinfo(addr), net_loss(at)/1000.0, net_returned(at), net_xmit(at), net_best(at) /1000.0, net_avg(at)/1000.0, net_worst(at)/1000.0, net_stdev(at)/1000.0); } else { sprintf(newLine, "???"); } if (strcmp(newLine, Lines[at]) == 0) { /* The same, so do nothing */ #if DEBUG SPLIT_PRINT(("SAME LINE")); #endif } else { SPLIT_PRINT(("%d\t%s", at+1, newLine)); if (strcmp(newLine, "???") != 0) { /* Multi path */ for (i=0; i < MAXPATH; i++ ) { addrs = net_addrs(at, i); if ( addrcmp( (void *) addrs, (void *) addr, af ) == 0 ) continue; if ( addrcmp( (void *) addrs, (void *) &unspec_addr, af ) == 0 ) break; char *name; if (!(name = dns_lookup(addrs))) name = strlongip(addrs); if (show_ips) { SPLIT_PRINT(("-\t%d\t%d\t%s\t%s\t%s", at+1, i+1, name, strlongip(addrs), fmt_ipinfo(addrs))); } else { SPLIT_PRINT(("-\t%d\t%d\t%s\t%s", at+1, i+1, name, fmt_ipinfo(addrs))); } } } fflush(stdout); strcpy(Lines[at], newLine); if (LineCount < (at+1)) { LineCount = at+1; } } } }
void report_close(void) { int i, j, at, max; ip_t *addr; char name[81]; char buf[1024]; char fmt[16]; int len=0; int len_hosts = 33; struct hostent *host; if (reportwide) { // get the longest hostname len_hosts = strlen(LocalHostname); max = net_max(); at = net_min(); for (; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { snprintf(name, sizeof(name), "%s", strlongip( addr ) ); } if (len_hosts < strlen(name)) { len_hosts = strlen(name); } } } } snprintf( fmt, sizeof(fmt), "HOST: %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, LocalHostname); len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[i]]; if (j < 0) continue; snprintf( fmt, sizeof(fmt), "%%%ds", data_fields[j].length ); snprintf( buf + len, sizeof(buf), fmt, data_fields[j].title ); len += data_fields[j].length; } printf("%s\n",buf); max = net_max(); at = net_min(); for(; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) == 0 ) { sprintf(name, "???"); } else { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { snprintf(name, sizeof(name), "%s", strlongip( addr ) ); } } snprintf( fmt, sizeof(fmt), " %%2d. %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, at+1, name); len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active [i]]; if (j < 0) continue; /* 1000.0 is a temporay hack for stats usec to ms, impacted net_loss. */ if( index( data_fields[j].format, 'f' ) ) { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) /1000.0 ); } else { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) ); } len += data_fields[j].length; } printf("%s\n",buf); } }
void csv_close(void) { int i, j, at, max; ip_t *addr; char name[81]; struct hostent *host; /* Caption */ printf("<SRC=%s DST=%s", LocalHostname, Hostname); printf(" TOS=0x%X", tos); if(cpacketsize >= 0) { printf(" PSIZE=%d", cpacketsize); } else { printf(" PSIZE=rand(%d-%d)",MINPACKET, -cpacketsize); } if( bitpattern>=0 ) { printf(" BITPATTERN=0x%02X", (unsigned char)(bitpattern)); } else { printf(" BITPATTERN=rand(0x00-FF)"); } printf(" TESTS=%d>\n", MaxPing); /* Header */ printf("HUPCOUNT, HOST"); for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[i]]; if (j < 0) continue; printf( ", %s", data_fields[j].title ); } printf("\n"); max = net_max(); at = net_min(); for(; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) == 0 ) { sprintf(name, "???"); } else { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { sprintf(name, "%s", strlongip( addr ) ); } } printf("%d, %s", at+1, name); for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[j]]; if (j < 0) continue; /* 1000.0 is a temporay hack for stats usec to ms, impacted net_loss. */ if( index( data_fields[j].format, 'f' ) ) { printf( ", %.2f", data_fields[j].net_xxx(at) / 1000.0); } else { printf( ", %d", data_fields[j].net_xxx(at) ); } } printf("\n"); } }
void gc_redraw(void) { gc_keyaction(cr_dispatch_event()); if (paused) return; int i, at; int min = net_min(); int max = net_max(); int hops = max - min /* + 1 */; if (!hops) hops++; cr_set_hops(hops, min); struct timeval now; gettimeofday(&now, NULL); int dt = (now.tv_sec - lasttime.tv_sec) * USECONDS + (now.tv_usec - lasttime.tv_usec); lasttime = now; if (dt < timeout) { int pings = net_xmit(min); for (at = min + 1; at < max; at++) if (net_xmit(at) != pings) return; if (pings > num_pings) num_pings = pings; else return; } if (params.enable_legend) { static int hi_max; if (!hostinfo_max) hi_max = 0; if (hostinfo_max > hi_max) { hi_max = hostinfo_max; curses_cols = cr_recalc(hostinfo_max); pr_lastd(); } cr_init_legend(); cr_print_legend_header(display_mode ? legend_hd[LEGEND_HEADER] : legend_hd[LEGEND_HEADER_STATIC]); } for (i = 0, at = min; i < hops; i++, at++) { ip_t *addr = net_addr(at); if (addrcmp((void *)addr, (void *)&unspec_addr, af) != 0) { int *saved = net_saved_pings(at); int saved_ndx = SAVED_PINGS - 2; // waittime ago if (params.jitter_graph) { // jitter, defined as "tN - tN-1" (net.c) if ((saved[saved_ndx] < 0) || (saved[saved_ndx - 1] < 0)) // unsent, unknown, etc. data[i] = -1; else { int saved_jttr = saved[saved_ndx] - saved[saved_ndx - 1]; data[i] = (saved_jttr < 0) ? -saved_jttr : saved_jttr; } } else data[i] = (saved[saved_ndx] >= 0) ? saved[saved_ndx] : -1; if (params.enable_legend) { // line+hop cr_print_hop(i); // hostinfo fill_hostinfo(at, addr); char *stat = buf + strlen(buf) + 1; // statistics if (display_mode) { mtr_gen_scale_gc(); char *pos = stat; int j; #ifdef UNICODE if (display_mode == 3) { for (j = SAVED_PINGS - curses_cols; j < SAVED_PINGS; j++) { *(wchar_t*)pos = mtr_curses_saved_wch(saved[j]); pos += sizeof(wchar_t); } *(wchar_t*)pos = L'\0'; } else #endif { for (j = SAVED_PINGS - curses_cols; j < SAVED_PINGS; j++) *pos++ = mtr_curses_saved_ch(saved[j]); *pos = 0; } } else mtr_fill_data(at, stat); cr_print_host(i, data[i], buf, stat); // mpls if (enablempls) gc_print_mpls(i, data[i], net_mpls(at)); // multipath if (params.enable_multipath) { int j; for (j = 0; j < MAXPATH; j++) { ip_t *addrs = net_addrs(at, j); if (addrcmp((void *)addrs, (void *)addr, af) == 0) continue; if (addrcmp((void *)addrs, (void *)&unspec_addr, af) == 0) break; fill_hostinfo(at, addrs); cr_print_host(i, data[i], buf, NULL); if (enablempls) // multipath+mpls gc_print_mpls(i, data[i], net_mplss(at, j)); } } } } else // empty hop if (params.enable_legend) { cr_print_hop(i); cr_print_host(i, 0, NULL, NULL); } } if (params.enable_legend) if (display_mode) { mtr_curses_scale_desc(legend_hd[LEGEND_FOOTER]); cr_print_legend_footer(legend_hd[LEGEND_FOOTER]); } cr_redraw(data); if (hops) timeout = POS_ROUND(((WaitTime * hops) / NUMHOSTS) * USECONDS); }
void report_close(void) { int i, j, at, max, z, w; struct mplslen *mpls, *mplss; ip_t *addr; ip_t *addr2 = NULL; char name[81]; char buf[1024]; char fmt[16]; int len=0; int len_hosts = 33; if (reportwide) { // get the longest hostname len_hosts = strlen(LocalHostname); max = net_max(); at = net_min(); for (; at < max; at++) { int nlen; addr = net_addr(at); if ((nlen = snprint_addr(name, sizeof(name), addr))) if (len_hosts < nlen) len_hosts = nlen; } } #ifndef NO_IPINFO int len_tmp = len_hosts; if (ipinfo_no >= 0) { ipinfo_no %= iiwidth_len; if (reportwide) { len_hosts++; // space len_tmp += get_iiwidth(); if (!ipinfo_no) len_tmp += 2; // align header: AS } } snprintf( fmt, sizeof(fmt), "HOST: %%-%ds", len_tmp); #else snprintf( fmt, sizeof(fmt), "HOST: %%-%ds", len_hosts); #endif snprintf(buf, sizeof(buf), fmt, LocalHostname); len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[i]]; if (j < 0) continue; snprintf( fmt, sizeof(fmt), "%%%ds", data_fields[j].length ); snprintf( buf + len, sizeof(buf), fmt, data_fields[j].title ); len += data_fields[j].length; } printf("%s\n",buf); max = net_max(); at = net_min(); for(; at < max; at++) { addr = net_addr(at); mpls = net_mpls(at); snprint_addr(name, sizeof(name), addr); #ifndef NO_IPINFO if (is_printii()) { snprintf(fmt, sizeof(fmt), " %%2d. %%s%%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, at+1, fmt_ipinfo(addr), name); } else { #endif snprintf( fmt, sizeof(fmt), " %%2d.|-- %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, at+1, name); #ifndef NO_IPINFO } #endif len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active [i]]; if (j < 0) continue; /* 1000.0 is a temporay hack for stats usec to ms, impacted net_loss. */ if( index( data_fields[j].format, 'f' ) ) { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) /1000.0 ); } else { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) ); } len += data_fields[j].length; } printf("%s\n",buf); /* This feature shows 'loadbalances' on routes */ /* z is starting at 1 because addrs[0] is the same that addr */ for (z = 1; z < MAXPATH ; z++) { addr2 = net_addrs(at, z); mplss = net_mplss(at, z); int found = 0; if ((addrcmp ((void *) &unspec_addr, (void *) addr2, af)) == 0) break; for (w = 0; w < z; w++) /* Ok... checking if there are ips repeated on same hop */ if ((addrcmp ((void *) addr2, (void *) net_addrs (at,w), af)) == 0) { found = 1; break; } if (!found) { #ifndef NO_IPINFO if (is_printii()) { if (mpls->labels && z == 1 && enablempls) print_mpls(mpls); snprint_addr(name, sizeof(name), addr2); printf(" %s%s\n", fmt_ipinfo(addr2), name); if (enablempls) print_mpls(mplss); } else { #else int k; if (mpls->labels && z == 1 && enablempls) { for (k=0; k < mpls->labels; k++) { printf(" | |+-- [MPLS: Lbl %lu Exp %u S %u TTL %u]\n", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } } if (z == 1) { printf (" | `|-- %s\n", strlongip(addr2)); for (k=0; k < mplss->labels && enablempls; k++) { printf(" | +-- [MPLS: Lbl %lu Exp %u S %u TTL %u]\n", mplss->label[k], mplss->exp[k], mplss->s[k], mplss->ttl[k]); } } else { printf (" | |-- %s\n", strlongip(addr2)); for (k=0; k < mplss->labels && enablempls; k++) { printf(" | +-- [MPLS: Lbl %lu Exp %u S %u TTL %u]\n", mplss->label[k], mplss->exp[k], mplss->s[k], mplss->ttl[k]); } } #endif #ifndef NO_IPINFO } #endif } } /* No multipath */ #ifndef NO_IPINFO if (is_printii()) { if (mpls->labels && z == 1 && enablempls) print_mpls(mpls); } else { #else if(mpls->labels && z == 1 && enablempls) { int k; for (k=0; k < mpls->labels; k++) { printf(" | +-- [MPLS: Lbl %lu Exp %u S %u TTL %u]\n", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } } #endif #ifndef NO_IPINFO } #endif } }
void mtr_curses_hosts(int startstat) { int max; int at; struct mplslen *mpls, *mplss; ip_t *addr, *addrs; int y; char *name; char *country,*province,*city,*district,*isp,*type,*desc; int icountry,iprovince,icity,idistrict,iisp,itype,idesc; int i, j, k; int hd_len; char buf[1024]; max = net_max(); for(at = net_min () + display_offset; at < max; at++) { printw("%2d. ", at + 1); addr = net_addr(at); mpls = net_mpls(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { name = dns_lookup(addr); if (! net_up(at)) attron(A_BOLD); #ifdef IPINFO if (is_printii()) printw(fmt_ipinfo(addr)); #endif if(name != NULL) { if (show_ips) printw("%s (%s)", name, strlongip(addr)); else printw("%s", name); } else { printw("%s", strlongip( addr ) ); } attroff(A_BOLD); getyx(stdscr, y, __unused_int); #ifdef ENABLE_BILIIP if (ip_resolve) { long key = biliip_query(biliip,ntohl(*(unsigned long *)addr)); if (key != 0) { if (biliip->index_ptr[key] == 0) { biliip->ptr = BILIIP_BASE_PTR+(biliip->nCount*2+key-1)*sizeof(int); biliip->index_ptr[key] = htonl(*(int *)(biliip->mmap+biliip->ptr)); } biliip->ptr = biliip->index_ptr[key]+4; country = biliip_getstr(biliip,&icountry); province = biliip_getstr(biliip,&iprovince); city = biliip_getstr(biliip,&icity); district = biliip_getstr(biliip,&idistrict); isp = biliip_getstr(biliip,&iisp); type = biliip_getstr(biliip,&itype); desc = biliip_getstr(biliip,&idesc); char biliip_buf[64]; char *biliip_last = biliip_buf; if (startstat > 70) { if (icountry > 0 && country) { biliip_last += sprintf(biliip_last, "%.*s", icountry, country); } move(y, startstat - 50); attron(A_BOLD | COLOR_PAIR(4)); printw("%s", biliip_buf); attroff(A_BOLD | COLOR_PAIR(4)); biliip_last = biliip_buf; *biliip_last = '\0'; if (iprovince > 0 && province) { biliip_last += sprintf(biliip_last, " %.*s", iprovince, province); while (biliip_last > biliip_buf && *(biliip_last-1) == ' ') *--biliip_last = '\0'; printw("%s", biliip_buf); biliip_last = biliip_buf; *biliip_last = '\0'; } if (icity > 0 && city && (iprovince != icity || strncmp(province, city, icity) != 0)) { biliip_last += sprintf(biliip_last, " %.*s", icity, city); while (biliip_last > biliip_buf && *(biliip_last-1) == ' ') *--biliip_last = '\0'; printw("%s", biliip_buf); biliip_last = biliip_buf; *biliip_last = '\0'; } } if (iisp > 0 && isp) { biliip_last += sprintf(biliip_last, "%.*s", iisp, isp); while (biliip_last > biliip_buf && *(biliip_last-1) == ' ') *--biliip_last = '\0'; } if (idesc > 0 && desc) { biliip_last += sprintf(biliip_last, " %.*s", idesc, desc); while (biliip_last > biliip_buf && *(biliip_last-1) == ' ') *--biliip_last = '\0'; } move(y, startstat - 25); attron(A_BOLD | COLOR_PAIR(7)); printw("%s", biliip_buf); attroff(A_BOLD | COLOR_PAIR(7)); } } #endif move(y, startstat); /* net_xxx returns times in usecs. Just display millisecs */ hd_len = 0; for( i=0; i<MAXFLD; i++ ) { /* Ignore options that don't exist */ /* On the other hand, we now check the input side. Shouldn't happen, can't be careful enough. */ j = fld_index[fld_active[i]]; if (j == -1) continue; /* temporay hack for stats usec to ms... */ if( index( data_fields[j].format, 'f' ) ) { sprintf(buf + hd_len, data_fields[j].format, data_fields[j].net_xxx(at) /1000.0 ); } else { sprintf(buf + hd_len, data_fields[j].format, data_fields[j].net_xxx(at) ); } hd_len += data_fields[j].length; } buf[hd_len] = 0; printw("%s", buf); for (k=0; k < mpls->labels && enablempls; k++) { if((k+1 < mpls->labels) || (mpls->labels == 1)) { /* if we have more labels */ printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } else { /* bottom label */ printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mpls->label[k], mpls->exp[k], mpls->s[k], mpls->ttl[k]); } } // printw("aasdfasdf"); /* Multi path */ for (i=0; i < MAXPATH; i++ ) { addrs = net_addrs(at, i); mplss = net_mplss(at, i); if ( addrcmp( (void *) addrs, (void *) addr, af ) == 0 ) continue; if ( addrcmp( (void *) addrs, (void *) &unspec_addr, af ) == 0 ) break; name = dns_lookup(addrs); if (! net_up(at)) attron(A_BOLD); printw("\n "); #ifdef IPINFO if (is_printii()) printw(fmt_ipinfo(addrs)); #endif if (name != NULL) { if (show_ips) printw("%s (%s)", name, strlongip(addrs)); else printw("%s", name); } else { printw("%s", strlongip( addrs ) ); } for (k=0; k < mplss->labels && enablempls; k++) { printw("\n [MPLS: Lbl %lu Exp %u S %u TTL %u]", mplss->label[k], mplss->exp[k], mplss->s[k], mplss->ttl[k]); } attroff(A_BOLD); } } else { printw("???"); } printw("\n"); } move(2, 0); }