Browse code

Splitting fix_sock_str into socket2str and fix_sock_str

This patch splits fix_sock_str into two function, socket2str and
fix_sock_str. The function socket2str is exported and can be used
to print the socket into a pre-allocated memory buffer. Function
fix_sock_str allocates the memory buffer and calls socket2str
internally.

The primary consumer of this change is the serial forking code in
tm module.

This patch also defines a new macro called MAX_SOCKET_STR which
evaluates to the maximum size of textual representation of any
socket.

Jan Janak authored on 06/05/2009 09:10:48
Showing 2 changed files
... ...
@@ -298,6 +298,50 @@ static char* get_proto_name(unsigned short proto)
298 298
 	}
299 299
 }
300 300
 
301
+/** Convert socket to its textual representation.
302
+ *
303
+ * This function converts the transport protocol, the IP address and the port
304
+ * number in a comma delimited string of form proto:ip:port. The resulting
305
+ * string is NOT zero terminated
306
+ *
307
+ * @param s is a pointer to the destination memory buffer
308
+ * @param len is a pointer to an integer variable. Initially the variable
309
+ *        should contain the size of the buffer in s. The value of the variable
310
+ *        will be changed to the length of the resulting string on success and
311
+ *        to the desired size of the destination buffer if it is too small
312
+ * @param si is a pointer to the socket_info structure to be printed
313
+ * @return -1 on error and 0 on success
314
+ */
315
+int socket2str(char* s, int* len, struct socket_info* si)
316
+{
317
+	str proto;
318
+	int l;
319
+	
320
+	proto.s = get_proto_name(si->proto);
321
+	proto.len = strlen(proto.s);
322
+	
323
+	l = proto.len + si->address_str.len + si->port_no_str.len + 2;
324
+	
325
+	if (*len < l) {
326
+		ERR("socket2str: Destionation buffer too short\n");
327
+		*len = l;
328
+		return -1;
329
+	}
330
+	
331
+	memcpy(s, proto.s, proto.len);
332
+	s += proto.len;
333
+	*s = ':'; s++;
334
+	memcpy(s, si->address_str.s, si->address_str.len);
335
+	s += si->address_str.len;
336
+	*s = ':'; s++;
337
+	memcpy(s, si->port_no_str.s, si->port_no_str.len);
338
+	s += si->port_no_str.len;
339
+
340
+	*len = l;
341
+	return 0;
342
+}
343
+
344
+
301 345
 
302 346
 /* Fill si->sock_str with string representing the socket_info structure,
303 347
  * format of the string is 'proto:address:port'. Returns 0 on success and
... ...
@@ -305,33 +349,20 @@ static char* get_proto_name(unsigned short proto)
305 305
  */
306 306
 static int fix_sock_str(struct socket_info* si)
307 307
 {
308
-	char* p;
309
-	str proto;
308
+	int len = MAX_SOCKET_STR;
310 309
 
311 310
 	if (si->sock_str.s) pkg_free(si->sock_str.s);
312 311
 	
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);
312
+	si->sock_str.s = pkg_malloc(len + 1);
320 313
 	if (si->sock_str.s == NULL) {
321
-		LOG(L_ERR, "fix_sock_str: No pkg memory left\n");
314
+		ERR("fix_sock_str: No memory left\n");
322 315
 		return -1;
323 316
 	}
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
-
317
+	if (socket2str(si->sock_str.s, &len, si) < 0) {
318
+		BUG("fix_sock_str: Error in socket2str\n");
319
+		return -1;
320
+	}
321
+	si->sock_str.s[len] = '\0';
335 322
 	return 0;
336 323
 }
337 324
 
... ...
@@ -43,6 +43,13 @@
43 43
 #include "dprint.h"
44 44
 #include "globals.h"
45 45
 
46
+/* This macro evaluates to the maximum length of string buffer needed to print
47
+ * the text description of any socket, not counting the terminating zero added
48
+ * by socket2str */
49
+#define MAX_SOCKET_STR (sizeof("unknown") - 1 + IP_ADDR_MAX_STR_SIZE + \
50
+	INT2STR_MAX_LEN + 2)
51
+
52
+int socket2str(char* s, int* len, struct socket_info* si);
46 53
 
47 54
 
48 55
 /* struct socket_info is defined in ip_addr.h */