[RTL]
[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 CHAR Buffer[IPV4_ADDR_STRING_MAX_LEN];
35 INT Length;
36
37 if(!S)
38 return NULL;
39
40 Length = sprintf(Buffer, "%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
41 Addr->S_un.S_un_b.s_b2,
42 Addr->S_un.S_un_b.s_b3,
43 Addr->S_un.S_un_b.s_b4);
44
45 strcpy(S, Buffer);
46 return S + Length;
47 }
48
49 /*
50 * @implemented
51 */
52 NTSTATUS
53 NTAPI
54 RtlIpv4AddressToStringExA(IN struct in_addr *Address,
55 IN USHORT Port,
56 OUT PCHAR AddressString,
57 IN OUT PULONG AddressStringLength)
58 {
59 CHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
60 ULONG Length;
61
62 if (!Address || !AddressString || !AddressStringLength)
63 return STATUS_INVALID_PARAMETER;
64
65 Length = sprintf(Buffer, "%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
66 Address->S_un.S_un_b.s_b2,
67 Address->S_un.S_un_b.s_b3,
68 Address->S_un.S_un_b.s_b4);
69
70 if (Port) Length += sprintf(Buffer + Length, ":%u", WN2H(Port));
71
72 if (*AddressStringLength > Length)
73 {
74 *AddressStringLength = Length + 1;
75 strcpy(AddressString, Buffer);
76 return STATUS_SUCCESS;
77 }
78
79 *AddressStringLength = Length + 1;
80 return STATUS_INVALID_PARAMETER;
81 }
82
83 /*
84 * @implemented
85 */
86 LPWSTR
87 NTAPI
88 RtlIpv4AddressToStringW(IN struct in_addr *Addr,
89 OUT PWCHAR S)
90 {
91 WCHAR Buffer[IPV4_ADDR_STRING_MAX_LEN];
92 INT Length;
93
94 if (!S)
95 return NULL;
96
97 Length = swprintf(Buffer, L"%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
98 Addr->S_un.S_un_b.s_b2,
99 Addr->S_un.S_un_b.s_b3,
100 Addr->S_un.S_un_b.s_b4);
101
102 wcscpy(S, Buffer);
103
104 return S + Length;
105 }
106
107 /*
108 * @implemented
109 */
110 NTSTATUS
111 NTAPI
112 RtlIpv4AddressToStringExW(IN struct in_addr *Address,
113 IN USHORT Port,
114 OUT PWCHAR AddressString,
115 IN OUT PULONG AddressStringLength)
116 {
117 WCHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
118 ULONG Length;
119
120 if (!Address || !AddressString || !AddressStringLength)
121 return STATUS_INVALID_PARAMETER;
122
123 Length = swprintf(Buffer, L"%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
124 Address->S_un.S_un_b.s_b2,
125 Address->S_un.S_un_b.s_b3,
126 Address->S_un.S_un_b.s_b4);
127
128 if (Port) Length += swprintf(Buffer + Length, L":%u", WN2H(Port));
129
130 if (*AddressStringLength > Length)
131 {
132 *AddressStringLength = Length + 1;
133 wcscpy(AddressString, Buffer);
134 return STATUS_SUCCESS;
135 }
136
137 *AddressStringLength = Length + 1;
138 return STATUS_INVALID_PARAMETER;
139 }
140
141 /*
142 * @unimplemented
143 */
144 NTSTATUS
145 NTAPI
146 RtlIpv4StringToAddressA(IN PCHAR String,
147 IN BOOLEAN Strict,
148 OUT PCHAR *Terminator,
149 OUT struct in_addr *Addr)
150 {
151 UNIMPLEMENTED;
152 return STATUS_NOT_IMPLEMENTED;
153 }
154
155 /*
156 * @unimplemented
157 */
158 NTSTATUS
159 NTAPI
160 RtlIpv4StringToAddressExA(IN PCHAR AddressString,
161 IN BOOLEAN Strict,
162 OUT struct in_addr *Address,
163 IN PUSHORT Port)
164 {
165 UNIMPLEMENTED;
166 return STATUS_NOT_IMPLEMENTED;
167 }
168
169 /*
170 * @unimplemented
171 */
172 NTSTATUS
173 NTAPI
174 RtlIpv4StringToAddressW(IN PCWSTR String,
175 IN BOOLEAN Strict,
176 OUT LPWSTR *Terminator,
177 OUT struct in_addr *Addr)
178 {
179 UNIMPLEMENTED;
180 return STATUS_NOT_IMPLEMENTED;
181 }
182
183 /*
184 * @unimplemented
185 */
186 NTSTATUS
187 NTAPI
188 RtlIpv4StringToAddressExW(IN PWCHAR AddressString,
189 IN BOOLEAN Strict,
190 OUT struct in_addr *Address,
191 OUT PUSHORT Port)
192 {
193 UNIMPLEMENTED;
194 return STATUS_NOT_IMPLEMENTED;
195 }
196
197 /*
198 * @unimplemented
199 */
200 NTSTATUS
201 NTAPI
202 RtlIpv6AddressToStringA(IN struct in6_addr *Addr,
203 OUT PCHAR S)
204 {
205 UNIMPLEMENTED;
206 return STATUS_NOT_IMPLEMENTED;
207 }
208
209 /*
210 * @unimplemented
211 */
212 NTSTATUS
213 NTAPI
214 RtlIpv6AddressToStringExA(IN struct in6_addr *Address,
215 IN ULONG ScopeId,
216 IN ULONG Port,
217 OUT PCHAR AddressString,
218 IN OUT PULONG AddressStringLength)
219 {
220 UNIMPLEMENTED;
221 return STATUS_NOT_IMPLEMENTED;
222 }
223
224 /*
225 * @unimplemented
226 */
227 NTSTATUS
228 NTAPI
229 RtlIpv6AddressToStringW(IN struct in6_addr *Addr,
230 OUT PWCHAR S)
231 {
232 UNIMPLEMENTED;
233 return STATUS_NOT_IMPLEMENTED;
234 }
235
236 /*
237 * @unimplemented
238 */
239 NTSTATUS
240 NTAPI
241 RtlIpv6AddressToStringExW(IN struct in6_addr *Address,
242 IN ULONG ScopeId,
243 IN USHORT Port,
244 IN OUT PWCHAR AddressString,
245 IN OUT PULONG AddressStringLength)
246 {
247 UNIMPLEMENTED;
248 return STATUS_NOT_IMPLEMENTED;
249 }
250
251 /*
252 * @unimplemented
253 */
254 NTSTATUS
255 NTAPI
256 RtlIpv6StringToAddressA(IN PCHAR Name,
257 OUT PCHAR *Terminator,
258 OUT struct in6_addr *Addr)
259 {
260 UNIMPLEMENTED;
261 return STATUS_NOT_IMPLEMENTED;
262 }
263
264 /*
265 * @unimplemented
266 */
267 NTSTATUS
268 NTAPI
269 RtlIpv6StringToAddressExA(IN PCHAR AddressString,
270 OUT struct in6_addr *Address,
271 OUT PULONG ScopeId,
272 OUT PUSHORT Port)
273 {
274 UNIMPLEMENTED;
275 return STATUS_NOT_IMPLEMENTED;
276 }
277
278 /*
279 * @unimplemented
280 */
281 NTSTATUS
282 NTAPI
283 RtlIpv6StringToAddressW(IN PWCHAR Name,
284 OUT PCHAR *Terminator,
285 OUT struct in6_addr *Addr)
286 {
287 UNIMPLEMENTED;
288 return STATUS_NOT_IMPLEMENTED;
289 }
290
291 /*
292 * @unimplemented
293 */
294 NTSTATUS
295 NTAPI
296 RtlIpv6StringToAddressExW(IN PWCHAR AddressName,
297 OUT struct in6_addr *Address,
298 OUT PULONG ScopeId,
299 OUT PUSHORT Port)
300 {
301 UNIMPLEMENTED;
302 return STATUS_NOT_IMPLEMENTED;
303 }
304
305 /* EOF */