2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-09-01 14:55:30 +00:00

Major rewrite

This commit is contained in:
Ted Lemon
1996-02-26 01:58:31 +00:00
parent 98dd66895c
commit e581d6150f
2 changed files with 332 additions and 304 deletions

View File

@@ -42,6 +42,9 @@
#include "dhcpd.h" #include "dhcpd.h"
#include "dhctoken.h" #include "dhctoken.h"
void do_a_packet (int);
void do_a_line (int);
TIME cur_time; TIME cur_time;
unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */ unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */
@@ -77,7 +80,7 @@ int main (argc, argv, envp)
GET_TIME (&cur_time); GET_TIME (&cur_time);
name.sin_family = AF_INET; name.sin_family = AF_INET;
name.sin_port = htons (2000); name.sin_port = htons (2001);
name.sin_addr.s_addr = htonl (INADDR_ANY); name.sin_addr.s_addr = htonl (INADDR_ANY);
memset (name.sin_zero, 0, sizeof (name.sin_zero)); memset (name.sin_zero, 0, sizeof (name.sin_zero));
@@ -99,32 +102,74 @@ int main (argc, argv, envp)
if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0) if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0)
error ("Can't bind to dhcp address: %m"); error ("Can't bind to dhcp address: %m");
do { if (fork() > 0) {
fd_set r, w, x; while (1)
FD_ZERO (&r); do_a_packet (sock);
FD_ZERO (&w); } else {
FD_ZERO (&x); while (1)
FD_SET (sock, &r); do_a_line (sock);
FD_SET (sock, &w);
FD_SET (sock, &x);
FD_SET (0, &r); /* stdin */
if (select (sock + 1, &r, &w, &x, (struct timeval *)0) < 0) {
error ("select: %m");
} }
if (FD_ISSET (sock, &r) || FD_ISSET (sock, &w) }
|| FD_ISSET (sock, &x)) {
if ((result = /* statement :== host_statement */
recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen)) void parse_client_statement (cfile, decl)
< 0) { FILE *cfile;
struct host_decl *decl;
{
char *val;
jmp_buf bc;
int token;
switch (next_token (&val, cfile)) {
case PACKET:
memset (decl, 0, sizeof decl);
if (!setjmp (bc)) {
do {
token = peek_token (&val, cfile);
if (token == SEMI) {
token = next_token (&val, cfile);
break;
}
parse_host_decl (cfile, &bc, decl);
} while (1);
}
break;
default:
parse_warn ("expecting a declaration.");
skip_to_semi (cfile);
break;
}
}
void cleanup ()
{
}
void do_a_packet (sock)
int sock;
{
struct sockaddr_in name;
int flag;
struct dhcp_packet incoming;
struct packet ip;
struct sockaddr_in from;
struct iaddr ifrom;
int fromlen = sizeof from;
int max = 0;
int count;
int result;
int i;
int xid = 1;
if ((result = recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
warn ("recvfrom failed: %m"); warn ("recvfrom failed: %m");
sleep (5); sleep (5);
continue; return;
} }
note ("request from %s, port %d", note ("request from %s, port %d",
inet_ntoa (from.sin_addr), inet_ntoa (from.sin_addr), htons (from.sin_port));
htons (from.sin_port));
ifrom.len = 4; ifrom.len = 4;
memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len); memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);
memcpy (&incoming, packbuf, result); memcpy (&incoming, packbuf, result);
@@ -135,24 +180,33 @@ int main (argc, argv, envp)
ip.client_addr = ifrom; ip.client_addr = ifrom;
ip.client_sock = sock; ip.client_sock = sock;
parse_options (&ip); parse_options (&ip);
printf ("\nPacket from %s, port %d\n",
piaddr (ip.client_addr), ip.client_port);
for (i = 0; i < 256; i++)
if (ip.options [i].len)
printf ("%s = %s;\n",
dhcp_options [i].name,
pretty_print_option
(i,
ip.options [i].data,
ip.options [i].len));
} else if (FD_ISSET (0, &r)) {
int bufs = 0;
/* Parse a packet declaration from stdin, or dump_packet (&ip);
exit if we've hit EOF. */ }
void do_a_line (sock)
int sock;
{
int bufs = 0;
FILE *cfile = stdin;
char *val;
int flag;
int token;
struct dhcp_packet raw;
struct packet outgoing;
struct sockaddr_in to;
int max = 0;
int count;
int result;
int i;
struct host_decl decl;
int xid = 1;
/* Parse a packet declaration from stdin, or exit if
we've hit EOF. */
token = peek_token (&val, cfile); token = peek_token (&val, cfile);
if (token == EOF) if (token == EOF)
break; return;
memset (&decl, 0, sizeof decl); memset (&decl, 0, sizeof decl);
parse_client_statement (cfile, &decl); parse_client_statement (cfile, &decl);
@@ -195,9 +249,7 @@ int main (argc, argv, envp)
if (decl.ciaddr) { if (decl.ciaddr) {
tree_evaluate (decl.ciaddr); tree_evaluate (decl.ciaddr);
memcpy (&raw.ciaddr, memcpy (&raw.ciaddr, decl.ciaddr -> value, decl.ciaddr -> len);
decl.ciaddr -> value,
decl.ciaddr -> len);
} else } else
memset (&raw.ciaddr, 0, sizeof raw.ciaddr); memset (&raw.ciaddr, 0, sizeof raw.ciaddr);
@@ -232,7 +284,7 @@ int main (argc, argv, envp)
raw.hops = 0; raw.hops = 0;
raw.op = BOOTREQUEST; raw.op = BOOTREQUEST;
to.sin_port = htons (2001); to.sin_port = htons (2000);
to.sin_addr.s_addr = INADDR_BROADCAST; to.sin_addr.s_addr = INADDR_BROADCAST;
to.sin_family = AF_INET; to.sin_family = AF_INET;
to.sin_len = sizeof to; to.sin_len = sizeof to;
@@ -248,41 +300,3 @@ int main (argc, argv, envp)
if (result < 0) if (result < 0)
warn ("sendto: %m"); warn ("sendto: %m");
} }
} while (1);
exit (0);
}
/* statement :== host_statement */
void parse_client_statement (cfile, decl)
FILE *cfile;
struct host_decl *decl;
{
char *val;
jmp_buf bc;
int token;
switch (next_token (&val, cfile)) {
case PACKET:
memset (decl, 0, sizeof decl);
if (!setjmp (bc)) {
do {
token = peek_token (&val, cfile);
if (token == SEMI) {
token = next_token (&val, cfile);
break;
}
parse_host_decl (cfile, &bc, decl);
} while (1);
}
break;
default:
parse_warn ("expecting a declaration.");
skip_to_semi (cfile);
break;
}
}
void cleanup ()
{
}

