msg.c (125104) | msg.c (125113) |
---|---|
1/* 2 * msg.c 3 * 4 * Copyright (c) 1996-1999 Whistle Communications, Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or --- 25 unchanged lines hidden (view full) --- 34 * OF SUCH DAMAGE. 35 * 36 * Author: Archie Cobbs <archie@whistle.com> 37 * 38 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $ 39 */ 40 41#include <sys/cdefs.h> | 1/* 2 * msg.c 3 * 4 * Copyright (c) 1996-1999 Whistle Communications, Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or --- 25 unchanged lines hidden (view full) --- 34 * OF SUCH DAMAGE. 35 * 36 * Author: Archie Cobbs <archie@whistle.com> 37 * 38 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $ 39 */ 40 41#include <sys/cdefs.h> |
42__FBSDID("$FreeBSD: head/lib/libnetgraph/msg.c 125104 2004-01-27 18:38:22Z ru $"); | 42__FBSDID("$FreeBSD: head/lib/libnetgraph/msg.c 125113 2004-01-27 20:25:14Z ru $"); |
43 44#include <sys/types.h> 45#include <stdarg.h> 46#include <netgraph/ng_message.h> 47#include <netgraph/ng_socket.h> 48 49#include "netgraph.h" 50#include "internal.h" --- 35 unchanged lines hidden (view full) --- 86 87/* 88 * Send a message given in ASCII format. We first ask the node to translate 89 * the command into binary, and then we send the binary. 90 */ 91int 92NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...) 93{ | 43 44#include <sys/types.h> 45#include <stdarg.h> 46#include <netgraph/ng_message.h> 47#include <netgraph/ng_socket.h> 48 49#include "netgraph.h" 50#include "internal.h" --- 35 unchanged lines hidden (view full) --- 86 87/* 88 * Send a message given in ASCII format. We first ask the node to translate 89 * the command into binary, and then we send the binary. 90 */ 91int 92NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...) 93{ |
94 const int bufSize = 1024; 95 char replybuf[2 * sizeof(struct ng_mesg) + bufSize]; 96 struct ng_mesg *const reply = (struct ng_mesg *)replybuf; 97 struct ng_mesg *const binary = (struct ng_mesg *)reply->data; 98 struct ng_mesg *ascii; | 94 struct ng_mesg *reply, *binary, *ascii; |
99 char *buf, *cmd, *args; 100 va_list fmtargs; | 95 char *buf, *cmd, *args; 96 va_list fmtargs; |
97 int token; |
|
101 102 /* Parse out command and arguments */ 103 va_start(fmtargs, fmt); 104 vasprintf(&buf, fmt, fmtargs); 105 va_end(fmtargs); 106 if (buf == NULL) 107 return (-1); 108 --- 25 unchanged lines hidden (view full) --- 134 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY, 135 (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) { 136 free(ascii); 137 return (-1); 138 } 139 free(ascii); 140 141 /* Get reply */ | 98 99 /* Parse out command and arguments */ 100 va_start(fmtargs, fmt); 101 vasprintf(&buf, fmt, fmtargs); 102 va_end(fmtargs); 103 if (buf == NULL) 104 return (-1); 105 --- 25 unchanged lines hidden (view full) --- 131 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY, 132 (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) { 133 free(ascii); 134 return (-1); 135 } 136 free(ascii); 137 138 /* Get reply */ |
142 if (NgRecvMsg(cs, reply, sizeof(replybuf), NULL) < 0) | 139 if (NgAllocRecvMsg(cs, &reply, NULL) < 0) |
143 return (-1); 144 145 /* Now send binary version */ | 140 return (-1); 141 142 /* Now send binary version */ |
143 binary = (struct ng_mesg *)reply->data; |
|
146 if (++gMsgId < 0) 147 gMsgId = 1; 148 binary->header.token = gMsgId; 149 if (NgDeliverMsg(cs, | 144 if (++gMsgId < 0) 145 gMsgId = 1; 146 binary->header.token = gMsgId; 147 if (NgDeliverMsg(cs, |
150 path, binary, binary->data, binary->header.arglen) < 0) | 148 path, binary, binary->data, binary->header.arglen) < 0) { 149 free(reply); |
151 return (-1); | 150 return (-1); |
152 return (binary->header.token); | 151 } 152 token = binary->header.token; 153 free(reply); 154 return (token); |
153} 154 155/* 156 * Send a message that is a reply to a previously received message. 157 * Returns -1 and sets errno on error, otherwise returns zero. 158 */ 159int 160NgSendReplyMsg(int cs, const char *path, --- 111 unchanged lines hidden (view full) --- 272 return (len); 273 274errout: 275 errno = errnosv; 276 return (-1); 277} 278 279/* | 155} 156 157/* 158 * Send a message that is a reply to a previously received message. 159 * Returns -1 and sets errno on error, otherwise returns zero. 160 */ 161int 162NgSendReplyMsg(int cs, const char *path, --- 111 unchanged lines hidden (view full) --- 274 return (len); 275 276errout: 277 errno = errnosv; 278 return (-1); 279} 280 281/* |
282 * Identical to NgRecvMsg() except buffer is dynamically allocated. 283 */ 284int 285NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path) 286{ 287 int len; 288 socklen_t optlen; 289 290 optlen = sizeof(len); 291 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 || 292 (*rep = malloc(len)) == NULL) 293 return (-1); 294 if ((len = NgRecvMsg(cs, *rep, len, path)) < 0) 295 free(*rep); 296 return (len); 297} 298 299/* |
|
280 * Receive a control message and convert the arguments to ASCII 281 */ 282int 283NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path) 284{ 285 struct ng_mesg *msg, *ascii; 286 int bufSize, errnosv; 287 u_char *buf; --- 28 unchanged lines hidden (view full) --- 316 } 317 strncpy(reply->data, ascii->data, ascii->header.arglen); 318 319 /* Done */ 320 free(buf); 321 return (0); 322} 323 | 300 * Receive a control message and convert the arguments to ASCII 301 */ 302int 303NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path) 304{ 305 struct ng_mesg *msg, *ascii; 306 int bufSize, errnosv; 307 u_char *buf; --- 28 unchanged lines hidden (view full) --- 336 } 337 strncpy(reply->data, ascii->data, ascii->header.arglen); 338 339 /* Done */ 340 free(buf); 341 return (0); 342} 343 |
344/* 345 * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated. 346 */ 347int 348NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path) 349{ 350 int len; 351 socklen_t optlen; 352 353 optlen = sizeof(len); 354 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 || 355 (*reply = malloc(len)) == NULL) 356 return (-1); 357 if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0) 358 free(*reply); 359 return (len); 360} |
|