cleanup composer/compositing/composition -> compositor
[AROS.git] / workbench / prefs / network / prefsdata.c
blobdcc19531e8bdb3db61b05158bb91a4f720aa75b4
1 /*
2 Copyright © 2009-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include "prefsdata.h"
13 #include <aros/debug.h>
15 static struct TCPPrefs prefs;
17 struct Tokenizer
19 STRPTR tokenizerLine;
20 STRPTR token;
21 FILE * tokenizedFile;
22 BOOL newline;
23 BOOL fend;
26 /* List of devices that require NOTRACKING option */
27 static STRPTR notrackingdevices[] = {"prm-rtl8029.device", NULL};
29 void OpenTokenFile(struct Tokenizer * tok, STRPTR FileName)
31 tok->tokenizedFile = fopen(FileName, "r");
32 tok->token = NULL;
33 tok->newline = TRUE;
34 if (!tok->tokenizedFile)
35 tok->fend = TRUE;
36 else
38 tok->tokenizerLine = malloc(8192);
39 tok->fend = FALSE;
40 tok->newline = TRUE;
44 void CloseTokenFile(struct Tokenizer * tok)
46 if (tok->tokenizedFile)
48 fclose(tok->tokenizedFile);
49 free(tok->tokenizerLine);
50 tok->fend = TRUE;
51 tok->token = NULL;
55 void GetNextToken(struct Tokenizer * tok, STRPTR tk)
57 tok->newline = FALSE;
58 if (tok->token != NULL)
60 tok->token = strtok(NULL, tk);
61 if (!tok->token)
62 GetNextToken(tok, tk);
64 else
66 tok->newline = TRUE;
67 if (!feof(tok->tokenizedFile))
69 tok->tokenizerLine[0] = 0;
70 fgets(tok->tokenizerLine, 8192, tok->tokenizedFile);
71 if (tok->tokenizerLine == NULL)
73 tok->token = NULL;
74 GetNextToken(tok, tk);
76 else
77 tok->token = strtok(tok->tokenizerLine, tk);
79 else
80 tok->fend = TRUE;
84 void SetDefaultNetworkPrefsValues()
86 LONG i;
87 for (i = 0; i < MAXINTERFACES; i++)
89 InitInterface(GetInterface(i));
91 SetInterfaceCount(0);
92 SetDomain(DEFAULTDOMAIN);
93 SetHost(DEFAULTHOST);
94 SetGate(DEFAULTGATE);
95 SetDNS(0, DEFAULTDNS);
96 SetDNS(1, DEFAULTDNS);
97 SetDHCP(FALSE);
99 SetAutostart(FALSE);
102 void SetDefaultWirelessPrefsValues()
104 LONG i;
105 for (i = 0; i < MAXNETWORKS; i++)
107 InitNetwork(GetNetwork(i));
109 SetNetworkCount(0);
111 SetWirelessDevice(NULL);
114 void SetDefaultMobilePrefsValues()
116 LONG i;
117 for (i = 0; i < MAXATCOMMANDS; i++)
119 prefs.mobile.atcommand[i][0] = 0;
121 SetMobile_atcommand(0,"AT+CGDCONT=1,\"IP\",\"insert.your.apn.here\"");
122 SetMobile_atcommand(1,"ATDT*99***1#");
123 SetMobile_timeout( 10 );
124 SetMobile_Autostart(FALSE);
125 SetMobile_devicename( "usbmodem.device" );
126 SetMobile_unit( 0 );
127 SetMobile_username("");
128 SetMobile_password("");
131 void InitInterface(struct Interface *iface)
133 SetName(iface, DEFAULTNAME);
134 SetIfDHCP(iface, TRUE);
135 SetIP(iface, DEFAULTIP);
136 SetMask(iface, DEFAULTMASK);
137 SetDevice(iface, DEFAULTDEVICE);
138 SetUnit(iface, 0);
139 SetUp(iface, FALSE);
142 /* Returns TRUE if directory has been created or already existed */
143 BOOL RecursiveCreateDir(CONST_STRPTR dirpath)
145 /* Will create directory even if top level directory does not exist */
147 BPTR lock = BNULL;
148 ULONG lastdirseparator = 0;
149 ULONG dirpathlen = strlen(dirpath);
150 STRPTR tmpdirpath = AllocVec(dirpathlen + 2, MEMF_CLEAR | MEMF_PUBLIC);
152 CopyMem(dirpath, tmpdirpath, dirpathlen);
154 /* Recurvice directory creation */
155 while(TRUE)
157 if (lastdirseparator >= dirpathlen) break;
159 for (; lastdirseparator < dirpathlen; lastdirseparator++)
160 if (tmpdirpath[lastdirseparator] == '/') break;
162 tmpdirpath[lastdirseparator] = '\0'; /* cut */
164 /* Unlock any lock from previous interation. Last iteration lock will be returned. */
165 if (lock != BNULL)
167 UnLock(lock);
168 lock = BNULL;
171 /* Check if directory exists */
172 lock = Lock(tmpdirpath, SHARED_LOCK);
173 if (lock == BNULL)
175 lock = CreateDir(tmpdirpath);
176 if (lock == BNULL)
177 break; /* Error with creation */
180 tmpdirpath[lastdirseparator] = '/'; /* restore */
181 lastdirseparator++;
184 FreeVec(tmpdirpath);
186 if (lock == BNULL)
187 return FALSE;
188 else
190 UnLock(lock);
191 lock = BNULL;
192 return TRUE;
196 /* Returns TRUE if selected device needs to use NOTRACKING option */
197 BOOL GetNoTracking(struct Interface *iface)
199 STRPTR devicename = NULL;
200 LONG pos = 0;
201 TEXT devicepath[strlen(GetDevice(iface)) + 1];
202 strcpy(devicepath, GetDevice(iface));
203 strupr(devicepath);
205 while ((devicename = notrackingdevices[pos++]) != NULL)
207 /* Comparison is done on upper case string so it is case insensitive */
208 strupr(devicename);
209 if (strstr(devicepath, devicename) != NULL)
210 return TRUE;
213 return FALSE;
216 /* Puts part 1 into empty buffer */
217 VOID CombinePath1P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1)
219 dstbuffer[0] = '\0'; /* Make sure buffer is treated as empty */
220 AddPart(dstbuffer, part1, dstbufferlen);
223 /* Combines part1 with part2 into an empty buffer */
224 VOID CombinePath2P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1, CONST_STRPTR part2)
226 CombinePath1P(dstbuffer, dstbufferlen, part1);
227 AddPart(dstbuffer, part2, dstbufferlen);
230 /* Combines part1 with part2 with part3 into an empty buffer */
231 VOID CombinePath3P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1, CONST_STRPTR part2, CONST_STRPTR part3)
233 CombinePath2P(dstbuffer, dstbufferlen, part1, part2);
234 AddPart(dstbuffer, part3, dstbufferlen);
237 BOOL WriteNetworkPrefs(CONST_STRPTR destdir)
239 FILE *ConfFile;
240 LONG i;
241 struct Interface *iface;
242 ULONG filenamelen = strlen(destdir) + 4 + 20;
243 TEXT filename[filenamelen];
244 ULONG destdbdirlen = strlen(destdir) + 3 + 1;
245 TEXT destdbdir[destdbdirlen];
246 LONG interfacecount = GetInterfaceCount();
248 CombinePath2P(destdbdir, destdbdirlen, destdir, "db");
250 /* Create necessary directories */
251 if(!RecursiveCreateDir(destdir)) return FALSE;
252 if(!RecursiveCreateDir(destdbdir)) return FALSE;
254 /* Write configuration files */
255 CombinePath2P(filename, filenamelen, destdbdir, "general.config");
256 ConfFile = fopen(filename, "w");
257 if (!ConfFile) return FALSE;
258 fprintf(ConfFile, "USELOOPBACK=YES\n");
259 fprintf(ConfFile, "DEBUGSANA=NO\n");
260 fprintf(ConfFile, "USENS=SECOND\n");
261 fprintf(ConfFile, "GATEWAY=NO\n");
262 fprintf(ConfFile, "HOSTNAME=%s.%s\n", GetHost(), GetDomain());
263 fprintf(ConfFile, "LOG FILTERFILE=5\n");
264 fprintf(ConfFile, "GUI PANEL=MUI\n");
265 fprintf(ConfFile, "OPENGUI=YES\n");
266 fclose(ConfFile);
268 CombinePath2P(filename, filenamelen, destdbdir, "interfaces");
269 ConfFile = fopen(filename, "w");
270 if (!ConfFile) return FALSE;
271 for(i = 0; i < interfacecount; i++)
273 iface = GetInterface(i);
274 fprintf
276 ConfFile, "%s DEV=%s UNIT=%d %s IP=%s NETMASK=%s %s\n",
277 GetName(iface), GetDevice(iface), (int)GetUnit(iface),
278 (GetNoTracking(iface) ? (CONST_STRPTR)"NOTRACKING" : (CONST_STRPTR)""),
279 (GetIfDHCP(iface) ?
280 (strstr(GetDevice(iface), "ppp.device") == NULL ?
281 (CONST_STRPTR)"DHCP" : (CONST_STRPTR)"0.0.0.0") :
282 GetIP(iface)),
283 GetMask(iface),
284 (GetUp(iface) ? (CONST_STRPTR)"UP" : (CONST_STRPTR)"")
286 if (strstr(GetDevice(iface), "atheros5000.device") != NULL
287 || strstr(GetDevice(iface), "prism2.device") != NULL
288 || strstr(GetDevice(iface), "realtek8180.device") != NULL)
290 SetWirelessDevice(GetDevice(iface));
291 SetWirelessUnit(GetUnit(iface));
293 else if (strstr(GetDevice(iface), "ppp.device") != NULL)
294 SetMobile_Autostart(TRUE);
296 fclose(ConfFile);
298 CombinePath2P(filename, filenamelen, destdbdir, "netdb-myhost");
299 ConfFile = fopen(filename, "w");
300 if (!ConfFile) return FALSE;
302 for(i = 0; i < interfacecount; i++)
304 iface = GetInterface(i);
305 if (!GetIfDHCP(iface))
307 fprintf
309 ConfFile, "HOST %s %s.%s %s\n",
310 GetIP(iface), GetHost(), GetDomain(), GetHost()
315 if (!GetDHCP())
317 // FIXME: old version wrote Gateway even when DHCP was enabled
318 fprintf(ConfFile, "HOST %s gateway\n", GetGate());
319 fprintf(ConfFile, "; Domain names\n");
320 fprintf(ConfFile, "; Name servers\n");
321 fprintf(ConfFile, "NAMESERVER %s\n", GetDNS(0));
322 fprintf(ConfFile, "NAMESERVER %s\n", GetDNS(1));
324 fclose(ConfFile);
326 CombinePath2P(filename, filenamelen, destdbdir, "static-routes");
327 ConfFile = fopen(filename, "w");
328 if (!ConfFile) return FALSE;
329 if (!GetDHCP())
331 // FIXME: old version wrote Gateway even when DHCP was enabled
332 fprintf(ConfFile, "DEFAULT GATEWAY %s\n", GetGate());
334 fclose(ConfFile);
336 /* Write variables */
337 CombinePath2P(filename, filenamelen, destdir, "Config");
338 ConfFile = fopen(filename, "w");
339 if (!ConfFile) return FALSE;
340 fprintf(ConfFile, "%s/db", PREFS_PATH_ENV);
341 fclose(ConfFile);
343 CombinePath2P(filename, filenamelen, destdir, "AutoRun");
344 ConfFile = fopen(filename, "w");
345 if (!ConfFile) return FALSE;
346 fprintf(ConfFile, "%s", (GetAutostart()) ? "True" : "False");
347 fclose(ConfFile);
349 CombinePath2P(filename, filenamelen, destdir, "MobileAutorun");
350 ConfFile = fopen(filename, "w");
351 if (!ConfFile) return FALSE;
352 fprintf(ConfFile, "%s", (GetMobile_Autostart()) ? "True" : "False");
353 fclose(ConfFile);
355 CombinePath2P(filename, filenamelen, destdir, "WirelessAutoRun");
356 ConfFile = fopen(filename, "w");
357 if (!ConfFile) return FALSE;
358 fprintf(ConfFile, "%s", (GetWirelessDevice() != NULL) ? "True" : "False");
359 fclose(ConfFile);
361 if (GetWirelessDevice() != NULL)
363 CombinePath2P(filename, filenamelen, destdir, "WirelessDevice");
364 ConfFile = fopen(filename, "w");
365 if (!ConfFile) return FALSE;
366 fprintf(ConfFile, "%s UNIT %ld", GetWirelessDevice(),
367 (long int)GetWirelessUnit());
368 fclose(ConfFile);
371 return TRUE;
374 BOOL WriteWirelessPrefs(CONST_STRPTR destdir)
376 FILE *ConfFile;
377 LONG i;
378 struct Network *net;
379 ULONG filenamelen = strlen(destdir) + 4 + 20;
380 TEXT filename[filenamelen];
382 /* Write wireless config */
383 if (prefs.networkCount > 0)
385 CombinePath2P(filename, filenamelen, destdir, "Wireless.prefs");
386 ConfFile = fopen(filename, "w");
387 if (!ConfFile) return FALSE;
389 for (i = 0; i < prefs.networkCount; i++)
391 net = &prefs.networks[i];
392 fprintf(ConfFile, "network={\n");
393 if (net->name[0] != '\0')
394 fprintf(ConfFile, "\tssid=\"%s\"\n", net->name);
395 switch (net->encType)
397 case 2:
398 if (net->keyIsHex)
399 fprintf(ConfFile, "\tpsk=%s\n", net->key);
400 else
401 fprintf(ConfFile, "\tpsk=\"%s\"\n", net->key);
402 fprintf(ConfFile, "\tkey_mgmt=WPA-PSK\n");
403 break;
404 case 1:
405 if (net->keyIsHex)
406 fprintf(ConfFile, "\twep_key0=%s\n", net->key);
407 else
408 fprintf(ConfFile, "\twep_key0=\"%s\"\n", net->key);
409 fprintf(ConfFile, "\twep_tx_keyidx=0\n");
410 default:
411 fprintf(ConfFile, "\tkey_mgmt=NONE\n");
413 if (net->hidden)
414 fprintf(ConfFile, "\tscan_ssid=1\n");
415 if (net->adHoc)
416 fprintf(ConfFile, "\tmode=1\n");
417 fprintf(ConfFile, "}\n\n");
420 fclose(ConfFile);
423 return TRUE;
427 BOOL WriteMobilePrefs(CONST_STRPTR destdir)
429 FILE *ConfFile;
430 LONG i;
431 ULONG filenamelen = strlen(destdir) + 4 + 30;
432 TEXT filename[filenamelen];
434 CombinePath2P(filename, filenamelen, destdir, "MobileBroadband.prefs");
435 ConfFile = fopen(filename, "w");
436 if (!ConfFile) return FALSE;
438 if( strlen(GetMobile_devicename()) > 0 ) fprintf(ConfFile, "DEVICE %s\n" ,GetMobile_devicename() );
439 fprintf(ConfFile, "UNIT %d\n" , (int)GetMobile_unit() );
440 if( strlen(GetMobile_username()) > 0 ) fprintf(ConfFile, "USERNAME %s\n" ,GetMobile_username() );
441 if( strlen(GetMobile_password()) > 0 ) fprintf(ConfFile, "PASSWORD %s\n" ,GetMobile_password() );
443 for (i = 0; i < MAXATCOMMANDS; i++)
445 if( strlen( GetMobile_atcommand(i) ) > 0 ){
446 fprintf(ConfFile, "SEND %s\n" ,GetMobile_atcommand(i) );
450 fclose(ConfFile);
452 return TRUE;
456 #define BUFSIZE 2048
457 BOOL CopyFile(CONST_STRPTR srcfile, CONST_STRPTR dstfile)
459 BPTR from = BNULL, to = BNULL;
460 TEXT buffer[BUFSIZE];
462 if ((from = Open(srcfile, MODE_OLDFILE)))
464 if ((to = Open(dstfile, MODE_NEWFILE)))
466 LONG s = 0;
470 if ((s = Read(from, buffer, BUFSIZE)) == -1)
472 Close(to);
473 Close(from);
474 return FALSE;
477 if (Write(to, buffer, s) == -1)
479 Close(to);
480 Close(from);
481 return FALSE;
483 } while (s == BUFSIZE);
485 Close(to);
486 Close(from);
487 return TRUE;
490 Close(from);
493 return FALSE;
496 CONST_STRPTR GetDefaultStackLocation()
498 /* Use static variable so that it is initialized only once (and can be returned) */
499 static TEXT path [1024] = {0};
501 /* Load path if needed - this will happen only once */
502 if (path[0] == '\0')
504 GetVar(AROSTCP_PACKAGE_VARIABLE, path, 1024, LV_VAR);
507 return path;
510 BOOL IsStackRunning()
512 return FindTask("bsdsocket.library") != NULL;
515 BOOL RestartStack()
517 ULONG trycount = 0;
519 /* Shutdown */
520 if (IsStackRunning())
522 struct Task * arostcptask = FindTask("bsdsocket.library");
523 if (arostcptask != NULL)
524 Signal(arostcptask, SIGBREAKF_CTRL_C);
527 /* Check if shutdown successful */
528 trycount = 0;
529 while(IsStackRunning())
531 if (trycount > 4) return FALSE;
532 Delay(50);
533 trycount++;
536 /* Startup */
538 CONST_STRPTR srcdir = GetDefaultStackLocation();
539 ULONG arostcppathlen = strlen(srcdir) + 3 + 20;
540 TEXT arostcppath[arostcppathlen];
541 struct TagItem tags[] =
543 { SYS_Input, (IPTR)NULL },
544 { SYS_Output, (IPTR)NULL },
545 { SYS_Error, (IPTR)NULL },
546 { SYS_Asynch, (IPTR)TRUE },
547 { TAG_DONE, 0 }
550 CombinePath3P(arostcppath, arostcppathlen, srcdir, "C", "AROSTCP");
552 SystemTagList(arostcppath, tags);
555 /* Check if startup successful */
556 trycount = 0;
557 while (!IsStackRunning())
559 if (trycount > 9) return FALSE;
560 Delay(50);
561 trycount++;
564 /* All ok */
565 return TRUE;
568 BOOL StopWireless()
570 ULONG trycount = 0;
572 /* Shutdown */
574 struct Task *task = FindTask("C:WirelessManager");
575 if (task != NULL)
576 Signal(task, SIGBREAKF_CTRL_C);
579 /* Check if shutdown successful */
580 trycount = 0;
581 while(FindTask("C:WirelessManager") != NULL)
583 if (trycount > 4) return FALSE;
584 Delay(50);
585 trycount++;
588 /* All ok */
589 return TRUE;
592 BOOL StartWireless()
594 ULONG trycount = 0;
595 TEXT command[80];
597 /* Startup */
599 struct TagItem tags[] =
601 { SYS_Input, (IPTR)NULL },
602 { SYS_Output, (IPTR)NULL },
603 { SYS_Error, (IPTR)NULL },
604 { SYS_Asynch, (IPTR)TRUE },
605 { TAG_DONE, 0 }
608 snprintf(command, 80, "C:WirelessManager \"%s\" UNIT %ld\n",
609 GetWirelessDevice(), (long int)GetWirelessUnit());
610 SystemTagList(command, tags);
613 /* Check if startup successful */
614 trycount = 0;
615 while(FindTask("C:WirelessManager") == NULL)
617 if (trycount > 9) return FALSE;
618 Delay(50);
619 trycount++;
622 /* All ok */
623 return TRUE;
626 BOOL StopMobile()
628 ULONG trycount = 0;
630 /* Shutdown */
632 struct Task *task = FindTask("C:ModemManager");
633 if (task != NULL)
634 Signal(task, SIGBREAKF_CTRL_C);
637 /* Check if shutdown successful */
638 trycount = 0;
639 while(FindTask("C:ModemManager") != NULL)
641 if (trycount > 4) return FALSE;
642 Delay(50);
643 trycount++;
646 /* All ok */
647 return TRUE;
650 BOOL StartMobile()
652 ULONG trycount = 0;
654 /* Startup */
656 struct TagItem tags[] =
658 { SYS_Input, (IPTR)NULL },
659 { SYS_Output, (IPTR)NULL },
660 { SYS_Error, (IPTR)NULL },
661 { SYS_Asynch, (IPTR)TRUE },
662 { TAG_DONE, 0 }
665 SystemTagList("C:ModemManager", tags);
668 /* Check if startup successful */
669 trycount = 0;
670 while(FindTask("C:ModemManager") == NULL)
672 if (trycount > 9) return FALSE;
673 Delay(50);
674 trycount++;
677 /* All ok */
678 return TRUE;
681 /* This is not a general use function! It assumes destinations directory exists */
682 BOOL AddFileFromDefaultStackLocation(CONST_STRPTR filename, CONST_STRPTR dstdir)
684 /* Build paths */
685 CONST_STRPTR srcdir = GetDefaultStackLocation();
686 ULONG srcfilelen = strlen(srcdir) + 4 + strlen(filename) + 1;
687 TEXT srcfile[srcfilelen];
688 ULONG dstfilelen = strlen(dstdir) + 4 + strlen(filename) + 1;
689 TEXT dstfile[dstfilelen];
690 BPTR dstlock = BNULL;
692 CombinePath3P(srcfile, srcfilelen, srcdir, "db", filename);
693 CombinePath3P(dstfile, dstfilelen, dstdir, "db", filename);
695 /* Check if the destination file already exists. If yes, do not copy */
696 dstlock = Lock(dstfile, SHARED_LOCK);
697 if (dstlock != BNULL)
699 UnLock(dstlock);
700 return TRUE;
703 return CopyFile(srcfile, dstfile);
706 /* Copies files not created by prefs but needed to start stack */
707 BOOL CopyDefaultConfiguration(CONST_STRPTR destdir)
709 ULONG destdbdirlen = strlen(destdir) + 3 + 1;
710 TEXT destdbdir[destdbdirlen];
711 CombinePath2P(destdbdir, destdbdirlen, destdir, "db");
713 /* Create necessary directories */
714 if (!RecursiveCreateDir(destdir)) return FALSE;
715 if (!RecursiveCreateDir(destdbdir)) return FALSE;
717 /* Copy files */
718 if (!AddFileFromDefaultStackLocation("hosts", destdir)) return FALSE;
719 if (!AddFileFromDefaultStackLocation("inet.access", destdir)) return FALSE;
720 if (!AddFileFromDefaultStackLocation("netdb", destdir)) return FALSE;
721 if (!AddFileFromDefaultStackLocation("networks", destdir)) return FALSE;
722 if (!AddFileFromDefaultStackLocation("protocols", destdir)) return FALSE;
723 if (!AddFileFromDefaultStackLocation("services", destdir)) return FALSE;
725 return TRUE;
728 enum ErrorCode SaveNetworkPrefs()
730 if (!CopyDefaultConfiguration(PREFS_PATH_ENVARC)) return NOT_COPIED_FILES_ENVARC;
731 if (!WriteNetworkPrefs(PREFS_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
732 if (!WriteWirelessPrefs(WIRELESS_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
733 if (!WriteMobilePrefs(MOBILEBB_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
734 return UseNetworkPrefs();
737 enum ErrorCode UseNetworkPrefs()
739 if (!CopyDefaultConfiguration(PREFS_PATH_ENV)) return NOT_COPIED_FILES_ENV;
740 if (!WriteNetworkPrefs(PREFS_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
741 if (!WriteWirelessPrefs(WIRELESS_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
742 if (!WriteMobilePrefs(MOBILEBB_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
743 if(StopWireless())
744 if (GetWirelessDevice() != NULL)
745 if (!StartWireless()) return NOT_RESTARTED_WIRELESS;
746 if (!RestartStack()) return NOT_RESTARTED_STACK;
747 if(StopMobile())
748 if (GetMobile_Autostart())
749 if (!StartMobile()) return NOT_RESTARTED_MOBILE;
750 return ALL_OK;
753 /* Directory points to top of config, so to AAA/AROSTCP not to AAA/AROSTCP/db */
754 void ReadNetworkPrefs(CONST_STRPTR directory)
756 ULONG filenamelen = strlen(directory) + 4 + 20;
757 TEXT filename[filenamelen];
758 BOOL comment = FALSE;
759 STRPTR tstring;
760 struct Tokenizer tok;
761 LONG interfacecount;
762 struct Interface *iface = NULL;
764 /* This function will not fail. It will load as much data as possible. Rest will be default values */
766 SetDHCP(FALSE);
768 CombinePath3P(filename, filenamelen, directory, "db", "general.config");
769 OpenTokenFile(&tok, filename);
770 while (!tok.fend)
772 if (tok.newline)
773 { // read tokens from the beginning of line
774 if (tok.token)
776 if (strcmp(tok.token, "HOSTNAME") == 0)
778 GetNextToken(&tok, "=\n");
779 tstring = strchr(tok.token, '.');
780 SetDomain(tstring + 1);
781 tstring[0] = 0;
782 SetHost(tok.token);
786 GetNextToken(&tok, "=\n");
788 CloseTokenFile(&tok);
790 CombinePath3P(filename, filenamelen, directory, "db", "interfaces");
791 OpenTokenFile(&tok, filename);
793 SetInterfaceCount(0);
794 interfacecount = 0;
796 while (!tok.fend && (interfacecount < MAXINTERFACES))
798 GetNextToken(&tok, " \n");
799 if (tok.token)
801 if (tok.newline) comment = FALSE;
802 if (strncmp(tok.token, "#", 1) == 0) comment = TRUE;
804 if (!comment)
806 if (tok.newline)
808 iface = GetInterface(interfacecount);
809 SetName(iface, tok.token);
810 interfacecount++;
811 SetInterfaceCount(interfacecount);
813 else if (strncmp(tok.token, "DEV=", 4) == 0)
815 tstring = strchr(tok.token, '=');
816 SetDevice(iface, tstring + 1);
818 else if (strncmp(tok.token, "UNIT=", 5) == 0)
820 tstring = strchr(tok.token, '=');
821 SetUnit(iface, atol(tstring + 1));
823 else if (strncmp(tok.token, "IP=", 3) == 0)
825 tstring = strchr(tok.token, '=');
826 if (strncmp(tstring + 1, "DHCP", 4) == 0
827 || strstr(GetDevice(iface), "ppp.device") != NULL)
829 SetIfDHCP(iface, TRUE);
830 SetIP(iface, DEFAULTIP);
832 else
834 SetIP(iface, tstring + 1);
835 SetIfDHCP(iface, FALSE);
838 else if (strncmp(tok.token, "NETMASK=", 8) == 0)
840 tstring = strchr(tok.token, '=');
841 SetMask(iface, tstring + 1);
843 else if (strncmp(tok.token, "UP", 2) == 0)
845 SetUp(iface, TRUE);
850 CloseTokenFile(&tok);
852 CombinePath3P(filename, filenamelen, directory, "db", "netdb-myhost");
853 OpenTokenFile(&tok, filename);
854 int dnsc = 0;
855 while (!tok.fend)
857 GetNextToken(&tok, " \n");
858 if (tok.token)
860 // Host and Domain are already read from general.config
861 if (strncmp(tok.token, "NAMESERVER", 10) == 0)
863 GetNextToken(&tok, " \n");
864 SetDNS(dnsc, tok.token);
865 dnsc++;
866 if (dnsc > 1) dnsc = 1;
870 // Assume DHCP if there is no nameserver
871 if (dnsc == 0)
873 SetDHCP(TRUE);
875 CloseTokenFile(&tok);
877 CombinePath3P(filename, filenamelen, directory, "db", "static-routes");
878 OpenTokenFile(&tok, filename);
879 while (!tok.fend)
881 GetNextToken(&tok, " \n");
882 if (tok.token)
884 if (strncmp(tok.token, "DEFAULT", 7) == 0)
886 GetNextToken(&tok, " \n");
887 if (strncmp(tok.token, "GATEWAY", 7) == 0)
889 GetNextToken(&tok, " \n");
890 SetGate(tok.token);
895 CloseTokenFile(&tok);
897 CombinePath2P(filename, filenamelen, directory, "Autorun");
898 OpenTokenFile(&tok, filename);
899 while (!tok.fend)
901 GetNextToken(&tok, " \n");
902 if (tok.token)
904 if (strncmp(tok.token, "True", 4) == 0)
906 SetAutostart(TRUE);
907 break;
909 else
911 SetAutostart(FALSE);
912 break;
916 CloseTokenFile(&tok);
919 void ReadWirelessPrefs(CONST_STRPTR directory)
921 ULONG filenamelen = strlen(directory) + 4 + 20;
922 TEXT filename[filenamelen];
923 BOOL comment = FALSE;
924 STRPTR tstring;
925 struct Tokenizer tok;
926 LONG networkCount;
927 struct Network *net = NULL;
928 BOOL keyIsHex;
930 CombinePath2P(filename, filenamelen, directory, "Wireless.prefs");
931 OpenTokenFile(&tok, filename);
933 SetNetworkCount(0);
934 networkCount = 0;
936 while (!tok.fend && (networkCount < MAXNETWORKS))
938 GetNextToken(&tok, " \n\t");
939 if (tok.token)
941 if (tok.newline) comment = FALSE;
942 if (strncmp(tok.token, "#", 1) == 0) comment = TRUE;
944 if (!comment)
946 if (strncmp(tok.token, "network=", 8) == 0)
948 net = GetNetwork(networkCount);
949 net->adHoc = FALSE;
950 net->hidden = FALSE;
951 networkCount++;
952 SetNetworkCount(networkCount);
954 else if (strncmp(tok.token, "ssid=", 5) == 0)
956 tstring = strchr(tok.token, '=') + 2;
957 *strchr(tstring, '\"') = '\0';
958 SetNetworkName(net, tstring);
960 else if (strncmp(tok.token, "psk=", 4) == 0
961 || strncmp(tok.token, "wep_key0=", 9) == 0)
963 tstring = strchr(tok.token, '=') + 1;
964 if (*tstring == '\"')
966 keyIsHex = FALSE;
967 tstring++;
968 *strchr(tstring, '\"') = '\0';
970 else
971 keyIsHex = TRUE;
972 SetKey(net, tstring, keyIsHex);
973 SetEncType(net, (*tok.token == 'p') ? 2 : 1);
975 else if (strncmp(tok.token, "scan_ssid=", 10) == 0)
977 tstring = strchr(tok.token, '=') + 1;
978 SetHidden(net, *tstring == '1');
980 else if (strncmp(tok.token, "mode=", 5) == 0)
982 tstring = strchr(tok.token, '=') + 1;
983 SetAdHoc(net, *tstring == '1');
988 CloseTokenFile(&tok);
992 void ReadMobilePrefs(CONST_STRPTR directory)
994 ULONG filenamelen = strlen(directory) + 4 + 30;
995 TEXT filename[filenamelen];
996 struct Tokenizer tok;
997 LONG command=0;
999 CombinePath2P(filename, filenamelen, directory, "MobileBroadband.prefs");
1000 OpenTokenFile(&tok, filename);
1001 while (!tok.fend)
1003 GetNextToken(&tok, " \n");
1004 if (tok.token)
1006 if ( tok.newline && tok.token[0] == '#' ) continue;
1008 if (tok.newline)
1010 if (strcasecmp( tok.token, "SEND" ) == 0)
1012 GetNextToken(&tok, "\n");
1013 if ( tok.token && ! tok.newline )
1015 SetMobile_atcommand( command++ , tok.token );
1018 else if (strcasecmp( tok.token, "DEVICE" ) == 0)
1020 GetNextToken(&tok, " \n");
1021 if ( tok.token && ! tok.newline )
1023 SetMobile_devicename( tok.token );
1026 else if (strcasecmp( tok.token, "USERNAME" ) == 0)
1028 GetNextToken(&tok, " \n");
1029 if ( tok.token && ! tok.newline )
1031 SetMobile_username( tok.token );
1034 else if (strcasecmp( tok.token, "PASSWORD" ) == 0)
1036 GetNextToken(&tok, " \n");
1037 if ( tok.token && ! tok.newline )
1039 SetMobile_password( tok.token );
1042 else if (strcasecmp( tok.token, "UNIT" ) == 0)
1044 GetNextToken(&tok, " \n");
1045 if ( tok.token && ! tok.newline )
1047 SetMobile_unit( atoi( tok.token ) );
1053 CloseTokenFile(&tok);
1057 void InitNetworkPrefs(CONST_STRPTR directory, BOOL use, BOOL save)
1059 SetDefaultNetworkPrefsValues();
1060 SetDefaultWirelessPrefsValues();
1061 SetDefaultMobilePrefsValues();
1063 ReadNetworkPrefs(directory);
1064 ReadWirelessPrefs(WIRELESS_PATH_ENV);
1065 ReadMobilePrefs(MOBILEBB_PATH_ENV);
1067 if (save)
1069 SaveNetworkPrefs();
1070 return; /* save equals to use */
1073 if (use)
1075 UseNetworkPrefs();
1079 // check if 'str' contains only characters from 'accept'
1080 BOOL IsLegal(STRPTR str, STRPTR accept)
1082 int i, len;
1084 if ((str == NULL) || (accept == NULL) || (str[0] == '\0'))
1086 return FALSE;
1089 len = strlen(str);
1090 for (i = 0; i < len; i++)
1092 if (strchr(accept, str[i]) == NULL)
1094 return FALSE;
1097 return TRUE;
1101 /* Getters */
1103 struct Interface * GetInterface(LONG index)
1105 return &prefs.interface[index];
1108 STRPTR GetName(struct Interface *iface)
1110 return iface->name;
1113 BOOL GetIfDHCP(struct Interface *iface)
1115 return iface->ifDHCP;
1118 STRPTR GetIP(struct Interface *iface)
1120 return iface->IP;
1123 STRPTR GetMask(struct Interface *iface)
1125 return iface->mask;
1128 STRPTR GetDevice(struct Interface *iface)
1130 return iface->device;
1133 LONG GetUnit(struct Interface *iface)
1135 return iface->unit;
1138 BOOL GetUp(struct Interface *iface)
1140 return iface->up;
1144 STRPTR GetGate(void)
1146 return prefs.gate;
1149 STRPTR GetDNS(LONG m)
1151 return prefs.DNS[m];
1154 STRPTR GetHost(void)
1156 return prefs.host;
1159 STRPTR GetDomain(void)
1161 return prefs.domain;
1164 LONG GetInterfaceCount(void)
1166 return prefs.interfacecount;
1169 BOOL GetAutostart(void)
1171 return prefs.autostart;
1174 BOOL GetDHCP(void)
1176 return prefs.DHCP;
1180 /* Setters */
1182 void SetInterface
1184 struct Interface *iface, STRPTR name, BOOL dhcp, STRPTR IP, STRPTR mask,
1185 STRPTR device, LONG unit, BOOL up
1188 SetName(iface, name);
1189 SetIfDHCP(iface, dhcp);
1190 SetIP(iface, IP);
1191 SetMask(iface, mask);
1192 SetDevice(iface, device);
1193 SetUnit(iface, unit);
1194 SetUp(iface, up);
1197 void SetName(struct Interface *iface, STRPTR w)
1199 if (!IsLegal(w, NAMECHARS))
1201 w = DEFAULTNAME;
1203 strlcpy(iface->name, w, NAMEBUFLEN);
1206 void SetIfDHCP(struct Interface *iface, BOOL w)
1208 iface->ifDHCP = w;
1211 void SetIP(struct Interface *iface, STRPTR w)
1213 if (!IsLegal(w, IPCHARS))
1215 w = DEFAULTIP;
1217 strlcpy(iface->IP, w, IPBUFLEN);
1220 void SetMask(struct Interface *iface, STRPTR w)
1222 if (!IsLegal(w, IPCHARS))
1224 w = DEFAULTMASK;
1226 strlcpy(iface->mask, w, IPBUFLEN);
1229 void SetDevice(struct Interface *iface, STRPTR w)
1231 if (w == NULL || w[0] == '\0')
1233 w = DEFAULTDEVICE;
1235 strlcpy(iface->device, w, NAMEBUFLEN);
1238 void SetUnit(struct Interface *iface, LONG w)
1240 iface->unit = w;
1243 void SetUp(struct Interface *iface, BOOL w)
1245 iface->up = w;
1249 void SetGate(STRPTR w)
1251 if (!IsLegal(w, IPCHARS))
1253 w = DEFAULTGATE;
1255 strlcpy(prefs.gate, w, IPBUFLEN);
1258 void SetDNS(LONG m, STRPTR w)
1260 if (!IsLegal(w, IPCHARS))
1262 w = DEFAULTDNS;
1264 strlcpy(prefs.DNS[m], w, IPBUFLEN);
1267 void SetHost(STRPTR w)
1269 if (!IsLegal(w, NAMECHARS))
1271 w = DEFAULTHOST;
1273 strlcpy(prefs.host, w, NAMEBUFLEN);
1276 void SetDomain(STRPTR w)
1278 if (!IsLegal(w, NAMECHARS))
1280 w = DEFAULTDOMAIN;
1282 strlcpy(prefs.domain, w, NAMEBUFLEN);
1285 void SetInterfaceCount(LONG w)
1287 prefs.interfacecount = w;
1290 void SetAutostart(BOOL w)
1292 prefs.autostart = w;
1295 void SetDHCP(BOOL w)
1297 prefs.DHCP = w;
1300 void InitNetwork(struct Network *net)
1302 SetNetworkName(net, "");
1303 SetKey(net, "", FALSE);
1304 SetEncType(net, 0);
1305 SetAdHoc(net, FALSE);
1309 /* Getters */
1311 struct Network *GetNetwork(LONG index)
1313 return &prefs.networks[index];
1316 STRPTR GetNetworkName(struct Network *net)
1318 return net->name;
1321 UWORD GetEncType(struct Network *net)
1323 return net->encType;
1326 STRPTR GetKey(struct Network *net)
1328 return net->key;
1331 BOOL GetHidden(struct Network *net)
1333 return net->hidden;
1336 BOOL GetAdHoc(struct Network *net)
1338 return net->adHoc;
1341 LONG GetNetworkCount(void)
1343 return prefs.networkCount;
1346 STRPTR GetWirelessDevice(void)
1348 return prefs.wirelessDevice;
1351 LONG GetWirelessUnit(void)
1353 return prefs.wirelessUnit;
1356 BOOL GetMobile_Autostart(void)
1358 return prefs.mobile.autostart;
1361 STRPTR GetMobile_atcommand(ULONG i)
1363 if( i < MAXATCOMMANDS )
1364 return prefs.mobile.atcommand[i];
1365 else return "";
1368 LONG GetMobile_atcommandcount(void)
1370 ULONG count=0;
1371 ULONG i;
1372 for (i=0;i<MAXATCOMMANDS;i++)
1374 if( prefs.mobile.atcommand[i][0] != 0 ) count++;
1376 return count;
1379 STRPTR GetMobile_devicename(void)
1381 return prefs.mobile.devicename;
1384 STRPTR GetMobile_username(void)
1386 return prefs.mobile.username;
1389 STRPTR GetMobile_password(void)
1391 return prefs.mobile.password;
1394 LONG GetMobile_unit(void)
1396 return prefs.mobile.unit;
1399 LONG GetMobile_timeout(void)
1401 return prefs.mobile.timeout;
1404 /* Setters */
1406 void SetNetwork
1408 struct Network *net, STRPTR name, UWORD encType, STRPTR key,
1409 BOOL keyIsHex, BOOL hidden, BOOL adHoc
1412 SetNetworkName(net, name);
1413 SetEncType(net, encType);
1414 SetKey(net, key, keyIsHex);
1415 SetHidden(net, hidden);
1416 SetAdHoc(net, adHoc);
1419 void SetNetworkName(struct Network *net, STRPTR w)
1421 strlcpy(net->name, w, SSIDBUFLEN);
1424 void SetEncType(struct Network *net, UWORD w)
1426 net->encType = w;
1429 void SetKey(struct Network *net, STRPTR w, BOOL keyIsHex)
1431 strlcpy(net->key, w, KEYBUFLEN);
1432 net->keyIsHex = keyIsHex;
1435 void SetHidden(struct Network *net, BOOL w)
1437 net->hidden = w;
1440 void SetAdHoc(struct Network *net, BOOL w)
1442 net->adHoc = w;
1445 void SetNetworkCount(LONG w)
1447 prefs.networkCount = w;
1450 void SetWirelessDevice(STRPTR w)
1452 prefs.wirelessDevice = w;
1455 void SetWirelessUnit(LONG w)
1457 prefs.wirelessUnit = w;
1460 void SetMobile_Autostart(BOOL w)
1462 prefs.mobile.autostart = w;
1465 void SetMobile_atcommand(ULONG i,STRPTR w)
1467 if( strlen(w) < NAMEBUFLEN && i >= 0 && i < MAXATCOMMANDS ){
1468 strcpy(prefs.mobile.atcommand[i], w);
1472 void SetMobile_devicename(STRPTR w)
1474 if( strlen(w) < NAMEBUFLEN ) strcpy( prefs.mobile.devicename , w );
1477 void SetMobile_username(STRPTR w)
1479 if( strlen(w) < NAMEBUFLEN ) strcpy( prefs.mobile.username , w );
1482 void SetMobile_password(STRPTR w)
1484 if( strlen(w) < NAMEBUFLEN ) strcpy( prefs.mobile.password , w );
1487 void SetMobile_unit(LONG w)
1489 prefs.mobile.unit = w;
1492 void SetMobile_timeout(LONG w)
1494 prefs.mobile.timeout = w;