2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-08-31 06:15:55 +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 "dhctoken.h"
void do_a_packet (int);
void do_a_line (int);
TIME cur_time;
unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */
@@ -77,7 +80,7 @@ int main (argc, argv, envp)
GET_TIME (&cur_time);
name.sin_family = AF_INET;
name.sin_port = htons (2000);
name.sin_port = htons (2001);
name.sin_addr.s_addr = htonl (INADDR_ANY);
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)
error ("Can't bind to dhcp address: %m");
do {
fd_set r, w, x;
FD_ZERO (&r);
FD_ZERO (&w);
FD_ZERO (&x);
FD_SET (sock, &r);
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 (fork() > 0) {
while (1)
do_a_packet (sock);
} else {
while (1)
do_a_line (sock);
}
if (FD_ISSET (sock, &r) || FD_ISSET (sock, &w)
|| FD_ISSET (sock, &x)) {
if ((result =
recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen))
< 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 ()
{
}
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");
sleep (5);
continue;
return;
}
note ("request from %s, port %d",
inet_ntoa (from.sin_addr),
htons (from.sin_port));
inet_ntoa (from.sin_addr), htons (from.sin_port));
ifrom.len = 4;
memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);
memcpy (&incoming, packbuf, result);
@@ -135,24 +180,33 @@ int main (argc, argv, envp)
ip.client_addr = ifrom;
ip.client_sock = sock;
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
exit if we've hit EOF. */
dump_packet (&ip);
}
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);
if (token == EOF)
break;
return;
memset (&decl, 0, sizeof decl);
parse_client_statement (cfile, &decl);
@@ -195,9 +249,7 @@ int main (argc, argv, envp)
if (decl.ciaddr) {
tree_evaluate (decl.ciaddr);
memcpy (&raw.ciaddr,
decl.ciaddr -> value,
decl.ciaddr -> len);
memcpy (&raw.ciaddr, decl.ciaddr -> value, decl.ciaddr -> len);
} else
memset (&raw.ciaddr, 0, sizeof raw.ciaddr);
@@ -232,7 +284,7 @@ int main (argc, argv, envp)
raw.hops = 0;
raw.op = BOOTREQUEST;
to.sin_port = htons (2001);
to.sin_port = htons (2000);
to.sin_addr.s_addr = INADDR_BROADCAST;
to.sin_family = AF_INET;
to.sin_len = sizeof to;
@@ -247,42 +299,4 @@ int main (argc, argv, envp)
0, (struct sockaddr *)&to, sizeof to);
if (result < 0)
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 "dhctoken.h"
void do_a_packet (int);
void do_a_line (int);
TIME cur_time;
unsigned char packbuf [65536]; /* Should cover the gnarliest MTU... */
@@ -77,7 +80,7 @@ int main (argc, argv, envp)
GET_TIME (&cur_time);
name.sin_family = AF_INET;
name.sin_port = htons (2000);
name.sin_port = htons (2001);
name.sin_addr.s_addr = htonl (INADDR_ANY);
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)
error ("Can't bind to dhcp address: %m");
do {
fd_set r, w, x;
FD_ZERO (&r);
FD_ZERO (&w);
FD_ZERO (&x);
FD_SET (sock, &r);
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 (fork() > 0) {
while (1)
do_a_packet (sock);
} else {
while (1)
do_a_line (sock);
}
if (FD_ISSET (sock, &r) || FD_ISSET (sock, &w)
|| FD_ISSET (sock, &x)) {
if ((result =
recvfrom (sock, packbuf, sizeof packbuf, 0,
(struct sockaddr *)&from, &fromlen))
< 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 ()
{
}
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");
sleep (5);
continue;
return;
}
note ("request from %s, port %d",
inet_ntoa (from.sin_addr),
htons (from.sin_port));
inet_ntoa (from.sin_addr), htons (from.sin_port));
ifrom.len = 4;
memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);
memcpy (&incoming, packbuf, result);
@@ -135,24 +180,33 @@ int main (argc, argv, envp)
ip.client_addr = ifrom;
ip.client_sock = sock;
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
exit if we've hit EOF. */
dump_packet (&ip);
}
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);
if (token == EOF)
break;
return;
memset (&decl, 0, sizeof decl);
parse_client_statement (cfile, &decl);
@@ -195,9 +249,7 @@ int main (argc, argv, envp)
if (decl.ciaddr) {
tree_evaluate (decl.ciaddr);
memcpy (&raw.ciaddr,
decl.ciaddr -> value,
decl.ciaddr -> len);
memcpy (&raw.ciaddr, decl.ciaddr -> value, decl.ciaddr -> len);
} else
memset (&raw.ciaddr, 0, sizeof raw.ciaddr);
@@ -232,7 +284,7 @@ int main (argc, argv, envp)
raw.hops = 0;
raw.op = BOOTREQUEST;
to.sin_port = htons (2001);
to.sin_port = htons (2000);
to.sin_addr.s_addr = INADDR_BROADCAST;
to.sin_family = AF_INET;
to.sin_len = sizeof to;
@@ -247,42 +299,4 @@ int main (argc, argv, envp)
0, (struct sockaddr *)&to, sizeof to);
if (result < 0)
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 ()
{
}