socsvn commit: r287682 - soc2015/roam/ng_ayiya

roam at FreeBSD.org roam at FreeBSD.org
Sun Jun 28 01:39:31 UTC 2015


Author: roam
Date: Sun Jun 28 01:39:29 2015
New Revision: 287682
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=287682

Log:
  Implement the GET/SET_VERSION/MOTD messages.
  
  Keep the ng_ayiya node's version and "message of the day" text strings
  in almost-ready-to-send mbufs.  Update them on a SET_* control message
  and return them on a GET_* one.
  
  Not replying to actual AYIYA "version" and "motd" queries yet, nor
  querying the remote side for theirs yet.
  
  ObQuote:	"I can't remember my name, will I ever find myself"

Modified:
  soc2015/roam/ng_ayiya/ng_ayiya.c
  soc2015/roam/ng_ayiya/ng_ayiya.h

Modified: soc2015/roam/ng_ayiya/ng_ayiya.c
==============================================================================
--- soc2015/roam/ng_ayiya/ng_ayiya.c	Sun Jun 28 01:39:25 2015	(r287681)
+++ soc2015/roam/ng_ayiya/ng_ayiya.c	Sun Jun 28 01:39:29 2015	(r287682)
@@ -78,6 +78,34 @@
 		NULL,
 		&ng_parse_uint32_type,
 	},
+	{
+		NGM_AYIYA_COOKIE,
+		NGM_AYIYA_SET_VERSION,
+		"set_version",
+		&ng_parse_string_type,
+		NULL,
+	},
+	{
+		NGM_AYIYA_COOKIE,
+		NGM_AYIYA_SET_MOTD,
+		"set_motd",
+		&ng_parse_string_type,
+		NULL,
+	},
+	{
+		NGM_AYIYA_COOKIE,
+		NGM_AYIYA_GET_VERSION,
+		"get_version",
+		NULL,
+		&ng_parse_string_type,
+	},
+	{
+		NGM_AYIYA_COOKIE,
+		NGM_AYIYA_GET_MOTD,
+		"get_motd",
+		NULL,
+		&ng_parse_string_type,
+	},
 	{ 0 }
 };
 
@@ -119,12 +147,48 @@
 	hook_p	hooks[AYIYA_HOOK_LAST];
 	node_p	node;
 	item_p	configuring;
+	struct mbuf	*m_version, *m_motd;
 	bool	configured;
 };
 typedef struct ng_ayiya_private *priv_p;
 
 static int	send_heartbeat(const node_p node);
 
+static struct mbuf *
+ayiya_m_extend_to(struct mbuf * const m, size_t left)
+{
+	for (struct mbuf *mm = m; mm != NULL && left > 0; mm = mm->m_next)
+	{
+		const size_t len = min(M_TRAILINGSPACE(mm), left);
+		mm->m_len = len;
+		m->m_pkthdr.len += len;
+		left -= len;
+	}
+	if (left > 0)
+		/* FIXME: Hmm, tack another mbuf at the end or something? */
+		return (NULL);
+	return (m);
+}
+
+static struct mbuf *
+ayiya_m_getm(const size_t sz, const int how)
+{
+	struct mbuf * const m = m_getm2(NULL, sz, how, MT_DATA, M_PKTHDR);
+	if (m == NULL)
+		return (NULL);
+	struct mbuf * const m2 = ayiya_m_extend_to(m, sz);
+	if (m2 == NULL) {
+		m_freem(m);
+		return (NULL);
+	}
+	return (m2);
+}
+
+#define AYIYA_VERSION	"ng_ayiya 0.1.0.dev177"
+#define AYIYA_VERSION_SZ	sizeof(AYIYA_VERSION)
+#define AYIYA_MOTD	"No message of the day defined yet."
+#define AYIYA_MOTD_SZ	sizeof(AYIYA_MOTD)
+
 static int
 ng_ayiya_constructor(const node_p node)
 {
@@ -133,7 +197,27 @@
 	priv = malloc(sizeof(*priv), M_NETGRAPH_AYIYA, M_WAITOK | M_ZERO);
 	NG_NODE_SET_PRIVATE(node, priv);
 	priv->node = node;
+	
+	priv->m_version = ayiya_m_getm(AYIYA_VERSION_SZ, M_WAITOK);
+	if (priv->m_version == NULL)
+		goto no_mem;
+	bcopy(AYIYA_VERSION, priv->m_version->m_data, AYIYA_VERSION_SZ);
+
+	priv->m_motd = ayiya_m_getm(AYIYA_MOTD_SZ, M_WAITOK);
+	if (priv->m_motd == NULL)
+		goto no_mem;
+	bcopy(AYIYA_MOTD, priv->m_motd->m_data, AYIYA_MOTD_SZ);
+
 	return (0);
+
+no_mem:
+	m_freem(priv->m_motd);
+	priv->m_motd = NULL;
+	m_freem(priv->m_version);
+	priv->m_version = NULL;
+	free(priv, M_NETGRAPH_AYIYA);
+	NG_NODE_SET_PRIVATE(node, NULL);
+	return (ENOMEM);
 }
 
 #define ERROUT(x)	do { error = (x); goto done; } while (0)
