summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2010-09-06 15:48:12 +0300
committerTimo Teräs <timo.teras@iki.fi>2010-09-06 15:48:12 +0300
commitb9290a29fae3b229372fc444a8d3f4631b62a57d (patch)
tree6ba1cf010dd44c492c4b182ad4ba36deb5a99157
parent050e200061d2b5002251c85a83c55bb7e1864a39 (diff)
downloadsquark-b9290a29fae3b229372fc444a8d3f4631b62a57d.tar.bz2
squark-b9290a29fae3b229372fc444a8d3f4631b62a57d.tar.xz
auth-snmp: improve line parser
get rid of sscanf and use the blob api.
-rw-r--r--squark-auth-snmp.c59
1 files changed, 41 insertions, 18 deletions
diff --git a/squark-auth-snmp.c b/squark-auth-snmp.c
index 817a466..3fd253c 100644
--- a/squark-auth-snmp.c
+++ b/squark-auth-snmp.c
@@ -104,6 +104,9 @@ static int l3_if_ndx, l2_vlan_ndx;
static time_t current_time;
static int username_format_flags;
+static const blob_t space = BLOB_STR_INIT(" ");
+static const blob_t lf = BLOB_STR_INIT("\n");
+
/* ----------------------------------------------------------------- */
#define BLOB_OID(objid) BLOB_BUF(objid)
@@ -926,7 +929,7 @@ static int auth_handle_arp_reply(int oper, netsnmp_session *s, int reqid, netsnm
return 1;
}
-void start_authentication(const char *token, const char *ip)
+void start_authentication(blob_t token, blob_t ip)
{
struct auth_context *auth;
oid query_oids[MAX_OID_LEN];
@@ -936,8 +939,8 @@ void start_authentication(const char *token, const char *ip)
num_queries++;
auth = calloc(1, sizeof(*auth));
- auth->token = strdup(token);
- if (addr_parse(BLOB_STRLEN(ip), &auth->addr) == NULL) {
+ auth->token = blob_cstr_dup(token);
+ if (addr_parse(ip, &auth->addr) == NULL) {
auth_completed(auth);
return;
}
@@ -957,33 +960,52 @@ void start_authentication(const char *token, const char *ip)
auth_talk_snmp(auth, l3_root_dev->session, pdu, auth_handle_arp_reply);
}
-void read_input(void)
+static void handle_line(blob_t line)
+{
+ blob_t id, ipaddr;
+
+ id = blob_pull_cspn(&line, space);
+ blob_pull_spn(&line, space);
+ ipaddr = blob_pull_cspn(&line, space);
+
+ start_authentication(id, ipaddr);
+}
+
+static void read_input(void)
{
static char buffer[256];
- static int len = 0;
- char token[32], ip[256], *p;
+ static blob_t left;
+
+ blob_t b, line;
int r;
- r = read(STDIN_FILENO, &buffer[len], sizeof(buffer) - len);
+ if (blob_is_null(left))
+ left = BLOB_BUF(buffer);
+
+ r = read(STDIN_FILENO, left.ptr, left.len);
if (r < 0)
return;
if (r == 0) {
- running = FALSE;
+ running = 0;
return;
}
+ left.ptr += r;
+ left.len -= r;
- len += r;
+ b = blob_pushed(BLOB_BUF(buffer), left);
do {
- p = strchr(buffer, '\n');
- if (p == NULL)
+ line = blob_pull_cspn(&b, lf);
+ if (!blob_pull_matching(&b, lf))
return;
- *p = 0;
- if (sscanf(buffer, "%s %s", token, ip) == 2)
- start_authentication(token, ip);
- len -= (p - buffer) + 1;
- memcpy(buffer, p + 1, len);
- } while (len);
+ handle_line(line);
+
+ if (b.len) {
+ memcpy(buffer, b.ptr, b.len);
+ b.ptr = buffer;
+ }
+ left = BLOB_PTR_LEN(buffer + b.len, sizeof(buffer) - b.len);
+ } while (b.len);
}
void load_topology(const char *file)
@@ -1065,7 +1087,8 @@ int main(int argc, char **argv)
username_format_flags |= FORMAT_PORT_WEBAUTH;
for (i = 0; i < argc; i++) {
- start_authentication(argv[i], argv[i]);
+ blob_t b = BLOB_STRLEN(argv[i]);
+ start_authentication(b, b);
running = FALSE;
}