1 2 /* 3 * sock.c 4 * 5 * Copyright (c) 1996-1999 Whistle Communications, Inc. 6 * All rights reserved. 7 * 8 * Subject to the following obligations and disclaimer of warranty, use and 9 * redistribution of this software, in source or object code forms, with or 10 * without modifications are expressly permitted by Whistle Communications; 11 * provided, however, that: 12 * 1. Any and all reproductions of the source or object code must include the 13 * copyright notice above and the following disclaimer of warranties; and 14 * 2. No rights are granted, in any manner or form, to use Whistle 15 * Communications, Inc. trademarks, including the mark "WHISTLE 16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17 * such appears in the above copyright notice or in the software. 18 * 19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35 * OF SUCH DAMAGE. 36 * 37 * Author: Archie Cobbs <archie@whistle.com> 38 * 39 * $FreeBSD$ 40 * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $ 41 */ 42 43 #include <sys/types.h> 44 #include <stdarg.h> 45 #include <netgraph/ng_message.h> 46 #include <netgraph/ng_socket.h> 47 48 #include "netgraph.h" 49 #include "internal.h" 50 51 /* 52 * Create a socket type node and give it the supplied name. 53 * Return data and control sockets corresponding to the node. 54 * Returns -1 if error and sets errno. 55 */ 56 int 57 NgMkSockNode(const char *name, int *csp, int *dsp) 58 { 59 char namebuf[NG_NODELEN + 1]; 60 int cs = -1; /* control socket */ 61 int ds = -1; /* data socket */ 62 int errnosv; 63 64 /* Empty name means no name */ 65 if (name && *name == 0) 66 name = NULL; 67 68 /* Create control socket; this also creates the netgraph node */ 69 if ((cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL)) < 0) { 70 errnosv = errno; 71 if (_gNgDebugLevel >= 1) 72 NGLOG("socket"); 73 goto errout; 74 } 75 76 /* Assign the node the desired name, if any */ 77 if (name != NULL) { 78 u_char sbuf[NG_NODELEN + 3]; 79 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf; 80 81 /* Assign name */ 82 snprintf(sg->sg_data, NG_NODELEN + 1, "%s", name); 83 sg->sg_family = AF_NETGRAPH; 84 sg->sg_len = strlen(sg->sg_data) + 3; 85 if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) { 86 errnosv = errno; 87 if (_gNgDebugLevel >= 1) 88 NGLOG("bind(%s)", sg->sg_data); 89 goto errout; 90 } 91 92 /* Save node name */ 93 snprintf(namebuf, sizeof(namebuf), "%s", name); 94 } else if (dsp != NULL) { 95 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)]; 96 struct ng_mesg *const resp = (struct ng_mesg *) rbuf; 97 struct nodeinfo *const ni = (struct nodeinfo *) resp->data; 98 99 /* Find out the node ID */ 100 if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE, 101 NGM_NODEINFO, NULL, 0) < 0) { 102 errnosv = errno; 103 if (_gNgDebugLevel >= 1) 104 NGLOG("send nodeinfo"); 105 goto errout; 106 } 107 if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) { 108 errnosv = errno; 109 if (_gNgDebugLevel >= 1) 110 NGLOG("recv nodeinfo"); 111 goto errout; 112 } 113 114 /* Save node "name" */ 115 snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id); 116 } 117 118 /* Create data socket if desired */ 119 if (dsp != NULL) { 120 u_char sbuf[NG_NODELEN + 4]; 121 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf; 122 123 /* Create data socket, initially just "floating" */ 124 if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) { 125 errnosv = errno; 126 if (_gNgDebugLevel >= 1) 127 NGLOG("socket"); 128 goto errout; 129 } 130 131 /* Associate the data socket with the node */ 132 snprintf(sg->sg_data, NG_NODELEN + 2, "%s:", namebuf); 133 sg->sg_family = AF_NETGRAPH; 134 sg->sg_len = strlen(sg->sg_data) + 3; 135 if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) { 136 errnosv = errno; 137 if (_gNgDebugLevel >= 1) 138 NGLOG("connect(%s)", sg->sg_data); 139 goto errout; 140 } 141 } 142 143 /* Return the socket(s) */ 144 if (csp) 145 *csp = cs; 146 else 147 close(cs); 148 if (dsp) 149 *dsp = ds; 150 return (0); 151 152 errout: 153 /* Failed */ 154 if (cs >= 0) 155 close(cs); 156 if (ds >= 0) 157 close(ds); 158 errno = errnosv; 159 return (-1); 160 } 161 162 /* 163 * Assign a globally unique name to a node 164 * Returns -1 if error and sets errno. 165 */ 166 int 167 NgNameNode(int cs, const char *path, const char *fmt, ...) 168 { 169 struct ngm_name ngn; 170 va_list args; 171 172 /* Build message arg */ 173 va_start(args, fmt); 174 vsnprintf(ngn.name, sizeof(ngn.name), fmt, args); 175 va_end(args); 176 177 /* Send message */ 178 if (NgSendMsg(cs, path, 179 NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) { 180 if (_gNgDebugLevel >= 1) 181 NGLOGX("%s: failed", __FUNCTION__); 182 return (-1); 183 } 184 185 /* Done */ 186 return (0); 187 } 188 189 /* 190 * Read a packet from a data socket 191 * Returns -1 if error and sets errno. 192 */ 193 int 194 NgRecvData(int ds, u_char * buf, size_t len, char *hook) 195 { 196 u_char frombuf[NG_HOOKLEN + sizeof(struct sockaddr_ng)]; 197 struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf; 198 int fromlen = sizeof(frombuf); 199 int rtn, errnosv; 200 201 /* Read packet */ 202 rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen); 203 if (rtn < 0) { 204 errnosv = errno; 205 if (_gNgDebugLevel >= 1) 206 NGLOG("recvfrom"); 207 errno = errnosv; 208 return (-1); 209 } 210 211 /* Copy hook name */ 212 if (hook != NULL) 213 snprintf(hook, NG_HOOKLEN + 1, "%s", from->sg_data); 214 215 /* Debugging */ 216 if (_gNgDebugLevel >= 2) { 217 NGLOGX("READ %s from hook \"%s\" (%d bytes)", 218 rtn ? "PACKET" : "EOF", from->sg_data, rtn); 219 if (_gNgDebugLevel >= 3) 220 _NgDebugBytes(buf, rtn); 221 } 222 223 /* Done */ 224 return (rtn); 225 } 226 227 /* 228 * Write a packet to a data socket. The packet will be sent 229 * out the corresponding node on the specified hook. 230 * Returns -1 if error and sets errno. 231 */ 232 int 233 NgSendData(int ds, const char *hook, const u_char * buf, size_t len) 234 { 235 u_char sgbuf[NG_HOOKLEN + sizeof(struct sockaddr_ng)]; 236 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf; 237 int errnosv; 238 239 /* Set up destination hook */ 240 sg->sg_family = AF_NETGRAPH; 241 snprintf(sg->sg_data, NG_HOOKLEN + 1, "%s", hook); 242 sg->sg_len = strlen(sg->sg_data) + 3; 243 244 /* Debugging */ 245 if (_gNgDebugLevel >= 2) { 246 NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len); 247 _NgDebugSockaddr(sg); 248 if (_gNgDebugLevel >= 3) 249 _NgDebugBytes(buf, len); 250 } 251 252 /* Send packet */ 253 if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) { 254 errnosv = errno; 255 if (_gNgDebugLevel >= 1) 256 NGLOG("sendto(%s)", sg->sg_data); 257 errno = errnosv; 258 return (-1); 259 } 260 261 /* Done */ 262 return (0); 263 } 264 265