Added a little packet parsing.

git-svn-id: http://repo.ham.fi/svn/aprsc/trunk@6 3ce903b1-3385-4e86-93cd-f9a4a239f7ac
This commit is contained in:
Heikki Hannikainen 2008-02-27 18:46:15 +00:00
parent fbf9527d2b
commit 078b44eb70
5 changed files with 100 additions and 7 deletions

View File

@ -23,6 +23,7 @@ distclean: clean
BITS = aprsc.o accept.o worker.o \
login.o incoming.o dupecheck.o outgoing.o \
parse_aprs.o \
config.o netlib.o xpoll.o \
cfgfile.o passcode.o \
rwlock.o hmalloc.o hlog.o \
@ -36,9 +37,11 @@ aprsc.o: aprsc.c hmalloc.h hlog.h config.h splay.h accept.h
accept.o: accept.c accept.h hmalloc.h hlog.h config.h netlib.h worker.h dupecheck.h
worker.o: worker.c worker.h hmalloc.h hlog.h config.h xpoll.h incoming.h outgoing.h
login.o: login.c login.h hmalloc.h hlog.h worker.h passcode.h incoming.h config.h
incoming.o: incoming.c incoming.h hmalloc.h hlog.h worker.h
incoming.o: incoming.c incoming.h hmalloc.h hlog.h worker.h parse_aprs.h
outgoing.o: outgoing.c outgoing.h hlog.h worker.h
dupecheck.o: dupecheck.c dupecheck.h hmalloc.h hlog.h worker.h
parse_aprs.o: parse_aprs.c parse_aprs.h hlog.h
parse_aprs.h: worker.h
passcode.o: passcode.c passcode.h
xpoll.o: xpoll.c xpoll.h hmalloc.h hlog.h
netlib.o: netlib.c netlib.h worker.h

View File

@ -10,6 +10,7 @@
#include "incoming.h"
#include "hmalloc.h"
#include "hlog.h"
#include "parse_aprs.h"
/*
* Get a buffer for a packet
@ -125,9 +126,63 @@ void incoming_flush(struct worker_t *self)
int incoming_parse(struct worker_t *self, struct pbuf_t *pb)
{
/* TODO: get lat/lon out of position packets */
char *src_end; /* pointer to the > after srccall */
char *path_start; /* pointer to the start of the path */
char *path_end; /* pointer to the : after the path */
char *packet_end; /* pointer to the end of the packet */
char *info_start; /* pointer to the beginning of the info */
char *info_end; /* end of the info */
char *dstcall_end; /* end of dstcall ([:,]) */
return 0;
/* a packet looks like:
* SRCCALL>DSTCALL,PATH,PATH:INFO\r\n
* (we have normalized the \r\n by now)
*/
packet_end = pb->data + pb->packet_len; /* for easier overflow checking */
/* look for the '>' */
src_end = memchr(pb->data, '>', pb->packet_len < CALLSIGNLEN_MAX+1 ? pb->packet_len : CALLSIGNLEN_MAX+1);
if (!src_end)
return -1;
path_start = src_end+1;
if (path_start >= packet_end)
return -1;
if (src_end - pb->data > CALLSIGNLEN_MAX)
return -1; /* too long source callsign */
/* look for the : */
path_end = memchr(path_start, ':', packet_end - path_start);
if (!path_end)
return -1;
info_start = path_end+1;
if (info_start >= packet_end)
return -1;
/* see that there is at least some data in the packet */
info_end = packet_end - 2;
if (info_end <= info_start)
return -1;
/* look up end of dstcall */
/* the logic behind this is slightly uncertain. */
dstcall_end = path_start;
while (dstcall_end < path_end && *dstcall_end != ',' && *dstcall_end != ',' && *dstcall_end != '-')
dstcall_end++;
if (dstcall_end - path_start > CALLSIGNLEN_MAX)
return -1; /* too long destination callsign */
/* fill necessary info for parsing and dupe checking in the packet buffer */
pb->srccall_end = src_end;
pb->dstcall_end = dstcall_end;
pb->info_start = info_start;
/* just try APRS parsing */
return parse_aprs(self, pb);
}
/*
@ -137,6 +192,7 @@ int incoming_parse(struct worker_t *self, struct pbuf_t *pb)
int incoming_handler(struct worker_t *self, struct client_t *c, char *s, int len)
{
struct pbuf_t *pb;
int e;
/* note: len does not include CRLF, it's reconstructed here... we accept
* CR, LF or CRLF on input but make sure to use CRLF on output.
@ -171,7 +227,16 @@ int incoming_handler(struct worker_t *self, struct client_t *c, char *s, int len
memcpy((void *)&pb->addr, (void *)&c->addr, c->addr_len);
/* do some parsing */
incoming_parse(self, pb);
e = incoming_parse(self, pb);
if (e < 0) {
/* failed parsing */
fprintf(stderr, "Failed parsing (%d):\n", e);
fwrite(pb->data, len, 1, stderr);
fprintf(stderr, "\n");
// TODO: return buffer pbuf_put(self, pb);
return 0;
}
/* put the buffer in the thread's incoming queue */
pb->next = NULL;

9
src/parse_aprs.c Normal file
View File

@ -0,0 +1,9 @@
#include "parse_aprs.h"
#include "hlog.h"
int parse_aprs(struct worker_t *self, struct pbuf_t *pb)
{
return 0;
}

10
src/parse_aprs.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef PARSE_APRS_H
#define PARSE_APRS_H
#include "worker.h"
#include "parse_aprs.h"
extern int parse_aprs(struct worker_t *self, struct pbuf_t *pb);
#endif

View File

@ -17,6 +17,8 @@
extern time_t now; /* current time - updated by the main thread */
#define CALLSIGNLEN_MAX 9
/* packet length limiters and buffer sizes */
#define PACKETLEN_MIN 4 /* minimum length for a valid APRS-IS packet: "A>B:" */
#define PACKETLEN_MAX 600 /* max... arbitrary and not documented */
@ -46,13 +48,17 @@ struct pbuf_t {
int packettype; /* bitmask: one or more of T_* */
int flags; /* bitmask: one or more of F_* */
float lat; /* if the packet is PT_POSITION, latitude and longitude go here */
float lng;
int packet_len; /* the actual length of the packet, including CRLF */
int buf_len; /* the length of this buffer */
char *data; /* contains the whole packet, including CRLF, ready to transmit */
char *srccall_end; /* source callsign with SSID */
char *dstcall_end; /* end of dest callsign SSID */
char *info_start; /* pointer to start of info field */
float lat; /* if the packet is PT_POSITION, latitude and longitude go here */
float lng;
struct sockaddr addr; /* where did we get it from (don't send it back) */
};