View File

@@ -42,6 +42,9 @@
#include "dhcpd.h" #include "dhcpd.h"
#include "dhctoken.h" #include "dhctoken.h"
void do_a_packet (int);
void do_a_line (int);
TIME cur_time; TIME cur_time;
unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */ unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */
@@ -77,7 +80,7 @@ int main (argc, argv, envp)
GET_TIME (&cur_time); GET_TIME (&cur_time);
name.sin_family = AF_INET; name.sin_family = AF_INET;
name.sin_port = htons (2000); name.sin_port = htons (2001);
name.sin_addr.s_addr = htonl (INADDR_ANY); name.sin_addr.s_addr = htonl (INADDR_ANY);
memset (name.sin_zero, 0, sizeof (name.sin_zero)); memset (name.sin_zero, 0, sizeof (name.sin_zero));
@@ -99,32 +102,74 @@ int main (argc, argv, envp)
if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0) if (bind (sock, (struct sockaddr *)&name, sizeof name) < 0)
error ("Can't bind to dhcp address: %m"); error ("Can't bind to dhcp address: %m");
do { if (fork() > 0) {
fd_set r, w, x; while (1)
FD_ZERO (&r); do_a_packet (sock);
FD_ZERO (&w); } else {
FD_ZERO (&x); while (1)
FD_SET (sock, &r); do_a_line (sock);
FD_SET (sock, &w);
FD_SET (sock, &x);
FD_SET (0, &r); /* stdin */
if (select (sock + 1, &r, &w, &x, (struct timeval *)0) < 0) {
error ("select: %m");
} }
if (FD_ISSET (sock, &r) || FD_ISSET (sock, &w) }
|| FD_ISSET (sock, &x)) {
if ((result = /* statement :== host_statement */
recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen)) void parse_client_statement (cfile, decl)
< 0) { FILE *cfile;
struct host_decl *decl;
{
char *val;
jmp_buf bc;
int token;
switch (next_token (&val, cfile)) {
case PACKET:
memset (decl, 0, sizeof decl);
if (!setjmp (bc)) {
do {
token = peek_token (&val, cfile);
if (token == SEMI) {
token = next_token (&val, cfile);
break;
}
parse_host_decl (cfile, &bc, decl);
} while (1);
}
break;
default:
parse_warn ("expecting a declaration.");
skip_to_semi (cfile);
break;
}
}
void cleanup ()
{
}
void do_a_packet (sock)
int sock;
{
struct sockaddr_in name;
int flag;
struct dhcp_packet incoming;
struct packet ip;
struct sockaddr_in from;
struct iaddr ifrom;
int fromlen = sizeof from;
int max = 0;
int count;
int result;
int i;
int xid = 1;
if ((result = recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen)) < 0) {
warn ("recvfrom failed: %m"); warn ("recvfrom failed: %m");
sleep (5); sleep (5);
continue; return;
} }
note ("request from %s, port %d", note ("request from %s, port %d",
inet_ntoa (from.sin_addr), inet_ntoa (from.sin_addr), htons (from.sin_port));
htons (from.sin_port));
ifrom.len = 4; ifrom.len = 4;
memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len); memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);
memcpy (&incoming, packbuf, result); memcpy (&incoming, packbuf, result);
@@ -135,24 +180,33 @@ int main (argc, argv, envp)
ip.client_addr = ifrom; ip.client_addr = ifrom;
ip.client_sock = sock; ip.client_sock = sock;
parse_options (&ip); parse_options (&ip);
printf ("\nPacket from %s, port %d\n",
piaddr (ip.client_addr), ip.client_port);
for (i = 0; i < 256; i++)
if (ip.options [i].len)
printf ("%s = %s;\n",
dhcp_options [i].name,
pretty_print_option
(i,
ip.options [i].data,
ip.options [i].len));
} else if (FD_ISSET (0, &r)) {
int bufs = 0;
/* Parse a packet declaration from stdin, or dump_packet (&ip);
exit if we've hit EOF. */ }
void do_a_line (sock)
int sock;
{
int bufs = 0;
FILE *cfile = stdin;
char *val;
int flag;
int token;
struct dhcp_packet raw;
struct packet outgoing;
struct sockaddr_in to;
int max = 0;
int count;
int result;
int i;
struct host_decl decl;
int xid = 1;
/* Parse a packet declaration from stdin, or exit if
we've hit EOF. */
token = peek_token (&val, cfile); token = peek_token (&val, cfile);
if (token == EOF) if (token == EOF)
break; return;
memset (&decl, 0, sizeof decl); memset (&decl, 0, sizeof decl);
parse_client_statement (cfile, &decl); parse_client_statement (cfile, &decl);
@@ -195,9 +249,7 @@ int main (argc, argv, envp)
if (decl.ciaddr) { if (decl.ciaddr) {
tree_evaluate (decl.ciaddr); tree_evaluate (decl.ciaddr);
memcpy (&raw.ciaddr, memcpy (&raw.ciaddr, decl.ciaddr -> value, decl.ciaddr -> len);
decl.ciaddr -> value,
decl.ciaddr -> len);
} else } else
memset (&raw.ciaddr, 0, sizeof raw.ciaddr); memset (&raw.ciaddr, 0, sizeof raw.ciaddr);
@@ -232,7 +284,7 @@ int main (argc, argv, envp)
raw.hops = 0; raw.hops = 0;
raw.op = BOOTREQUEST; raw.op = BOOTREQUEST;
to.sin_port = htons (2001); to.sin_port = htons (2000);
to.sin_addr.s_addr = INADDR_BROADCAST; to.sin_addr.s_addr = INADDR_BROADCAST;
to.sin_family = AF_INET; to.sin_family = AF_INET;
to.sin_len = sizeof to; to.sin_len = sizeof to;
@@ -248,41 +300,3 @@ int main (argc, argv, envp)
if (result < 0) if (result < 0)
warn ("sendto: %m"); warn ("sendto: %m");
} }
} while (1);
exit (0);
}
/* statement :== host_statement */
void parse_client_statement (cfile, decl)
FILE *cfile;
struct host_decl *decl;
{
char *val;
jmp_buf bc;
int token;
switch (next_token (&val, cfile)) {
case PACKET:
memset (decl, 0, sizeof decl);
if (!setjmp (bc)) {
do {
token = peek_token (&val, cfile);
if (token == SEMI) {
token = next_token (&val, cfile);
break;
}
parse_host_decl (cfile, &bc, decl);
} while (1);
}
break;
default:
parse_warn ("expecting a declaration.");
skip_to_semi (cfile);
break;
}
}
void cleanup ()
{
}