瀏覽代碼

[socket] Add concept of a generalised socket address converter

Add sock_aton() and sock_ntoa() to allow for parsing and transcription
of arbitrary socket addresses.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 年之前
父節點
當前提交
b6a9152f8c
共有 3 個文件被更改,包括 102 次插入0 次删除
  1. 1
    0
      src/include/ipxe/errfile.h
  2. 36
    0
      src/include/ipxe/socket.h
  3. 65
    0
      src/net/socket.c

+ 1
- 0
src/include/ipxe/errfile.h 查看文件

@@ -215,6 +215,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
215 215
 #define ERRFILE_mount			( ERRFILE_NET | 0x00350000 )
216 216
 #define ERRFILE_oncrpc_iob		( ERRFILE_NET | 0x00360000 )
217 217
 #define ERRFILE_neighbour		( ERRFILE_NET | 0x00370000 )
218
+#define ERRFILE_socket			( ERRFILE_NET | 0x00380000 )
218 219
 
219 220
 #define ERRFILE_image		      ( ERRFILE_IMAGE | 0x00000000 )
220 221
 #define ERRFILE_elf		      ( ERRFILE_IMAGE | 0x00010000 )

+ 36
- 0
src/include/ipxe/socket.h 查看文件

@@ -10,6 +10,7 @@
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12 12
 #include <stdint.h>
13
+#include <ipxe/tables.h>
13 14
 
14 15
 /**
15 16
  * @defgroup commtypes Communication semantics
@@ -99,4 +100,39 @@ struct sockaddr {
99 100
 	char pad[ SA_LEN - sizeof ( sa_family_t ) ];
100 101
 } __attribute__ (( may_alias ));
101 102
 
103
+/**
104
+ * Socket address converter
105
+ *
106
+ */
107
+struct sockaddr_converter {
108
+	/** Socket address family
109
+	 *
110
+	 * This is an AF_XXX constant.
111
+	 */
112
+        sa_family_t family;
113
+	/** Transcribe socket address
114
+	 *
115
+	 * @v sa		Socket address
116
+	 * @ret string		Socket address string
117
+	 */
118
+	const char * ( * ntoa ) ( struct sockaddr *sa );
119
+	/** Parse socket address
120
+	 *
121
+	 * @v string		Socket address stringh
122
+	 * @v sa		Socket address to fill in
123
+	 * @ret rc		Return status code
124
+	 */
125
+	int ( * aton ) ( const char *string, struct sockaddr *sa );
126
+};
127
+
128
+/** Socket address converter table */
129
+#define SOCKADDR_CONVERTERS \
130
+	__table ( struct sockaddr_converter, "sockaddr_converters" )
131
+
132
+/** Declare a socket address converter */
133
+#define __sockaddr_converter __table_entry ( SOCKADDR_CONVERTERS, 01 )
134
+
135
+extern const char * sock_ntoa ( struct sockaddr *sa );
136
+extern int sock_aton ( const char *string, struct sockaddr *sa );
137
+
102 138
 #endif /* _IPXE_SOCKET_H */

+ 65
- 0
src/net/socket.c 查看文件

@@ -0,0 +1,65 @@
1
+/*
2
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ */
19
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+#include <stddef.h>
23
+#include <errno.h>
24
+#include <ipxe/socket.h>
25
+
26
+/** @file
27
+ *
28
+ * Sockets
29
+ *
30
+ */
31
+
32
+/**
33
+ * Transcribe socket address
34
+ *
35
+ * @v sa		Socket address
36
+ * @ret string		Socket address string
37
+ */
38
+const char * sock_ntoa ( struct sockaddr *sa ) {
39
+	struct sockaddr_converter *converter;
40
+
41
+	for_each_table_entry ( converter, SOCKADDR_CONVERTERS ) {
42
+		if ( converter->family == sa->sa_family )
43
+			return converter->ntoa ( sa );
44
+	}
45
+	return NULL;
46
+}
47
+
48
+/**
49
+ * Parse socket address
50
+ *
51
+ * @v string		Socket address string
52
+ * @v sa		Socket address to fill in
53
+ * @ret rc		Return status code
54
+ */
55
+int sock_aton ( const char *string, struct sockaddr *sa ) {
56
+	struct sockaddr_converter *converter;
57
+
58
+	for_each_table_entry ( converter, SOCKADDR_CONVERTERS ) {
59
+		if ( converter->aton ( string, sa ) == 0 ) {
60
+			sa->sa_family = converter->family;
61
+			return 0;
62
+		}
63
+	}
64
+	return -EINVAL;
65
+}

Loading…
取消
儲存