Procházet zdrojové kódy

[ipv6] Add "ipv6" setting type

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown před 10 roky
rodič
revize
c1570d3dfb
3 změnil soubory, kde provedl 70 přidání a 0 odebrání
  1. 1
    0
      src/include/ipxe/settings.h
  2. 53
    0
      src/net/ipv6.c
  3. 16
    0
      src/tests/settings_test.c

+ 1
- 0
src/include/ipxe/settings.h Zobrazit soubor

@@ -377,6 +377,7 @@ extern char * expand_settings ( const char *string );
377 377
 extern struct setting_type setting_type_string __setting_type;
378 378
 extern struct setting_type setting_type_uristring __setting_type;
379 379
 extern struct setting_type setting_type_ipv4 __setting_type;
380
+extern struct setting_type setting_type_ipv6 __setting_type;
380 381
 extern struct setting_type setting_type_int8 __setting_type;
381 382
 extern struct setting_type setting_type_int16 __setting_type;
382 383
 extern struct setting_type setting_type_int32 __setting_type;

+ 53
- 0
src/net/ipv6.c Zobrazit soubor

@@ -866,6 +866,59 @@ struct sockaddr_converter ipv6_sockaddr_converter __sockaddr_converter = {
866 866
 	.aton = ipv6_sock_aton,
867 867
 };
868 868
 
869
+/**
870
+ * Parse IPv6 address setting value
871
+ *
872
+ * @v type		Setting type
873
+ * @v value		Formatted setting value
874
+ * @v buf		Buffer to contain raw value
875
+ * @v len		Length of buffer
876
+ * @ret len		Length of raw value, or negative error
877
+ */
878
+static int parse_ipv6_setting ( struct setting_type *type __unused,
879
+				const char *value, void *buf, size_t len ) {
880
+	struct in6_addr ipv6;
881
+	int rc;
882
+
883
+	/* Parse IPv6 address */
884
+	if ( ( rc = inet6_aton ( value, &ipv6 ) ) != 0 )
885
+		return rc;
886
+
887
+	/* Copy to buffer */
888
+	if ( len > sizeof ( ipv6 ) )
889
+		len = sizeof ( ipv6 );
890
+	memcpy ( buf, &ipv6, len );
891
+
892
+	return ( sizeof ( ipv6 ) );
893
+}
894
+
895
+/**
896
+ * Format IPv6 address setting value
897
+ *
898
+ * @v type		Setting type
899
+ * @v raw		Raw setting value
900
+ * @v raw_len		Length of raw setting value
901
+ * @v buf		Buffer to contain formatted value
902
+ * @v len		Length of buffer
903
+ * @ret len		Length of formatted value, or negative error
904
+ */
905
+static int format_ipv6_setting ( struct setting_type *type __unused,
906
+				 const void *raw, size_t raw_len, char *buf,
907
+				 size_t len ) {
908
+	const struct in6_addr *ipv6 = raw;
909
+
910
+	if ( raw_len < sizeof ( *ipv6 ) )
911
+		return -EINVAL;
912
+	return snprintf ( buf, len, "%s", inet6_ntoa ( ipv6 ) );
913
+}
914
+
915
+/** An IPv6 address setting type */
916
+struct setting_type setting_type_ipv6 __setting_type = {
917
+	.name = "ipv6",
918
+	.parse = parse_ipv6_setting,
919
+	.format = format_ipv6_setting,
920
+};
921
+
869 922
 /**
870 923
  * Perform IPv6 stateless address autoconfiguration (SLAAC)
871 924
  *

+ 16
- 0
src/tests/settings_test.c Zobrazit soubor

@@ -173,6 +173,12 @@ static struct setting test_ipv4_setting = {
173 173
 	.type = &setting_type_ipv4,
174 174
 };
175 175
 
176
+/** Test IPv6 address setting type */
177
+static struct setting test_ipv6_setting = {
178
+	.name = "test_ipv6",
179
+	.type = &setting_type_ipv6,
180
+};
181
+
176 182
 /** Test signed 8-bit integer setting type */
177 183
 static struct setting test_int8_setting = {
178 184
 	.name = "test_int8",
@@ -267,6 +273,16 @@ static void settings_test_exec ( void ) {
267 273
 	fetchf_ok ( &test_settings, &test_ipv4_setting,
268 274
 		    RAW ( 212, 13, 204, 60 ), "212.13.204.60" );
269 275
 
276
+	/* "ipv6" setting type */
277
+	storef_ok ( &test_settings, &test_ipv6_setting,
278
+		    "2001:ba8:0:1d4::6950:5845",
279
+		    RAW ( 0x20, 0x01, 0x0b, 0xa8, 0x00, 0x00, 0x01, 0xd4,
280
+			  0x00, 0x00, 0x00, 0x00, 0x69, 0x50, 0x58, 0x45 ) );
281
+	fetchf_ok ( &test_settings, &test_ipv6_setting,
282
+		    RAW ( 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283
+			  0x02, 0x0c, 0x29, 0xff, 0xfe, 0xc5, 0x39, 0xa1 ),
284
+		    "fe80::20c:29ff:fec5:39a1" );
285
+
270 286
 	/* Integer setting types (as formatted strings) */
271 287
 	storef_ok ( &test_settings, &test_int8_setting,
272 288
 		    "54", RAW ( 54 ) );

Načítá se…
Zrušit
Uložit