Revision 534e126a

View differences:

include/net_helper.h
118 118
*
119 119
* Give a string representation of a nodeID.
120 120
* @param[in] s A pointer to the nodeID to be printed.
121
* @return A string representing the nodeID.
121
* @param[out] addr A pointer to the buffer where to write the address
122
* @param[in] len The length of the addr buffer
123
* @return >= 0 on success, or < 0 on error (buffer too small)
122 124
*/
123
const char *node_addr(const struct nodeID *s);
125
int node_addr(const struct nodeID *s, char *addr, int len);
124 126

  
125 127
/**
126 128
* @brief Create a nodeID structure from a serialized object.
......
148 150
*
149 151
* Serialize the public IP address of a given node and return it.
150 152
* @param[in] s A pointer to the nodeID.
151
* @return the publicly accessible IP address of the node.
153
* @param[out] ip A  pointer to the buffer where to store the ip address
154
* @param[in] len The length of the ip buffer
155
* @return >= 0 on success, or < 0 on error (buffer too small)
152 156
*/
153
const char *node_ip(const struct nodeID *s);
157
int node_ip(const struct nodeID *s, char *ip, int len);
154 158

  
155 159
#endif /* NET_HELPER_H */
src/Tests/tman_test.c
129 129
    } else
130 130
      topoParseData(NULL, 0);
131 131
    if (++cnt % 1 == 0) {
132
    	const uint8_t *mdata;
133
    	const struct nodeID **neighbours;
134
    	int n, i, msize;
135
    	mdata = topoGetMetadata(&msize);
136
    	neighbours = topoGetNeighbourhood(&n);
137
    	fprintf(stderr, "\tMy metadata = %d\nIteration # %d -- Cache size now is : %d -- I have %d neighbours:\n",my_metadata,cnt,now,n);
138
    	for (i = 0; i < n; i++) {
139
    		const int *d;
140
    		d = (const int*)((mdata+i*msize));
141
    		fprintf(stderr, "\t%d: %s -- %d\n", i, node_addr(neighbours[i]), *d);
142
    	}
132
        const uint8_t *mdata;
133
        const struct nodeID **neighbours;
134
        char addr[256];
135
        int n, i, msize;
136
        mdata = topoGetMetadata(&msize);
137
        neighbours = topoGetNeighbourhood(&n);
138
        fprintf(stderr, "\tMy metadata = %d\nIteration # %d -- Cache size now is : %d -- I have %d neighbours:\n",my_metadata,cnt,now,n);
139
        for (i = 0; i < n; i++) {
140
                const int *d;
141
                d = (const int*)((mdata+i*msize));
142
                node_addr(neighbours[i], addr, 256);
143
                fprintf(stderr, "\t%d: %s -- %d\n", i, addr, *d);
144
        }
143 145
    }
144 146
    if (cnt % 20 == 0) {
145
    	change_metadata(s);
147
        change_metadata(s);
146 148
    }
147 149
//    if (cnt % 13 == 0) {
148
//    	more = ((double)rand() / (double)RAND_MAX)*10;
149
//    	now = topoGrowNeighbourhood(more);
150
//    	printf("Growing : +%d -- Cache size now is : %d\n", more,now);
150
//      more = ((double)rand() / (double)RAND_MAX)*10;
151
//      now = topoGrowNeighbourhood(more);
152
//      printf("Growing : +%d -- Cache size now is : %d\n", more,now);
151 153
//    }
152 154
//    if (cnt % 10 == 0) {
153
//    	less = ((double)rand() / (double)RAND_MAX)*10;
154
//    	now = topoShrinkNeighbourhood(less);
155
//    	printf("Shrinking : -%d -- Cache size now is : %d\n", less,now);
155
//      less = ((double)rand() / (double)RAND_MAX)*10;
156
//      now = topoShrinkNeighbourhood(less);
157
//      printf("Shrinking : -%d -- Cache size now is : %d\n", less,now);
156 158
//    }
157 159
  }
158 160

  
src/Tests/topology_test.c
85 85
#define BUFFSIZE 1024
86 86
  static uint8_t buff[BUFFSIZE];
87 87
  int cnt = 0;
88
  
88

  
89 89
  psample_parse_data(context, NULL, 0);
