浏览代码

Add dhcp_snprintf() for extracting DHCP string options.

tags/v0.9.3
Michael Brown 18 年前
父节点
当前提交
b26806cf18
共有 2 个文件被更改,包括 44 次插入0 次删除
  1. 10
    0
      src/include/gpxe/dhcp.h
  2. 34
    0
      src/net/dhcpopts.c

+ 10
- 0
src/include/gpxe/dhcp.h 查看文件

@@ -160,6 +160,13 @@
160 160
  */
161 161
 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
162 162
 
163
+/** BIOS drive number
164
+ *
165
+ * This is the drive number for a drive emulated via INT 13.  0x80 is
166
+ * the first hard disk, 0x81 is the second hard disk, etc.
167
+ */
168
+#define DHCP_EB_BIOS_DRIVE DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 0xbd )
169
+
163 170
 /** Maximum normal DHCP option */
164 171
 #define DHCP_MAX_OPTION 254
165 172
 
@@ -255,6 +262,7 @@ struct dhcp_option {
255 262
 		uint32_t dword;
256 263
 		struct in_addr in;
257 264
 		uint8_t bytes[0];
265
+		char string[0];
258 266
 	} data;
259 267
 } __attribute__ (( packed ));
260 268
 
@@ -435,6 +443,8 @@ struct dhcp_session {
435 443
 extern unsigned long dhcp_num_option ( struct dhcp_option *option );
436 444
 extern void dhcp_ipv4_option ( struct dhcp_option *option,
437 445
 			       struct in_addr *inp );
446
+extern int dhcp_snprintf ( char *buf, size_t size,
447
+			   struct dhcp_option *option );
438 448
 extern struct dhcp_option *
439 449
 find_dhcp_option ( struct dhcp_option_block *options, unsigned int tag );
440 450
 extern void register_dhcp_options ( struct dhcp_option_block *options );

+ 34
- 0
src/net/dhcpopts.c 查看文件

@@ -101,6 +101,40 @@ void dhcp_ipv4_option ( struct dhcp_option *option, struct in_addr *inp ) {
101 101
 		*inp = option->data.in;
102 102
 }
103 103
 
104
+/**
105
+ * Print DHCP string option value into buffer
106
+ *
107
+ * @v buf		Buffer into which to write the string
108
+ * @v size		Size of buffer
109
+ * @v option		DHCP option, or NULL
110
+ * @ret len		Length of formatted string
111
+ *
112
+ * DHCP option strings are stored without a NUL terminator.  This
113
+ * function provides a convenient way to extract these DHCP strings
114
+ * into standard C strings.  It is permitted to call dhcp_snprintf()
115
+ * with @c option set to NULL; in this case the buffer will be filled
116
+ * with an empty string.
117
+ *
118
+ * The usual snprintf() semantics apply with regard to buffer size,
119
+ * return value when the buffer is too small, etc.
120
+ */
121
+int dhcp_snprintf ( char *buf, size_t size, struct dhcp_option *option ) {
122
+	size_t len;
123
+	char *content = "";
124
+
125
+	if ( option ) {
126
+		/* Shrink buffer size so that it is only just large
127
+		 * enough to contain the option data.  snprintf() will
128
+		 * take care of everything else (inserting the NUL etc.)
129
+		 */
130
+		len = ( option->len + 1 );
131
+		if ( len < size )
132
+			size = len;
133
+		content = option->data.string;
134
+	}
135
+	return snprintf ( buf, size, "%s", content );
136
+}
137
+
104 138
 /**
105 139
  * Calculate length of a normal DHCP option
106 140
  *

正在加载...
取消
保存