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 headerlen = strnlen(buf, len);
33 pb_log("%s: bad header, no data\n", __func__);
37 /* we should see an <action>@<device>\0 at the head of the buffer */
38 sep = strchr(buf, '@');
40 pb_log("%s: bad header: %s\n", __func__, buf);
44 /* terminate the action string */
47 if (streq(buf, "add"))
48 *action = EVENT_ACTION_ADD;
49 else if (streq(buf, "remove"))
50 *action = EVENT_ACTION_REMOVE;
51 else if (streq(buf, "conf"))
52 *action = EVENT_ACTION_CONF;
54 pb_log("%s: unknown action: %s\n", __func__, buf);
59 pb_log("%s: bad device: %s\n", __func__, buf);
68 * event_parse_params - Parse a <name>=<value> buffer.
70 * The buffer is not modified.
73 static void event_parse_params(struct event *event, const char *buf, int len)
75 int param_len, name_len, value_len;
79 for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
81 /* find the length of the whole parameter */
82 param_len = strnlen(buf, len);
84 /* multiple NULs? skip over */
89 /* update the params array */
90 event->params = talloc_realloc(event, event->params,
91 struct param, ++event->n_params);
92 param = &event->params[event->n_params - 1];
94 sep = memchr(buf, '=', param_len);
100 name_len = sep - buf;
101 value_len = param_len - name_len - 1;
102 param->value = talloc_strndup(event, sep + 1,
105 param->name = talloc_strndup(event, buf, name_len);
109 int event_parse_ad_message(struct event *event, char *buf, int len)
111 enum event_action action;
115 headerlen = event_parse_ad_header(buf, len, &action, &device);
120 /* now we have an action and a device, we can construct the event */
121 event->action = action;
122 event->device = talloc_strdup(event, device);
124 event->params = NULL;
126 len -= headerlen + 1;
127 buf += headerlen + 1;
128 event_parse_params(event, buf, len);
133 const char *event_get_param(const struct event *event, const char *name)
137 for (i = 0; i < event->n_params; i++)
138 if (!strcasecmp(event->params[i].name, name))
139 return event->params[i].value;
144 void event_set_param(struct event *event, const char *name, const char *value)
149 /* if it's already present, replace the value of the old param */
150 for (i = 0; i < event->n_params; i++) {
151 param = &event->params[i];
152 if (!strcasecmp(param->name, name)) {
153 talloc_free(param->value);
154 param->value = talloc_strdup(event, value);
159 /* not found - create a new param */
160 event->params = talloc_realloc(event, event->params,
161 struct param, ++event->n_params);
162 param = &event->params[event->n_params - 1];
164 param->name = talloc_strdup(event, name);
165 param->value = talloc_strdup(event, value);