/* babble.c */ /* This package just sends out random packets formatted according to our network protocol, trying to confuse all other nodes. Well, the purpose is really to test the packet sniffer. */ /* I just include everything, to be on the safe side. */ #include #include #include #include #include #include #include #include #include #include #include /* range of IR sensor */ #define IR_NEAR 0 #define IR_FAR 1 /* This is the random number generator. It returns an integer in the range 0 ... l-1 */ static int seed1, seed2; int get_ran (int l) { seed1 = (seed1 * 13 + 29) % 67; seed2 = (seed2 * 43 + 31) % 59; return ((seed1 + seed2) % l); } /* This function makes a random list, puts it into a buffer, and returns the length of the list (for convenience, as it is equal to buf[0]). maxlen is the max n. of list elements. The list is guaranteed to be non-empty. */ int get_list (char buf[], int maxlen) { int len, i; len = 1 + get_ran (maxlen); buf [0] = (char) len; for (i = 0; i < len; i++) { buf [i] = (char) get_ran (32); } return (len); } /* This function creates a random packet, and returns the packet length (in bytes). */ int make_packet (char buf[]) { int len, is_broadcast, body_type; len = 0; is_broadcast = get_ran (2); if (is_broadcast) { /* Broadcast packet */ buf [len++] = (char) 1; /* broadcast */ buf [len++] = (char) get_ran (32); /* source */ buf [len++] = (char) get_ran (32); /* nonce */ /* chooses body type */ body_type = 1 + get_ran (2); buf [len++] = (char) body_type; switch (body_type) { case 1: /* RR body */ buf [len++] = (char) get_ran (32); /* dest */ len += get_list (buf + len, 16); /* visited list */ break; case 2: /* RP body */ buf [len++] = (char) get_ran (32); /* dest */ len += get_list (buf + len, 16); /* visited list */ len += get_list (buf + len, 16); /* reverse path */ break; } } else { /* Routed packet */ buf [len++] = (char) 2; buf [len++] = (char) get_ran (32); /* source */ len += get_list (buf + len, 16); /* route list */ /* chooses body type */ body_type = 1 + get_ran (3); buf [len++] = (char) body_type; switch (body_type) { case 1: /* RC packet */ len += get_list (buf + len, 16); /* reverse path */ break; case 2: /* DT packet */ buf [len++] = (char) get_ran (32); /* id */ len += get_list (buf + len, 16); /* data content */ break; case 3: /* AK packet */ buf [len++] = (char) get_ran (32); /* ack id */ break; } } return (len); } /* This is the data sender */ int talk () { unsigned long seed = 34; /* why 34? why not? */ int i; int len, result; char send_buffer [128]; while (1) { len = make_packet (send_buffer); /* This sends a byte to the other process; keeps trying until it succeeds */ do { result = lnp_integrity_write(send_buffer, len); /* Displays how many bytes it has sent */ lcd_int(len); /* waits between attempts */ msleep (100); } while (result); /* interval of time before sending another command */ msleep (900); } } int main() { pid_t pid1, pid2; /* Sets the IR distance */ lnp_logical_range(IR_FAR); /* Spawns the processes that talk, and listen */ pid1=execi(&talk, 0, NULL, 1, DEFAULT_STACK_SIZE); return 0; }