1 #if defined(HAVE_CONFIG_H)
9 #include <talloc/talloc.h>
13 #define streq(a, b) (!strcasecmp((a), (b)))
16 * event_parse_ad_header - Parse an <action>@<device> event header.
18 * The buffer is modified in place.
19 * Returns zero on success.
22 static int event_parse_ad_header(char *buf, int len, enum event_action *action,
30 /* we should see an <action>@<device>\0 at the head of the buffer */
31 sep = strchr(buf, '@');
33 pb_log("%s: bad header: %s\n", __func__, buf);
37 /* terminate the action string */
41 if (streq(buf, "add"))
42 *action = EVENT_ACTION_ADD;
43 else if (streq(buf, "remove"))
44 *action = EVENT_ACTION_REMOVE;
45 else if (streq(buf, "conf"))
46 *action = EVENT_ACTION_CONF;
48 pb_log("%s: unknown action: %s\n", __func__, buf);
53 pb_log("%s: bad device: %s\n", __func__, buf);
62 * event_parse_params - Parse a <name>=<value> buffer.
64 * The buffer is not modified.
67 static void event_parse_params(struct event *event, const char *buf, int len)
69 int param_len, name_len, value_len;
73 for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
75 /* find the length of the whole parameter */
76 param_len = strnlen(buf, len);
78 /* multiple NULs? skip over */
83 /* find the separator */
84 sep = memchr(buf, '=', param_len);
89 value_len = param_len - name_len - 1;
91 /* update the params array */
92 event->params = talloc_realloc(event, event->params,
93 struct param, ++event->n_params);
94 param = &event->params[event->n_params - 1];
96 param->name = talloc_strndup(event, buf, name_len);
97 param->value = talloc_strndup(event, sep + 1, value_len);
101 int event_parse_ad_message(struct event *event, char *buf, int len)
105 enum event_action action;
108 result = event_parse_ad_header(buf, len, &action, &device);
113 device_len = strlen(device);
115 /* now we have an action and a device, we can construct the event */
116 event->action = action;
117 event->device = talloc_strndup(event, device, device_len);
119 event->params = NULL;
121 len -= device_len + 1;
122 event_parse_params(event, device + device_len + 1, len);
127 const char *event_get_param(const struct event *event, const char *name)
131 for (i = 0; i < event->n_params; i++)
132 if (!strcasecmp(event->params[i].name, name))
133 return event->params[i].value;