Hint added.
[AROS.git] / workbench / prefs / network / prefsdata.c
blob314beca9b6a4a615477c2786f8e576993514c2f7
1 /*
2 Copyright © 2009-2014, 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 #define EX_BUF_SIZE 256
17 enum
19 ARG_HANDLER,
20 ARG_EHANDLER,
21 ARG_FILESYSTEM,
22 ARG_DEVICE,
23 ARG_UNIT,
24 ARG_FLAGS,
25 ARG_BLOCKSIZE,
26 ARG_SURFACES,
27 ARG_BLOCKSPERTRACK,
28 ARG_SECTORSPERBLOCK,
29 ARG_RESERVED,
30 ARG_PREALLOC,
31 ARG_INTERLEAVE,
32 ARG_LOWCYL,
33 ARG_HIGHCYL,
34 ARG_BUFFERS,
35 ARG_BUFMEMTYPE,
36 ARG_MAXTRANSFER,
37 ARG_MASK,
38 ARG_BOOTPRI,
39 ARG_DOSTYPE,
40 ARG_BAUD,
41 ARG_CONTROL,
42 ARG_STACKSIZE,
43 ARG_PRIORITY,
44 ARG_GLOBVEC,
45 ARG_STARTUP,
46 ARG_ACTIVATE,
47 ARG_FORCELOAD,
48 NUM_MOUNTARGS
51 static const TEXT mount_template[] =
52 "HANDLER/K,"
53 "EHANDLER/K,"
54 "FILESYSTEM/K,"
55 "DEVICE/K,"
56 "UNIT/K,"
57 "FLAGS/K,"
58 "SECTORSIZE=BLOCKSIZE/K,"
59 "SURFACES/K,"
60 "SECTORSPERTRACK=BLOCKSPERTRACK/K,"
61 "SECTORSPERBLOCK/K,"
62 "RESERVED/K,"
63 "PREALLOC/K,"
64 "INTERLEAVE/K,"
65 "LOWCYL/K,"
66 "HIGHCYL/K,"
67 "BUFFERS/K,"
68 "BUFMEMTYPE/K,"
69 "MAXTRANSFER/K,"
70 "MASK/K,"
71 "BOOTPRI/K,"
72 "DOSTYPE/K,"
73 "BAUD/K,"
74 "CONTROL/K,"
75 "STACKSIZE/K,"
76 "PRIORITY/K,"
77 "GLOBVEC/K,"
78 "STARTUP/K,"
79 "MOUNT=ACTIVATE/K,"
80 "FORCELOAD/K";
82 enum
84 ARG_WORKGROUP,
85 ARG_USERNAME,
86 ARG_PASSWORD,
87 ARG_CHANGECASE,
88 ARG_CASESENSITIVE,
89 ARG_OMITHIDDEN,
90 ARG_QUIET,
91 ARG_CLIENTNAME,
92 ARG_SERVERNAME,
93 ARG_DEVICENAME,
94 ARG_VOLUMENAME,
95 ARG_CACHESIZE,
96 ARG_DEBUGLEVEL,
97 ARG_TIMEZONEOFFSET,
98 ARG_DSTOFFSET,
99 ARG_TRANSLATIONFILE,
100 ARG_SERVICE,
101 NUM_CONTROLARGS
104 static const TEXT control_template[] =
105 "DOMAIN=WORKGROUP/K,"
106 "USER=USERNAME/K,"
107 "PASSWORD/K,"
108 "CHANGECASE/S,"
109 "CASE=CASESENSITIVE/S,"
110 "OMITHIDDEN/S,"
111 "QUIET/S,"
112 "CLIENT=CLIENTNAME/K,"
113 "SERVER=SERVERNAME/K,"
114 "DEVICE=DEVICENAME/K,"
115 "VOLUME=VOLUMENAME/K,"
116 "CACHE=CACHESIZE/N/K,"
117 "DEBUGLEVEL=DEBUG/N/K,"
118 "TZ=TIMEZONEOFFSET/N/K,"
119 "DST=DSTOFFSET/N/K,"
120 "TRANSLATE=TRANSLATIONFILE/K,"
121 "SERVICE/A";
123 static struct TCPPrefs prefs;
125 struct Tokenizer
127 STRPTR tokenizerLine;
128 STRPTR token;
129 FILE * tokenizedFile;
130 BOOL newline;
131 BOOL fend;
134 /* List of devices that require NOTRACKING option */
135 static STRPTR notrackingdevices[] = {"prm-rtl8029.device", NULL};
137 static BOOL ReadServer(struct Server *server, BPTR file, LONG size);
138 static CONST_STRPTR GetActiveServers();
140 void OpenTokenFile(struct Tokenizer * tok, STRPTR FileName)
142 tok->tokenizedFile = fopen(FileName, "r");
143 tok->token = NULL;
144 tok->newline = TRUE;
145 if (!tok->tokenizedFile)
146 tok->fend = TRUE;
147 else
149 tok->tokenizerLine = malloc(8192);
150 tok->fend = FALSE;
151 tok->newline = TRUE;
155 void CloseTokenFile(struct Tokenizer * tok)
157 if (tok->tokenizedFile)
159 fclose(tok->tokenizedFile);
160 free(tok->tokenizerLine);
161 tok->fend = TRUE;
162 tok->token = NULL;
166 void GetNextToken(struct Tokenizer * tok, STRPTR tk)
168 tok->newline = FALSE;
169 if (tok->token != NULL)
171 tok->token = strtok(NULL, tk);
172 if (!tok->token)
173 GetNextToken(tok, tk);
175 else
177 tok->newline = TRUE;
178 if (!feof(tok->tokenizedFile))
180 tok->tokenizerLine[0] = 0;
181 fgets(tok->tokenizerLine, 8192, tok->tokenizedFile);
182 if (tok->tokenizerLine == NULL)
184 tok->token = NULL;
185 GetNextToken(tok, tk);
187 else
188 tok->token = strtok(tok->tokenizerLine, tk);
190 else
191 tok->fend = TRUE;
195 void SetDefaultNetworkPrefsValues()
197 LONG i;
198 for (i = 0; i < MAXINTERFACES; i++)
200 InitInterface(GetInterface(i));
202 SetInterfaceCount(0);
203 SetDomain(DEFAULTDOMAIN);
204 SetHostname(DEFAULTHOST);
205 SetGate(DEFAULTGATE);
206 SetDNS(0, DEFAULTDNS);
207 SetDNS(1, DEFAULTDNS);
208 SetDHCP(FALSE);
210 SetAutostart(FALSE);
212 for (i = 0; i < MAXHOSTS; i++)
214 InitHost(GetHost(i));
216 SetHostCount(0);
219 void SetDefaultWirelessPrefsValues()
221 LONG i;
222 for (i = 0; i < MAXNETWORKS; i++)
224 InitNetwork(GetNetwork(i));
226 SetNetworkCount(0);
228 SetWirelessDevice(NULL);
231 void SetDefaultMobilePrefsValues()
233 LONG i;
234 for (i = 0; i < MAXATCOMMANDS; i++)
236 prefs.mobile.atcommand[i][0] = 0;
238 SetMobile_atcommand(0,"AT+CGDCONT=1,\"IP\",\"insert.your.apn.here\"");
239 SetMobile_atcommand(1,"ATDT*99***1#");
240 SetMobile_timeout( 10 );
241 SetMobile_Autostart(FALSE);
242 SetMobile_devicename( "usbmodem.device" );
243 SetMobile_unit( 0 );
244 SetMobile_username("");
245 SetMobile_password("");
248 void InitInterface(struct Interface *iface)
250 SetName(iface, DEFAULTNAME);
251 SetIfDHCP(iface, TRUE);
252 SetIP(iface, DEFAULTIP);
253 SetMask(iface, DEFAULTMASK);
254 SetDevice(iface, DEFAULTDEVICE);
255 SetUnit(iface, 0);
256 SetUp(iface, FALSE);
259 /* Returns TRUE if directory has been created or already existed */
260 BOOL RecursiveCreateDir(CONST_STRPTR dirpath)
262 /* Will create directory even if top level directory does not exist */
264 BPTR lock = BNULL;
265 ULONG lastdirseparator = 0;
266 ULONG dirpathlen = strlen(dirpath);
267 STRPTR tmpdirpath = AllocVec(dirpathlen + 2, MEMF_CLEAR | MEMF_PUBLIC);
269 CopyMem(dirpath, tmpdirpath, dirpathlen);
271 /* Recurvice directory creation */
272 while (TRUE)
274 if (lastdirseparator >= dirpathlen) break;
276 for (; lastdirseparator < dirpathlen; lastdirseparator++)
277 if (tmpdirpath[lastdirseparator] == '/') break;
279 tmpdirpath[lastdirseparator] = '\0'; /* cut */
281 /* Unlock any lock from previous interation. Last iteration lock will be returned. */
282 if (lock != BNULL)
284 UnLock(lock);
285 lock = BNULL;
288 /* Check if directory exists */
289 lock = Lock(tmpdirpath, SHARED_LOCK);
290 if (lock == BNULL)
292 lock = CreateDir(tmpdirpath);
293 if (lock == BNULL)
294 break; /* Error with creation */
297 tmpdirpath[lastdirseparator] = '/'; /* restore */
298 lastdirseparator++;
301 FreeVec(tmpdirpath);
303 if (lock == BNULL)
304 return FALSE;
305 else
307 UnLock(lock);
308 lock = BNULL;
309 return TRUE;
313 /* Returns TRUE if selected device needs to use NOTRACKING option */
314 BOOL GetNoTracking(struct Interface *iface)
316 STRPTR devicename = NULL;
317 LONG pos = 0;
318 TEXT devicepath[strlen(GetDevice(iface)) + 1];
319 strcpy(devicepath, GetDevice(iface));
320 strupr(devicepath);
322 while ((devicename = notrackingdevices[pos++]) != NULL)
324 /* Comparison is done on upper case string so it is case insensitive */
325 strupr(devicename);
326 if (strstr(devicepath, devicename) != NULL)
327 return TRUE;
330 return FALSE;
333 /* Puts part 1 into empty buffer */
334 VOID CombinePath1P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1)
336 dstbuffer[0] = '\0'; /* Make sure buffer is treated as empty */
337 AddPart(dstbuffer, part1, dstbufferlen);
340 /* Combines part1 with part2 into an empty buffer */
341 VOID CombinePath2P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1, CONST_STRPTR part2)
343 CombinePath1P(dstbuffer, dstbufferlen, part1);
344 AddPart(dstbuffer, part2, dstbufferlen);
347 /* Combines part1 with part2 with part3 into an empty buffer */
348 VOID CombinePath3P(STRPTR dstbuffer, ULONG dstbufferlen, CONST_STRPTR part1, CONST_STRPTR part2, CONST_STRPTR part3)
350 CombinePath2P(dstbuffer, dstbufferlen, part1, part2);
351 AddPart(dstbuffer, part3, dstbufferlen);
354 BOOL WriteNetworkPrefs(CONST_STRPTR destdir)
356 FILE *ConfFile;
357 LONG i;
358 struct Interface *iface;
359 struct Host *host;
360 ULONG filenamelen = strlen(destdir) + 4 + 20;
361 TEXT filename[filenamelen];
362 ULONG destdbdirlen = strlen(destdir) + 3 + 1;
363 TEXT destdbdir[destdbdirlen];
364 LONG interfacecount = GetInterfaceCount();
365 LONG hostcount = GetHostCount();
367 CombinePath2P(destdbdir, destdbdirlen, destdir, "db");
369 /* Create necessary directories */
370 if (!RecursiveCreateDir(destdir)) return FALSE;
371 if (!RecursiveCreateDir(destdbdir)) return FALSE;
373 /* Write configuration files */
374 CombinePath2P(filename, filenamelen, destdbdir, "general.config");
375 ConfFile = fopen(filename, "w");
376 if (!ConfFile) return FALSE;
377 fprintf(ConfFile, "USELOOPBACK=YES\n");
378 fprintf(ConfFile, "DEBUGSANA=NO\n");
379 fprintf(ConfFile, "USENS=SECOND\n");
380 fprintf(ConfFile, "GATEWAY=NO\n");
381 fprintf(ConfFile, "HOSTNAME=%s.%s\n", GetHostname(), GetDomain());
382 fprintf(ConfFile, "LOG FILTERFILE=5\n");
383 fprintf(ConfFile, "GUI PANEL=MUI\n");
384 fprintf(ConfFile, "OPENGUI=YES\n");
385 fclose(ConfFile);
387 CombinePath2P(filename, filenamelen, destdbdir, "interfaces");
388 ConfFile = fopen(filename, "w");
389 if (!ConfFile) return FALSE;
390 for (i = 0; i < interfacecount; i++)
392 iface = GetInterface(i);
393 fprintf
395 ConfFile, "%s DEV=%s UNIT=%d %s IP=%s NETMASK=%s %s\n",
396 GetName(iface), GetDevice(iface), (int)GetUnit(iface),
397 (GetNoTracking(iface) ? (CONST_STRPTR)"NOTRACKING" : (CONST_STRPTR)""),
398 (GetIfDHCP(iface) ?
399 (strstr(GetDevice(iface), "ppp.device") == NULL ?
400 (CONST_STRPTR)"DHCP" : (CONST_STRPTR)"0.0.0.0") :
401 GetIP(iface)),
402 GetMask(iface),
403 (GetUp(iface) ? (CONST_STRPTR)"UP" : (CONST_STRPTR)"")
405 if (strstr(GetDevice(iface), "atheros5000.device") != NULL
406 || strstr(GetDevice(iface), "prism2.device") != NULL
407 || strstr(GetDevice(iface), "realtek8180.device") != NULL)
409 SetWirelessDevice(GetDevice(iface));
410 SetWirelessUnit(GetUnit(iface));
412 else if (strstr(GetDevice(iface), "ppp.device") != NULL)
413 SetMobile_Autostart(TRUE);
415 fclose(ConfFile);
417 CombinePath2P(filename, filenamelen, destdbdir, "netdb-myhost");
418 ConfFile = fopen(filename, "w");
419 if (!ConfFile) return FALSE;
421 for (i = 0; i < interfacecount; i++)
423 iface = GetInterface(i);
424 if (!GetIfDHCP(iface))
426 fprintf
428 ConfFile, "HOST %s %s.%s %s\n",
429 GetIP(iface), GetHostname(), GetDomain(), GetHostname()
434 if (!GetDHCP())
436 // FIXME: old version wrote Gateway even when DHCP was enabled
437 fprintf(ConfFile, "HOST %s gateway\n", GetGate());
438 fprintf(ConfFile, "; Domain names\n");
439 fprintf(ConfFile, "; Name servers\n");
440 fprintf(ConfFile, "NAMESERVER %s\n", GetDNS(0));
441 fprintf(ConfFile, "NAMESERVER %s\n", GetDNS(1));
443 fclose(ConfFile);
445 CombinePath2P(filename, filenamelen, destdbdir, "static-routes");
446 ConfFile = fopen(filename, "w");
447 if (!ConfFile) return FALSE;
448 if (!GetDHCP())
450 // FIXME: old version wrote Gateway even when DHCP was enabled
451 fprintf(ConfFile, "DEFAULT GATEWAY %s\n", GetGate());
453 fclose(ConfFile);
455 /* Write variables */
456 CombinePath2P(filename, filenamelen, destdir, "Config");
457 ConfFile = fopen(filename, "w");
458 if (!ConfFile) return FALSE;
459 fprintf(ConfFile, "%s/db", PREFS_PATH_ENV);
460 fclose(ConfFile);
462 CombinePath2P(filename, filenamelen, destdir, "AutoRun");
463 ConfFile = fopen(filename, "w");
464 if (!ConfFile) return FALSE;
465 fprintf(ConfFile, "%s", (GetAutostart()) ? "True" : "False");
466 fclose(ConfFile);
468 CombinePath2P(filename, filenamelen, destdir, "MobileAutorun");
469 ConfFile = fopen(filename, "w");
470 if (!ConfFile) return FALSE;
471 fprintf(ConfFile, "%s", (GetMobile_Autostart()) ? "True" : "False");
472 fclose(ConfFile);
474 CombinePath2P(filename, filenamelen, destdir, "WirelessAutoRun");
475 ConfFile = fopen(filename, "w");
476 if (!ConfFile) return FALSE;
477 fprintf(ConfFile, "%s", (GetWirelessDevice() != NULL) ? "True" : "False");
478 fclose(ConfFile);
480 if (GetWirelessDevice() != NULL)
482 CombinePath2P(filename, filenamelen, destdir, "WirelessDevice");
483 ConfFile = fopen(filename, "w");
484 if (!ConfFile) return FALSE;
485 fprintf(ConfFile, "%s UNIT %ld", GetWirelessDevice(),
486 (long int)GetWirelessUnit());
487 fclose(ConfFile);
490 CombinePath2P(filename, filenamelen, destdbdir, "hosts");
491 ConfFile = fopen(filename, "w");
492 if (!ConfFile) return FALSE;
493 for (i = 0; i < hostcount; i++)
495 host = GetHost(i);
496 fprintf
498 ConfFile, "%s %s\n",
499 GetHostAddress(host), GetHostNames(host)
502 fclose(ConfFile);
504 return TRUE;
507 BOOL WriteWirelessPrefs(CONST_STRPTR destdir)
509 FILE *ConfFile;
510 LONG i;
511 struct Network *net;
512 ULONG filenamelen = strlen(destdir) + 4 + 20;
513 TEXT filename[filenamelen];
515 /* Write wireless config */
516 CombinePath2P(filename, filenamelen, destdir, "Wireless.prefs");
517 if (prefs.networkCount > 0)
519 ConfFile = fopen(filename, "w");
520 if (!ConfFile) return FALSE;
522 for (i = 0; i < prefs.networkCount; i++)
524 net = &prefs.networks[i];
525 fprintf(ConfFile, "network={\n");
526 if (net->name[0] != '\0')
527 fprintf(ConfFile, "\tssid=\"%s\"\n", net->name);
528 switch (net->encType)
530 case 2:
531 if (net->keyIsHex)
532 fprintf(ConfFile, "\tpsk=%s\n", net->key);
533 else
534 fprintf(ConfFile, "\tpsk=\"%s\"\n", net->key);
535 fprintf(ConfFile, "\tkey_mgmt=WPA-PSK\n");
536 break;
537 case 1:
538 if (net->keyIsHex)
539 fprintf(ConfFile, "\twep_key0=%s\n", net->key);
540 else
541 fprintf(ConfFile, "\twep_key0=\"%s\"\n", net->key);
542 fprintf(ConfFile, "\twep_tx_keyidx=0\n");
543 default:
544 fprintf(ConfFile, "\tkey_mgmt=NONE\n");
546 if (net->hidden)
547 fprintf(ConfFile, "\tscan_ssid=1\n");
548 if (net->adHoc)
549 fprintf(ConfFile, "\tmode=1\n");
550 fprintf(ConfFile, "}\n\n");
553 fclose(ConfFile);
555 else
556 DeleteFile(filename);
558 return TRUE;
562 BOOL WriteMobilePrefs(CONST_STRPTR destdir)
564 FILE *ConfFile;
565 LONG i;
566 ULONG filenamelen = strlen(destdir) + 4 + 30;
567 TEXT filename[filenamelen];
569 CombinePath2P(filename, filenamelen, destdir, "MobileBroadband.prefs");
570 ConfFile = fopen(filename, "w");
571 if (!ConfFile) return FALSE;
573 if (strlen(GetMobile_devicename()) > 0)
574 fprintf(ConfFile, "DEVICE %s\n", GetMobile_devicename());
575 fprintf(ConfFile, "UNIT %d\n" , (int)GetMobile_unit() );
576 if (strlen(GetMobile_username()) > 0)
577 fprintf(ConfFile, "USERNAME %s\n", GetMobile_username());
578 if (strlen(GetMobile_password()) > 0)
579 fprintf(ConfFile, "PASSWORD %s\n", GetMobile_password());
581 for (i = 0; i < MAXATCOMMANDS; i++)
583 if (strlen( GetMobile_atcommand(i) ) > 0)
584 fprintf(ConfFile, "SEND %s\n", GetMobile_atcommand(i));
587 fclose(ConfFile);
589 return TRUE;
593 BOOL WriteServers(CONST_STRPTR destdir, CONST_STRPTR envdir)
595 FILE *mount_file, *env_file;
596 LONG i;
597 struct Server *server;
598 ULONG filenamelen = strlen(destdir) + 4 + 20;
599 TEXT filename[filenamelen];
601 CombinePath2P(filename, filenamelen, envdir, "ServerAutoMounts");
602 env_file = fopen(filename, "w");
603 if (!env_file) return FALSE;
605 for (i = 0; i < prefs.serverCount; i++)
607 server = &prefs.servers[i];
608 CombinePath2P(filename, filenamelen, destdir, server->device);
609 mount_file = fopen(filename, "w");
610 if (!mount_file)
612 fclose(env_file);
613 return FALSE;
616 fprintf(mount_file, "EHandler = " SERVER_HANDLER "\nActivate = 1\n");
617 fprintf(mount_file, "Control = \"");
618 if (server->user[0] != '\0')
619 fprintf(mount_file, "USER=*\"%s*\" ", server->user);
620 if (server->group[0] != '\0')
621 fprintf(mount_file, "WORKGROUP=*\"%s*\" ", server->group);
622 if (server->pass[0] != '\0')
623 fprintf(mount_file, "PASSWORD=*\"%s*\" ", server->pass);
624 fprintf(mount_file, "SERVICE=*\"//%s/%s*\"\"\n", server->host,
625 server->service);
626 fclose(mount_file);
628 if (server->active)
629 fprintf(env_file, "%s: ", server->device);
632 fclose(env_file);
634 return TRUE;
638 #define BUFSIZE 2048
639 BOOL CopyFile(CONST_STRPTR srcfile, CONST_STRPTR dstfile)
641 BPTR from = BNULL, to = BNULL;
642 TEXT buffer[BUFSIZE];
644 if ((from = Open(srcfile, MODE_OLDFILE)))
646 if ((to = Open(dstfile, MODE_NEWFILE)))
648 LONG s = 0;
652 if ((s = Read(from, buffer, BUFSIZE)) == -1)
654 Close(to);
655 Close(from);
656 return FALSE;
659 if (Write(to, buffer, s) == -1)
661 Close(to);
662 Close(from);
663 return FALSE;
665 } while (s == BUFSIZE);
667 Close(to);
668 Close(from);
669 return TRUE;
672 Close(from);
675 return FALSE;
678 CONST_STRPTR GetDefaultStackLocation()
680 /* Use static variable so that it is initialized only once (and can be returned) */
681 static TEXT path [1024] = {0};
683 /* Load path if needed - this will happen only once */
684 if (path[0] == '\0')
686 GetVar(AROSTCP_PACKAGE_VARIABLE, path, 1024, LV_VAR);
689 return path;
692 BOOL IsStackRunning()
694 return FindTask("bsdsocket.library") != NULL;
697 BOOL RestartStack()
699 ULONG trycount = 0;
701 /* Shutdown */
702 if (IsStackRunning())
704 struct Task * arostcptask = FindTask("bsdsocket.library");
705 if (arostcptask != NULL)
706 Signal(arostcptask, SIGBREAKF_CTRL_C);
709 /* Check if shutdown successful */
710 trycount = 0;
711 while (IsStackRunning())
713 if (trycount > 4) return FALSE;
714 Delay(50);
715 trycount++;
718 /* Startup */
720 CONST_STRPTR srcdir = GetDefaultStackLocation();
721 ULONG arostcppathlen = strlen(srcdir) + 3 + 20;
722 TEXT arostcppath[arostcppathlen];
723 struct TagItem tags[] =
725 { SYS_Input, (IPTR)NULL },
726 { SYS_Output, (IPTR)NULL },
727 { SYS_Error, (IPTR)NULL },
728 { SYS_Asynch, (IPTR)TRUE },
729 { TAG_DONE, 0 }
732 CombinePath3P(arostcppath, arostcppathlen, srcdir, "C", "AROSTCP");
734 SystemTagList(arostcppath, tags);
737 /* Check if startup successful */
738 trycount = 0;
739 while (!IsStackRunning())
741 if (trycount > 9) return FALSE;
742 Delay(50);
743 trycount++;
746 /* All ok */
747 return TRUE;
750 BOOL StopWireless()
752 ULONG trycount = 0;
754 /* Shutdown */
756 struct Task *task = FindTask("C:WirelessManager");
757 if (task != NULL)
758 Signal(task, SIGBREAKF_CTRL_C);
761 /* Check if shutdown successful */
762 trycount = 0;
763 while (FindTask("C:WirelessManager") != NULL)
765 if (trycount > 4) return FALSE;
766 Delay(50);
767 trycount++;
770 /* All ok */
771 return TRUE;
774 BOOL StartWireless()
776 ULONG trycount = 0;
777 TEXT command[80];
779 /* Startup */
781 struct TagItem tags[] =
783 { SYS_Input, (IPTR)NULL },
784 { SYS_Output, (IPTR)NULL },
785 { SYS_Error, (IPTR)NULL },
786 { SYS_Asynch, (IPTR)TRUE },
787 { TAG_DONE, 0 }
790 snprintf(command, 80, "C:WirelessManager \"%s\" UNIT %ld\n",
791 GetWirelessDevice(), (long int)GetWirelessUnit());
792 SystemTagList(command, tags);
795 /* Check if startup successful */
796 trycount = 0;
797 while (FindTask("C:WirelessManager") == NULL)
799 if (trycount > 9) return FALSE;
800 Delay(50);
801 trycount++;
804 /* All ok */
805 return TRUE;
808 BOOL StopMobile()
810 ULONG trycount = 0;
812 /* Shutdown */
814 struct Task *task = FindTask("C:ModemManager");
815 if (task != NULL)
816 Signal(task, SIGBREAKF_CTRL_C);
819 /* Check if shutdown successful */
820 trycount = 0;
821 while (FindTask("C:ModemManager") != NULL)
823 if (trycount > 4) return FALSE;
824 Delay(50);
825 trycount++;
828 /* All ok */
829 return TRUE;
832 BOOL StartMobile()
834 ULONG trycount = 0;
836 /* Startup */
838 struct TagItem tags[] =
840 { SYS_Input, (IPTR)NULL },
841 { SYS_Output, (IPTR)NULL },
842 { SYS_Error, (IPTR)NULL },
843 { SYS_Asynch, (IPTR)TRUE },
844 { TAG_DONE, 0 }
847 SystemTagList("C:ModemManager", tags);
850 /* Check if startup successful */
851 trycount = 0;
852 while (FindTask("C:ModemManager") == NULL)
854 if (trycount > 9) return FALSE;
855 Delay(50);
856 trycount++;
859 /* All ok */
860 return TRUE;
863 static CONST_STRPTR GetActiveServers()
865 /* Use static variable so that it is initialized only once (and can be returned) */
866 static TEXT servers [256] = {0};
868 /* Load variable if needed - this will happen only once */
869 if (servers[0] == '\0')
871 GetVar(AUTOMOUNT_VARIABLE, servers, 256, LV_VAR);
874 return servers;
877 BOOL MountServers()
879 BPTR dir;
881 dir = Lock(SERVER_PATH_ENV, SHARED_LOCK);
882 if (dir == BNULL)
883 return FALSE;
885 /* Startup */
886 if (GetServerCount() > 0)
888 struct TagItem tags[] =
890 { SYS_Input, (IPTR)NULL },
891 { SYS_Output, (IPTR)NULL },
892 { SYS_Error, (IPTR)NULL },
893 { SYS_Asynch, (IPTR)TRUE },
894 { NP_CurrentDir, (IPTR)dir },
895 { TAG_DONE, 0 }
898 SystemTagList("C:Mount ${AROSTCP/ServerAutoMounts}\n", tags);
901 /* All ok */
902 return TRUE;
905 /* This is not a general use function! It assumes destinations directory exists */
906 BOOL AddFileFromDefaultStackLocation(CONST_STRPTR filename, CONST_STRPTR dstdir)
908 /* Build paths */
909 CONST_STRPTR srcdir = GetDefaultStackLocation();
910 ULONG srcfilelen = strlen(srcdir) + 4 + strlen(filename) + 1;
911 TEXT srcfile[srcfilelen];
912 ULONG dstfilelen = strlen(dstdir) + 4 + strlen(filename) + 1;
913 TEXT dstfile[dstfilelen];
914 BPTR dstlock = BNULL;
916 CombinePath3P(srcfile, srcfilelen, srcdir, "db", filename);
917 CombinePath3P(dstfile, dstfilelen, dstdir, "db", filename);
919 /* Check if the destination file already exists. If yes, do not copy */
920 dstlock = Lock(dstfile, SHARED_LOCK);
921 if (dstlock != BNULL)
923 UnLock(dstlock);
924 return TRUE;
927 return CopyFile(srcfile, dstfile);
930 /* Copies files not created by prefs but needed to start stack */
931 BOOL CopyDefaultConfiguration(CONST_STRPTR destdir)
933 ULONG destdbdirlen = strlen(destdir) + 3 + 1;
934 TEXT destdbdir[destdbdirlen];
935 CombinePath2P(destdbdir, destdbdirlen, destdir, "db");
937 /* Create necessary directories */
938 if (!RecursiveCreateDir(destdir)) return FALSE;
939 if (!RecursiveCreateDir(destdbdir)) return FALSE;
941 /* Copy files */
942 if (!AddFileFromDefaultStackLocation("inet.access", destdir)) return FALSE;
943 if (!AddFileFromDefaultStackLocation("netdb", destdir)) return FALSE;
944 if (!AddFileFromDefaultStackLocation("networks", destdir)) return FALSE;
945 if (!AddFileFromDefaultStackLocation("protocols", destdir)) return FALSE;
946 if (!AddFileFromDefaultStackLocation("services", destdir)) return FALSE;
948 return TRUE;
951 enum ErrorCode SaveNetworkPrefs()
953 if (!CopyDefaultConfiguration(PREFS_PATH_ENVARC)) return NOT_COPIED_FILES_ENVARC;
954 if (!WriteNetworkPrefs(PREFS_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
955 if (!WriteWirelessPrefs(WIRELESS_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
956 if (!WriteMobilePrefs(MOBILEBB_PATH_ENVARC)) return NOT_SAVED_PREFS_ENVARC;
957 if (!WriteServers(SERVER_PATH_STORAGE, PREFS_PATH_ENVARC))
958 return NOT_SAVED_PREFS_ENVARC;
960 return UseNetworkPrefs();
963 enum ErrorCode UseNetworkPrefs()
965 if (!CopyDefaultConfiguration(PREFS_PATH_ENV)) return NOT_COPIED_FILES_ENV;
966 if (!WriteNetworkPrefs(PREFS_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
967 if (!WriteWirelessPrefs(WIRELESS_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
968 if (!WriteMobilePrefs(MOBILEBB_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
969 if (!RecursiveCreateDir(SERVER_PATH_ENV)) return NOT_SAVED_PREFS_ENV;
970 if (!WriteServers(SERVER_PATH_ENV, PREFS_PATH_ENV))
971 return NOT_SAVED_PREFS_ENV;
973 if (StopWireless())
974 if (GetWirelessDevice() != NULL)
975 if (!StartWireless()) return NOT_RESTARTED_WIRELESS;
976 if (!RestartStack()) return NOT_RESTARTED_STACK;
977 if (StopMobile())
978 if (GetMobile_Autostart())
979 if (!StartMobile()) return NOT_RESTARTED_MOBILE;
980 MountServers();
982 return ALL_OK;
985 /* Directory points to top of config, so to AAA/AROSTCP not to AAA/AROSTCP/db */
986 void ReadNetworkPrefs(CONST_STRPTR directory)
988 ULONG filenamelen = strlen(directory) + 4 + 20;
989 TEXT filename[filenamelen];
990 BOOL comment = FALSE;
991 STRPTR tstring;
992 struct Tokenizer tok;
993 LONG interfacecount, hostcount;
994 struct Interface *iface = NULL;
995 struct Host *host = NULL;
997 /* This function will not fail. It will load as much data as possible. Rest will be default values */
999 SetDHCP(FALSE);
1001 CombinePath3P(filename, filenamelen, directory, "db", "general.config");
1002 OpenTokenFile(&tok, filename);
1003 while (!tok.fend)
1005 if (tok.newline)
1006 { // read tokens from the beginning of line
1007 if (tok.token)
1009 if (strcmp(tok.token, "HOSTNAME") == 0)
1011 GetNextToken(&tok, "=\n");
1012 tstring = strchr(tok.token, '.');
1013 SetDomain(tstring + 1);
1014 tstring[0] = 0;
1015 SetHostname(tok.token);
1019 GetNextToken(&tok, "=\n");
1021 CloseTokenFile(&tok);
1023 CombinePath3P(filename, filenamelen, directory, "db", "interfaces");
1024 OpenTokenFile(&tok, filename);
1026 SetInterfaceCount(0);
1027 interfacecount = 0;
1029 while (!tok.fend && (interfacecount < MAXINTERFACES))
1031 GetNextToken(&tok, " \n");
1032 if (tok.token)
1034 if (tok.newline) comment = FALSE;
1035 if (strncmp(tok.token, "#", 1) == 0) comment = TRUE;
1037 if (!comment)
1039 if (tok.newline)
1041 iface = GetInterface(interfacecount);
1042 SetName(iface, tok.token);
1043 interfacecount++;
1044 SetInterfaceCount(interfacecount);
1046 else if (strncmp(tok.token, "DEV=", 4) == 0)
1048 tstring = strchr(tok.token, '=');
1049 SetDevice(iface, tstring + 1);
1051 else if (strncmp(tok.token, "UNIT=", 5) == 0)
1053 tstring = strchr(tok.token, '=');
1054 SetUnit(iface, atol(tstring + 1));
1056 else if (strncmp(tok.token, "IP=", 3) == 0)
1058 tstring = strchr(tok.token, '=');
1059 if (strncmp(tstring + 1, "DHCP", 4) == 0
1060 || strstr(GetDevice(iface), "ppp.device") != NULL)
1062 SetIfDHCP(iface, TRUE);
1063 SetIP(iface, DEFAULTIP);
1065 else
1067 SetIP(iface, tstring + 1);
1068 SetIfDHCP(iface, FALSE);
1071 else if (strncmp(tok.token, "NETMASK=", 8) == 0)
1073 tstring = strchr(tok.token, '=');
1074 SetMask(iface, tstring + 1);
1076 else if (strncmp(tok.token, "UP", 2) == 0)
1078 SetUp(iface, TRUE);
1083 CloseTokenFile(&tok);
1085 CombinePath3P(filename, filenamelen, directory, "db", "netdb-myhost");
1086 OpenTokenFile(&tok, filename);
1087 int dnsc = 0;
1088 while (!tok.fend)
1090 GetNextToken(&tok, " \n");
1091 if (tok.token)
1093 // Host and Domain are already read from general.config
1094 if (strncmp(tok.token, "NAMESERVER", 10) == 0)
1096 GetNextToken(&tok, " \n");
1097 SetDNS(dnsc, tok.token);
1098 dnsc++;
1099 if (dnsc > 1) dnsc = 1;
1103 // Assume DHCP if there is no nameserver
1104 if (dnsc == 0)
1106 SetDHCP(TRUE);
1108 CloseTokenFile(&tok);
1110 CombinePath3P(filename, filenamelen, directory, "db", "static-routes");
1111 OpenTokenFile(&tok, filename);
1112 while (!tok.fend)
1114 GetNextToken(&tok, " \n");
1115 if (tok.token)
1117 if (strncmp(tok.token, "DEFAULT", 7) == 0)
1119 GetNextToken(&tok, " \n");
1120 if (strncmp(tok.token, "GATEWAY", 7) == 0)
1122 GetNextToken(&tok, " \n");
1123 SetGate(tok.token);
1128 CloseTokenFile(&tok);
1130 CombinePath2P(filename, filenamelen, directory, "Autorun");
1131 OpenTokenFile(&tok, filename);
1132 while (!tok.fend)
1134 GetNextToken(&tok, " \n");
1135 if (tok.token)
1137 if (strncmp(tok.token, "True", 4) == 0)
1139 SetAutostart(TRUE);
1140 break;
1142 else
1144 SetAutostart(FALSE);
1145 break;
1149 CloseTokenFile(&tok);
1151 CombinePath3P(filename, filenamelen, directory, "db", "hosts");
1152 OpenTokenFile(&tok, filename);
1154 SetHostCount(0);
1155 hostcount = 0;
1157 while (!tok.fend && (hostcount < MAXHOSTS))
1159 GetNextToken(&tok, " \n");
1160 if (tok.token)
1162 if (tok.newline) comment = FALSE;
1163 if (strncmp(tok.token, "#", 1) == 0) comment = TRUE;
1165 if (!comment)
1167 if (tok.newline)
1169 host = GetHost(hostcount);
1170 SetHostAddress(host, tok.token);
1171 hostcount++;
1172 SetHostCount(hostcount);
1174 else
1176 AddHostName(host, tok.token);
1181 CloseTokenFile(&tok);
1184 void ReadWirelessPrefs(CONST_STRPTR directory)
1186 ULONG filenamelen = strlen(directory) + 4 + 20;
1187 TEXT filename[filenamelen];
1188 BOOL comment = FALSE;
1189 STRPTR tstring;
1190 struct Tokenizer tok;
1191 LONG networkCount;
1192 struct Network *net = NULL;
1193 BOOL keyIsHex;
1195 CombinePath2P(filename, filenamelen, directory, "Wireless.prefs");
1196 OpenTokenFile(&tok, filename);
1198 SetNetworkCount(0);
1199 networkCount = 0;
1201 while (!tok.fend && (networkCount < MAXNETWORKS))
1203 GetNextToken(&tok, "\n\t");
1204 if (tok.token)
1206 if (tok.newline) comment = FALSE;
1207 if (strncmp(tok.token, "#", 1) == 0) comment = TRUE;
1209 if (!comment)
1211 if (strncmp(tok.token, "network=", 8) == 0)
1213 net = GetNetwork(networkCount);
1214 net->adHoc = FALSE;
1215 net->hidden = FALSE;
1216 networkCount++;
1217 SetNetworkCount(networkCount);
1219 else if (strncmp(tok.token, "ssid=", 5) == 0)
1221 tstring = strchr(tok.token, '=') + 2;
1222 *strchr(tstring, '\"') = '\0';
1223 SetNetworkName(net, tstring);
1225 else if (strncmp(tok.token, "psk=", 4) == 0
1226 || strncmp(tok.token, "wep_key0=", 9) == 0)
1228 tstring = strchr(tok.token, '=') + 1;
1229 if (*tstring == '\"')
1231 keyIsHex = FALSE;
1232 tstring++;
1233 *strchr(tstring, '\"') = '\0';
1235 else
1236 keyIsHex = TRUE;
1237 SetKey(net, tstring, keyIsHex);
1238 SetEncType(net, (*tok.token == 'p') ? 2 : 1);
1240 else if (strncmp(tok.token, "scan_ssid=", 10) == 0)
1242 tstring = strchr(tok.token, '=') + 1;
1243 SetHidden(net, *tstring == '1');
1245 else if (strncmp(tok.token, "mode=", 5) == 0)
1247 tstring = strchr(tok.token, '=') + 1;
1248 SetAdHoc(net, *tstring == '1');
1253 CloseTokenFile(&tok);
1257 void ReadMobilePrefs(CONST_STRPTR directory)
1259 ULONG filenamelen = strlen(directory) + 4 + 30;
1260 TEXT filename[filenamelen];
1261 struct Tokenizer tok;
1262 LONG command=0;
1264 CombinePath2P(filename, filenamelen, directory, "MobileBroadband.prefs");
1265 OpenTokenFile(&tok, filename);
1266 while (!tok.fend)
1268 GetNextToken(&tok, " \n");
1269 if (tok.token)
1271 if ( tok.newline && tok.token[0] == '#' ) continue;
1273 if (tok.newline)
1275 if (strcasecmp( tok.token, "SEND" ) == 0)
1277 GetNextToken(&tok, "\n");
1278 if ( tok.token && ! tok.newline )
1280 SetMobile_atcommand( command++ , tok.token );
1283 else if (strcasecmp( tok.token, "DEVICE" ) == 0)
1285 GetNextToken(&tok, " \n");
1286 if ( tok.token && ! tok.newline )
1288 SetMobile_devicename( tok.token );
1291 else if (strcasecmp( tok.token, "USERNAME" ) == 0)
1293 GetNextToken(&tok, " \n");
1294 if ( tok.token && ! tok.newline )
1296 SetMobile_username( tok.token );
1299 else if (strcasecmp( tok.token, "PASSWORD" ) == 0)
1301 GetNextToken(&tok, " \n");
1302 if ( tok.token && ! tok.newline )
1304 SetMobile_password( tok.token );
1307 else if (strcasecmp( tok.token, "UNIT" ) == 0)
1309 GetNextToken(&tok, " \n");
1310 if ( tok.token && ! tok.newline )
1312 SetMobile_unit( atoi( tok.token ) );
1318 CloseTokenFile(&tok);
1322 BOOL ReadServers()
1324 BPTR dir, file;
1325 APTR ex_buffer = NULL;
1326 struct ExAllControl *ex_control = NULL;
1327 struct ExAllData *entry;
1328 BOOL success = TRUE, more = TRUE;
1329 LONG i = 0;
1330 struct Server *server;
1332 dir = Lock(SERVER_PATH_ENV, SHARED_LOCK);
1333 if (dir == BNULL)
1335 dir = Lock(SERVER_PATH_STORAGE, SHARED_LOCK);
1336 if (dir == BNULL)
1337 success = FALSE;
1338 else
1340 D(bug("[Network Prefs/ReadServers] scan directory " SERVER_PATH_STORAGE "\n"));
1343 else
1345 D(bug("[Network Prefs/ReadServers] scan directory " SERVER_PATH_ENV "\n"));
1348 if (success)
1350 ex_buffer = AllocVec(EX_BUF_SIZE, MEMF_PUBLIC);
1351 if (ex_buffer == NULL)
1352 success = FALSE;
1354 ex_control = AllocDosObject(DOS_EXALLCONTROL, NULL);
1355 if (ex_control == NULL)
1356 success = FALSE;
1359 if (success)
1361 ex_control->eac_LastKey = 0;
1362 while (more && i < MAXSERVERS)
1364 more = ExAll(dir, ex_buffer, EX_BUF_SIZE, ED_SIZE, ex_control);
1366 if (!more && (IoErr() != ERROR_NO_MORE_ENTRIES))
1367 break;
1368 if (ex_control->eac_Entries == 0)
1369 continue;
1371 entry = ex_buffer;
1372 while (entry != NULL)
1374 if (entry->ed_Type < 0)
1376 dir = CurrentDir(dir);
1377 D(bug("[Network Prefs/ReadServers] filename %s\n", entry->ed_Name));
1378 file = Open(entry->ed_Name, MODE_OLDFILE);
1379 if (file != BNULL)
1381 server = &prefs.servers[i];
1382 if (ReadServer(server, file, entry->ed_Size))
1384 i++;
1385 SetServerDevice(server, entry->ed_Name);
1386 if (strstr(GetActiveServers(), entry->ed_Name)
1387 != NULL)
1388 SetServerActive(server, TRUE);
1390 Close(file);
1392 dir = CurrentDir(dir);
1394 entry = entry->ed_Next;
1397 prefs.serverCount = i;
1400 if (ex_control != NULL)
1402 FreeDosObject(DOS_EXALLCONTROL, ex_control);
1405 FreeVec(ex_buffer);
1407 return success;
1411 /* Read and parse a server mount file */
1412 static BOOL ReadServer(struct Server *server, BPTR file, LONG size)
1414 BOOL success = TRUE;
1415 UBYTE *mount_buffer;
1416 IPTR mount_args[NUM_MOUNTARGS] = {0}, control_args[NUM_CONTROLARGS] = {0};
1417 struct RDArgs *mount_rdargs = NULL, *control_rdargs = NULL;
1418 LONG i;
1419 STRPTR host, service;
1421 /* Allocate buffer for entire mount file */
1422 mount_buffer = AllocVec(size+100, MEMF_ANY|MEMF_CLEAR);
1423 if (mount_buffer == NULL)
1424 success = FALSE;
1426 /* Read mount file into buffer */
1427 if (success)
1429 if (FRead(file, mount_buffer, size, 1) != 1)
1430 success = FALSE;
1433 if (success)
1435 for (i = 0; i < size; i++)
1436 if (mount_buffer[i] == '\n')
1437 mount_buffer[i] = ' ';
1439 mount_rdargs = AllocDosObject(DOS_RDARGS, NULL);
1440 control_rdargs = AllocDosObject(DOS_RDARGS, NULL);
1441 if (mount_rdargs == NULL || control_rdargs == NULL)
1442 success = FALSE;
1445 /* Parse mount parameters */
1446 if (success)
1448 mount_rdargs->RDA_Source.CS_Buffer = mount_buffer;
1449 mount_rdargs->RDA_Source.CS_Length = size+1;
1450 mount_rdargs->RDA_Flags = RDAF_NOPROMPT;
1451 mount_rdargs =
1452 ReadArgs(mount_template, (IPTR *)&mount_args, mount_rdargs);
1453 if (mount_rdargs == NULL)
1454 success = FALSE;
1457 /* Check if this is a server mount */
1458 if (success)
1460 if ((char *)mount_args[ARG_EHANDLER] == NULL)
1461 success = FALSE;
1462 else if (strcasecmp((char *)mount_args[ARG_EHANDLER],
1463 SERVER_HANDLER) != 0)
1464 success = FALSE;
1467 /* Parse control parameters */
1468 if (success)
1470 control_rdargs->RDA_Source.CS_Buffer = (UBYTE *)mount_args[ARG_CONTROL];
1471 control_rdargs->RDA_Source.CS_Length =
1472 strlen((STRPTR)mount_args[ARG_CONTROL]);
1473 control_rdargs =
1474 ReadArgs(control_template, (IPTR *)&control_args, control_rdargs);
1475 if (control_rdargs == NULL)
1476 success = FALSE;
1479 /* Extract needed control parameters */
1480 if (success)
1482 service = FilePart((STRPTR)control_args[ARG_SERVICE]);
1483 SetServerService(server, service);
1484 service--;
1485 *service = '\0';
1486 host = (STRPTR)control_args[ARG_SERVICE] + 2;
1487 SetServerHost(server, host);
1489 SetServerUser(server, (STRPTR)control_args[ARG_USERNAME]);
1490 SetServerGroup(server, (STRPTR)control_args[ARG_WORKGROUP]);
1491 SetServerPass(server, (STRPTR)control_args[ARG_PASSWORD]);
1494 if (control_rdargs != NULL)
1496 FreeArgs(control_rdargs);
1497 FreeDosObject(DOS_RDARGS, control_rdargs);
1500 if (mount_rdargs != NULL)
1502 FreeArgs(mount_rdargs);
1503 FreeDosObject(DOS_RDARGS, mount_rdargs);
1506 return success;
1510 void InitNetworkPrefs(CONST_STRPTR directory, BOOL use, BOOL save)
1512 SetDefaultNetworkPrefsValues();
1513 SetDefaultWirelessPrefsValues();
1514 SetDefaultMobilePrefsValues();
1516 ReadNetworkPrefs(directory);
1517 ReadWirelessPrefs(WIRELESS_PATH_ENV);
1518 ReadMobilePrefs(MOBILEBB_PATH_ENV);
1519 ReadServers();
1521 if (save)
1523 SaveNetworkPrefs();
1524 return; /* save equals to use */
1527 if (use)
1529 UseNetworkPrefs();
1533 // check if 'str' contains only characters from 'accept'
1534 BOOL IsLegal(STRPTR str, STRPTR accept)
1536 int i, len;
1538 if ((str == NULL) || (accept == NULL) || (str[0] == '\0'))
1540 return FALSE;
1543 len = strlen(str);
1544 for (i = 0; i < len; i++)
1546 if (strchr(accept, str[i]) == NULL)
1548 return FALSE;
1551 return TRUE;
1555 /* Getters */
1557 struct Interface * GetInterface(LONG index)
1559 return &prefs.interface[index];
1562 STRPTR GetName(struct Interface *iface)
1564 return iface->name;
1567 BOOL GetIfDHCP(struct Interface *iface)
1569 return iface->ifDHCP;
1572 STRPTR GetIP(struct Interface *iface)
1574 return iface->IP;
1577 STRPTR GetMask(struct Interface *iface)
1579 return iface->mask;
1582 STRPTR GetDevice(struct Interface *iface)
1584 return iface->device;
1587 LONG GetUnit(struct Interface *iface)
1589 return iface->unit;
1592 BOOL GetUp(struct Interface *iface)
1594 return iface->up;
1597 STRPTR GetGate(void)
1599 return prefs.gate;
1602 STRPTR GetDNS(LONG m)
1604 return prefs.DNS[m];
1607 STRPTR GetHostname(void)
1609 return prefs.host;
1612 STRPTR GetDomain(void)
1614 return prefs.domain;
1617 LONG GetInterfaceCount(void)
1619 return prefs.interfacecount;
1622 BOOL GetAutostart(void)
1624 return prefs.autostart;
1627 BOOL GetDHCP(void)
1629 return prefs.DHCP;
1633 /* Setters */
1635 void SetInterface
1637 struct Interface *iface, STRPTR name, BOOL dhcp, STRPTR IP, STRPTR mask,
1638 STRPTR device, LONG unit, BOOL up
1641 SetName(iface, name);
1642 SetIfDHCP(iface, dhcp);
1643 SetIP(iface, IP);
1644 SetMask(iface, mask);
1645 SetDevice(iface, device);
1646 SetUnit(iface, unit);
1647 SetUp(iface, up);
1650 void SetName(struct Interface *iface, STRPTR w)
1652 if (!IsLegal(w, NAMECHARS))
1654 w = DEFAULTNAME;
1656 strlcpy(iface->name, w, NAMEBUFLEN);
1659 void SetIfDHCP(struct Interface *iface, BOOL w)
1661 iface->ifDHCP = w;
1664 void SetIP(struct Interface *iface, STRPTR w)
1666 if (!IsLegal(w, IPCHARS))
1668 w = DEFAULTIP;
1670 strlcpy(iface->IP, w, IPBUFLEN);
1673 void SetMask(struct Interface *iface, STRPTR w)
1675 if (!IsLegal(w, IPCHARS))
1677 w = DEFAULTMASK;
1679 strlcpy(iface->mask, w, IPBUFLEN);
1682 void SetDevice(struct Interface *iface, STRPTR w)
1684 if (w == NULL || w[0] == '\0')
1686 w = DEFAULTDEVICE;
1688 strlcpy(iface->device, w, NAMEBUFLEN);
1691 void SetUnit(struct Interface *iface, LONG w)
1693 iface->unit = w;
1696 void SetUp(struct Interface *iface, BOOL w)
1698 iface->up = w;
1701 void SetGate(STRPTR w)
1703 if (!IsLegal(w, IPCHARS))
1705 w = DEFAULTGATE;
1707 strlcpy(prefs.gate, w, IPBUFLEN);
1710 void SetDNS(LONG m, STRPTR w)
1712 if (!IsLegal(w, IPCHARS))
1714 w = DEFAULTDNS;
1716 strlcpy(prefs.DNS[m], w, IPBUFLEN);
1719 void SetHostname(STRPTR w)
1721 if (!IsLegal(w, NAMECHARS))
1723 w = DEFAULTHOST;
1725 strlcpy(prefs.host, w, NAMEBUFLEN);
1728 void SetDomain(STRPTR w)
1730 if (!IsLegal(w, NAMECHARS))
1732 w = DEFAULTDOMAIN;
1734 strlcpy(prefs.domain, w, NAMEBUFLEN);
1737 void SetInterfaceCount(LONG w)
1739 prefs.interfacecount = w;
1742 void SetAutostart(BOOL w)
1744 prefs.autostart = w;
1747 void SetDHCP(BOOL w)
1749 prefs.DHCP = w;
1752 void InitHost(struct Host *host)
1754 SetHostNames(host, "");
1755 SetHostAddress(host, "");
1758 void InitNetwork(struct Network *net)
1760 SetNetworkName(net, "");
1761 SetKey(net, "", FALSE);
1762 SetEncType(net, 0);
1763 SetAdHoc(net, FALSE);
1766 void InitServer(struct Server *server, char *workgroup)
1768 if ((workgroup == NULL) && ((workgroup = GetDomain()) == NULL))
1769 workgroup = "workgroup";
1771 SetServerDevice(server, DEFAULTSERVERDEV);
1772 SetServerHost(server, "");
1773 SetServerGroup(server, workgroup);
1774 SetServerService(server, "share");
1775 SetServerUser(server, "guest");
1776 SetServerPass(server, "");
1777 SetServerActive(server, TRUE);
1781 /* Getters */
1783 struct Host *GetHost(LONG index)
1785 return &prefs.hosts[index];
1788 STRPTR GetHostNames(struct Host *host)
1790 return host->names;
1793 STRPTR GetHostAddress(struct Host *host)
1795 return host->address;
1798 LONG GetHostCount(void)
1800 return prefs.hostCount;
1803 struct Network *GetNetwork(LONG index)
1805 return &prefs.networks[index];
1808 STRPTR GetNetworkName(struct Network *net)
1810 return net->name;
1813 UWORD GetEncType(struct Network *net)
1815 return net->encType;
1818 STRPTR GetKey(struct Network *net)
1820 return net->key;
1823 BOOL GetHidden(struct Network *net)
1825 return net->hidden;
1828 BOOL GetAdHoc(struct Network *net)
1830 return net->adHoc;
1833 LONG GetNetworkCount(void)
1835 return prefs.networkCount;
1838 STRPTR GetWirelessDevice(void)
1840 return prefs.wirelessDevice;
1843 LONG GetWirelessUnit(void)
1845 return prefs.wirelessUnit;
1848 BOOL GetMobile_Autostart(void)
1850 return prefs.mobile.autostart;
1853 STRPTR GetMobile_atcommand(ULONG i)
1855 if (i < MAXATCOMMANDS)
1856 return prefs.mobile.atcommand[i];
1857 else return "";
1860 LONG GetMobile_atcommandcount(void)
1862 ULONG count=0;
1863 ULONG i;
1864 for (i = 0; i < MAXATCOMMANDS; i++)
1866 if (prefs.mobile.atcommand[i][0] != 0) count++;
1868 return count;
1871 STRPTR GetMobile_devicename(void)
1873 return prefs.mobile.devicename;
1876 STRPTR GetMobile_username(void)
1878 return prefs.mobile.username;
1881 STRPTR GetMobile_password(void)
1883 return prefs.mobile.password;
1886 LONG GetMobile_unit(void)
1888 return prefs.mobile.unit;
1891 LONG GetMobile_timeout(void)
1893 return prefs.mobile.timeout;
1896 struct Server *GetServer(LONG index)
1898 return &prefs.servers[index];
1901 STRPTR GetServerDevice(struct Server *server)
1903 return server->device;
1906 STRPTR GetServerHost(struct Server *server)
1908 return server->host;
1911 STRPTR GetServerService(struct Server *server)
1913 return server->service;
1916 STRPTR GetServerUser(struct Server *server)
1918 return server->user;
1921 STRPTR GetServerGroup(struct Server *server)
1923 return server->group;
1926 STRPTR GetServerPass(struct Server *server)
1928 return server->pass;
1931 BOOL GetServerActive(struct Server *server)
1933 return server->active;
1936 LONG GetServerCount(void)
1938 return prefs.serverCount;
1941 /* Setters */
1943 void SetHost
1945 struct Host *host, STRPTR name, STRPTR address
1948 SetHostNames(host, name);
1949 SetHostAddress(host, address);
1952 void SetHostNames(struct Host *host, STRPTR w)
1954 strlcpy(host->names, w, NAMEBUFLEN);
1957 void AddHostName(struct Host *host, STRPTR w)
1959 if (host->names[0] != '\0')
1960 strlcat(host->names, " ", NAMEBUFLEN);
1961 strlcat(host->names, w, NAMEBUFLEN);
1964 void SetHostAddress(struct Host *host, STRPTR w)
1966 strlcpy(host->address, w, IPBUFLEN);
1969 void SetHostCount(LONG w)
1971 prefs.hostCount = w;
1974 void SetNetwork
1976 struct Network *net, STRPTR name, UWORD encType, STRPTR key,
1977 BOOL keyIsHex, BOOL hidden, BOOL adHoc
1980 SetNetworkName(net, name);
1981 SetEncType(net, encType);
1982 SetKey(net, key, keyIsHex);
1983 SetHidden(net, hidden);
1984 SetAdHoc(net, adHoc);
1987 void SetNetworkName(struct Network *net, STRPTR w)
1989 strlcpy(net->name, w, SSIDBUFLEN);
1992 void SetEncType(struct Network *net, UWORD w)
1994 net->encType = w;
1997 void SetKey(struct Network *net, STRPTR w, BOOL keyIsHex)
1999 strlcpy(net->key, w, KEYBUFLEN);
2000 net->keyIsHex = keyIsHex;
2003 void SetHidden(struct Network *net, BOOL w)
2005 net->hidden = w;
2008 void SetAdHoc(struct Network *net, BOOL w)
2010 net->adHoc = w;
2013 void SetNetworkCount(LONG w)
2015 prefs.networkCount = w;
2018 void SetWirelessDevice(STRPTR w)
2020 prefs.wirelessDevice = w;
2023 void SetWirelessUnit(LONG w)
2025 prefs.wirelessUnit = w;
2028 void SetMobile_Autostart(BOOL w)
2030 prefs.mobile.autostart = w;
2033 void SetMobile_atcommand(ULONG i,STRPTR w)
2035 if (strlen(w) < NAMEBUFLEN && i >= 0 && i < MAXATCOMMANDS)
2036 strcpy(prefs.mobile.atcommand[i], w);
2039 void SetMobile_devicename(STRPTR w)
2041 if (strlen(w) < NAMEBUFLEN)
2042 strcpy(prefs.mobile.devicename, w);
2045 void SetMobile_username(STRPTR w)
2047 if (strlen(w) < NAMEBUFLEN)
2048 strcpy(prefs.mobile.username, w);
2051 void SetMobile_password(STRPTR w)
2053 if (strlen(w) < NAMEBUFLEN)
2054 strcpy(prefs.mobile.password, w);
2057 void SetMobile_unit(LONG w)
2059 prefs.mobile.unit = w;
2062 void SetMobile_timeout(LONG w)
2064 prefs.mobile.timeout = w;
2067 void SetServer
2069 struct Server *server, STRPTR device, STRPTR host, STRPTR service,
2070 STRPTR user, STRPTR group, STRPTR pass, BOOL active
2073 SetServerDevice(server, device);
2074 SetServerHost(server, host);
2075 SetServerService(server, service);
2076 SetServerUser(server, user);
2077 SetServerGroup(server, group);
2078 SetServerPass(server, pass);
2079 SetServerActive(server, active);
2082 void SetServerDevice(struct Server *server, STRPTR w)
2084 strlcpy(server->device, w, SMBBUFLEN);
2087 void SetServerHost(struct Server *server, STRPTR w)
2089 strlcpy(server->host, w, NAMEBUFLEN);
2092 void SetServerService(struct Server *server, STRPTR w)
2094 strlcpy(server->service, w, SMBBUFLEN);
2097 void SetServerUser(struct Server *server, STRPTR w)
2099 strlcpy(server->user, w, SMBBUFLEN);
2102 void SetServerGroup(struct Server *server, STRPTR w)
2104 strlcpy(server->group, w, SMBBUFLEN);
2107 void SetServerPass(struct Server *server, STRPTR w)
2109 strlcpy(server->pass, w, SMBBUFLEN);
2112 void SetServerActive(struct Server *server, BOOL w)
2114 server->active = w;
2117 void SetServerCount(LONG w)
2119 prefs.serverCount = w;