1 /*
2  * scamper_tracelb_text.c
3  *
4  * Copyright (C) 2008-2011 The University of Waikato
5  * Copyright (C) 2012      The Regents of the University of California
6  * Author: Matthew Luckie
7  *
8  * $Id: scamper_tracelb_text.c,v 1.7 2020/03/17 07:32:16 mjl Exp $
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include "internal.h"
29 
30 #include "scamper_addr.h"
31 #include "scamper_list.h"
32 #include "scamper_tracelb.h"
33 #include "scamper_file.h"
34 #include "scamper_tracelb_text.h"
35 #include "utils.h"
36 
probeset_summary_tostr(scamper_tracelb_probeset_summary_t * sum,char * buf,size_t len,size_t * off)37 static void probeset_summary_tostr(scamper_tracelb_probeset_summary_t *sum,
38 				   char *buf, size_t len, size_t *off)
39 {
40   char dst[64];
41   int k;
42 
43   if(sum->nullc > 0 && sum->addrc == 0)
44     {
45       string_concat(buf, len, off, "*");
46       return;
47     }
48 
49   scamper_addr_tostr(sum->addrs[0], dst, sizeof(dst));
50   string_concat(buf, len, off, "(%s", dst);
51   for(k=1; k<sum->addrc; k++)
52     {
53       scamper_addr_tostr(sum->addrs[k], dst, sizeof(dst));
54       string_concat(buf, len, off, ", %s", dst);
55     }
56   if(sum->nullc > 0)
57     string_concat(buf, len, off, ", *)");
58   else
59     string_concat(buf, len, off, ")");
60 
61   return;
62 }
63 
scamper_file_text_tracelb_write(const scamper_file_t * sf,const scamper_tracelb_t * trace)64 int scamper_file_text_tracelb_write(const scamper_file_t *sf,
65 				    const scamper_tracelb_t *trace)
66 {
67   scamper_tracelb_probeset_summary_t *sum = NULL;
68   scamper_tracelb_probeset_t *set;
69   const scamper_tracelb_node_t *node;
70   scamper_tracelb_link_t *link;
71   size_t len;
72   size_t off;
73   char buf[1024], src[64], dst[64];
74   int fd = scamper_file_getfd(sf);
75   int i, j;
76 
77   snprintf(buf, sizeof(buf),
78 	   "tracelb from %s to %s, %d nodes, %d links, %d probes, %d%%\n",
79 	   scamper_addr_tostr(trace->src, src, sizeof(src)),
80 	   scamper_addr_tostr(trace->dst, dst, sizeof(dst)),
81 	   trace->nodec, trace->linkc, trace->probec, trace->confidence);
82 
83   len = strlen(buf);
84   write_wrap(fd, buf, NULL, len);
85 
86   for(i=0; i<trace->nodec; i++)
87     {
88       node = trace->nodes[i];
89 
90       if(node->addr != NULL)
91 	scamper_addr_tostr(node->addr, src, sizeof(src));
92       else
93 	snprintf(src, sizeof(src), "*");
94 
95       if(node->linkc > 1)
96 	{
97 	  for(j=0; j<node->linkc; j++)
98 	    {
99 	      scamper_addr_tostr(node->links[j]->to->addr, dst, sizeof(dst));
100 	      snprintf(buf, sizeof(buf), "%s -> %s\n", src, dst);
101 	      len = strlen(buf);
102 	      write_wrap(fd, buf, NULL, len);
103 	    }
104 	}
105       else if(node->linkc == 1)
106 	{
107 	  link = node->links[0];
108 	  off = 0;
109 
110 	  string_concat(buf, sizeof(buf), &off, "%s -> ", src);
111 	  for(j=0; j<link->hopc-1; j++)
112 	    {
113 	      set = link->sets[j];
114 	      if((sum = scamper_tracelb_probeset_summary_alloc(set)) == NULL)
115 		return -1;
116 	      probeset_summary_tostr(sum, buf, sizeof(buf), &off);
117 	      string_concat(buf, sizeof(buf), &off, " -> ");
118 	      scamper_tracelb_probeset_summary_free(sum); sum = NULL;
119 	    }
120 
121 	  if(link->to != NULL)
122 	    {
123 	      scamper_addr_tostr(link->to->addr, dst, sizeof(dst));
124 	      string_concat(buf, sizeof(buf), &off, "%s", dst);
125 	    }
126 	  else
127 	    {
128 	      set = link->sets[link->hopc-1];
129 	      if((sum = scamper_tracelb_probeset_summary_alloc(set)) == NULL)
130 		return -1;
131 	      probeset_summary_tostr(sum, buf, sizeof(buf), &off);
132 	      scamper_tracelb_probeset_summary_free(sum); sum = NULL;
133 	    }
134 
135 	  string_concat(buf, sizeof(buf), &off, "\n");
136 	  write_wrap(fd, buf, NULL, off);
137 	}
138     }
139 
140   return 0;
141 }
142