@@ -305,6 +389,42 @@
 			return (0);
 			}
 
+		case NGM_AYIYA_GET_VERSION:
+			{
+			const priv_p priv = NG_NODE_PRIVATE(node);
+			NG_MKRESPONSE(resp, msg, priv->m_version->m_len, M_WAITOK);
+			bcopy(priv->m_version->m_data, resp->data, priv->m_version->m_len);
+			break;
+			}
+
+		case NGM_AYIYA_GET_MOTD:
+			{
+			const priv_p priv = NG_NODE_PRIVATE(node);
+			NG_MKRESPONSE(resp, msg, priv->m_motd->m_len, M_WAITOK);
+			bcopy(priv->m_motd->m_data, resp->data, priv->m_motd->m_len);
+			break;
+			}
+
+		case NGM_AYIYA_SET_VERSION:
+		case NGM_AYIYA_SET_MOTD:
+			{
+			const size_t sz = msg->header.arglen;
+			struct mbuf * const m = ayiya_m_getm(sz, M_WAITOK);
+			if (m == NULL) {
+				error = ENOMEM;
+				break;
+			}
+			bcopy(msg->data, m->m_data, sz);
+
+			const priv_p priv = NG_NODE_PRIVATE(node);
+			struct mbuf ** const mm =
+			    msg->header.cmd == NGM_AYIYA_SET_VERSION?
+			    &priv->m_version: &priv->m_motd;
+			m_freem(*mm);
+			*mm = m;
+			break;
+			}
+
 		default:
 			error = EINVAL;
 			break;
@@ -413,10 +533,14 @@
 {
 	const priv_p priv = NG_NODE_PRIVATE(node);
 
-	if (priv->configuring)
-		configuring_respond(node, ECONNABORTED);
-	free(priv, M_NETGRAPH_AYIYA);
-	NG_NODE_SET_PRIVATE(node, NULL);
+	if (priv != NULL) {
+		if (priv->configuring)
+			configuring_respond(node, ECONNABORTED);
+		m_freem(priv->m_motd);
+		m_freem(priv->m_version);
+		free(priv, M_NETGRAPH_AYIYA);
+		NG_NODE_SET_PRIVATE(node, NULL);
+	}
 	NG_NODE_UNREF(node);
 	return (0);
 }
@@ -443,21 +567,10 @@
 {
 	struct mbuf *m = *mb;
 
-	if (m == NULL) {
-		size_t left = sizeof(struct ng_ayiya_packet);
-		m = m_getm2(NULL, left, M_NOWAIT, MT_DATA, M_PKTHDR);
-		if (m == NULL)
-			return (ENOMEM);
-		for (struct mbuf *mm = m; mm != NULL && left > 0; mm = mm->m_next)
-		{
-			const size_t len = min(M_TRAILINGSPACE(mm), left);
-			mm->m_len = len;
-			m->m_pkthdr.len += len;
-			left -= len;
-		}
-	} else {
+	if (m == NULL)
+		m = ayiya_m_getm(sizeof(struct ng_ayiya_packet), M_NOWAIT);
+	else
 		M_PREPEND(m, sizeof(struct ng_ayiya_packet), M_NOWAIT);
-	}
 	if (m->m_next)
 		m = m_defrag(m, M_NOWAIT);
 	if (m == NULL)

Modified: soc2015/roam/ng_ayiya/ng_ayiya.h
==============================================================================
--- soc2015/roam/ng_ayiya/ng_ayiya.h	Sun Jun 28 01:39:25 2015	(r287681)
+++ soc2015/roam/ng_ayiya/ng_ayiya.h	Sun Jun 28 01:39:29 2015	(r287682)
@@ -34,6 +34,10 @@
 enum {
 	NGM_AYIYA_SECRETHASH = 1,
 	NGM_AYIYA_CONFIGURE,
+	NGM_AYIYA_SET_VERSION,
+	NGM_AYIYA_SET_MOTD,
+	NGM_AYIYA_GET_VERSION,
+	NGM_AYIYA_GET_MOTD,
 };
 
 struct ng_ayiya_header {


More information about the svn-soc-all mailing list