summaryrefslogtreecommitdiffstats
path: root/lib/libnetgraph/msg.c
diff options
context:
space:
mode:
authorru <ru@FreeBSD.org>2004-01-27 20:25:14 +0000
committerru <ru@FreeBSD.org>2004-01-27 20:25:14 +0000
commitadc82a1e80759afba2cfae432d4ee1ca190be1f4 (patch)
tree92ee6204e54cff03c751ded7b2d193159acbf5f2 /lib/libnetgraph/msg.c
parentf24f6452fe6f2143c7c182fae610d62cfa8994b8 (diff)
downloadFreeBSD-src-adc82a1e80759afba2cfae432d4ee1ca190be1f4.zip
FreeBSD-src-adc82a1e80759afba2cfae432d4ee1ca190be1f4.tar.gz
- Added three new interfaces, NgAllocRecvMsg(), NgAllocRecvAsciiMsg(),
and NgAllocRecvData(), that dynamically allocate buffer for a binary message, an ascii message, and a data packet, respectively. The size of the allocated buffer is equal to the socket's receive buffer size to guarantee that a message or a data packet is not truncated. - Get rid of the static size buffer in NgSendAsciiMsg(). OK'ed by: archie, julian
Diffstat (limited to 'lib/libnetgraph/msg.c')
-rw-r--r--lib/libnetgraph/msg.c53
1 files changed, 45 insertions, 8 deletions
diff --git a/lib/libnetgraph/msg.c b/lib/libnetgraph/msg.c
index f89c27f..90b6278 100644
--- a/lib/libnetgraph/msg.c
+++ b/lib/libnetgraph/msg.c
@@ -91,13 +91,10 @@ NgSendMsg(int cs, const char *path,
int
NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
{
- const int bufSize = 1024;
- char replybuf[2 * sizeof(struct ng_mesg) + bufSize];
- struct ng_mesg *const reply = (struct ng_mesg *)replybuf;
- struct ng_mesg *const binary = (struct ng_mesg *)reply->data;
- struct ng_mesg *ascii;
+ struct ng_mesg *reply, *binary, *ascii;
char *buf, *cmd, *args;
va_list fmtargs;
+ int token;
/* Parse out command and arguments */
va_start(fmtargs, fmt);
@@ -139,17 +136,22 @@ NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
free(ascii);
/* Get reply */
- if (NgRecvMsg(cs, reply, sizeof(replybuf), NULL) < 0)
+ if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
return (-1);
/* Now send binary version */
+ binary = (struct ng_mesg *)reply->data;
if (++gMsgId < 0)
gMsgId = 1;
binary->header.token = gMsgId;
if (NgDeliverMsg(cs,
- path, binary, binary->data, binary->header.arglen) < 0)
+ path, binary, binary->data, binary->header.arglen) < 0) {
+ free(reply);
return (-1);
- return (binary->header.token);
+ }
+ token = binary->header.token;
+ free(reply);
+ return (token);
}
/*
@@ -277,6 +279,24 @@ errout:
}
/*
+ * Identical to NgRecvMsg() except buffer is dynamically allocated.
+ */
+int
+NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
+{
+ int len;
+ socklen_t optlen;
+
+ optlen = sizeof(len);
+ if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
+ (*rep = malloc(len)) == NULL)
+ return (-1);
+ if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
+ free(*rep);
+ return (len);
+}
+
+/*
* Receive a control message and convert the arguments to ASCII
*/
int
@@ -321,3 +341,20 @@ fail:
return (0);
}
+/*
+ * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
+ */
+int
+NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
+{
+ int len;
+ socklen_t optlen;
+
+ optlen = sizeof(len);
+ if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
+ (*reply = malloc(len)) == NULL)
+ return (-1);
+ if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)
+ free(*reply);
+ return (len);
+}
OpenPOWER on IntegriCloud