Browse code

Add sock_str (si as string) to struct socket_info.

This patch adds a new variable to the structure socket_info. The
variable contains a string representation of the socket in form
'proto:address:port'.

Jan Janak authored on 15/03/2009 22:38:32
Showing 2 changed files
... ...
@@ -113,6 +113,7 @@ struct socket_info{
113 113
 	struct socket_info* prev;
114 114
 	unsigned short port_no;  /* port number */
115 115
 	char proto; /* tcp or udp*/
116
+	str sock_str; /* Socket proto, ip, and port as string */
116 117
 	struct addr_info* addr_info_lst; /* extra addresses (e.g. SCTP mh) */
117 118
 };
118 119
 
... ...
@@ -268,6 +268,7 @@ static void free_sock_info(struct socket_info* si)
268 268
 		if(si->address_str.s) pkg_free(si->address_str.s);
269 269
 		if(si->port_no_str.s) pkg_free(si->port_no_str.s);
270 270
 		if (si->addr_info_lst) free_addr_info_lst(&si->addr_info_lst);
271
+		if(si->sock_str.s) pkg_free(si->sock_str.s);
271 272
 	}
272 273
 }
273 274
 
... ...
@@ -298,6 +299,42 @@ static char* get_proto_name(unsigned short proto)
298 299
 }
299 300
 
300 301
 
302
+/* Fill si->sock_str with string representing the socket_info structure,
303
+ * format of the string is 'proto:address:port'. Returns 0 on success and
304
+ * negative number on failure.
305
+ */
306
+static int fix_sock_str(struct socket_info* si)
307
+{
308
+	char* p;
309
+	str proto;
310
+
311
+	if (si->sock_str.s) pkg_free(si->sock_str.s);
312
+	
313
+	proto.s = get_proto_name(si->proto);
314
+	proto.len = strlen(proto.s);
315
+	
316
+	si->sock_str.len = proto.len + si->address_str.len + 
317
+		si->port_no_str.len + 2;
318
+	
319
+	si->sock_str.s = pkg_malloc(si->sock_str.len + 1);
320
+	if (si->sock_str.s == NULL) {
321
+		LOG(L_ERR, "fix_sock_str: No pkg memory left\n");
322
+		return -1;
323
+	}
324
+	p = si->sock_str.s;
325
+	memcpy(p, proto.s, proto.len);
326
+	p += proto.len;
327
+	*p = ':'; p++;
328
+	memcpy(p, si->address_str.s, si->address_str.len);
329
+	p += si->address_str.len;
330
+	*p = ':'; p++;
331
+	memcpy(p, si->port_no_str.s, si->port_no_str.len);
332
+	p += si->port_no_str.len;
333
+	*p = '\0';
334
+
335
+	return 0;
336
+}
337
+
301 338
 
302 339
 /* returns 0 if support for the protocol is not compiled or if proto is 
303 340
    invalid */
... ...
@@ -1028,6 +1065,8 @@ static int fix_socket_list(struct socket_info **list, int* type_flags)
1028 1065
 						&ail->flags, type_flags, si) !=0 )
1029 1066
 				goto error;
1030 1067
 		}
1068
+
1069
+		if (fix_sock_str(si) < 0) goto error;
1031 1070
 		
1032 1071
 #ifdef EXTRA_DEBUG
1033 1072
 		printf("              %.*s [%s]:%s%s\n", si->name.len,