[PSEH3]
[reactos.git] / reactos / lib / rtl / network.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Runtime Library
4 * PURPOSE: Network Address Translation implementation
5 * PROGRAMMER: Alex Ionescu (alexi@tinykrnl.org)
6 */
7
8 /* INCLUDES *****************************************************************/
9
10 #include <rtl.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 /* maximum length of an ipv4 address expressed as a string */
16 #define IPV4_ADDR_STRING_MAX_LEN 16
17
18 /* maximum length of an ipv4 port expressed as a string */
19 #define IPV4_PORT_STRING_MAX_LEN 7 /* with the leading ':' */
20
21 /* network to host order conversion for little endian machines */
22 #define WN2H(w) (((w & 0xFF00) >> 8) | ((w & 0x00FF) << 8))
23
24 /* FUNCTIONS ***************************************************************/
25
26 /*
27 * @implemented
28 */
29 LPSTR
30 NTAPI
31 RtlIpv4AddressToStringA(IN struct in_addr *Addr,
32 OUT PCHAR S)
33 {
34 INT Length;
35
36 if (!S) return (LPSTR)~0;
37
38 Length = sprintf(S, "%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
39 Addr->S_un.S_un_b.s_b2,
40 Addr->S_un.S_un_b.s_b3,
41 Addr->S_un.S_un_b.s_b4);
42
43 return S + Length;
44 }
45
46 /*
47 * @implemented
48 */
49 NTSTATUS
50 NTAPI
51 RtlIpv4AddressToStringExA(IN struct in_addr *Address,
52 IN USHORT Port,
53 OUT PCHAR AddressString,
54 IN OUT PULONG AddressStringLength)
55 {
56 CHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
57 ULONG Length;
58
59 if (!Address || !AddressString || !AddressStringLength)
60 return STATUS_INVALID_PARAMETER;
61
62 Length = sprintf(Buffer, "%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
63 Address->S_un.S_un_b.s_b2,
64 Address->S_un.S_un_b.s_b3,
65 Address->S_un.S_un_b.s_b4);
66
67 if (Port) Length += sprintf(Buffer + Length, ":%u", WN2H(Port));
68
69 if (*AddressStringLength > Length)
70 {
71 *AddressStringLength = Length + 1;
72 strcpy(AddressString, Buffer);
73 return STATUS_SUCCESS;
74 }
75
76 *AddressStringLength = Length + 1;
77 return STATUS_INVALID_PARAMETER;
78 }
79
80 /*
81 * @implemented
82 */
83 LPWSTR
84 NTAPI
85 RtlIpv4AddressToStringW(IN struct in_addr *Addr,
86 OUT PWCHAR S)
87 {
88 INT Length;
89
90 if (!S) return (LPWSTR)~0;
91
92 Length = swprintf(S, L"%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
93 Addr->S_un.S_un_b.s_b2,
94 Addr->S_un.S_un_b.s_b3,
95 Addr->S_un.S_un_b.s_b4);
96 return S + Length;
97 }
98
99 /*
100 * @implemented
101 */
102 NTSTATUS
103 NTAPI
104 RtlIpv4AddressToStringExW(IN struct in_addr *Address,
105 IN USHORT Port,
106 OUT PWCHAR AddressString,
107 IN OUT PULONG AddressStringLength)
108 {
109 WCHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
110 ULONG Length;
111
112 if (!Address || !AddressString || !AddressStringLength)
113 return STATUS_INVALID_PARAMETER;
114
115 Length = swprintf(Buffer, L"%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
116 Address->S_un.S_un_b.s_b2,
117 Address->S_un.S_un_b.s_b3,
118 Address->S_un.S_un_b.s_b4);
119
120 if (Port) Length += swprintf(Buffer + Length, L":%u", WN2H(Port));
121
122 if (*AddressStringLength > Length)
123 {
124 *AddressStringLength = Length + 1;
125 wcscpy(AddressString, Buffer);
126 return STATUS_SUCCESS;
127 }
128
129 *AddressStringLength = Length + 1;
130 return STATUS_INVALID_PARAMETER;
131 }
132
133 /*
134 * @unimplemented
135 */
136 NTSTATUS
137 NTAPI
138 RtlIpv4StringToAddressA(IN PCSTR String,
139 IN BOOLEAN Strict,
140 OUT PCSTR *Terminator,
141 OUT struct in_addr *Addr)
142 {
143 UNIMPLEMENTED;
144 return STATUS_NOT_IMPLEMENTED;
145 }
146
147 /*
148 * @unimplemented
149 */
150 NTSTATUS
151 NTAPI
152 RtlIpv4StringToAddressExA(IN PCSTR AddressString,
153 IN BOOLEAN Strict,
154 OUT struct in_addr *Address,
155 OUT PUSHORT Port)
156 {
157 UNIMPLEMENTED;
158 return STATUS_NOT_IMPLEMENTED;
159 }
160
161 /*
162 * @unimplemented
163 */
164 NTSTATUS
165 NTAPI
166 RtlIpv4StringToAddressW(IN PCWSTR String,
167 IN BOOLEAN Strict,
168 OUT PCWSTR *Terminator,
169 OUT struct in_addr *Addr)
170 {
171 UNIMPLEMENTED;
172 return STATUS_NOT_IMPLEMENTED;
173 }
174
175 /*
176 * @unimplemented
177 */
178 NTSTATUS
179 NTAPI
180 RtlIpv4StringToAddressExW(IN PCWSTR AddressString,
181 IN BOOLEAN Strict,
182 OUT struct in_addr *Address,
183 OUT PUSHORT Port)
184 {
185 UNIMPLEMENTED;
186 return STATUS_NOT_IMPLEMENTED;
187 }
188
189 /*
190 * @unimplemented
191 */
192 NTSTATUS
193 NTAPI
194 RtlIpv6AddressToStringA(IN struct in6_addr *Addr,
195 OUT PCHAR S)
196 {
197 UNIMPLEMENTED;
198 return STATUS_NOT_IMPLEMENTED;
199 }
200
201 /*
202 * @unimplemented
203 */
204 NTSTATUS
205 NTAPI
206 RtlIpv6AddressToStringExA(IN struct in6_addr *Address,
207 IN ULONG ScopeId,
208 IN ULONG Port,
209 OUT PCHAR AddressString,
210 IN OUT PULONG AddressStringLength)
211 {
212 UNIMPLEMENTED;
213 return STATUS_NOT_IMPLEMENTED;
214 }
215
216 /*
217 * @unimplemented
218 */
219 NTSTATUS
220 NTAPI
221 RtlIpv6AddressToStringW(IN struct in6_addr *Addr,
222 OUT PWCHAR S)
223 {
224 UNIMPLEMENTED;
225 return STATUS_NOT_IMPLEMENTED;
226 }
227
228 /*
229 * @unimplemented
230 */
231 NTSTATUS
232 NTAPI
233 RtlIpv6AddressToStringExW(IN struct in6_addr *Address,
234 IN ULONG ScopeId,
235 IN USHORT Port,
236 IN OUT PWCHAR AddressString,
237 IN OUT PULONG AddressStringLength)
238 {
239 UNIMPLEMENTED;
240 return STATUS_NOT_IMPLEMENTED;
241 }
242
243 /*
244 * @unimplemented
245 */
246 NTSTATUS
247 NTAPI
248 RtlIpv6StringToAddressA(IN PCHAR Name,
249 OUT PCHAR *Terminator,
250 OUT struct in6_addr *Addr)
251 {
252 UNIMPLEMENTED;
253 return STATUS_NOT_IMPLEMENTED;
254 }
255
256 /*
257 * @unimplemented
258 */
259 NTSTATUS
260 NTAPI
261 RtlIpv6StringToAddressExA(IN PCHAR AddressString,
262 OUT struct in6_addr *Address,
263 OUT PULONG ScopeId,
264 OUT PUSHORT Port)
265 {
266 UNIMPLEMENTED;
267 return STATUS_NOT_IMPLEMENTED;
268 }
269
270 /*
271 * @unimplemented
272 */
273 NTSTATUS
274 NTAPI
275 RtlIpv6StringToAddressW(IN PWCHAR Name,
276 OUT PCHAR *Terminator,
277 OUT struct in6_addr *Addr)
278 {
279 UNIMPLEMENTED;
280 return STATUS_NOT_IMPLEMENTED;
281 }
282
283 /*
284 * @unimplemented
285 */
286 NTSTATUS
287 NTAPI
288 RtlIpv6StringToAddressExW(IN PWCHAR AddressName,
289 OUT struct in6_addr *Address,
290 OUT PULONG ScopeId,
291 OUT PUSHORT Port)
292 {
293 UNIMPLEMENTED;
294 return STATUS_NOT_IMPLEMENTED;
295 }
296
297 /* EOF */