Module: sip-router
Branch: master
Commit: 14839e8ff8da792974058779dc0cf4daf4181ac5
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=14839e8…
Author: Jan Janak <jan(a)iptel.org>
Committer: Jan Janak <jan(a)iptel.org>
Date: Wed May 6 11:10:48 2009 +0200
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.
---
socket_info.c | 73 ++++++++++++++++++++++++++++++++++++++++----------------
socket_info.h | 7 +++++
2 files changed, 59 insertions(+), 21 deletions(-)
diff --git a/socket_info.c b/socket_info.c
index ac2c82f..93c453a 100644
--- a/socket_info.c
+++ b/socket_info.c
@@ -298,6 +298,50 @@ static char* get_proto_name(unsigned short proto)
}
}
+/** Convert socket to its textual representation.
+ *
+ * This function converts the transport protocol, the IP address and the port
+ * number in a comma delimited string of form proto:ip:port. The resulting
+ * string is NOT zero terminated
+ *
+ * @param s is a pointer to the destination memory buffer
+ * @param len is a pointer to an integer variable. Initially the variable
+ * should contain the size of the buffer in s. The value of the variable
+ * will be changed to the length of the resulting string on success and
+ * to the desired size of the destination buffer if it is too small
+ * @param si is a pointer to the socket_info structure to be printed
+ * @return -1 on error and 0 on success
+ */
+int socket2str(char* s, int* len, struct socket_info* si)
+{
+ str proto;
+ int l;
+
+ proto.s = get_proto_name(si->proto);
+ proto.len = strlen(proto.s);
+
+ l = proto.len + si->address_str.len + si->port_no_str.len + 2;
+
+ if (*len < l) {
+ ERR("socket2str: Destionation buffer too short\n");
+ *len = l;
+ return -1;
+ }
+
+ memcpy(s, proto.s, proto.len);
+ s += proto.len;
+ *s = ':'; s++;
+ memcpy(s, si->address_str.s, si->address_str.len);
+ s += si->address_str.len;
+ *s = ':'; s++;
+ memcpy(s, si->port_no_str.s, si->port_no_str.len);
+ s += si->port_no_str.len;
+
+ *len = l;
+ return 0;
+}
+
+
/* Fill si->sock_str with string representing the socket_info structure,
* 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)
*/
static int fix_sock_str(struct socket_info* si)
{
- char* p;
- str proto;
+ int len = MAX_SOCKET_STR;
if (si->sock_str.s) pkg_free(si->sock_str.s);
- proto.s = get_proto_name(si->proto);
- proto.len = strlen(proto.s);
-
- si->sock_str.len = proto.len + si->address_str.len +
- si->port_no_str.len + 2;
-
- si->sock_str.s = pkg_malloc(si->sock_str.len + 1);
+ si->sock_str.s = pkg_malloc(len + 1);
if (si->sock_str.s == NULL) {
- LOG(L_ERR, "fix_sock_str: No pkg memory left\n");
+ ERR("fix_sock_str: No memory left\n");
return -1;
}
- p = si->sock_str.s;
- memcpy(p, proto.s, proto.len);
- p += proto.len;
- *p = ':'; p++;
- memcpy(p, si->address_str.s, si->address_str.len);
- p += si->address_str.len;
- *p = ':'; p++;
- memcpy(p, si->port_no_str.s, si->port_no_str.len);
- p += si->port_no_str.len;
- *p = '\0';
-
+ if (socket2str(si->sock_str.s, &len, si) < 0) {
+ BUG("fix_sock_str: Error in socket2str\n");
+ return -1;
+ }
+ si->sock_str.s[len] = '\0';
return 0;
}
diff --git a/socket_info.h b/socket_info.h
index ebe051a..4179869 100644
--- a/socket_info.h
+++ b/socket_info.h
@@ -43,6 +43,13 @@
#include "dprint.h"
#include "globals.h"
+/* This macro evaluates to the maximum length of string buffer needed to print
+ * the text description of any socket, not counting the terminating zero added
+ * by socket2str */
+#define MAX_SOCKET_STR (sizeof("unknown") - 1 + IP_ADDR_MAX_STR_SIZE + \
+ INT2STR_MAX_LEN + 2)
+
+int socket2str(char* s, int* len, struct socket_info* si);
/* struct socket_info is defined in ip_addr.h */