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 298
 }
299 299
 
300 300
 
301
+/* Fill si->sock_str with string representing the socket_info structure,
302
+ * format of the string is 'proto:address:port'. Returns 0 on success and
303
+ * negative number on failure.
304
+ */
305
+static int fix_sock_str(struct socket_info* si)
306
+{
307
+	char* p;
308
+	str proto;
309
+
310
+	if (si->sock_str.s) pkg_free(si->sock_str.s);
311
+	
312
+	proto.s = get_proto_name(si->proto);
313
+	proto.len = strlen(proto.s);
314
+	
315
+	si->sock_str.len = proto.len + si->address_str.len + 
316
+		si->port_no_str.len + 2;
317
+	
318
+	si->sock_str.s = pkg_malloc(si->sock_str.len + 1);
319
+	if (si->sock_str.s == NULL) {
320
+		LOG(L_ERR, "fix_sock_str: No pkg memory left\n");
321
+		return -1;
322
+	}
323
+	p = si->sock_str.s;
324
+	memcpy(p, proto.s, proto.len);
325
+	p += proto.len;
326
+	*p = ':'; p++;
327
+	memcpy(p, si->address_str.s, si->address_str.len);
328
+	p += si->address_str.len;
329
+	*p = ':'; p++;
330
+	memcpy(p, si->port_no_str.s, si->port_no_str.len);
331
+	p += si->port_no_str.len;
332
+	*p = '\0';
333
+
334
+	return 0;
335
+}
336
+
301 337
 
302 338
 /* returns 0 if support for the protocol is not compiled or if proto is 
303 339
    invalid */
... ...
@@ -1028,6 +1065,8 @@ static int fix_socket_list(struct socket_info **list, int* type_flags)
1028 1028
 						&ail->flags, type_flags, si) !=0 )
1029 1029
 				goto error;
1030 1030
 		}
1031
+
1032
+		if (fix_sock_str(si) < 0) goto error;
1031 1033
 		
1032 1034
 #ifdef EXTRA_DEBUG
1033 1035
 		printf("              %.*s [%s]:%s%s\n", si->name.len,