[DHCP]
[reactos.git] / reactos / base / services / dhcp / util.c
1 #include <stdarg.h>
2 #include "rosdhcp.h"
3
4 #define NDEBUG
5 #include <reactos/debug.h>
6
7 char *piaddr( struct iaddr addr ) {
8 struct sockaddr_in sa;
9 memcpy(&sa.sin_addr,addr.iabuf,sizeof(sa.sin_addr));
10 return inet_ntoa( sa.sin_addr );
11 }
12
13 int note( char *format, ... ) {
14 char buf[0x100];
15 int ret;
16 va_list arg_begin;
17 va_start( arg_begin, format );
18
19 ret = _vsnprintf( buf, sizeof(buf), format, arg_begin );
20
21 DPRINT("NOTE: %s\n", buf);
22
23 return ret;
24 }
25
26 int debug( char *format, ... ) {
27 char buf[0x100];
28 int ret;
29 va_list arg_begin;
30 va_start( arg_begin, format );
31
32 ret = _vsnprintf( buf, sizeof(buf), format, arg_begin );
33
34 DPRINT("DEBUG: %s\n", buf);
35
36 return ret;
37 }
38
39 int warn( char *format, ... ) {
40 char buf[0x100];
41 int ret;
42 va_list arg_begin;
43 va_start( arg_begin, format );
44
45 ret = _vsnprintf( buf, sizeof(buf), format, arg_begin );
46
47 DPRINT("WARN: %s\n", buf);
48
49 return ret;
50 }
51
52 int warning( char *format, ... ) {
53 char buf[0x100];
54 int ret;
55 va_list arg_begin;
56 va_start( arg_begin, format );
57
58 ret = _vsnprintf( buf, sizeof(buf), format, arg_begin );
59
60 DPRINT("WARNING: %s\n", buf);
61
62 return ret;
63 }
64
65 void error( char *format, ... ) {
66 char buf[0x100];
67 va_list arg_begin;
68 va_start( arg_begin, format );
69
70 _vsnprintf( buf, sizeof(buf), format, arg_begin );
71
72 DPRINT1("ERROR: %s\n", buf);
73 }
74
75 int16_t getShort( unsigned char *data ) {
76 return (int16_t) ntohs(*(int16_t*) data);
77 }
78
79 u_int16_t getUShort( unsigned char *data ) {
80 return (u_int16_t) ntohs(*(u_int16_t*) data);
81 }
82
83 int32_t getLong( unsigned char *data ) {
84 return (int32_t) ntohl(*(u_int32_t*) data);
85 }
86
87 u_int32_t getULong( unsigned char *data ) {
88 return ntohl(*(u_int32_t*)data);
89 }
90
91 int addr_eq( struct iaddr a, struct iaddr b ) {
92 return a.len == b.len && !memcmp( a.iabuf, b.iabuf, a.len );
93 }
94
95 void *dmalloc( int size, char *name ) { return malloc( size ); }
96
97 int read_client_conf(struct interface_info *ifi) {
98 /* What a strange dance */
99 struct client_config *config;
100 char ComputerName [MAX_COMPUTERNAME_LENGTH + 1];
101 LPSTR lpCompName;
102 DWORD ComputerNameSize = sizeof ComputerName / sizeof ComputerName[0];
103
104 if ((ifi!= NULL) && (ifi->client->config != NULL))
105 config = ifi->client->config;
106 else
107 {
108 warn("util.c read_client_conf poorly implemented!");
109 return 0;
110 }
111
112
113 GetComputerName(ComputerName, & ComputerNameSize);
114 debug("Hostname: %s, length: %lu",
115 ComputerName, ComputerNameSize);
116 /* This never gets freed since it's only called once */
117 lpCompName =
118 HeapAlloc(GetProcessHeap(), 0, ComputerNameSize + 1);
119 if (lpCompName !=NULL) {
120 memcpy(lpCompName, ComputerName, ComputerNameSize + 1);
121 /* Send our hostname, some dhcpds use this to update DNS */
122 config->send_options[DHO_HOST_NAME].data = (u_int8_t*)lpCompName;
123 config->send_options[DHO_HOST_NAME].len = ComputerNameSize;
124 debug("Hostname: %s, length: %d",
125 config->send_options[DHO_HOST_NAME].data,
126 config->send_options[DHO_HOST_NAME].len);
127 } else {
128 error("Failed to allocate heap for hostname");
129 }
130 /* Both Linux and Windows send this */
131 config->send_options[DHO_DHCP_CLIENT_IDENTIFIER].data =
132 ifi->hw_address.haddr;
133 config->send_options[DHO_DHCP_CLIENT_IDENTIFIER].len =
134 ifi->hw_address.hlen;
135
136 /* Setup the requested option list */
137 config->requested_options
138 [config->requested_option_count++] = DHO_SUBNET_MASK;
139 config->requested_options
140 [config->requested_option_count++] = DHO_BROADCAST_ADDRESS;
141 config->requested_options
142 [config->requested_option_count++] = DHO_TIME_OFFSET;
143 config->requested_options
144 [config->requested_option_count++] = DHO_ROUTERS;
145 config->requested_options
146 [config->requested_option_count++] = DHO_DOMAIN_NAME;
147 config->requested_options
148 [config->requested_option_count++] = DHO_DOMAIN_NAME_SERVERS;
149 config->requested_options
150 [config->requested_option_count++] = DHO_HOST_NAME;
151 config->requested_options
152 [config->requested_option_count++] = DHO_NTP_SERVERS;
153
154 warn("util.c read_client_conf poorly implemented!");
155 return 0;
156 }
157
158 struct iaddr broadcast_addr( struct iaddr addr, struct iaddr mask ) {
159 struct iaddr bcast = { 0 };
160 return bcast;
161 }
162
163 struct iaddr subnet_number( struct iaddr addr, struct iaddr mask ) {
164 struct iaddr bcast = { 0 };
165 return bcast;
166 }