msg.c (b1ea638805d88c05bf9e976dc1bba2efe340b57a) msg.c (6ebb8ebbd13511cfe7132822b176215e461583c8)
1
2/*
3 * msg.c
4 *
5 * Copyright (c) 1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and

--- 26 unchanged lines hidden (view full) ---

35 * OF SUCH DAMAGE.
36 *
37 * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
38 * $FreeBSD$
39 */
40
41#include "ngctl.h"
42
1
2/*
3 * msg.c
4 *
5 * Copyright (c) 1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and

--- 26 unchanged lines hidden (view full) ---

35 * OF SUCH DAMAGE.
36 *
37 * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
38 * $FreeBSD$
39 */
40
41#include "ngctl.h"
42
43#define BUF_SIZE 1024
43#define BUF_SIZE 4096
44
45static int MsgCmd(int ac, char **av);
46
47const struct ngcmd msg_cmd = {
48 MsgCmd,
49 "msg path command [args ... ]",
50 "Send a netgraph control message to the node at \"path\"",
51 "The msg command constructs a netgraph control message from the"

--- 23 unchanged lines hidden (view full) ---

75 }
76
77 /* Send it */
78 if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
79 warn("send msg");
80 return(CMDRTN_ERROR);
81 }
82
44
45static int MsgCmd(int ac, char **av);
46
47const struct ngcmd msg_cmd = {
48 MsgCmd,
49 "msg path command [args ... ]",
50 "Send a netgraph control message to the node at \"path\"",
51 "The msg command constructs a netgraph control message from the"

--- 23 unchanged lines hidden (view full) ---

75 }
76
77 /* Send it */
78 if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
79 warn("send msg");
80 return(CMDRTN_ERROR);
81 }
82
83 /* See if a synchronous reply awaits */
84 {
85 struct timeval tv;
86 fd_set rfds;
87
88 FD_ZERO(&rfds);
89 FD_SET(csock, &rfds);
90 memset(&tv, 0, sizeof(tv));
91 switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
92 case -1:
93 err(EX_OSERR, "select");
94 case 0:
95 break;
96 default:
97 MsgRead();
98 break;
99 }
100 }
101
83 /* Done */
84 return(CMDRTN_OK);
85}
86
102 /* Done */
103 return(CMDRTN_OK);
104}
105
106/*
107 * Read and display the next incoming control message
108 */
109void
110MsgRead()
111{
112 u_char buf[2 * sizeof(struct ng_mesg) + BUF_SIZE];
113 struct ng_mesg *const m = (struct ng_mesg *)buf;
114 struct ng_mesg *const ascii = (struct ng_mesg *)m->data;
115 char path[NG_PATHLEN+1];
116
117 /* Get incoming message (in binary form) */
118 if (NgRecvMsg(csock, m, sizeof(buf), path) < 0) {
119 warn("recv incoming message");
120 return;
121 }
122
123 /* Ask originating node to convert message to ASCII */
124 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
125 NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
126 || NgRecvMsg(csock, m, sizeof(buf), NULL) < 0) {
127 printf("Rec'd %s %d from \"%s\":\n",
128 (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
129 m->header.cmd, path);
130 if (m->header.arglen == 0)
131 printf("No arguments\n");
132 else
133 DumpAscii(m->data, m->header.arglen);
134 return;
135 }
136
137 /* Display message in ASCII form */
138 printf("Rec'd %s \"%s\" (%d) from \"%s\":\n",
139 (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command",
140 ascii->header.cmdstr, ascii->header.cmd, path);
141 if (*ascii->data != '\0')
142 printf("Args:\t%s\n", ascii->data);
143 else
144 printf("No arguments\n");
145}
146