90 90
  while (!done) {
91 91
    int len;
92 92
    int news;
93 93
    const struct timeval tout = {1, 0};
94 94
    struct timeval t1;
95
    char addr[256];
95 96

  
96 97
    t1 = tout;
97 98
    news = wait4data(s, &t1, NULL);
......
110 111
        neighbourhoods = psample_get_cache(context, &n);
111 112
        printf("I have %d neighbours:\n", n);
112 113
        for (i = 0; i < n; i++) {
113
          printf("\t%d: %s\n", i, node_addr(neighbourhoods[i]));
114
          node_addr(neighbourhoods[i], addr, 256);
115
          printf("\t%d: %s\n", i, addr);
114 116
        }
115 117
        fflush(stdout);
116 118
        if (fprefix) {
......
121 123
          f = fopen(fname, "w");
122 124
          if (f) fprintf(f, "#Cache size: %d\n", n);
123 125
          for (i = 0; i < n; i++) {
124
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, node_addr(neighbourhoods[i]));
126
            node_addr(neighbourhoods[i], addr, 256);
127
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr);
125 128
          }
126 129
          fclose(f);
127 130
        }
src/Tests/topology_test_attr.c
78 78
static struct nodeID *init(void)
79 79
{
80 80
  struct nodeID *myID;
81
  char addr[256];
81 82

  
82 83
  myID = net_helper_init(my_addr, port, "");
83 84
  if (myID == NULL) {
......
86 87
    return NULL;
87 88
  }
88 89

  
89
  strcpy(my_attr.name, node_addr(myID));
90
  node_addr(myID, addr, 256);
91
  strcpy(my_attr.name, addr);
90 92
  context = psample_init(myID, &my_attr, sizeof(struct peer_attributes), "");
91 93

  
92 94
  return myID;
......
132 134
  int done = 0;
133 135
#define BUFFSIZE 1024
134 136
  static uint8_t buff[BUFFSIZE];
137
  char addr[256];
135 138
  int cnt = 0;
136
  
139

  
137 140
  psample_parse_data(context, NULL, 0);
138 141
  while (!done) {
139 142
    int len;
......
169 172
        }
170 173
        printf("I have %d neighbourhoods:\n", n);
171 174
        for (i = 0; i < n; i++) {
172
          printf("\t%d: %s", i, node_addr(neighbourhoods[i]));
175
          node_addr(neighbourhoods[i], addr, 256);
176
          printf("\t%d: %s", i, addr);
173 177
          if (meta) {
174 178
            printf("\tPeer %s is a %s peer and is %s", meta[i].name, meta[i].colour, status_print(meta[i].state));
175 179
          }
src/Tests/topology_test_th.c
83 83

  
84 84
static void *cycle_loop(void *p)
85 85
{
86
  char addr[256];
86 87
  int done = 0;
87 88
  int cnt = 0;
88
  
89

  
89 90
  while (!done) {
90 91
    const int tout = 1;
91 92

  
......
100 101
      neighbours = psample_get_cache(context, &n);
101 102
      printf("I have %d neighbours:\n", n);
102 103
      for (i = 0; i < n; i++) {
103
        printf("\t%d: %s\n", i, node_addr(neighbours[i]));
104
        node_addr(neighbours[i], addr, 256);
105
        printf("\t%d: %s\n", i, addr);
104 106
      }
105 107
      fflush(stdout);
106 108
      if (fprefix) {
......
111 113
        f = fopen(fname, "w");
112 114
        if (f) fprintf(f, "#Cache size: %d\n", n);
113 115
        for (i = 0; i < n; i++) {
114
          if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, node_addr(neighbours[i]));
116
          node_addr(neighbours[i], addr, 256);
117
          if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr);
115 118
        }
116 119
        fclose(f);
117 120
      }
......
130 133
  int done = 0;
131 134
#define BUFFSIZE 1024
132 135
  static uint8_t buff[BUFFSIZE];
133
  
136

  
134 137
  while (!done) {
135 138
    int len;
136 139
    struct nodeID *remote;
src/net_helper-ml.c
619 619
	return remote;
620 620
}
621 621

  
622
const char *node_ip(const struct nodeID *s) {
623
	static char ip[64];
622
int node_ip(const struct nodeID *s, char *ip, int size)
624 623
	int len;
625 624
	char *start, *end;
626
	const char *tmp = node_addr(s);
625
	char tmp[64];
626

  
627
	len = node_addr(s, tmp, 64);
628
	if (len < 0) {
629
		return len;
630
	}
627 631
	start = strstr(tmp, "-") + 1;
628 632
	end = strstr(start, ":");
629 633
	len = end - start;
634
	if (len >= size) {
635
		return -1;
636
	}
630 637
	memcpy(ip, start, len);
631 638
	ip[len] = 0;
632 639

  
633
	return (const char *)ip;
640
	return 1;
634 641
}
635 642

  
636 643

  
......
649 656
}
650 657

  
651 658

  
652
const char *node_addr(const struct nodeID *s)
659
int node_addr(const struct nodeID *s, char *addr, int len)
653 660
{
654
  static char addr[256];
655 661
  // TODO: mlSocketIDToString always return 0 !!!
656
  int r = mlSocketIDToString(s->addr,addr,256);
662
  int r = mlSocketIDToString(s->addr,addr,len);
657 663
  if (!r)
658
	  return addr;
664
	  return 1;
659 665
  else
660
	  return "";
666
	  return -1;
661 667
}
662 668

  
663 669
struct nodeID *nodeid_dup(struct nodeID *s)
src/net_helper-win32.c
172 172
  return recv;
173 173
}
174 174

  
175
const char *node_addr(const struct nodeID *s)
175
int node_addr(const struct nodeID *s, char *addr, int len)
176 176
{
177
  static char addr[256];
177
  char buff[96];
178
  int n;
178 179

  
179
  sprintf(addr, "%s:%d", inet_ntoa(s->addr.sin_addr), ntohs(s->addr.sin_port));
180
  if (!inet_ntop(AF_INET, &(s->addr.sin_addr), addr, len)) {
181
    return -1;
182
  }
183
  n = snprintf(addr + strlen(addr), len - strlen(addr) - 1, ":%d", ntohs(s->addr.sin_port));
180 184

  
181
  return addr;
185
  return n;
182 186
}
183 187

  
184 188
struct nodeID *nodeid_dup(struct nodeID *s)
......
225 229
  free(s);
