2 Copyright © 2009-2012, The AROS Development Team. All rights reserved.
7 #include <proto/exec.h>
11 #include "prefsdata.h"
13 #include <aros/debug.h>
15 static struct TCPPrefs prefs
;
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");
34 if (!tok
->tokenizedFile
)
38 tok
->tokenizerLine
= malloc(8192);
44 void CloseTokenFile(struct Tokenizer
* tok
)
46 if (tok
->tokenizedFile
)
48 fclose(tok
->tokenizedFile
);
49 free(tok
->tokenizerLine
);
55 void GetNextToken(struct Tokenizer
* tok
, STRPTR tk
)
58 if (tok
->token
!= NULL
)
60 tok
->token
= strtok(NULL
, tk
);
62 GetNextToken(tok
, tk
);
67 if (!feof(tok
->tokenizedFile
))
69 tok
->tokenizerLine
[0] = 0;
70 fgets(tok
->tokenizerLine
, 8192, tok
->tokenizedFile
);
71 if (tok
->tokenizerLine
== NULL
)
74 GetNextToken(tok
, tk
);
77 tok
->token
= strtok(tok
->tokenizerLine
, tk
);
84 void SetDefaultNetworkPrefsValues()
87 for (i
= 0; i
< MAXINTERFACES
; i
++)
89 InitInterface(GetInterface(i
));
92 SetDomain(DEFAULTDOMAIN
);
95 SetDNS(0, DEFAULTDNS
);
96 SetDNS(1, DEFAULTDNS
);
102 void SetDefaultWirelessPrefsValues()
105 for (i
= 0; i
< MAXNETWORKS
; i
++)
107 InitNetwork(GetNetwork(i
));
111 SetWirelessDevice(NULL
);
114 void SetDefaultMobilePrefsValues()
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" );
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
);
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 */
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 */
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. */
171 /* Check if directory exists */
172 lock
= Lock(tmpdirpath
, SHARED_LOCK
);
175 lock
= CreateDir(tmpdirpath
);
177 break; /* Error with creation */
180 tmpdirpath
[lastdirseparator
] = '/'; /* restore */
196 /* Returns TRUE if selected device needs to use NOTRACKING option */
197 BOOL
GetNoTracking(struct Interface
*iface
)
199 STRPTR devicename
= NULL
;
201 TEXT devicepath
[strlen(GetDevice(iface
)) + 1];
202 strcpy(devicepath
, GetDevice(iface
));
205 while ((devicename
= notrackingdevices
[pos
++]) != NULL
)
207 /* Comparison is done on upper case string so it is case insensitive */
209 if (strstr(devicepath
, devicename
) != NULL
)
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
)
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");
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
);
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
)""),
280 (strstr(GetDevice(iface
), "ppp.device") == NULL
?
281 (CONST_STRPTR
)"DHCP" : (CONST_STRPTR
)"0.0.0.0") :
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
);
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
))
309 ConfFile
, "HOST %s %s.%s %s\n",
310 GetIP(iface
), GetHost(), GetDomain(), GetHost()
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));
326 CombinePath2P(filename
, filenamelen
, destdbdir
, "static-routes");
327 ConfFile
= fopen(filename
, "w");
328 if (!ConfFile
) return FALSE
;
331 // FIXME: old version wrote Gateway even when DHCP was enabled
332 fprintf(ConfFile
, "DEFAULT GATEWAY %s\n", GetGate());
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
);
343 CombinePath2P(filename
, filenamelen
, destdir
, "AutoRun");
344 ConfFile
= fopen(filename
, "w");
345 if (!ConfFile
) return FALSE
;
346 fprintf(ConfFile
, "%s", (GetAutostart()) ? "True" : "False");
349 CombinePath2P(filename
, filenamelen
, destdir
, "MobileAutorun");
350 ConfFile
= fopen(filename
, "w");
351 if (!ConfFile
) return FALSE
;
352 fprintf(ConfFile
, "%s", (GetMobile_Autostart()) ? "True" : "False");
355 CombinePath2P(filename
, filenamelen
, destdir
, "WirelessAutoRun");
356 ConfFile
= fopen(filename
, "w");
357 if (!ConfFile
) return FALSE
;
358 fprintf(ConfFile
, "%s", (GetWirelessDevice() != NULL
) ? "True" : "False");
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());
374 BOOL
WriteWirelessPrefs(CONST_STRPTR destdir
)
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
)
399 fprintf(ConfFile
, "\tpsk=%s\n", net
->key
);
401 fprintf(ConfFile
, "\tpsk=\"%s\"\n", net
->key
);
402 fprintf(ConfFile
, "\tkey_mgmt=WPA-PSK\n");
406 fprintf(ConfFile
, "\twep_key0=%s\n", net
->key
);
408 fprintf(ConfFile
, "\twep_key0=\"%s\"\n", net
->key
);
409 fprintf(ConfFile
, "\twep_tx_keyidx=0\n");
411 fprintf(ConfFile
, "\tkey_mgmt=NONE\n");
414 fprintf(ConfFile
, "\tscan_ssid=1\n");
416 fprintf(ConfFile
, "\tmode=1\n");
417 fprintf(ConfFile
, "}\n\n");
427 BOOL
WriteMobilePrefs(CONST_STRPTR destdir
)
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
) );
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
)))
470 if ((s
= Read(from
, buffer
, BUFSIZE
)) == -1)
477 if (Write(to
, buffer
, s
) == -1)
483 } while (s
== BUFSIZE
);
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 */
504 GetVar(AROSTCP_PACKAGE_VARIABLE
, path
, 1024, LV_VAR
);
510 BOOL
IsStackRunning()
512 return FindTask("bsdsocket.library") != NULL
;
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 */
529 while(IsStackRunning())
531 if (trycount
> 4) return FALSE
;
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
},
550 CombinePath3P(arostcppath
, arostcppathlen
, srcdir
, "C", "AROSTCP");
552 SystemTagList(arostcppath
, tags
);
555 /* Check if startup successful */
557 while (!IsStackRunning())
559 if (trycount
> 9) return FALSE
;
574 struct Task
*task
= FindTask("C:WirelessManager");
576 Signal(task
, SIGBREAKF_CTRL_C
);
579 /* Check if shutdown successful */
581 while(FindTask("C:WirelessManager") != NULL
)
583 if (trycount
> 4) return FALSE
;
599 struct TagItem tags
[] =
601 { SYS_Input
, (IPTR
)NULL
},
602 { SYS_Output
, (IPTR
)NULL
},
603 { SYS_Error
, (IPTR
)NULL
},
604 { SYS_Asynch
, (IPTR
)TRUE
},
608 snprintf(command
, 80, "C:WirelessManager \"%s\" UNIT %ld\n",
609 GetWirelessDevice(), (long int)GetWirelessUnit());
610 SystemTagList(command
, tags
);
613 /* Check if startup successful */
615 while(FindTask("C:WirelessManager") == NULL
)
617 if (trycount
> 9) return FALSE
;
632 struct Task
*task
= FindTask("C:ModemManager");
634 Signal(task
, SIGBREAKF_CTRL_C
);
637 /* Check if shutdown successful */
639 while(FindTask("C:ModemManager") != NULL
)
641 if (trycount
> 4) return FALSE
;
656 struct TagItem tags
[] =
658 { SYS_Input
, (IPTR
)NULL
},
659 { SYS_Output
, (IPTR
)NULL
},
660 { SYS_Error
, (IPTR
)NULL
},
661 { SYS_Asynch
, (IPTR
)TRUE
},
665 SystemTagList("C:ModemManager", tags
);
668 /* Check if startup successful */
670 while(FindTask("C:ModemManager") == NULL
)
672 if (trycount
> 9) return FALSE
;
681 /* This is not a general use function! It assumes destinations directory exists */
682 BOOL
AddFileFromDefaultStackLocation(CONST_STRPTR filename
, CONST_STRPTR dstdir
)
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
)
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
;
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
;
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
;
744 if (GetWirelessDevice() != NULL
)
745 if (!StartWireless()) return NOT_RESTARTED_WIRELESS
;
746 if (!RestartStack()) return NOT_RESTARTED_STACK
;
748 if (GetMobile_Autostart())
749 if (!StartMobile()) return NOT_RESTARTED_MOBILE
;
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
;
760 struct Tokenizer tok
;
762 struct Interface
*iface
= NULL
;
764 /* This function will not fail. It will load as much data as possible. Rest will be default values */
768 CombinePath3P(filename
, filenamelen
, directory
, "db", "general.config");
769 OpenTokenFile(&tok
, filename
);
773 { // read tokens from the beginning of line
776 if (strcmp(tok
.token
, "HOSTNAME") == 0)
778 GetNextToken(&tok
, "=\n");
779 tstring
= strchr(tok
.token
, '.');
780 SetDomain(tstring
+ 1);
786 GetNextToken(&tok
, "=\n");
788 CloseTokenFile(&tok
);
790 CombinePath3P(filename
, filenamelen
, directory
, "db", "interfaces");
791 OpenTokenFile(&tok
, filename
);
793 SetInterfaceCount(0);
796 while (!tok
.fend
&& (interfacecount
< MAXINTERFACES
))
798 GetNextToken(&tok
, " \n");
801 if (tok
.newline
) comment
= FALSE
;
802 if (strncmp(tok
.token
, "#", 1) == 0) comment
= TRUE
;
808 iface
= GetInterface(interfacecount
);
809 SetName(iface
, tok
.token
);
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
);
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)
850 CloseTokenFile(&tok
);
852 CombinePath3P(filename
, filenamelen
, directory
, "db", "netdb-myhost");
853 OpenTokenFile(&tok
, filename
);
857 GetNextToken(&tok
, " \n");
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
);
866 if (dnsc
> 1) dnsc
= 1;
870 // Assume DHCP if there is no nameserver
875 CloseTokenFile(&tok
);
877 CombinePath3P(filename
, filenamelen
, directory
, "db", "static-routes");
878 OpenTokenFile(&tok
, filename
);
881 GetNextToken(&tok
, " \n");
884 if (strncmp(tok
.token
, "DEFAULT", 7) == 0)
886 GetNextToken(&tok
, " \n");
887 if (strncmp(tok
.token
, "GATEWAY", 7) == 0)
889 GetNextToken(&tok
, " \n");
895 CloseTokenFile(&tok
);
897 CombinePath2P(filename
, filenamelen
, directory
, "Autorun");
898 OpenTokenFile(&tok
, filename
);
901 GetNextToken(&tok
, " \n");
904 if (strncmp(tok
.token
, "True", 4) == 0)
916 CloseTokenFile(&tok
);
919 void ReadWirelessPrefs(CONST_STRPTR directory
)
921 ULONG filenamelen
= strlen(directory
) + 4 + 20;
922 TEXT filename
[filenamelen
];
923 BOOL comment
= FALSE
;
925 struct Tokenizer tok
;
927 struct Network
*net
= NULL
;
930 CombinePath2P(filename
, filenamelen
, directory
, "Wireless.prefs");
931 OpenTokenFile(&tok
, filename
);
936 while (!tok
.fend
&& (networkCount
< MAXNETWORKS
))
938 GetNextToken(&tok
, " \n\t");
941 if (tok
.newline
) comment
= FALSE
;
942 if (strncmp(tok
.token
, "#", 1) == 0) comment
= TRUE
;
946 if (strncmp(tok
.token
, "network=", 8) == 0)
948 net
= GetNetwork(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
== '\"')
968 *strchr(tstring
, '\"') = '\0';
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
;
999 CombinePath2P(filename
, filenamelen
, directory
, "MobileBroadband.prefs");
1000 OpenTokenFile(&tok
, filename
);
1003 GetNextToken(&tok
, " \n");
1006 if ( tok
.newline
&& tok
.token
[0] == '#' ) continue;
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
);
1070 return; /* save equals to use */
1079 // check if 'str' contains only characters from 'accept'
1080 BOOL
IsLegal(STRPTR str
, STRPTR accept
)
1084 if ((str
== NULL
) || (accept
== NULL
) || (str
[0] == '\0'))
1090 for (i
= 0; i
< len
; i
++)
1092 if (strchr(accept
, str
[i
]) == NULL
)
1103 struct Interface
* GetInterface(LONG index
)
1105 return &prefs
.interface
[index
];
1108 STRPTR
GetName(struct Interface
*iface
)
1113 BOOL
GetIfDHCP(struct Interface
*iface
)
1115 return iface
->ifDHCP
;
1118 STRPTR
GetIP(struct Interface
*iface
)
1123 STRPTR
GetMask(struct Interface
*iface
)
1128 STRPTR
GetDevice(struct Interface
*iface
)
1130 return iface
->device
;
1133 LONG
GetUnit(struct Interface
*iface
)
1138 BOOL
GetUp(struct Interface
*iface
)
1144 STRPTR
GetGate(void)
1149 STRPTR
GetDNS(LONG m
)
1151 return prefs
.DNS
[m
];
1154 STRPTR
GetHost(void)
1159 STRPTR
GetDomain(void)
1161 return prefs
.domain
;
1164 LONG
GetInterfaceCount(void)
1166 return prefs
.interfacecount
;
1169 BOOL
GetAutostart(void)
1171 return prefs
.autostart
;
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
);
1191 SetMask(iface
, mask
);
1192 SetDevice(iface
, device
);
1193 SetUnit(iface
, unit
);
1197 void SetName(struct Interface
*iface
, STRPTR w
)
1199 if (!IsLegal(w
, NAMECHARS
))
1203 strlcpy(iface
->name
, w
, NAMEBUFLEN
);
1206 void SetIfDHCP(struct Interface
*iface
, BOOL w
)
1211 void SetIP(struct Interface
*iface
, STRPTR w
)
1213 if (!IsLegal(w
, IPCHARS
))
1217 strlcpy(iface
->IP
, w
, IPBUFLEN
);
1220 void SetMask(struct Interface
*iface
, STRPTR w
)
1222 if (!IsLegal(w
, IPCHARS
))
1226 strlcpy(iface
->mask
, w
, IPBUFLEN
);
1229 void SetDevice(struct Interface
*iface
, STRPTR w
)
1231 if (w
== NULL
|| w
[0] == '\0')
1235 strlcpy(iface
->device
, w
, NAMEBUFLEN
);
1238 void SetUnit(struct Interface
*iface
, LONG w
)
1243 void SetUp(struct Interface
*iface
, BOOL w
)
1249 void SetGate(STRPTR w
)
1251 if (!IsLegal(w
, IPCHARS
))
1255 strlcpy(prefs
.gate
, w
, IPBUFLEN
);
1258 void SetDNS(LONG m
, STRPTR w
)
1260 if (!IsLegal(w
, IPCHARS
))
1264 strlcpy(prefs
.DNS
[m
], w
, IPBUFLEN
);
1267 void SetHost(STRPTR w
)
1269 if (!IsLegal(w
, NAMECHARS
))
1273 strlcpy(prefs
.host
, w
, NAMEBUFLEN
);
1276 void SetDomain(STRPTR w
)
1278 if (!IsLegal(w
, NAMECHARS
))
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
)
1300 void InitNetwork(struct Network
*net
)
1302 SetNetworkName(net
, "");
1303 SetKey(net
, "", FALSE
);
1305 SetAdHoc(net
, FALSE
);
1311 struct Network
*GetNetwork(LONG index
)
1313 return &prefs
.networks
[index
];
1316 STRPTR
GetNetworkName(struct Network
*net
)
1321 UWORD
GetEncType(struct Network
*net
)
1323 return net
->encType
;
1326 STRPTR
GetKey(struct Network
*net
)
1331 BOOL
GetHidden(struct Network
*net
)
1336 BOOL
GetAdHoc(struct Network
*net
)
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
];
1368 LONG
GetMobile_atcommandcount(void)
1372 for (i
=0;i
<MAXATCOMMANDS
;i
++)
1374 if( prefs
.mobile
.atcommand
[i
][0] != 0 ) 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
;
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
)
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
)
1440 void SetAdHoc(struct Network
*net
, BOOL 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
;