1 #if defined(HAVE_CONFIG_H)
8 #include <talloc/talloc.h>
12 #define streq(a, b) (!strcasecmp((a), (b)))
15 * event_parse_ad_header - Parse an <action>@<device> event header.
17 * The buffer is modified in place.
18 * Returns zero on success.
21 static int event_parse_ad_header(char *buf, int len, enum event_action *action,
29 headerlen = strnlen(buf, len);
32 pb_log_fn("bad header, no data\n");
36 /* we should see an <action>@<device>\0 at the head of the buffer */
37 sep = strchr(buf, '@');
39 pb_log_fn("bad header: %s\n", buf);
43 /* terminate the action string */
46 if (streq(buf, "add"))
47 *action = EVENT_ACTION_ADD;
48 else if (streq(buf, "remove"))
49 *action = EVENT_ACTION_REMOVE;
50 else if (streq(buf, "url"))
51 *action = EVENT_ACTION_URL;
52 else if (streq(buf, "dhcp"))
53 *action = EVENT_ACTION_DHCP;
54 else if (streq(buf, "boot"))
55 *action = EVENT_ACTION_BOOT;
56 else if (streq(buf, "sync"))
57 *action = EVENT_ACTION_SYNC;
58 else if (streq(buf, "plugin"))
59 *action = EVENT_ACTION_PLUGIN;
61 pb_log_fn("unknown action: %s\n", buf);
66 pb_log_fn("bad device: %s\n", buf);
75 * event_parse_params - Parse a <name>=<value> buffer.
77 * The buffer is not modified.
80 static void event_parse_params(struct event *event, const char *buf, int len)
82 int param_len, name_len, value_len;
83 struct event_param *param;
86 for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
88 /* find the length of the whole parameter */
89 param_len = strnlen(buf, len);
91 /* multiple NULs? skip over */
96 /* update the params array */
97 event->params = talloc_realloc(event, event->params,
98 struct event_param, ++event->n_params);
99 param = &event->params[event->n_params - 1];
101 sep = memchr(buf, '=', param_len);
103 name_len = param_len;
107 name_len = sep - buf;
108 value_len = param_len - name_len - 1;
109 param->value = talloc_strndup(event, sep + 1,
112 param->name = talloc_strndup(event, buf, name_len);
116 int event_parse_ad_message(struct event *event, char *buf, int len)
118 enum event_action action;
122 headerlen = event_parse_ad_header(buf, len, &action, &device);
127 /* now we have an action and a device, we can construct the event */
128 event->action = action;
129 event->device = talloc_strdup(event, device);
131 event->params = NULL;
133 len -= headerlen + 1;
134 buf += headerlen + 1;
135 event_parse_params(event, buf, len);
140 const char *event_get_param(const struct event *event, const char *name)
144 for (i = 0; i < event->n_params; i++)
145 if (!strcasecmp(event->params[i].name, name))
146 return event->params[i].value;
151 void event_set_param(struct event *event, const char *name, const char *value)
153 struct event_param *param;
156 /* if it's already present, replace the value of the old param */
157 for (i = 0; i < event->n_params; i++) {
158 param = &event->params[i];
159 if (!strcasecmp(param->name, name)) {
160 talloc_free(param->value);
161 param->value = talloc_strdup(event, value);
166 /* not found - create a new param */
167 event->params = talloc_realloc(event, event->params,
168 struct event_param, ++event->n_params);
169 param = &event->params[event->n_params - 1];
171 param->name = talloc_strdup(event, name);
172 param->value = talloc_strdup(event, value);