226 230
}
227 231

  
228
const char *node_ip(const struct nodeID *s)
232
int node_ip(const struct nodeID *s, char *ip, int len)
229 233
{
230
  static char ip[64];
231

  
232
  sprintf(ip, "%s", inet_ntoa(s->addr.sin_addr));
234
  if (inet_ntop(AF_INET, &(s->addr.sin_addr), ip, len) == 0) {
235
    return -1;
236
  }
233 237

  
234
  return ip;
238
  return 1;
235 239
}
240

  
src/net_helper.c
184 184
  return recv;
185 185
}
186 186

  
187
const char *node_addr(const struct nodeID *s)
187
int node_addr(const struct nodeID *s, char *addr, int len)
188 188
{
189
  static char addr[256];
189
  int n;
190 190

  
191
  sprintf(addr, "%s:%d", inet_ntoa(s->addr.sin_addr), ntohs(s->addr.sin_port));
191
  if (!inet_ntop(AF_INET, &(s->addr.sin_addr), addr, len)) {
192
    return -1;
193
  }
194
  n = snprintf(addr + strlen(addr), len - strlen(addr) - 1, ":%d", ntohs(s->addr.sin_port));
192 195

  
193
  return addr;
196
  return n;
194 197
}
195 198

  
196 199
struct nodeID *nodeid_dup(struct nodeID *s)
......
237 240
  free(s);
238 241
}
239 242

  
240
const char *node_ip(const struct nodeID *s)
243
int node_ip(const struct nodeID *s, char *ip, int len)
241 244
{
242
  static char ip[64];
243

  
244
  sprintf(ip, "%s", inet_ntoa(s->addr.sin_addr));
245
  if (inet_ntop(AF_INET, &(s->addr.sin_addr), ip, len) == 0) {
246
    return -1;
247
  }
245 248

  
246
  return ip;
249
  return 1;
247 250
}

Also available in: Unified diff