-
Notifications
You must be signed in to change notification settings - Fork 0
/
setnet.c
1135 lines (954 loc) · 26.3 KB
/
setnet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ. OpenVZ is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*
* methods for setting network parameters
*/
#include <sdkddkver.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <io.h>
#include <fcntl.h>
#include <ctype.h>
#include "common.h"
#include "netinfo.h"
#include "namelist.h"
#include "options.h"
#define TOOL_GUEST_UTILS_REGISTRY_PATH "Software\\Virtuozzo\\prl_nettool"
typedef LONG DNS_STATUS;
typedef enum _DNS_NAME_FORMAT {
DnsNameDomain,
DnsNameDomainLabel,
DnsNameHostnameFull,
DnsNameHostnameLabel,
DnsNameWildcard,
DnsNameSrvRecord,
DnsNameValidateTld
} DNS_NAME_FORMAT;
DNS_STATUS WINAPI DnsValidateName_A(PCSTR pszName, DNS_NAME_FORMAT Format);
static int exec_cmd(char *cmd)
{
DWORD rc = 0;
PROCESS_INFORMATION procinfo = { 0 };
STARTUPINFOW startinfo = { 0 };
WCHAR cmdline[1024];
debug("run: %s\n", cmd);
startinfo.cb = sizeof(startinfo);
if (MultiByteToWideChar(CP_UTF8, 0, cmd, -1, cmdline, sizeof(cmdline)/sizeof(*cmdline)) == 0) {
rc = GetLastError();
error(rc, "Failed to format %s", cmd);
return rc;
}
if (!CreateProcessW(NULL,
cmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
FALSE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&startinfo, // STARTUPINFO pointer
&procinfo)) // receives PROCESS_INFORMATION
{
rc = GetLastError();
error(rc, "Failed to exec %s", cmd);
return rc;
}
WaitForSingleObject(procinfo.hProcess, INFINITE);
GetExitCodeProcess(procinfo.hProcess, &rc);
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
if (rc)
error(0, "failed to run cmd: %s. return %d (0x%X)", cmd, rc, rc);
return rc;
}
static int exec_netsh(char *text)
{
char ex_buf[1024];
sprintf(ex_buf, "netsh %s", text);
return exec_cmd(ex_buf);
}
int remove_ipv6_ips(struct netinfo *if_it)
{
char str[1024];
int rc;
struct namelist *ip_mask = if_it->ip;
char ip[100], mask[IP_LENGTH+1];
while (ip_mask != NULL)
{
if (!is_ipv6(ip_mask->name) ||
namelist_search(ip_mask->name, &if_it->ip_link))
{
//skip link-local and site-local
ip_mask = ip_mask->next;
continue;
}
if (split_ip_mask(ip_mask->name, ip, mask) < 0)
return -1;
sprintf(str, "interface ipv6 del address %d %s", if_it->idx, ip);
if ((rc = exec_netsh(str)))
return -1;
ip_mask = ip_mask->next;
}
return 0;
}
// return 1st not link-local addr of iface
static int find_if_first_ip(struct netinfo *if_it, char * ip)
{
struct namelist *ip_mask;
char mask[IP_LENGTH+1];
for (ip_mask = if_it->ip; ip_mask != NULL; ip_mask = ip_mask->next)
{
if (namelist_search(ip_mask->name, &if_it->ip_link))
//skip link-local and site-local
continue;
if (split_ip_mask(ip_mask->name, ip, mask) < 0)
return -1;
return 0;
}
return -1;
}
int set_ip(struct netinfo *if_it, struct nettool_mac *params)
{
char str[1024];
int rc;
struct namelist *ip_masks = NULL;
struct namelist *ip_mask;
char ip[100], mask[IP_LENGTH+1];
int first_ipv4=1, first_ipv6=1, remove=0;
struct nettool_mac *mac_it;
//parse value
namelist_split(&ip_masks, params->value);
//for first
ip_mask = ip_masks;
while(ip_mask)
{
if (!strcmp(ip_mask->name, "remove"))
{
remove=1;
}
else if (!is_ipv6(ip_mask->name))
{
if (split_ip_mask(ip_mask->name, ip, mask) < 0)
return -1;
#if (NTDDI_VERSION < NTDDI_LONGHORN)
//workaround on win2k3
if (!strcmp(mask, "255.255.255.255"))
strcpy(mask, "255.255.255.254");
#endif
if (first_ipv4)
{
sprintf(str, "interface ip set address %d static %s %s", if_it->idx, ip, mask);
if ((rc = exec_netsh(str)))
return -1;
if_it->configured_with_dhcp = 0;
first_ipv4 = 0;
}
else
{
sprintf(str, "interface ip add address %d %s %s", if_it->idx, ip, mask);
rc = exec_netsh(str);
#if (NTDDI_VERSION >= NTDDI_LONGHORN)
//on win2k3 during set second ip on disabled adapter - netsh return error without any
//description. workaround: don't check exit code on w2k3. check on vista and later
if (rc)
return -1;
#endif
}
}
else
{
if (first_ipv6)
{
//remove all IPs except of local-link
if (remove_ipv6_ips(if_it))
return -1;
if (split_ip_mask(ip_mask->name, ip, mask) < 0)
return -1;
sprintf(str, "interface ipv6 set address %d %s/%s", if_it->idx, ip, mask);
if ((rc = exec_netsh(str)))
return -1;
if_it->configured_with_dhcpv6 = 0;
first_ipv6 = 0;
}
else
{
if (split_ip_mask(ip_mask->name, ip, mask) < 0)
return -1;
sprintf(str, "interface ipv6 add address %d %s/%s", if_it->idx, ip, mask);
if ((rc = exec_netsh(str)))
return -1;
}
}
ip_mask = ip_mask->next;
}
if (is_ipv6_supported()
&& first_ipv6 /*ipv6 IPs are not specified*/)
{
//remove all ipv6 configuration
sprintf(str, "interface ipv6 set dns %d dhcp", if_it->idx);
exec_netsh(str);
//remove all IPs except of local-link
remove_ipv6_ips(if_it);
}
mac_it = get_opt_mac(if_it->mac, NET_OPT_DHCP);
if (remove ||
(first_ipv4 &&
(mac_it == NULL || mac_it->value == NULL || strchr(mac_it->value, '4') == NULL)) )
{
//remove all ipv4 configuration
sprintf(str, "interface ip set address %d dhcp", if_it->idx);
exec_netsh(str);
}
namelist_clean(&ip_masks);
return 0;
}
int set_dns(struct netinfo *if_it, struct nettool_mac *params)
{
char str[1024];
int rc;
struct namelist *ips = NULL;
struct namelist *ip;
int clear_ipv4=0, clear_ipv6=0;
if (params->value == NULL)
return 0;
if (strlen(params->value) == 0)
return 0;
//split params->value
namelist_split(&ips, params->value);
//for each ip do
ip = ips;
while (ip)
{
//set none
if (!is_ipv6(ip->name))
{
if (!clear_ipv4)
{
sprintf(str, "interface ip set dns %d static none", if_it->idx);
clear_ipv4 = 1;
if ((rc = exec_netsh(str)) < 0)
return rc;
}
}
else
{
if (!clear_ipv6)
{
sprintf(str, "interface ipv6 set dns %d static none", if_it->idx);
clear_ipv6 = 1;
if ((rc = exec_netsh(str)) < 0)
return rc;
}
}
if (!is_ipv6(ip->name))
sprintf(str, "interface ip add dns %d addr=%s", if_it->idx, ip->name);
else
sprintf(str, "interface ipv6 add dns %d addr=%s", if_it->idx, ip->name);
if ((rc = exec_netsh(str))< 0)
return rc;
ip = ip->next;
}
namelist_clean(&ips);
return 0;
}
int set_gateway(struct netinfo *if_it, struct nettool_mac *params)
{
char str[1024];
struct namelist *gws = NULL;
struct namelist *gw;
//if (if_it->configured_with_dhcp){
// error(0, "WARNING: configured with dhcp. don't change");
// return 0;
//}
//split params->value
namelist_split(&gws, params->value);
//for each ip do
gw = gws;
while (gw)
{
if (is_ipv6(gw->name) || is_removev6(gw->name))
sprintf(str, "interface ipv6 del route ::/0 %d", if_it->idx);
else
#if (NTDDI_VERSION < NTDDI_LONGHORN)
sprintf(str, "interface ip del address %d gateway=all", if_it->idx);
#else
sprintf(str, "interface ip del route 0.0.0.0/0 %d", if_it->idx);
#endif
exec_netsh(str);
if (strlen(gw->name) > 0 && (!is_remove(gw->name)) && (!is_removev6(gw->name))) {
if (is_ipv6(gw->name))
sprintf(str, "interface ipv6 add route ::/0 %d %s store=persistent",
if_it->idx, gw->name);
else
sprintf(str, "interface ip add address %d gateway=%s gwmetric=1",
if_it->idx, gw->name);
exec_netsh(str);
}
gw = gw->next;
}
return 0;
}
int set_search_domain(struct nettool_mac *params)
{
char domains[1024]; //to store value
DWORD rc;
HKEY hKey;
ULONG len;
struct namelist *names = NULL;
struct namelist *name;
const char *value = params->value;
if (value == NULL)
return 0;
if (!strcmp(value, "remove"))
/* empty search domain list */
value = "";
domains[0] = '\0';
if (strlen(params->value) > 0) {
namelist_split(&names, params->value);
name = names;
while(name){
int dlen = strlen(domains);
if (dlen == 0)
sprintf(domains + dlen, "%s", name->name);
else
sprintf(domains + dlen, ",%s", name->name);
name = name->next;
}
namelist_clean(&names);
}
len = strlen(domains) + 1;
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, S_TCPPARAMS_REG_VALUE_KEY, 0, KEY_WRITE, &hKey);
if (rc != ERROR_SUCCESS){
error(rc, "Can't open registry %s\n", S_TCPPARAMS_REG_VALUE_KEY);
return 1;
}
rc = RegSetValueExA(hKey, S_SEARCHLIST_REG_NAME, 0, REG_SZ, (LPBYTE)domains, len);
if (rc != ERROR_SUCCESS){
error(rc, "Can't set registry %s\\%s. return %d\n",
S_TCPPARAMS_REG_VALUE_KEY, S_SEARCHLIST_REG_NAME, rc);
RegCloseKey(hKey);
return 1;
}
rc = RegCloseKey(hKey);
/* refresh tcp/ip config to take into effect changes immediatly */
exec_cmd("ipconfig /renew");
debug("set_search_domains: %s\n", domains);
return 0;
}
static int set_reg_value(const char *path, const char *key, const char *value)
{
DWORD rc;
HKEY hKey;
ULONG len;
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, path, 0, KEY_WRITE|KEY_QUERY_VALUE, &hKey);
if (rc != ERROR_SUCCESS) {
DWORD dwDisposition;
rc = RegCreateKeyExA(HKEY_LOCAL_MACHINE, path, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (rc != ERROR_SUCCESS) {
error(rc, "Can't open/create registry %s\n", path);
return 1;
}
}
len = strlen(value) + 1;
rc = RegSetValueExA(hKey, key, 0, REG_SZ, (LPBYTE)value, len);
if (rc != ERROR_SUCCESS) {
error(rc, "Can't set registry %s\\%s. return %d\n", path, key, rc);
RegCloseKey(hKey);
return 1;
}
rc = RegFlushKey(hKey);
if (rc != ERROR_SUCCESS) {
error(rc, "Can't flush registry %s. return %d\n", path, rc);
}
RegCloseKey(hKey);
return 0;
}
static int get_reg_value(const char *path, const char *key, char **value)
{
DWORD rc;
HKEY hKey;
ULONG len = 0;
DWORD type = 0;
char *buf;
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hKey);
if (rc != ERROR_SUCCESS){
error(rc, "Can't open registry for read %s", path);
return 1;
}
rc = RegQueryValueExA(hKey, key, 0, &type, (LPBYTE)NULL, &len);
if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS) {
RegCloseKey(hKey);
if (rc == ERROR_FILE_NOT_FOUND)
return -1;
error(rc, "Can't get registry value %s\\%s. return %d", path, key, rc);
return 1;
}
if (type != REG_SZ) {
error(0, "Wrong value type");
return 1;
}
buf = malloc(len+1);
if (buf == NULL) {
error(rc, "Can't malloc(%d). return %d", len+1, rc);
RegCloseKey(hKey);
return 1;
}
rc = RegQueryValueExA(hKey, key, 0, &type, (LPBYTE)buf, &len);
if (rc != ERROR_SUCCESS) {
error(rc, "Failed to get registry %s\\%s. return %d", path, key, rc);
RegCloseKey(hKey);
return 1;
}
*value = buf;
RegCloseKey(hKey);
return 0;
}
#define DISABLED_ADAPTERS_REG_KEY "prl_nettool_disabled"
static int save_adapter_disabled(const char *name, int enable)
{
char *buf = NULL;
char save_buf[4096];
int rc, found;
struct namelist *adapters = NULL;
rc = get_reg_value(TOOL_GUEST_UTILS_REGISTRY_PATH, DISABLED_ADAPTERS_REG_KEY, &buf);
//ignore error. reg path can be not found
if (rc == 0) {
namelist_split_delim(&adapters, buf, ":");
free(buf);
buf = NULL;
}
found = namelist_search(name, &adapters);
if (enable) {
//enable adapter. need to remove mention of it
if (!found) //nothing to do
return 0;
else
namelist_remove(name, &adapters);
} else {
//disable adapter. need to save it
if (found) //nothing to do
return 0;
else
namelist_add(name, &adapters);
}
print_namelist_to_str_delim(&adapters, save_buf, sizeof(save_buf), ":");
namelist_clean(&adapters);
debug("disabled adapters list: %s", save_buf);
rc = set_reg_value(TOOL_GUEST_UTILS_REGISTRY_PATH, DISABLED_ADAPTERS_REG_KEY, save_buf);
return rc;
}
int set_hostname(struct nettool_mac *params)
{
char hostname[1024]; //to store value
DNS_STATUS status;
char netbios_name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD netbios_name_len = (DWORD)sizeof(netbios_name);
char *computer_name = NULL;
char *domain = NULL;
const char *reg_hostname[] = {S_HOSTNAME_NV_REG_NAME,
S_HOSTNAME_REG_NAME, NULL};
const char *reg_domain[] = {S_DOMAIN_NV_REG_NAME,
S_DOMAIN_REG_NAME, NULL};
const char *reg_computer_name[] = {S_ACTIVE_NAME_REG_VALUE_KEY,
S_COMPUTER_NAME_REG_VALUE_KEY, NULL};
int ret, i = 0;
int has_dot = 0;
size_t len;
if (params->value == NULL)
return 0;
hostname[0] = '\0';
if (strlen(params->value) > 0) {
strncpy(hostname, params->value, sizeof(hostname));
hostname[sizeof(hostname)-1] = '\0';
}
if (strchr(hostname, '.') != NULL)
has_dot = 1;
computer_name = strtok(hostname, ".");
if (computer_name == NULL)
return 0;
len = strlen(computer_name);
/* strip trailing spaces, overwise Windows will have a bug with workgroup (PSBM-17913) */
while (len > 0 && isspace(computer_name[len-1]))
computer_name[--len] = 0;
status = DnsValidateName_A(computer_name, DnsNameHostnameLabel);
if (status != ERROR_SUCCESS && status != DNS_ERROR_NON_RFC_NAME) {
error(status, "Hostname '%s' is invalid\n", computer_name);
return status;
}
domain = strtok(NULL, ",");
/* process "hostname." string as empty domain (PSBM-17894) */
if (domain == NULL && has_dot)
domain = "";
/* set TCP properties first */
/* set hostname in TCP properties in registry */
for (i = 0; reg_hostname[i] != NULL; i++) {
ret = set_reg_value(S_TCPPARAMS_REG_VALUE_KEY, reg_hostname[i], computer_name);
if (ret)
return ret;
}
/* set domain in TCP properties in registry */
if (domain != NULL) {
len = strlen(domain);
/* strip trailing dot/space, overwise Windows will have a bug with workgroup (PSBM-17913) */
while (len > 0 && (domain[len-1] == '.' || isspace(domain[len-1])))
domain[--len] = 0;
if (0 == len) {
for (i = 0; reg_domain[i] != NULL; ++i) {
set_reg_value(S_TCPPARAMS_REG_VALUE_KEY, reg_domain[i], "");
}
} else {
status = DnsValidateName_A(domain, DnsNameDomain);
if (status != ERROR_SUCCESS && status != DNS_ERROR_NON_RFC_NAME) {
error(status, "Domain name '%s' is invalid\n", domain );
return status;
}
for (i = 0; reg_domain[i] != NULL; i++) {
ret = set_reg_value(S_TCPPARAMS_REG_VALUE_KEY, reg_domain[i], domain);
if (ret)
return ret;
}
}
}
/* set ComputerName properties (in upper case) */
ret = DnsHostnameToComputerNameA(computer_name, netbios_name, &netbios_name_len);
if (ret == 0) { //DnsHostnameToComputerNameA return 0 if conversion fails.
status = GetLastError();
error(status, "Cannot convert dns hostname to computer name\n");
return status;
}
for (i = 0; reg_computer_name[i] != NULL; i++) {
ret = set_reg_value(reg_computer_name[i], S_COMPUTER_REG_NAME, netbios_name);
if (ret)
return ret;
}
debug("set_hostname host=%s domain=%s computer=%s\n", computer_name, domain, netbios_name);
return 0;
}
int set_dhcp(struct netinfo *if_it, struct nettool_mac *params)
{
int rc;
struct nettool_mac *mac_it;
if (strchr(params->value, '4'))
{
if (!if_it->configured_with_dhcp)
{
//switch to dynamic
char str[1024];
sprintf(str, "interface ip set dns %d dhcp", if_it->idx);
if ((rc = exec_netsh(str)))
return rc;
sprintf(str, "interface ip set address %d dhcp", if_it->idx);
if ((rc = exec_netsh(str)))
return rc;
if_it->configured_with_dhcp = 1;
}
else
{
error(0, "DHCP v4 is already enabled");
if_it->dhcp4_changed = 0;
}
}
else
{
if_it->dhcp4_changed = 0;
}
mac_it = get_opt_mac(if_it->mac, NET_OPT_IP);
if (strchr(params->value, '6') ||
(mac_it == NULL && is_ipv6_supported()) /*ipv6 not specified at all*/ )
{
if (!if_it->configured_with_dhcpv6)
{
//switch to dynamic
char str[1024];
sprintf(str, "interface ipv6 set dns %d dhcp", if_it->idx);
if ((rc = exec_netsh(str)))
return rc;
//remove all IPs except of local-link
if (remove_ipv6_ips(if_it))
return -1;
if_it->configured_with_dhcpv6 = 1;
}
else
{
if_it->dhcp6_changed = 0;
error(0, "DHCP v6 is already enabled");
}
}
else
{
if_it->dhcp6_changed = 0;
}
return 0;
}
#if (NTDDI_VERSION < NTDDI_LONGHORN)
static int remove_route_xp(void)
{
HKEY key;
DWORD res, sz;
char buf[MAX_PATH+1], str[MAX_PATH+1];
char * param[4], *s;
int i, j;
int rc = 0, ret;
res = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
0, KEY_ENUMERATE_SUB_KEYS, &key);
if (res != ERROR_SUCCESS) {
if (res != ERROR_PATH_NOT_FOUND) {
error(0, "RegOpenKeyEx() err %u", res);
return res;
}
return 0;
}
for (i = 0; ; ++i) {
sz = _countof(buf);
res = RegEnumKeyExA(key, i, buf, &sz, 0, 0, 0, 0);
if (res == ERROR_NO_MORE_ITEMS)
break;
if (res != ERROR_SUCCESS) {
error(0, "RegEnumKeyEx() err %u", res);
continue;
}
/* format: addr,mask,gw,metric */
strcpy(str, buf);
s = strtok(buf, ",");
j = 0;
while (s && j < _countof(param)) {
param[j++] = s;
s = strtok(NULL, ",");
}
if (j != _countof(param)) {
error(0, "Bad PersistentRoutes entry %ws", buf);
continue;
}
sprintf(buf, "route delete -p %s mask %s %s", param[0], param[1], param[2]);
ret = exec_cmd(buf);
if (ret != 0 && rc == 0)
rc = ret;
}
RegCloseKey(key);
return rc;
}
#else
static int exec_cmd_pipe_stdout(char * text, FILE ** filep)
{
int rc = -1;
HANDLE rd = NULL, wr = NULL;
SECURITY_ATTRIBUTES attr;
PROCESS_INFORMATION procinfo = { 0 };
STARTUPINFOW startinfo = { 0 };
WCHAR cmdline[MAX_PATH+1];
int fd;
debug("run: %s\n", text);
attr.nLength = sizeof(attr);
attr.bInheritHandle = TRUE;
attr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&rd, &wr, &attr, 0)) {
error(0, "CreatePipe() failed, err = %u", GetLastError());
goto out;
}
SetHandleInformation(rd, HANDLE_FLAG_INHERIT, 0);
startinfo.cb = sizeof(startinfo);
startinfo.hStdInput = NULL;
startinfo.hStdOutput = wr;
startinfo.hStdError = NULL;
startinfo.dwFlags = STARTF_USESTDHANDLES;
_snwprintf(cmdline, MAX_PATH, L"%hs", text);
cmdline[MAX_PATH] = 0;
if (!CreateProcessW(NULL,
cmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
CREATE_NO_WINDOW, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&startinfo, // STARTUPINFO pointer
&procinfo)) // receives PROCESS_INFORMATION
{
error(0, "CreateProcess(\"%hs\") failed, err = %u", text, GetLastError());
goto out;
}
CloseHandle(wr);
wr = NULL;
fd = _open_osfhandle((intptr_t)rd, _O_RDONLY);
if (fd < 0) {
error(0, "_open_osfhandle() failed, errno = %u\n", errno);
goto out;
}
// from now fclose() will do CloseHandle() as well
rd = NULL;
*filep = _fdopen(fd, "r");
if (*filep == NULL) {
error(0, "_open_osfhandle() failed, errno = %u\n", errno);
_close(fd);
goto out;
}
rc = 0;
out:
if (procinfo.hProcess)
CloseHandle(procinfo.hProcess);
if (procinfo.hThread)
CloseHandle(procinfo.hThread);
if (rd)
CloseHandle(rd);
if (wr)
CloseHandle(wr);
return rc;
}
static int remove_route_vista(struct netinfo *if_it, const char *family)
{
FILE * fd = NULL;
char line[256], str[256], cmd[256];
char * param[6], *s;
int rc;
int i, count, len;
sprintf(cmd, "netsh interface %s show route store=persistent", family);
rc = exec_cmd_pipe_stdout(cmd, &fd);
if (rc != 0)
return rc;
line[sizeof(line)-1] = 0;
count = 0;
while (fgets(line, sizeof(line)-1, fd) != NULL) {
/* skip three lines (header) */
if (++count <= 3)
continue;
/* trim spaces and newlines */
len = strlen(line);
while (len > 0 && isspace(line[len-1]))
line[--len] = 0;
/* format: Publish Type Metric Prefix Idx InterfaceName */
strcpy(str, line);
s = strtok(line, " \t");
i = 0;
while (s && i < _countof(param)) {
param[i++] = s;
s = strtok(NULL, " \t");
}
if (i != _countof(param))
continue;
/* a tricky part: InterfaceName may also contain spaces trimmed by strtok -
so use a pointer from origin string */
param[_countof(param) - 1] = str + (param[_countof(param) - 1] - line);
//for (i=0; i < _countof(param); ++i)
// printf("i=%d [%s]\n", i, param[i]);
if (if_it->idx == atoi(param[4])) {
sprintf(cmd, "interface %s delete route %s %d store=persistent",
family, param[3], if_it->idx);
exec_netsh(cmd);
}
}
fclose(fd);
return 0;
}
#endif
int set_route(struct netinfo *if_it, struct nettool_mac *params)
{
char str[1024];
struct namelist *gws = NULL;
struct namelist *gw;
int rc;
#if (NTDDI_VERSION < NTDDI_LONGHORN)
char if_ip[100];
rc = find_if_first_ip(if_it, if_ip);
if (rc < 0)
return rc;
#endif
//split params->value
namelist_split(&gws, params->value);
//for each ip do
gw = gws;
while (gw)
{
if (is_remove(gw->name))
{
#if (NTDDI_VERSION < NTDDI_LONGHORN)
rc = remove_route_xp();
#else
rc = remove_route_vista(if_it, "ipv4");
rc = remove_route_vista(if_it, "ipv6");
#endif
} else {
struct route route = {NULL, NULL, NULL};
parse_route(gw->name, &route);
if (is_ipv6(gw->name)) {
sprintf(str, "interface ipv6 add route %s%s %d %s %s%s store=persistent",
route.ip, strchr(route.ip, '/') ? "" : "/128", if_it->idx,
route.gw ? route.gw : "", route.metric ? "metric=" : "",
route.metric ? route.metric : "");
rc = exec_netsh(str);
} else {
#if (NTDDI_VERSION < NTDDI_LONGHORN)
char ip[100];
char mask[IP_LENGTH+1];
if (split_ip_mask(route.ip, ip, mask) < 0) {
strcpy(ip, route.ip);
strcpy(mask, "255.255.255.255");
}
char *hop = route.gw ? route.gw : if_ip;
sprintf(str, "route add -p %s mask %s %s %s %s", ip, mask, hop,
route.metric ? "metric" : "", route.metric ? route.metric : "");
rc = exec_cmd(str);
#else
sprintf(str, "interface ipv4 add route %s%s %d %s %s%s store=persistent",
route.ip, strchr(route.ip, '/') ? "" : "/32", if_it->idx,
route.gw ? route.gw : "", route.metric ? "metric=" : "",
route.metric ? route.metric : "");
rc = exec_netsh(str);
#endif
}
clear_route(&route);
}
gw = gw->next;
}
rc = 0;
return rc;
}
int clean(struct netinfo *if_it)
{
VARUNUSED(if_it);
return 0;
}
static int enable_adapter_name(const char *name, int enable)
{
char str[1024];
int rc = 0;
debug("enable_adapter_name(%s, %d)\n", name, enable);
if (!enable) //save disabled
save_adapter_disabled(name, 0);
sprintf(str, "interface set interface \"%s\" %s", name, enable ? "ENABLE" : "DISABLE");
rc = exec_netsh(str);
if (enable && rc == 0) //save only if successfully enabled
save_adapter_disabled(name, 1);
return 0;