aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/script.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/script.c')
-rw-r--r--networking/udhcp/script.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c
index 4ce23aafc..1c6f1bd33 100644
--- a/networking/udhcp/script.c
+++ b/networking/udhcp/script.c
@@ -37,6 +37,8 @@
37#include "options.h" 37#include "options.h"
38#include "debug.h" 38#include "debug.h"
39 39
40#include "config.h"
41
40/* get a rough idea of how long an option will be (rounding up...) */ 42/* get a rough idea of how long an option will be (rounding up...) */
41static int max_option_length[] = { 43static int max_option_length[] = {
42 [OPTION_IP] = sizeof("255.255.255.255 "), 44 [OPTION_IP] = sizeof("255.255.255.255 "),
@@ -62,6 +64,37 @@ static int sprintip(char *dest, char *pre, unsigned char *ip) {
62 return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]); 64 return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]);
63} 65}
64 66
67#ifdef CONFIG_FEATURE_UDHCPC_IP
68/* convert a netmask (255.255.255.0) into the length (24) */
69static int inet_ntom (const char *src, short *dst)
70{
71 in_addr_t mask, num;
72
73 mask = ntohl(*(unsigned int *)src);
74
75 for (num = mask; num & 1; num >>= 1);
76
77 if (num != 0 && mask != 0)
78 {
79 for (num = ~mask; num & 1; num >>= 1);
80 if (num)
81 return 0;
82 }
83
84 for (num = 0; mask; mask <<= 1)
85 num++;
86
87 *dst = num;
88
89 return 1;
90}
91
92static int sprintprefix(char *dest, char *pre, unsigned char *ip) {
93 short sdest = 0;
94 inet_ntom(ip, &sdest);
95 return sprintf(dest, "%s%hd ", pre, sdest);
96}
97#endif
65 98
66/* Fill dest with the text of option 'option'. */ 99/* Fill dest with the text of option 'option'. */
67static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p) 100static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p)
@@ -84,9 +117,20 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
84 *(dest++) = '/'; 117 *(dest++) = '/';
85 option += 4; 118 option += 4;
86 optlen = 4; 119 optlen = 4;
120#ifndef CONFIG_FEATURE_UDHCPC_IP
87 case OPTION_IP: /* Works regardless of host byte order. */ 121 case OPTION_IP: /* Works regardless of host byte order. */
122#endif
88 dest += sprintip(dest, "", option); 123 dest += sprintip(dest, "", option);
89 break; 124 break;
125#ifdef CONFIG_FEATURE_UDHCPC_IP
126 case OPTION_IP: /* Works regardless of host byte order. */
127 if (type_p->flags & OPTION_PREFIX) {
128 dest += sprintprefix(dest, "", option);
129 } else {
130 dest += sprintip(dest, "", option);
131 }
132 break;
133#endif
90 case OPTION_BOOLEAN: 134 case OPTION_BOOLEAN:
91 dest += sprintf(dest, *option ? "yes " : "no "); 135 dest += sprintf(dest, *option ? "yes " : "no ");
92 break; 136 break;