2 Copyright © 2009, The AROS Development Team. All rights reserved.
7 #include <proto/exec.h>
11 #include "prefsdata.h"
13 static struct TCPPrefs prefs
;
24 void OpenTokenFile(struct Tokenizer
* tok
, STRPTR FileName
)
26 tok
->tokenizedFile
= fopen(FileName
, "r");
29 if (!tok
->tokenizedFile
)
32 tok
->tokenizerLine
= malloc(8192);
38 void CloseTokenFile(struct Tokenizer
* tok
)
40 if (tok
->tokenizedFile
) {
41 fclose(tok
->tokenizedFile
);
42 free(tok
->tokenizerLine
);
48 void GetNextToken(struct Tokenizer
* tok
, STRPTR tk
)
51 if (tok
->token
!= NULL
) {
52 tok
->token
= strtok(NULL
, tk
);
54 GetNextToken(tok
, tk
);
57 if (!feof(tok
->tokenizedFile
)) {
58 tok
->tokenizerLine
[0] = 0;
59 fgets(tok
->tokenizerLine
, 8192, tok
->tokenizedFile
);
60 if (tok
->tokenizerLine
== NULL
) {
62 GetNextToken(tok
, tk
);
64 tok
->token
= strtok(tok
->tokenizerLine
, tk
);
70 void SetDefaultNetworkPrefsValues()
72 SetIP("192.168.0.188");
73 SetMask("255.255.255.0");
74 SetGate("192.168.0.1");
75 SetDNS(0, "192.168.0.1");
76 SetDNS(1, "192.168.0.1");
77 SetDevice("DEVS:networks/pcnet32.device");
84 /* Returns TRUE if directory has been created or already existed */
85 BOOL
RecursiveCreateDir(CONST_STRPTR dirpath
)
87 /* Will create directory even if top level directory does not exist */
90 ULONG lastdirseparator
= 0;
91 ULONG dirpathlen
= strlen(dirpath
);
92 STRPTR tmpdirpath
= AllocVec(dirpathlen
+ 2, MEMF_CLEAR
| MEMF_PUBLIC
);
94 CopyMem(dirpath
, tmpdirpath
, dirpathlen
);
96 /* Recurvice directory creation */
99 if (lastdirseparator
>= dirpathlen
) break;
101 for (; lastdirseparator
< dirpathlen
; lastdirseparator
++)
102 if (tmpdirpath
[lastdirseparator
] == '/') break;
104 tmpdirpath
[lastdirseparator
] = '\0'; /* cut */
106 /* Unlock any lock from previous interation. Last iteration lock will be returned. */
113 /* Check if directory exists */
114 lock
= Lock(tmpdirpath
, SHARED_LOCK
);
117 lock
= CreateDir(tmpdirpath
);
119 break; /* Error with creation */
122 tmpdirpath
[lastdirseparator
] = '/'; /* restore */
138 /* Puts part 1 into empy buffer */
139 VOID
CombinePath1P(STRPTR dstbuffer
, ULONG dstbufferlen
, CONST_STRPTR part1
)
141 dstbuffer
[0] = '\0'; /* Make sure buffer is treated as empty */
142 AddPart(dstbuffer
, part1
, dstbufferlen
);
145 /* Combines part1 with part2 into an empty buffer */
146 VOID
CombinePath2P(STRPTR dstbuffer
, ULONG dstbufferlen
, CONST_STRPTR part1
, CONST_STRPTR part2
)
148 CombinePath1P(dstbuffer
, dstbufferlen
, part1
);
149 AddPart(dstbuffer
, part2
, dstbufferlen
);
152 /* Combines part1 with part2 with part3 into an empty buffer */
153 VOID
CombinePath3P(STRPTR dstbuffer
, ULONG dstbufferlen
, CONST_STRPTR part1
, CONST_STRPTR part2
, CONST_STRPTR part3
)
155 CombinePath2P(dstbuffer
, dstbufferlen
, part1
, part2
);
156 AddPart(dstbuffer
, part3
, dstbufferlen
);
159 BOOL
WriteNetworkPrefs(CONST_STRPTR destdir
)
162 ULONG filenamelen
= strlen(destdir
) + 4 + 20;
163 TEXT filename
[filenamelen
];
164 ULONG destdbdirlen
= strlen(destdir
) + 3 + 1;
165 TEXT destdbdir
[destdbdirlen
];
167 CombinePath2P(destdbdir
, destdbdirlen
, destdir
, "db");
169 /* Create necessary directories */
170 if(!RecursiveCreateDir(destdir
)) return FALSE
;
171 if(!RecursiveCreateDir(destdbdir
)) return FALSE
;
173 /* Write variables */
174 CombinePath2P(filename
, filenamelen
, destdir
, "Config");
175 ConfFile
= fopen(filename
, "w");
176 if (!ConfFile
) return FALSE
;
177 fprintf(ConfFile
, "%s/db", PREFS_PATH_ENV
);
180 CombinePath2P(filename
, filenamelen
, destdir
, "AutoRun");
181 ConfFile
= fopen(filename
, "w");
182 if (!ConfFile
) return FALSE
;
183 fprintf(ConfFile
, "%s", (GetAutostart()) ? "True" : "False");
186 /* Write configuration files */
187 CombinePath2P(filename
, filenamelen
, destdbdir
, "general.config");
188 ConfFile
= fopen(filename
, "w");
189 if (!ConfFile
) return FALSE
;
190 fprintf(ConfFile
, "USELOOPBACK=YES\n");
191 fprintf(ConfFile
, "DEBUGSANA=NO\n");
192 fprintf(ConfFile
, "USENS=SECOND\n");
193 fprintf(ConfFile
, "GATEWAY=NO\n");
194 fprintf(ConfFile
, "HOSTNAME=%s.%s\n", GetHost(), GetDomain());
195 fprintf(ConfFile
, "LOG FILTERFILE=5\n");
196 fprintf(ConfFile
, "GUI PANEL=MUI\n");
197 fprintf(ConfFile
, "OPENGUI=YES\n");
200 CombinePath2P(filename
, filenamelen
, destdbdir
, "interfaces");
201 ConfFile
= fopen(filename
, "w");
202 if (!ConfFile
) return FALSE
;
203 fprintf(ConfFile
,"eth0 DEV=%s UNIT=0 NOTRACKING IP=%s NETMASK=%s UP\n", GetDevice(),
204 (GetDHCP() ? (CONST_STRPTR
)"DHCP" : GetIP()), GetMask());
208 CombinePath2P(filename
, filenamelen
, destdbdir
, "netdb-myhost");
209 ConfFile
= fopen(filename
, "w");
210 if (!ConfFile
) return FALSE
;
211 fprintf(ConfFile
, "HOST %s %s.%s %s\n", GetIP(), GetHost(), GetDomain(), GetHost());
212 fprintf(ConfFile
, "HOST %s gateway\n", GetGate());
213 fprintf(ConfFile
, "; Domain names\n");
214 fprintf(ConfFile
, "; Name servers\n");
215 fprintf(ConfFile
, "NAMESERVER %s\n", GetDNS(0));
216 fprintf(ConfFile
, "NAMESERVER %s\n", GetDNS(1));
219 CombinePath2P(filename
, filenamelen
, destdbdir
, "static-routes");
220 ConfFile
= fopen(filename
, "w");
221 if (!ConfFile
) return FALSE
;
222 fprintf(ConfFile
, "DEFAULT GATEWAY %s\n", GetGate());
229 BOOL
CopyFile(CONST_STRPTR srcfile
, CONST_STRPTR dstfile
)
231 BPTR from
= NULL
, to
= NULL
;
232 TEXT buffer
[BUFSIZE
];
234 if((from
= Open(srcfile
, MODE_OLDFILE
)))
236 if((to
= Open(dstfile
, MODE_NEWFILE
)))
242 if ((s
= Read(from
, buffer
, BUFSIZE
)) == -1)
249 if (Write(to
, buffer
, s
) == -1)
255 } while (s
== BUFSIZE
);
268 CONST_STRPTR
GetDefaultStackLocation()
270 /* Use static variable so that it is initialized only once (and can be returned) */
271 static TEXT path
[1024] = {0};
273 /* Load path if needed - this will happen only once */
276 GetVar(AROSTCP_PACKAGE_VARIABLE
, path
, 1024, LV_VAR
);
282 BOOL
IsStackRunning()
284 struct Library
* socketlib
= NULL
;
286 if ((socketlib
= OpenLibrary("bsdsocket.library", 0L)) != NULL
)
288 CloseLibrary(socketlib
);
301 if (IsStackRunning())
303 struct Task
* arostcptask
= FindTask("bsdsocket.library");
304 if (arostcptask
!= NULL
)
305 Signal(arostcptask
, SIGBREAKF_CTRL_C
);
308 /* Check if shutdown successfull */
310 while(IsStackRunning())
312 if (trycount
> 4) return FALSE
;
319 CONST_STRPTR srcdir
= GetDefaultStackLocation();
320 ULONG arostcppathlen
= strlen(srcdir
) + 3 + 20;
321 TEXT arostcppath
[arostcppathlen
];
322 struct TagItem tags
[] =
324 { SYS_Input
, (IPTR
)NULL
},
325 { SYS_Output
, (IPTR
)NULL
},
326 { SYS_Error
, (IPTR
)NULL
},
327 { SYS_Asynch
, (IPTR
)TRUE
},
331 CombinePath3P(arostcppath
, arostcppathlen
, srcdir
, "C", "AROSTCP");
333 SystemTagList(arostcppath
, tags
);
337 /* Check if startup successfull */
339 while(!IsStackRunning())
341 if (trycount
> 9) return FALSE
;
350 /* This is not a general use function! It assumes destinations directory exists */
351 BOOL
AddFileFromDefaultStackLocation(CONST_STRPTR filename
, CONST_STRPTR dstdir
)
354 CONST_STRPTR srcdir
= GetDefaultStackLocation();
355 ULONG srcfilelen
= strlen(srcdir
) + 4 + strlen(filename
) + 1;
356 TEXT srcfile
[srcfilelen
];
357 ULONG dstfilelen
= strlen(dstdir
) + 4 + strlen(filename
) + 1;
358 TEXT dstfile
[dstfilelen
];
361 CombinePath3P(srcfile
, srcfilelen
, srcdir
, "db", filename
);
362 CombinePath3P(dstfile
, dstfilelen
, dstdir
, "db", filename
);
364 /* Check if the destination file already exists. If yes, do not copy */
365 dstlock
= Lock(dstfile
, SHARED_LOCK
);
372 return CopyFile(srcfile
, dstfile
);
375 /* Copies files not created by prefs but needed to start stack */
376 BOOL
CopyDefaultConfiguration(CONST_STRPTR destdir
)
378 ULONG destdbdirlen
= strlen(destdir
) + 3 + 1;
379 TEXT destdbdir
[destdbdirlen
];
380 CombinePath2P(destdbdir
, destdbdirlen
, destdir
, "db");
382 /* Create necessary directories */
383 if (!RecursiveCreateDir(destdir
)) return FALSE
;
384 if (!RecursiveCreateDir(destdbdir
)) return FALSE
;
387 if (!AddFileFromDefaultStackLocation("hosts", destdir
)) return FALSE
;
388 if (!AddFileFromDefaultStackLocation("inet.access", destdir
)) return FALSE
;
389 if (!AddFileFromDefaultStackLocation("netdb", destdir
)) return FALSE
;
390 if (!AddFileFromDefaultStackLocation("networks", destdir
)) return FALSE
;
391 if (!AddFileFromDefaultStackLocation("protocols", destdir
)) return FALSE
;
392 if (!AddFileFromDefaultStackLocation("services", destdir
)) return FALSE
;
397 enum ErrorCode
SaveNetworkPrefs()
399 if (!CopyDefaultConfiguration(PREFS_PATH_ENVARC
)) return NOT_COPIED_FILES_ENVARC
;
400 if (!WriteNetworkPrefs(PREFS_PATH_ENVARC
)) return NOT_SAVED_PREFS_ENVARC
;
401 return UseNetworkPrefs();
404 enum ErrorCode
UseNetworkPrefs()
406 if (!CopyDefaultConfiguration(PREFS_PATH_ENV
)) return NOT_COPIED_FILES_ENV
;
407 if (!WriteNetworkPrefs(PREFS_PATH_ENV
)) return NOT_SAVED_PREFS_ENV
;
408 if (!RestartStack()) return NOT_RESTARTED_STACK
;
412 /* Directory points to top of config, so to AAA/AROSTCP not to AAA/AROSTCP/db */
413 void ReadNetworkPrefs(CONST_STRPTR directory
)
415 ULONG filenamelen
= strlen(directory
) + 4 + 20;
416 TEXT filename
[filenamelen
];
417 BOOL comment
= FALSE
;
419 struct Tokenizer tok
;
421 /* This function will not fail. It will load as much data as possible. Rest will be defaul values */
423 CombinePath3P(filename
, filenamelen
, directory
, "db", "general.config");
424 OpenTokenFile(&tok
, filename
);
426 if (tok
.newline
) { // read tokens from the beginning of line
428 if (strcmp(tok
.token
, "HOSTNAME") == 0) {
429 GetNextToken(&tok
, "=\n");
430 tstring
= strchr(tok
.token
, '.');
431 SetDomain(tstring
+ 1);
437 GetNextToken(&tok
, "=\n");
439 CloseTokenFile(&tok
);
441 CombinePath3P(filename
, filenamelen
, directory
, "db", "interfaces");
442 OpenTokenFile(&tok
, filename
);
443 /* Reads only first uncommented interface */
445 GetNextToken(&tok
, " \n");
447 if (tok
.newline
) comment
= FALSE
;
448 if (strncmp(tok
.token
, "#", 1) == 0) comment
= TRUE
;
451 if (strncmp(tok
.token
, "DEV=", 4) == 0) {
452 tstring
= strchr(tok
.token
, '=');
453 SetDevice(tstring
+ 1);
455 if (strncmp(tok
.token
, "IP=", 3) == 0) {
456 tstring
= strchr(tok
.token
, '=');
457 if (strncmp(tstring
+ 1, "DHCP", 4) == 0)
460 SetIP("192.168.0.188");
468 if (strncmp(tok
.token
, "NETMASK=", 8) == 0) {
469 tstring
= strchr(tok
.token
, '=');
470 SetMask(tstring
+ 1);
475 CloseTokenFile(&tok
);
477 CombinePath3P(filename
, filenamelen
, directory
, "db", "netdb-myhost");
478 OpenTokenFile(&tok
, filename
);
481 GetNextToken(&tok
, " \n");
483 if (strncmp(tok
.token
, "NAMESERVER", 4) == 0) {
484 GetNextToken(&tok
, " \n");
485 SetDNS(dnsc
, tok
.token
);
487 if (dnsc
> 1) dnsc
= 1;
491 CloseTokenFile(&tok
);
493 CombinePath3P(filename
, filenamelen
, directory
, "db", "static-routes");
494 OpenTokenFile(&tok
, filename
);
496 GetNextToken(&tok
, " \n");
498 if (strncmp(tok
.token
, "DEFAULT", 4) == 0) {
499 GetNextToken(&tok
, " \n");
500 if (strncmp(tok
.token
, "GATEWAY", 4) == 0) {
501 GetNextToken(&tok
, " \n");
507 CloseTokenFile(&tok
);
509 CombinePath2P(filename
, filenamelen
, directory
, "Autorun");
510 OpenTokenFile(&tok
, filename
);
513 GetNextToken(&tok
, " \n");
516 if (strncmp(tok
.token
, "True", 4) == 0)
528 CloseTokenFile(&tok
);
531 void InitNetworkPrefs(CONST_STRPTR directory
, BOOL use
, BOOL save
)
533 SetDefaultNetworkPrefsValues();
535 ReadNetworkPrefs(directory
);
540 return; /* save equals to use */
566 STRPTR
GetDNS(LONG m
)
593 return prefs
.autostart
;
598 strlcpy(prefs
.IP
, w
,63);
603 void SetMask(STRPTR w
)
605 strlcpy(prefs
.mask
, w
,63);
608 void SetGate(STRPTR w
)
610 strlcpy(prefs
.gate
, w
,63);
613 void SetDNS(LONG m
, STRPTR w
)
615 strlcpy(prefs
.DNS
[m
], w
,63);
623 void SetDevice(STRPTR w
)
625 strlcpy(prefs
.device
, w
,511);
628 void SetHost(STRPTR w
)
630 strlcpy(prefs
.host
, w
,511);
633 void SetDomain(STRPTR w
)
635 strlcpy(prefs
.domain
, w
,511);
637 void SetAutostart(BOOL w
)