Author: adrian.chadd
Date: Sun Feb 22 11:53:07 2009
New Revision: 13814
Added:
playpen/LUSCA_HEAD_bgp/libsqbgp/
playpen/LUSCA_HEAD_bgp/libsqbgp/Makefile.am
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c
playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c
Log:
Bring over my hacky BGP code, first cut.
Added: playpen/LUSCA_HEAD_bgp/libsqbgp/Makefile.am
==============================================================================
--- (empty file)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/Makefile.am Sun Feb 22 11:53:07 2009
@@ -0,0 +1,10 @@
+## Process this file with automake to produce Makefile.in
+
+libsqbgp_a_SOURCES = \
+ bgp_core.c \
+ bgp_packet.c
+ bgp_rib.c
+
+noinst_LIBRARIES = \
+ libsqbgp.a
+
Added: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c
==============================================================================
--- (empty file)
+++ playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_core.c Sun Feb 22 11:53:07 2009
@@ -0,0 +1,457 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "../include/util.h"
+#include "../libcore/tools.h"
+#include "../libsqdebug/debug.h"
+
+int
+bgp_msg_len(const char *buf, int len)
+{
+ u_int16_t msg_len;
+
+ if (len < 19)
+ return -1;
+
+ msg_len = ntohs(* (u_int16_t *) (buf + 16));
+ return msg_len;
+}
+
+int
+bgp_msg_type(const char *buf, int len)
+{
+ u_int8_t type;
+
+ if (len < 19)
+ return -1;
+
+ type = * (u_int8_t *) (buf + 18);
+ return type;
+}
+
+int
+bgp_msg_isvalid(const char *buf, int len)
+{
+ return -1;
+}
+
+int
+bgp_msg_complete(const char *buf, int len)
+{
+ int type;
+ int msg_len;
+
+ type = bgp_msg_type(buf, len);
+ msg_len = bgp_msg_len(buf, len);
+ //printf("bgp_msg_complete: type %d, pktlen %d, msglen %d\n", type,
len,
msg_len);
+
+ if (type < 0)
+ return 0;
+ if (msg_len > len)
+ return 0;
+ return 1;
+}
+
+/*
+ * XXX absolutely hacky!
+ */
+int
+bgp_send_hello(int fd, unsigned short asnum, short hold_time, struct
in_addr bgp_id)
+{
+ char send_buf[128];
+ char *p = send_buf;
+ u_int16_t t;
+ u_int16_t len;
+ u_int8_t t8;
+ int pkt_len;
+
+ /* marker */
+ memset(p, 255, 16);
+ p += 16;
+
+ /* length - fill out later! */
+ p += 2;
+
+ /* type */
+ t8 = 1; /* open message */
+ memcpy(p, &t8, sizeof(t8));
+ p += 1;
+
+ /* now, hello */
+ t8 = 4; /* bgp4 */
+ memcpy(p, &t8, sizeof(t8));
+ p += 1;
+
+ /* as number */
+ t = htons(asnum);
+ memcpy(p, &t, sizeof(t));
+ p += 2;
+
+ /* hold time */
+ t = htons(hold_time);
+ memcpy(p, &t, sizeof(t));
+ p += 2;
+
+ /* bgp identifier */
+ memcpy(p, &bgp_id.s_addr, sizeof(bgp_id.s_addr));
+ p += 4;
+
+ /* optional parameter */
+ t8 = 0;
+ memcpy(p, &t8, sizeof(t8));
+ p += 1;
+
+ pkt_len = p - send_buf;
+ printf("OPEN: len: %d\n", pkt_len);
+ len = htons(p - send_buf);
+ p = send_buf + 16;
+ memcpy(p, &len, sizeof(len));
+
+ return (write(fd, send_buf, pkt_len) == pkt_len);
+}
+
+int
+bgp_send_keepalive(int fd)
+{
+ char send_buf[128];
+ char *p = send_buf;
+ u_int8_t t8;
+ int pkt_len;
+ u_int16_t len;
+
+ /* marker */
+ memset(p, 255, 16);
+ p += 16;
+
+ /* length - fill out later! */
+ p += 2;
+
+ /* type */
+ t8 = 4; /* open message */
+ memcpy(p, &t8, sizeof(t8));
+ p += 1;
+
+ pkt_len = p - send_buf;
+ //printf("bgp_send_keepalive: KEEPALIVE: len: %d\n", pkt_len);
+ len = htons(p - send_buf);
+ p = send_buf + 16;
+ memcpy(p, &len, sizeof(len));
+
+ return (write(fd, send_buf, pkt_len) == pkt_len);
+
+
+}
+
+int
+bgp_handle_notification(int fd, const char *buf, int len)
+{
+ u_int8_t err_code;
+ u_int8_t err_subcode;
+ u_int16_t err_data;
+
+ err_code = * (u_int8_t *) buf;
+ err_subcode = * (u_int8_t *) (buf + 2);
+ err_data = ntohs(* (u_int8_t *) (buf + 4));
+ printf("bgp_handle_notification: err %d, subcode %d, data %d\n",
err_code, err_subcode, err_data);
+ return 1;
+}
+
+int
+bgp_handle_open(int fd, const char *buf, int len)
+{
+ u_int8_t version;
+ u_int16_t bgp_as;
+ u_int16_t hold_timer;
+ struct in_addr bgp_id;
+ int parm_len;
+
+ /* XXX should ensure we have enough space! */
+
+ version = * (u_int8_t *) buf;
+ bgp_as = ntohs(* (u_int16_t *) (buf + 1));
+ hold_timer = ntohs(* (u_int16_t *) (buf + 3));
+ memcpy(&bgp_id, buf + 5, 4);
+
+ parm_len = * (u_int8_t *) (buf + 9);
+ /* XXX don't bother decoding the OPEN parameters for now! */
+
+ //printf("bgp_handle_open: got version %d, AS %d, timer %d,
parm_len %d\n", version, bgp_as, hold_timer, parm_len);
+
+ /* Queue a keepalive message */
+ bgp_send_keepalive(fd);
+
+ return 1;
+}
+
+int
+bgp_handle_update_withdraw(const char *buf, int len)
+{
+ struct in_addr pf;
+ u_int8_t pl, netmask;
+ int i = 0;
+
+ if (len == 0)
+ return 1;
+
+ printf(" bgp_handle_update_withdraw: len %d\n", len);
+ while (i < len) {
+ bzero(&pf, sizeof(pf));
+ /* The "length" is the number of bits which are "valid" .. */
+ netmask = (* (u_int8_t *) (buf + i));
+ if (netmask == 0)
+ pl = 0;
+ else
+ pl = ((netmask - 1) / 8) + 1;
+ i++;
+ printf(" bgp_handle_update_withdraw: netmask %d; len %d\n",
netmask,
pl);
+ /* XXX bounds check? */
+ memcpy(&pf, buf + i, pl);
+ printf(" bgp_handle_update_withdraw: prefix %s/%d\n",
inet_ntoa(pf),
netmask);
+ i += pl;
+ }
+ return 1;
+}
+
+int
+bgp_handle_update_pathattrib_origin(const char *buf, int len)
+{
+ u_int8_t origin;
+ if (len < 1)
+ return 0;
+ origin = * (u_int8_t *) buf;
+ printf(" bgp_handle_update_pathattrib_origin: origin id %d\n", origin);
+ return 1;
+}
+
+int
+bgp_handle_update_pathattrib_aspath(const char *buf, int len)
+{
+ int i;
+ u_int16_t aspath_entry;
+ u_int8_t aspath_type, aspath_len;
+
+ if (len < 2)
+ return 0;
+
+ aspath_type = * (u_int8_t *) (buf);
+ aspath_len = * (u_int8_t *) (buf + 1);
+
+ /* XXX well, the length should be verified / used / bounds checked? */
+
+ printf(" bgp_handle_update_pathattrib_aspath:");
+ for (i = 2; i < len; i += 2) {
+ aspath_entry = ntohs(* (u_int16_t *) (buf + i));
+ printf(" %d", aspath_entry);
+ }
+ printf("\n");
+
+ return 1;
+}
+
+int
+bgp_handle_update_pathattrib(const char *buf, int len)
+{
+ int i = 0;
+ u_int8_t a_flags, a_type;
+ int a_len;
+
+ /* Iterate over the buffer, pulling out <type, length, value> fields */
+ /* XXX should bounds check some more! */
+ //printf("bgp_handle_update_pathattrib: BEGIN\n");
+ while (i < len) {
+ a_flags = * (u_int8_t *) (buf + i);
+ a_type = * (u_int8_t *) (buf + i + 1);
+ i += 2;
+ /* Length is either 8 or 16 bits; encoded by the extended
length bit */
+ if (a_flags & 0x10) {
+ a_len = ntohs(* (u_int16_t *) (buf + i));
+ i += 2;
+ } else {
+ a_len = * (u_int8_t *) (buf + i);
+ i += 1;
+ }
+ //printf(" bgp_handle_update_pathattrib: flags %x, type %x,
len %d\n",
a_flags, a_type, a_len);
+
+ switch (a_type) {
+ case 1: /* origin */
+ if (bgp_handle_update_pathattrib_origin(buf +
i, a_len) < 0)
+ return 0;
+ break;
+ case 2: /* as path */
+ if (bgp_handle_update_pathattrib_aspath(buf +
i, a_len) < 0)
+ return 0;
+ break;
+ default:
+ //printf(" bgp_handle_path_attrib: don't know
type %d\n", a_type);
+ break;
+ }
+
+ i += a_len;
+ }
+ //printf("bgp_handle_update_pathattrib: DONE\n");
+ return 1;
+}
+
+int
+bgp_handle_update_nlri(const char *buf, int len)
+{
+ struct in_addr pf;
+ u_int8_t pl, netmask;
+ int i = 0;
+
+ if (len == 0)
+ return 1;
+
+ printf(" bgp_handle_update_nlri: len %d\n", len);
+ while (i < len) {
+ bzero(&pf, sizeof(pf));
+ /* The "length" is the number of bits which are "valid" .. */
+ netmask = (* (u_int8_t *) (buf + i));
+ if (netmask == 0)
+ pl = 0;
+ else
+ pl = ((netmask - 1) / 8) + 1;
+ i++;
+ printf(" bgp_handle_update_nlri: netmask %d; len %d\n",
netmask, pl);
+ /* XXX bounds check? */
+ memcpy(&pf, buf + i, pl);
+ printf(" bgp_handle_update_nlri: prefix %s/%d\n",
inet_ntoa(pf),
netmask);
+ i += pl;
+ }
+ return 1;
+}
+
+int
+bgp_handle_update(int fd, const char *buf, int len)
+{
+ u_int16_t withdraw_route_len;
+ u_int16_t path_attrib_len;
+
+ withdraw_route_len = ntohs(* (u_int16_t *) buf);
+ path_attrib_len = ntohs(* (u_int16_t *) (buf + withdraw_route_len + 2));
+
+ printf("bgp_handle_update: UPDATE pktlen %d: withdraw_route_len %d;
path
attrib len %d\n",
+ len, withdraw_route_len, path_attrib_len);
+
+ if (! bgp_handle_update_withdraw(buf + 2, withdraw_route_len))
+ return 0;
+ if (! bgp_handle_update_pathattrib(buf + 2 + withdraw_route_len + 2,
path_attrib_len))
+ return 0;
+ /* XXX need to calculate the length and offset correctly! */
+ if (! bgp_handle_update_nlri(buf + 2 + withdraw_route_len + 2 +
path_attrib_len, len - (2 + withdraw_route_len + 2 + path_attrib_len)))
+ return 0;
+
+ return 1;
+}
+
+int
+bgp_handle_keepalive(int fd, const char *buf, int len)
+{
+ //printf("bgp_handle_keepalive: KEEPALIVE RECEIVED\n");
+ return 1;
+}
+
+int
+bgp_decode_message(int fd, const const char *buf, int len)
+{
+ int r;
+
+ u_int8_t type;
+ u_int16_t pkt_len;
+ /* XXX should check the marker is 16 bytes */
+ /* XXX should make sure there's enough bytes in the msg! */
+
+ pkt_len = ntohs(* (u_int16_t *) (buf + 16));
+ type = * (u_int8_t *) (buf + 18);
+ //printf("bgp_decode_message: type %d; len %d\n", type, pkt_len);
+ switch (type) {
+ case 1: /* OPEN */
+ r = bgp_handle_open(fd, buf + 19, pkt_len - 19);
+ break;
+ case 2: /* UPDATE */
+ r = bgp_handle_update(fd, buf + 19, pkt_len - 19);
+ break;
+ case 3: /* NOTIFICATION */
+ r = bgp_handle_notification(fd, buf + 19, pkt_len - 19);
+ break;
+ case 4: /* KEEPALIVE */
+ r = bgp_handle_keepalive(fd, buf + 19, pkt_len - 19);
+ break;
+ default:
+ printf("bgp_decode_message: unknown message type:
%d\n", type);
+ exit(1);
+ }
+
+ return pkt_len;
+}
+
+#if 0
+int
+main(int argc, const char *argv[])
+{
+ int fd, r;
+ struct sockaddr_in sa;
+ struct in_addr bgp_id;
+ char buf[4096];
+ int bufofs = 0;
+ int i, len;
+
+ fd = socket(AF_INET, SOCK_STREAM, 0);
+ assert(fd != -1);
+
+ /* connect to bgp thing */
+ bzero(&sa, sizeof(sa));
+ inet_aton("216.12.163.51", &sa.sin_addr);
+ inet_aton("216.12.163.53", &bgp_id);
+ sa.sin_port = htons(179);
+ sa.sin_len = sizeof(struct sockaddr_in);
+ sa.sin_family = AF_INET;
+ r = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
+ assert(r > -1);
+
+ /* Now, loop over and read messages */
+ /* We'll eventually have to uhm, speak BGP.. */
+ r = bgp_send_hello(fd, 65535, 120, bgp_id);
+
+ printf("ready to read stuff\n");
+
+ while (1) {
+ bzero(buf + bufofs, sizeof(buf) - bufofs);
+ /* XXX should check there's space in the buffer first! */
+ printf("main: space in buf is %d bytes\n", (int) sizeof(buf) -
bufofs);
+ len = read(fd, buf + bufofs, sizeof(buf) - bufofs);
+ assert(len > 0);
+ bufofs += len;
+ printf("read: %d bytes; bufsize is now %d\n", len, bufofs);
+ i = 0;
+
+ /* loop over; try to handle partial messages */
+ while (i < len) {
+ printf("looping..\n");
+ /* Is there enough data here? */
+ if (! bgp_msg_complete(buf + i, bufofs - i)) {
+ printf("main: incomplete packet\n");
+ break;
+ }
+ r = bgp_decode_message(fd, buf + i, bufofs - i);
+ assert(r > 0);
+ i += r;
+ printf("main: pkt was %d bytes, i is now %d\n", r, i);
+ }
+ /* "consume" the rest of the buffer */
+ memmove(buf, buf + i, sizeof(buf) - i);
+ bufofs -= i;
+ printf("consumed %d bytes; bufsize is now %d\n", i, bufofs);
+ }
+
+ exit(0);
+}
+#endif
Added: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_packet.c
==============================================================================
Added: playpen/LUSCA_HEAD_bgp/libsqbgp/bgp_rib.c
==============================================================================
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"lusca-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/lusca-commit?hl=en
-~----------~----~----~----~------~----~------~--~---