2011-11-13 00:38:55 +01:00
|
|
|
/* tools.c 22-Jan-96 */
|
2011-11-13 00:38:55 +01:00
|
|
|
/* (C)opyright (C) 1993,1995 Martin Stover, Marburg, Germany
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2011-11-13 00:38:56 +01:00
|
|
|
#ifndef LINUX
|
|
|
|
#include <errno.h>
|
|
|
|
extern int _sys_nerr;
|
|
|
|
extern char _sys_errlist[];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
int nw_debug=0;
|
2011-11-13 00:38:55 +01:00
|
|
|
FILE *logfile=stdout;
|
2011-11-13 00:38:55 +01:00
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
static int in_module=0; /* in which process i am ? */
|
|
|
|
static char *modnames[] =
|
|
|
|
{ "???????",
|
|
|
|
"NWSERV ",
|
|
|
|
"NCPSERV",
|
2011-11-13 00:38:56 +01:00
|
|
|
"NWCONN ",
|
|
|
|
"NWCLIEN" };
|
2011-11-13 00:38:55 +01:00
|
|
|
|
|
|
|
static char *get_modstr(void)
|
|
|
|
{
|
|
|
|
return(modnames[in_module]);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
char *xmalloc(uint size)
|
|
|
|
{
|
|
|
|
char *p = (size) ? (char *)malloc(size) : (char*)NULL;
|
|
|
|
if (p == (char *)NULL && size){
|
2011-11-13 00:38:55 +01:00
|
|
|
fprintf(logfile, "not enough core, need %d Bytes\n", size);
|
2011-11-13 00:38:55 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
char *xcmalloc(uint size)
|
|
|
|
{
|
|
|
|
char *p = xmalloc(size);
|
|
|
|
if (size) memset(p, 0, size);
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
void x_x_xfree(char **p)
|
|
|
|
{
|
|
|
|
if (*p != (char *)NULL){
|
|
|
|
free(*p);
|
|
|
|
*p = (char*)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:56 +01:00
|
|
|
int strmaxcpy(uint8 *dest, uint8 *source, int len)
|
2011-11-13 00:38:55 +01:00
|
|
|
{
|
2011-11-13 00:38:56 +01:00
|
|
|
int slen = (source != (uint8 *)NULL) ? min(len, strlen((char*)source)) : 0;
|
2011-11-13 00:38:55 +01:00
|
|
|
if (slen) memcpy(dest, source, slen);
|
|
|
|
dest[slen] = '\0';
|
|
|
|
return(slen);
|
|
|
|
}
|
|
|
|
|
|
|
|
int x_x_xnewstr(uint8 **p, uint8 *s)
|
|
|
|
{
|
2011-11-13 00:38:56 +01:00
|
|
|
int len = (s == NULL) ? 0 : strlen((char*)s);
|
2011-11-13 00:38:55 +01:00
|
|
|
if (*p != (uint8 *)NULL) free((char*)*p);
|
|
|
|
*p = (uint8*)xmalloc(len+1);
|
|
|
|
if (len) strcpy((char*)(*p), (char*)s);
|
|
|
|
else **p = '\0';
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dprintf(char *p, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
if (nw_debug){
|
2011-11-13 00:38:55 +01:00
|
|
|
fprintf(logfile, "%s:", get_modstr());
|
2011-11-13 00:38:55 +01:00
|
|
|
va_start(ap, p);
|
2011-11-13 00:38:55 +01:00
|
|
|
vfprintf(logfile, p, ap);
|
2011-11-13 00:38:55 +01:00
|
|
|
va_end(ap);
|
2011-11-13 00:38:55 +01:00
|
|
|
fprintf(logfile, "\n");
|
2011-11-13 00:38:55 +01:00
|
|
|
fflush(logfile);
|
2011-11-13 00:38:56 +01:00
|
|
|
fflush(logfile);
|
2011-11-13 00:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
void xdprintf(int dlevel, int mode, char *p, ...)
|
2011-11-13 00:38:55 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
if (nw_debug >= dlevel) {
|
2011-11-13 00:38:55 +01:00
|
|
|
if (!(mode & 1)) fprintf(logfile, "%s:", get_modstr());
|
|
|
|
if (p) {
|
|
|
|
va_start(ap, p);
|
|
|
|
vfprintf(logfile, p, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
if (!(mode & 2)) fprintf(logfile, "\n");
|
2011-11-13 00:38:55 +01:00
|
|
|
fflush(logfile);
|
2011-11-13 00:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
void errorp(int mode, char *what, char *p, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2011-11-13 00:38:55 +01:00
|
|
|
int errnum = errno;
|
2011-11-13 00:38:55 +01:00
|
|
|
if (errnum >= 0 && errnum < _sys_nerr)
|
2011-11-13 00:38:55 +01:00
|
|
|
fprintf(logfile, "%s:%s:%s\n", get_modstr(), what, _sys_errlist[errnum]);
|
2011-11-13 00:38:55 +01:00
|
|
|
else
|
2011-11-13 00:38:55 +01:00
|
|
|
fprintf(logfile, "%s:%s:errno=%d\n", get_modstr(), what, errnum);
|
2011-11-13 00:38:55 +01:00
|
|
|
if (p) {
|
|
|
|
va_start(ap, p);
|
|
|
|
vfprintf(logfile, p, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(logfile, "\n");
|
|
|
|
}
|
|
|
|
fflush(logfile);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
FILE *open_nw_ini(void)
|
|
|
|
{
|
|
|
|
char *fname=FILENAME_NW_INI;
|
|
|
|
FILE *f=fopen(fname, "r");
|
2011-11-13 00:38:55 +01:00
|
|
|
if (f == (FILE*)NULL) fprintf(logfile, "Cannot open ini file `%s`\n", fname);
|
2011-11-13 00:38:55 +01:00
|
|
|
return(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_ini_entry(FILE *f, int entry, char *str, int strsize)
|
2011-11-13 00:38:55 +01:00
|
|
|
/* returns ini_entry or 0 if nothing found */
|
2011-11-13 00:38:55 +01:00
|
|
|
{
|
|
|
|
char buff[512];
|
|
|
|
int do_open = ((FILE*) NULL == f);
|
|
|
|
if (do_open) f = open_nw_ini();
|
|
|
|
if ((FILE*) NULL != f) {
|
|
|
|
while (fgets((char*)buff, sizeof(buff), f) != NULL){
|
|
|
|
int len = strlen(buff);
|
|
|
|
char *ppi = NULL;
|
2011-11-13 00:38:55 +01:00
|
|
|
char *ppe = NULL;
|
2011-11-13 00:38:55 +01:00
|
|
|
int se = 0;
|
|
|
|
int j = -1;
|
|
|
|
while (++j < len){
|
|
|
|
char *pp=(buff+j);
|
|
|
|
if (*pp == '#' || *pp == '\r' || *pp == '\n') {
|
|
|
|
*pp = '\0';
|
2011-11-13 00:38:55 +01:00
|
|
|
len = j;
|
2011-11-13 00:38:55 +01:00
|
|
|
break;
|
|
|
|
} else if ( *pp == 32 || *pp == '\t') {
|
|
|
|
if (!se) se = j;
|
2011-11-13 00:38:55 +01:00
|
|
|
} else {
|
|
|
|
if ((!ppi) && se) ppi = pp;
|
|
|
|
ppe=pp;
|
2011-11-13 00:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len > se+1 && se > 0 && se < 4 && ppi){
|
|
|
|
char sx[10];
|
|
|
|
int fentry;
|
2011-11-13 00:38:56 +01:00
|
|
|
strmaxcpy((uint8*)sx, (uint8*)buff, se);
|
2011-11-13 00:38:55 +01:00
|
|
|
fentry = atoi(sx);
|
|
|
|
if (fentry > 0 && ((!entry) || entry == fentry)) {
|
2011-11-13 00:38:55 +01:00
|
|
|
if (ppe) *(ppe+1) = '\0';
|
2011-11-13 00:38:56 +01:00
|
|
|
strmaxcpy((uint8*)str, (uint8*)ppi, strsize-1);
|
2011-11-13 00:38:55 +01:00
|
|
|
if (do_open) fclose(f);
|
|
|
|
return(fentry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* while */
|
|
|
|
if (do_open) fclose(f);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_exec_path(char *buff, char *progname)
|
|
|
|
{
|
|
|
|
sprintf(buff, "%s/%s", PATHNAME_PROGS, progname);
|
|
|
|
return(buff);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
int get_ini_int(int what)
|
|
|
|
{
|
|
|
|
char buff[30];
|
|
|
|
int i;
|
|
|
|
if (get_ini_entry(NULL, what, buff, sizeof(buff))
|
|
|
|
&& 1==sscanf(buff, "%d", &i) ) return(i);
|
|
|
|
return(-1);
|
|
|
|
}
|
2011-11-13 00:38:55 +01:00
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
void get_ini_debug(int module)
|
2011-11-13 00:38:55 +01:00
|
|
|
/* what:
|
|
|
|
* 1 = nwserv
|
|
|
|
* 2 = ncpserv
|
|
|
|
* 3 = nwconn
|
2011-11-13 00:38:56 +01:00
|
|
|
* 4 = nwclient
|
2011-11-13 00:38:55 +01:00
|
|
|
*/
|
|
|
|
{
|
2011-11-13 00:38:55 +01:00
|
|
|
int debug = get_ini_int(100+module);
|
|
|
|
if (debug > -1) nw_debug = debug;
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
static void sig_segv(int isig)
|
|
|
|
{
|
|
|
|
char *s= "PANIC signal SIGSEGV" ;
|
|
|
|
XDPRINTF((0, 0, s));
|
|
|
|
fprintf(stderr, s);
|
|
|
|
exit(99);
|
|
|
|
}
|
|
|
|
|
2011-11-13 00:38:55 +01:00
|
|
|
void init_tools(int module)
|
|
|
|
{
|
|
|
|
char buff[300];
|
|
|
|
char logfilename[300];
|
|
|
|
FILE *f=open_nw_ini();
|
|
|
|
int withlog=0;
|
|
|
|
int dodaemon=0;
|
|
|
|
int new_log=0;
|
2011-11-13 00:38:55 +01:00
|
|
|
in_module = module;
|
2011-11-13 00:38:55 +01:00
|
|
|
if (f) {
|
|
|
|
int what;
|
|
|
|
while (0 != (what=get_ini_entry(f, 0, buff, sizeof(buff)))) { /* daemonize */
|
|
|
|
if (200 == what) dodaemon = atoi(buff);
|
|
|
|
else if (201 == what) {
|
2011-11-13 00:38:56 +01:00
|
|
|
strmaxcpy((uint8*)logfilename, (uint8*)buff, sizeof(logfilename)-1);
|
2011-11-13 00:38:55 +01:00
|
|
|
withlog++;
|
|
|
|
} else if (202 == what) {
|
|
|
|
new_log = atoi(buff);
|
|
|
|
} else if (100+module == what) nw_debug=atoi(buff);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
if (dodaemon) {
|
|
|
|
if (!withlog) strcpy(logfilename, "./nw.log");
|
|
|
|
if (NWSERV == module) { /* now make daemon */
|
|
|
|
int fd=fork();
|
|
|
|
if (fd) exit((fd > 0) ? 0 : 1);
|
|
|
|
}
|
|
|
|
if (NULL == (logfile = fopen(logfilename,
|
|
|
|
(new_log && NWSERV == module) ? "w" : "a"))) {
|
|
|
|
char sxx[100];
|
|
|
|
sprintf(sxx, "\n\nOpen logfile `%s`", logfilename);
|
|
|
|
perror(sxx);
|
|
|
|
logfile = stdout;
|
|
|
|
fprintf(stderr, "\n!! ABORTED !!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (NWSERV == module) setsid();
|
|
|
|
}
|
2011-11-13 00:38:55 +01:00
|
|
|
if (NWSERV == module || NCPSERV == module) {
|
|
|
|
XDPRINTF((1, 0, "Starting Version: %d.%02dpl%d",
|
|
|
|
_VERS_H_, _VERS_L_, _VERS_P_ ));
|
|
|
|
}
|
2011-11-13 00:38:55 +01:00
|
|
|
signal(SIGSEGV, sig_segv);
|
2011-11-13 00:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit_tools(int what)
|
|
|
|
{
|
|
|
|
if (logfile != stdout) {
|
|
|
|
if (logfile != NULL) fclose(logfile);
|
|
|
|
logfile=stdout;
|
2011-11-13 00:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 down_char(uint8 ch)
|
|
|
|
{
|
|
|
|
if (ch > 64 && ch < 91) return(ch + 32);
|
|
|
|
switch(ch){
|
|
|
|
case 142: ch = 132; break;
|
|
|
|
case 153: ch = 148; break;
|
|
|
|
case 154: ch = 129; break;
|
|
|
|
default :break;
|
|
|
|
}
|
|
|
|
return(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 up_char(uint8 ch)
|
|
|
|
{
|
|
|
|
if (ch > 96 && ch < 123) return(ch - 32);
|
|
|
|
switch(ch) {
|
|
|
|
case 132: ch = 142; break;
|
|
|
|
case 148: ch = 153; break;
|
|
|
|
case 129: ch = 154; break;
|
|
|
|
default : break;
|
|
|
|
}
|
|
|
|
return(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 *upstr(uint8 *s)
|
|
|
|
{
|
|
|
|
if (!s) return((uint8*)NULL);
|
|
|
|
for (;*s;s++) *s=up_char(*s);
|
|
|
|
return(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 *downstr(uint8 *s)
|
|
|
|
{
|
|
|
|
if (!s) return((uint8*)NULL);
|
|
|
|
for (;*s;s++) *s=down_char(*s);
|
|
|
|
return(s);
|
|
|
|
}
|
|
|
|
|