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;
53 else if (streq(buf, "dhcp"))
54 *action = EVENT_ACTION_DHCP;
56 pb_log("%s: unknown action: %s\n", __func__, buf);
61 pb_log("%s: bad device: %s\n", __func__, buf);
70 * event_parse_params - Parse a <name>=<value> buffer.
72 * The buffer is not modified.
75 static void event_parse_params(struct event *event, const char *buf, int len)
77 int param_len, name_len, value_len;
81 for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
83 /* find the length of the whole parameter */
84 param_len = strnlen(buf, len);
86 /* multiple NULs? skip over */
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 sep = memchr(buf, '=', param_len);
102 name_len = sep - buf;
103 value_len = param_len - name_len - 1;
104 param->value = talloc_strndup(event, sep + 1,
107 param->name = talloc_strndup(event, buf, name_len);
111 int event_parse_ad_message(struct event *event, char *buf, int len)
113 enum event_action action;
117 headerlen = event_parse_ad_header(buf, len, &action, &device);
122 /* now we have an action and a device, we can construct the event */
123 event->action = action;
124 event->device = talloc_strdup(event, device);
126 event->params = NULL;
128 len -= headerlen + 1;
129 buf += headerlen + 1;
130 event_parse_params(event, buf, len);
135 const char *event_get_param(const struct event *event, const char *name)
139 for (i = 0; i < event->n_params; i++)
140 if (!strcasecmp(event->params[i].name, name))
141 return event->params[i].value;
146 void event_set_param(struct event *event, const char *name, const char *value)
151 /* if it's already present, replace the value of the old param */
152 for (i = 0; i < event->n_params; i++) {
153 param = &event->params[i];
154 if (!strcasecmp(param->name, name)) {
155 talloc_free(param->value);
156 param->value = talloc_strdup(event, value);
161 /* not found - create a new param */
162 event->params = talloc_realloc(event, event->params,
163 struct param, ++event->n_params);
164 param = &event->params[event->n_params - 1];
166 param->name = talloc_strdup(event, name);
167 param->value = talloc_strdup(event, value);