Annotation of embedaddon/mpd/src/ng.c, revision 1.1
1.1 ! misho 1:
! 2: /*
! 3: * ng.c
! 4: *
! 5: * Written by Archie Cobbs <archie@freebsd.org>
! 6: * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
! 7: * See ``COPYRIGHT.whistle''
! 8: */
! 9:
! 10: #include "ppp.h"
! 11: #include "ng.h"
! 12: #include "phys.h"
! 13: #include "ngfunc.h"
! 14: #include "log.h"
! 15:
! 16: #include <netgraph/ng_message.h>
! 17: #include <netgraph/ng_socket.h>
! 18: #include <netgraph.h>
! 19:
! 20: /*
! 21: * DEFINITIONS
! 22: */
! 23:
! 24: #define NG_MTU 1600
! 25: #define NG_MRU 1600
! 26:
! 27: struct nginfo {
! 28: char path[NG_PATHSIZ]; /* Node that takes PPP frames */
! 29: char hook[NG_HOOKSIZ]; /* Hook on that node */
! 30: };
! 31: typedef struct nginfo *NgInfo;
! 32:
! 33: /* Set menu options */
! 34: enum {
! 35: SET_NODE,
! 36: SET_HOOK
! 37: };
! 38:
! 39: /*
! 40: * INTERNAL FUNCTIONS
! 41: */
! 42:
! 43: static int NgInit(Link l);
! 44: static void NgOpen(Link l);
! 45: static void NgClose(Link l);
! 46: static void NgShutdown(Link l);
! 47: static void NgStat(Context ctx);
! 48: static int NgSetCommand(Context ctx, int ac, char *av[], void *arg);
! 49: static int NgIsSync(Link l);
! 50: static int NgPeerAddr(Link l, void *buf, size_t buf_len);
! 51:
! 52: /*
! 53: * GLOBAL VARIABLES
! 54: */
! 55:
! 56: const struct phystype gNgPhysType = {
! 57: .name = "ng",
! 58: .descr = "Netgraph hook",
! 59: .mtu = NG_MTU,
! 60: .mru = NG_MRU,
! 61: .tmpl = 0,
! 62: .init = NgInit,
! 63: .open = NgOpen,
! 64: .close = NgClose,
! 65: .shutdown = NgShutdown,
! 66: .showstat = NgStat,
! 67: .issync = NgIsSync,
! 68: .peeraddr = NgPeerAddr,
! 69: .callingnum = NULL,
! 70: .callednum = NULL,
! 71: };
! 72:
! 73: const struct cmdtab NgSetCmds[] = {
! 74: { "node {path}", "Set node to attach to",
! 75: NgSetCommand, NULL, 2, (void *) SET_NODE },
! 76: { "hook {hook}", "Set hook to attach to",
! 77: NgSetCommand, NULL, 2, (void *) SET_HOOK },
! 78: { NULL },
! 79: };
! 80:
! 81: /*
! 82: * NgInit()
! 83: *
! 84: * Initialize device-specific data in physical layer info
! 85: */
! 86:
! 87: static int
! 88: NgInit(Link l)
! 89: {
! 90: NgInfo ng;
! 91:
! 92: /* Allocate private struct */
! 93: ng = (NgInfo) (l->info = Malloc(MB_PHYS, sizeof(*ng)));
! 94: snprintf(ng->path, sizeof(ng->path), "undefined:");
! 95: snprintf(ng->hook, sizeof(ng->hook), "undefined");
! 96:
! 97: /* Done */
! 98: return(0);
! 99: }
! 100:
! 101: /*
! 102: * NgOpen()
! 103: */
! 104:
! 105: static void
! 106: NgOpen(Link l)
! 107: {
! 108: NgInfo const ng = (NgInfo) l->info;
! 109: char path[NG_PATHSIZ];
! 110: int csock = -1;
! 111: struct ngm_connect cn;
! 112:
! 113: if (!PhysGetUpperHook(l, path, cn.ourhook)) {
! 114: Log(LG_PHYS, ("[%s] NG: can't get upper hook", l->name));
! 115: goto fail;
! 116: }
! 117:
! 118: /* Get a temporary netgraph socket node */
! 119: if (NgMkSockNode(NULL, &csock, NULL) == -1) {
! 120: Perror("[%s] NG: NgMkSockNode", l->name);
! 121: goto fail;
! 122: }
! 123:
! 124: strlcpy(cn.path, ng->path, sizeof(cn.path));
! 125: strlcpy(cn.peerhook, ng->hook, sizeof(cn.peerhook));
! 126: if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)) < 0) {
! 127: Perror("[%s] NG: can't connect \"%s\"->\"%s\" and \"%s\"->\"%s\"",
! 128: l->name, path, cn.ourhook, cn.path, cn.peerhook);
! 129: goto fail;
! 130: }
! 131:
! 132: close(csock);
! 133: l->state = PHYS_STATE_UP;
! 134: PhysUp(l);
! 135: return;
! 136:
! 137: fail:
! 138: if (csock>=0) {
! 139: close(csock);
! 140: csock = -1;
! 141: }
! 142: l->state = PHYS_STATE_DOWN;
! 143: PhysDown(l, STR_CON_FAILED0, NULL);
! 144: }
! 145:
! 146: /*
! 147: * NgClose()
! 148: */
! 149:
! 150: static void
! 151: NgClose(Link l)
! 152: {
! 153: NgInfo const ng = (NgInfo) l->info;
! 154: int csock = -1;
! 155:
! 156: /* Get a temporary netgraph socket node */
! 157: if (NgMkSockNode(NULL, &csock, NULL) == -1) {
! 158: Perror("[%s] NG: NgMkSockNode", l->name);
! 159: goto fail;
! 160: }
! 161:
! 162: NgFuncDisconnect(csock, l->name, ng->path, ng->hook);
! 163:
! 164: close(csock);
! 165: /* FALL */
! 166:
! 167: fail:
! 168: l->state = PHYS_STATE_DOWN;
! 169: PhysDown(l, STR_MANUALLY, NULL);
! 170: }
! 171:
! 172: /*
! 173: * NgShutdown()
! 174: */
! 175: static void
! 176: NgShutdown(Link l)
! 177: {
! 178: Freee(l->info);
! 179: }
! 180:
! 181: /*
! 182: * NgStat()
! 183: */
! 184:
! 185: void
! 186: NgStat(Context ctx)
! 187: {
! 188: NgInfo const ng = (NgInfo) ctx->lnk->info;
! 189:
! 190: Printf("Netgraph node configuration:\r\n");
! 191: Printf("\tNode : %s\r\n", ng->path);
! 192: Printf("\tHook : %s\r\n", ng->hook);
! 193: }
! 194:
! 195: /*
! 196: * NgSetCommand()
! 197: */
! 198:
! 199: static int
! 200: NgSetCommand(Context ctx, int ac, char *av[], void *arg)
! 201: {
! 202: NgInfo const ng = (NgInfo) ctx->lnk->info;
! 203:
! 204: switch ((intptr_t)arg) {
! 205: case SET_NODE:
! 206: if (ac != 1)
! 207: return(-1);
! 208: strlcpy(ng->path, av[0], sizeof(ng->path));
! 209: break;
! 210: case SET_HOOK:
! 211: if (ac != 1)
! 212: return(-1);
! 213: strlcpy(ng->hook, av[0], sizeof(ng->hook));
! 214: break;
! 215: default:
! 216: assert(0);
! 217: }
! 218: return(0);
! 219: }
! 220:
! 221: /*
! 222: * NgIsSync()
! 223: */
! 224:
! 225: static int
! 226: NgIsSync(Link l)
! 227: {
! 228: return (1);
! 229: }
! 230:
! 231: /*
! 232: * NgPeerAddr()
! 233: */
! 234:
! 235: static int
! 236: NgPeerAddr(Link l, void *buf, size_t buf_len)
! 237: {
! 238: NgInfo const ng = (NgInfo) l->info;
! 239:
! 240: if (buf_len < sizeof(ng->path))
! 241: return(-1);
! 242:
! 243: memcpy(buf, ng->path, sizeof(ng->path));
! 244:
! 245: return(0);
! 246: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>