]>
git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 // #define LEGACY_MY_ATOLL
28 // #define LEGACY_TESTVALIDUSERCHAR
30 #include "include/conf.h"
31 #include "include/defs.h"
33 #if defined(__MINGW32__) && defined(HAVE_DIRECT_H)
38 #if defined(HAVE_BACKTRACE)
39 #define USE_GETWORD_BACKTRACE 1
41 #define USE_GETWORD_BACKTRACE 0
44 static char mtab1
[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
46 //! The list of the HTTP codes to exclude from the report.
47 static char *excludecode
=NULL
;
49 //! Directory where the images are stored.
50 char ImageDir
[MAXLEN
]=IMAGEDIR
;
52 extern char *CurrentLocale
;
54 #if USE_GETWORD_BACKTRACE
55 static void getword_backtrace(void)
61 n
=backtrace(buffer
,sizeof(buffer
)/sizeof(buffer
[0]));
63 calls
=backtrace_symbols(buffer
,n
);
65 debuga(__FILE__
,__LINE__
,_("getword backtrace:\n"));
66 for (i
=0 ; i
<n
; i
++) {
67 fprintf(stderr
,"SARG: %d:%s\n",i
+1,calls
[i
]);
72 #endif //USE_GETWORD_BACKTRACE
74 void getword_start(struct getwordstruct
*gwarea
, const char *line
)
76 gwarea
->beginning
=line
;
81 void getword_restart(struct getwordstruct
*gwarea
)
83 if (gwarea
->modified
) {
84 debuga(__FILE__
,__LINE__
,_("Cannot parse again the line as it was modified\n"));
87 gwarea
->current
=gwarea
->beginning
;
90 int getword(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
94 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++) {
97 TRANSLATORS: The %s is the name of the function reporting the
100 debuga(__FILE__
,__LINE__
,_("End of word not found in %s after %d bytes.\n"),__func__
,x
);
101 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
102 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
103 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
104 word
[(limit
>0) ? limit
-1 : 0]='\0';
105 #if USE_GETWORD_BACKTRACE
110 word
[x
] = gwarea
->current
[x
];
114 if (gwarea
->current
[x
]) ++x
;
119 int getword_limit(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
124 for(x
=0; x
<limit
&& gwarea
->current
[x
] && gwarea
->current
[x
] != stop
;x
++) {
125 word
[x
] = gwarea
->current
[x
];
129 while (*gwarea
->current
&& *gwarea
->current
!= stop
) gwarea
->current
++;
130 if (*gwarea
->current
) ++gwarea
->current
;
134 int getword_multisep(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
138 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++) {
140 debuga(__FILE__
,__LINE__
,_("End of word not found in %s after %d bytes.\n"),__func__
,x
);
141 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
142 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
143 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
144 if (limit
>0) word
[limit
-1]='\0';
145 #if USE_GETWORD_BACKTRACE
148 //exit(EXIT_FAILURE);
151 word
[x
] = gwarea
->current
[x
];
155 while (gwarea
->current
[x
] && gwarea
->current
[x
]==stop
) ++x
;
160 int getword_skip(int limit
, struct getwordstruct
*gwarea
, char stop
)
164 for(x
=0;(gwarea
->current
[x
] && (gwarea
->current
[x
] != stop
));x
++) {
166 debuga(__FILE__
,__LINE__
,_("End of word not found in %s after %d bytes.\n"),__func__
,x
);
167 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
168 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
169 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
170 #if USE_GETWORD_BACKTRACE
177 if (gwarea
->current
[x
]) ++x
;
182 int getword_atoll(long long int *number
, struct getwordstruct
*gwarea
, char stop
)
188 if (gwarea
->current
[0] == '-') {
191 } else if (gwarea
->current
[0] == '+') {
195 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
196 digit
=gwarea
->current
[x
]-'0';
197 if (*number
>= (LLONG_MAX
-digit
)/10) {
199 TRANSLATORS: The first %s is the function name (in the source code) where the
200 overflow is detected.
202 debuga(__FILE__
,__LINE__
,_("Integer overflow detected in %s in line %s\n"),__func__
,gwarea
->beginning
);
205 *number
=(*number
* 10) + digit
;
207 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
209 TRANSLATORS: The %s is the function name, in the source code, where the problem occured.
211 debuga(__FILE__
,__LINE__
,_("End of number not found in %s after %d bytes.\n"),__func__
,x
);
212 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
213 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
214 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
215 #if USE_GETWORD_BACKTRACE
222 if (gwarea
->current
[x
]) ++x
;
227 int getword_atoi(int *number
, struct getwordstruct
*gwarea
, char stop
)
233 if (gwarea
->current
[0] == '-') {
236 } else if (gwarea
->current
[0] == '+') {
240 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
241 digit
=gwarea
->current
[x
]-'0';
242 if (*number
> (INT_MAX
-digit
)/10) {
243 debuga(__FILE__
,__LINE__
,_("Integer overflow detected in %s in line %s\n"),__func__
,gwarea
->beginning
);
246 *number
=(*number
* 10) + digit
;
248 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
249 debuga(__FILE__
,__LINE__
,_("End of number not found in %s after %d bytes.\n"),__func__
,x
);
250 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
251 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
252 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
253 #if USE_GETWORD_BACKTRACE
260 if (gwarea
->current
[x
]) ++x
;
265 int getword_atol(long int *number
, struct getwordstruct
*gwarea
, char stop
)
271 if (gwarea
->current
[0] == '-') {
274 } else if (gwarea
->current
[0] == '+') {
278 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
279 digit
=gwarea
->current
[x
]-'0';
280 if (*number
> (LONG_MAX
-digit
)/10) {
281 debuga(__FILE__
,__LINE__
,_("Integer overflow detected in %s in line %s\n"),__func__
,gwarea
->beginning
);
284 *number
=(*number
* 10) + digit
;
286 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
287 debuga(__FILE__
,__LINE__
,_("End of number not found in %s after %d bytes.\n"),__func__
,x
);
288 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
289 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
290 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
291 #if USE_GETWORD_BACKTRACE
298 if (gwarea
->current
[x
]) ++x
;
303 int getword_atolu(unsigned long int *number
, struct getwordstruct
*gwarea
, char stop
)
308 if (gwarea
->current
[0] == '-') {
309 debuga(__FILE__
,__LINE__
,_("getword_atolu got a negative number.\n"));
310 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
311 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
314 if (gwarea
->current
[0] == '+') {
318 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
319 digit
=gwarea
->current
[x
]-'0';
320 if (*number
> (ULONG_MAX
-digit
)/10) {
321 debuga(__FILE__
,__LINE__
,_("Integer overflow detected in %s in line %s\n"),__func__
,gwarea
->beginning
);
324 *number
=(*number
* 10) + digit
;
326 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
327 debuga(__FILE__
,__LINE__
,_("End of number not found in %s after %d bytes.\n"),__func__
,x
);
328 debuga(__FILE__
,__LINE__
,_("Line=\"%s\"\n"),gwarea
->beginning
);
329 debuga(__FILE__
,__LINE__
,_("Record=\"%s\"\n"),gwarea
->current
);
330 debuga(__FILE__
,__LINE__
,_("searching for \'x%x\'\n"),stop
);
331 #if USE_GETWORD_BACKTRACE
337 if (gwarea
->current
[x
]) ++x
;
343 int getword_ptr(char *orig_line
,char **word
, struct getwordstruct
*gwarea
, char stop
)
346 \note Why pass the original buffer to the function ? Because we must modify it to
347 insert the terminating ASCII zero for the word we return and that's not compatible
348 with getword_restart(). Moreover, getword_start() sometime works on constant strings
349 so this function require the original buffer to detect any missuse.
355 if (orig_line
&& orig_line
!=gwarea
->beginning
) {
356 debuga(__FILE__
,__LINE__
,_("Invalid buffer passed to getword_ptr\n"));
360 start
=(gwarea
->current
-gwarea
->beginning
);
361 if (word
&& orig_line
) *word
=orig_line
+start
;
362 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++);
363 sep
=(gwarea
->current
[x
]!='\0');
364 if (word
&& orig_line
) orig_line
[start
+x
] = '\0';
371 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
372 long long int my_atoll (const char *nptr
)
374 long long int returnval
=0LL;
375 int max_digits
= MAXLLL
;
377 // Soak up all the white space
378 while (isspace( *nptr
)) {
382 //For each character left to right
383 //change the character to a single digit
384 //multiply what we had before by 10 and add the new digit
386 while (--max_digits
&& isdigit( *nptr
))
388 returnval
= ( returnval
* 10 ) + ( *nptr
++ - '0' ) ;
394 int is_absolute(const char *path
)
396 if (*path
=='/') return(1);
398 if (isalpha(path
[0]) && path
[1]==':') return(1);
403 int PortableMkDir(const char *path
,int mode
)
405 #if defined(__linux__)
406 int mkerror
=mkdir(path
,mode
);
409 int mkerror
=_mkdir(path
);
414 void my_mkdir(const char *name
)
420 if(!is_absolute(name
)) {
421 debuga(__FILE__
,__LINE__
,_("Invalid path (%s). Please, use absolute paths only.\n"),name
);
426 for (i
=0 ; name
[i
] ; i
++) {
428 debuga(__FILE__
,__LINE__
,_("Path too long: "));
429 debuga_more("%s\n",name
);
432 if (chars
>0 && name
[i
] == '/') {
434 if (access(w0
, R_OK
) != 0) {
435 if (PortableMkDir(w0
,0755)) {
436 debuga(__FILE__
,__LINE__
,_("Cannot create directory \"%s\": %s\n"),w0
,strerror(errno
));
441 if (name
[i
] != '/') chars
++;
445 if (access(name
, R_OK
) != 0) {
446 if (PortableMkDir(name
,0755)) {
447 debuga(__FILE__
,__LINE__
,_("Cannot create directory \"%s\": %s\n"),name
,strerror(errno
));
454 void my_lltoa(unsigned long long int n
, char *s
, int ssize
, int len
)
463 debuga(__FILE__
,__LINE__
,_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len
,ssize
);
468 s
[slen
++] = (n
% 10) + '0';
469 } while ((n
/= 10) > 0 && slen
<ssize
);
472 for (i
= 0, j
= slen
-1; i
<j
; i
++, j
--) {
480 for(j
=slen
; j
>=0; j
--)
487 int month2num(const char *month
)
491 for(m
=0 ; m
<12 && strcmp(mtab1
[m
],month
) != 0; m
++);
495 int builddia(int day
, int month
, int year
)
497 return(year
*10000+month
*100+day
);
503 \param date1 The first date to compare.
504 \param date2 The second date to compare.
506 \retval -1 If date1<date2.
507 \retval 0 If date1==date2.
508 \retval 1 if date1>date2.
510 int compare_date(const struct tm
*date1
,const struct tm
*date2
)
512 if (date1
->tm_year
<date2
->tm_year
) return(-1);
513 if (date1
->tm_year
>date2
->tm_year
) return(1);
514 if (date1
->tm_mon
<date2
->tm_mon
) return(-1);
515 if (date1
->tm_mon
>date2
->tm_mon
) return(1);
516 if (date1
->tm_mday
<date2
->tm_mday
) return(-1);
517 if (date1
->tm_mday
>date2
->tm_mday
) return(1);
518 if (date1
->tm_hour
<date2
->tm_hour
) return(-1);
519 if (date1
->tm_hour
>date2
->tm_hour
) return(1);
520 if (date1
->tm_min
<date2
->tm_min
) return(-1);
521 if (date1
->tm_min
>date2
->tm_min
) return(1);
522 if (date1
->tm_sec
<date2
->tm_sec
) return(-1);
523 if (date1
->tm_sec
>date2
->tm_sec
) return(1);
527 void buildymd(const char *dia
, const char *mes
, const char *ano
, char *wdata
,int wdata_size
)
532 snprintf(wdata
,wdata_size
,"%04d%02d%02d",atoi(ano
),nmes
+1,atoi(dia
));
536 int conv_month(const char *month
)
540 for(x
=0; x
<12 && strncmp(mtab1
[x
],month
,3)!=0; x
++);
545 const char *conv_month_name(int month
)
549 if (month
<1 || month
>12) {
550 snprintf(str
,sizeof(str
),"%03d",month
);
553 return(mtab1
[month
-1]);
557 Write a debug message to stderr. The message is prefixed by "SARG:" to identify its origin.
559 \param msg The printf like message to format.
560 \param ... The arguments to format in the message.
562 void debuga(const char *File
,int Line
,const char *msg
,...)
566 if (debugz
>=LogLevel_Source
) {
567 /* The path is removed because every source file is in the same directory.
568 * There is no point in reporting the full path from the build directory.
570 const char *ptr
=strrchr(File
,'/');
572 /* TRANSLATORS: This is the prefix to stderr messages when the debug level is
573 set to display the source file (%s) and the line number (%d). */
574 fprintf(stderr
,_("SARG(%s:%d): "),ptr
,Line
);
576 /* TRANSLATORS: This is the prefix to stderr messages when the debug level
578 fputs(_("SARG: "),stderr
);
581 vfprintf(stderr
,msg
,ap
);
586 Write a debug message to stderr. The message is supposed
587 to be displayed after a message from debuga().
589 \param msg The printf like message to format.
590 \param ... The arguments to format in the message.
592 void debuga_more(const char *msg
,...)
597 vfprintf(stderr
,msg
,ap
);
602 Write a debug message to stderr. The message is prefixed by "SARG: (info)".
604 \param msg The printf like message to format.
605 \param ... The arguments to format in the message.
607 void debugaz(const char *File
,int Line
,const char *msg
,...)
611 if (debugz
>=LogLevel_Source
) {
612 /* The path is removed because every source file is in the same directory.
613 * There is no point in reporting the full path from the build directory.
615 const char *ptr
=strrchr(File
,'/');
617 /* TRANSLATORS: This is the prefix to information messages when the debug level is
618 set to display the source file (%s) and the line number (%d). */
619 fprintf(stderr
,_("SARG(%s:%d): (info) "),ptr
,Line
);
621 /* TRANSLATORS: This is the prefix to information messages when the debug level
623 fputs(_("SARG: (info) "),stderr
);
626 vfprintf(stderr
,msg
,ap
);
631 char *fixnum(long long int value
, int n
)
633 #define MAXLEN_FIXNUM 256
634 char num
[MAXLEN_FIXNUM
]="";
635 char buf
[MAXLEN_FIXNUM
* 2];
637 static char ret
[MAXLEN_FIXNUM
* 2];
639 register int i
, j
, k
;
641 static char abbrev
[30]="";
643 my_lltoa(value
, num
, sizeof(num
), 0);
645 if(DisplayedValues
==DISPLAY_ABBREV
) {
646 numlen
= strlen(num
);
649 else if (numlen
%3 == 1) {
651 abbrev
[1]=(UseComma
) ? ',' : '.';
656 else if (numlen
%3 == 2) {
659 abbrev
[2]=(UseComma
) ? ',' : '.';
664 else if (numlen
%3 == 0) {
668 abbrev
[3]=(UseComma
) ? ',' : '.';
677 else if (numlen
<= 6)
679 else if (numlen
<= 9)
681 else if (numlen
<= 12)
683 else if (numlen
<= 15)
685 else if (numlen
>= 18)
687 else if (numlen
<= 21)
689 else if (numlen
<= 24)
691 else if (numlen
<= 27)
694 strcat(abbrev
,"???");
699 memset(buf
,0,MAXLEN_FIXNUM
*2);
705 for ( i
= strlen(num
) - 1, j
= 0 ; i
> -1; i
--) {
706 if ( k
== 2 && i
!= 0 ) {
709 pbuf
[j
++] = (UseComma
) ? ',' : '.';
719 for ( i
= strlen(pbuf
) - 1, j
= 0 ; i
> -1; i
--, j
++)
728 char *fixnum2(long long int value
, int n
)
730 #define MAXLEN_FIXNUM2 1024
731 char num
[MAXLEN_FIXNUM2
];
732 char buf
[MAXLEN_FIXNUM2
* 2];
734 static char ret
[MAXLEN_FIXNUM2
* 2];
736 register int i
, j
, k
;
738 my_lltoa(value
, num
, sizeof(num
), 0);
739 memset(buf
,0,MAXLEN_FIXNUM2
*2);
745 for ( i
= strlen(num
) - 1, j
= 0 ; i
> -1; i
--) {
746 if ( k
== 2 && i
!= 0 ) {
749 pbuf
[j
++] = (UseComma
) ? ',' : '.';
759 for ( i
= strlen(pbuf
) - 1, j
= 0 ; i
> -1; i
--, j
++)
768 char *buildtime(long long int elap
)
770 long int num
= elap
/ 1000LL;
777 min
=(num
% 3600L) / 60L;
779 snprintf(buf
,sizeof(buf
),"%02d:%02d:%02d",hor
,min
,sec
);
786 Get the date stored in the <tt>sarg-date</tt> file of a directory with the connection data.
788 \param dirname The directory to look for the connection directory.
789 \param name The name of the directory whose <tt>sarg-date</tt> file must be read.
790 \param data The buffer to store the content of the file. It must be more than 80
794 \retval -1 File not found.
796 int obtdate(const char *dirname
, const char *name
, char *data
)
801 if (snprintf(wdir
,sizeof(wdir
),"%s%s/sarg-date",dirname
,name
)>=sizeof(wdir
)) {
802 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
803 debuga_more("%s%s/sarg-date",dirname
,name
);
806 if ((fp_in
= fopen(wdir
, "rt")) == 0) {
807 if (snprintf(wdir
,sizeof(wdir
),"%s%s/date",dirname
,name
)>=sizeof(wdir
)) {
808 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
809 debuga_more("%s%s/date",dirname
,name
);
812 if ((fp_in
= fopen(wdir
, "rt")) == 0) {
818 if (!fgets(data
,80,fp_in
)) {
819 debuga(__FILE__
,__LINE__
,_("Failed to read the date in file \"%s\"\n"),wdir
);
822 if (fclose(fp_in
)==EOF
) {
823 debuga(__FILE__
,__LINE__
,_("Read error in \"%s\": %s\n"),wdir
,strerror(errno
));
832 void formatdate(char *date
,int date_size
,int year
,int month
,int day
,int hour
,int minute
,int second
,int dst
)
838 memset(<m
,0,sizeof(ltm
));
839 if (year
>=1900) ltm
.tm_year
=year
-1900;
840 if (month
>=1 && month
<=12) ltm
.tm_mon
=month
-1;
841 if (day
>=1 && day
<=31) ltm
.tm_mday
=day
;
842 if (hour
>=0 && hour
<24) ltm
.tm_hour
=hour
;
843 if (minute
>=0 && minute
<60) ltm
.tm_min
=minute
;
844 if (second
>=0 && second
<60) ltm
.tm_sec
=second
;
846 unixtime
=mktime(<m
); //fill the missing entries
847 fulltm
=localtime(&unixtime
);
848 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
849 strftime(date
,date_size
,"%c",fulltm
);
853 void computedate(int year
,int month
,int day
,struct tm
*t
)
855 memset(t
,0,sizeof(*t
));
856 t
->tm_year
=year
-1900;
862 int obtuser(const char *dirname
, const char *name
)
869 if (snprintf(wdir
,sizeof(wdir
),"%s%s/sarg-users",dirname
,name
)>=sizeof(wdir
)) {
870 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
871 debuga_more("%s%s/sarg-users",dirname
,name
);
874 if((fp_in
=fopen(wdir
,"r"))==NULL
) {
875 if (snprintf(wdir
,sizeof(wdir
),"%s%s/users",dirname
,name
)>=sizeof(wdir
)) {
876 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
877 debuga_more("%s%s/users",dirname
,name
);
880 if((fp_in
=fopen(wdir
,"r"))==NULL
) {
885 if (!fgets(tuser
,sizeof(tuser
),fp_in
)) {
886 debuga(__FILE__
,__LINE__
,_("Failed to read the number of users in file \"%s\"\n"),wdir
);
889 if (fclose(fp_in
)==EOF
) {
890 debuga(__FILE__
,__LINE__
,_("Read error in \"%s\": %s\n"),wdir
,strerror(errno
));
899 void obttotal(const char *dirname
, const char *name
, int nuser
, long long int *tbytes
, long long int *media
)
904 char user
[MAX_USER_LEN
];
906 struct getwordstruct gwarea
;
912 if (snprintf(wdir
,sizeof(wdir
),"%s%s/sarg-general",dirname
,name
)>=sizeof(wdir
)) {
913 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
914 debuga_more("%s%s/sarg-general",dirname
,name
);
917 if ((fp_in
= FileObject_Open(wdir
)) == NULL
) {
918 if (snprintf(wdir
,sizeof(wdir
),"%s%s/general",dirname
,name
)>=sizeof(wdir
)) {
919 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
920 debuga_more("%s%s/general",dirname
,name
);
923 if ((fp_in
= FileObject_Open(wdir
)) == NULL
) {
928 if ((line
=longline_create())==NULL
) {
929 debuga(__FILE__
,__LINE__
,_("Not enough memory to read file \"%s\"\n"),wdir
);
933 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
934 if (strncmp(buf
,"TOTAL\t",6) == 0)
936 else if (strncmp(buf
,"TOTAL ",6) == 0)
940 getword_start(&gwarea
,buf
);
941 if (getword(user
,sizeof(user
),&gwarea
,sep
)<0) {
942 debuga(__FILE__
,__LINE__
,_("Invalid user in file \"%s\"\n"),wdir
);
945 if(strcmp(user
,"TOTAL") != 0)
947 if (getword_skip(MAXLEN
,&gwarea
,sep
)<0) {
948 debuga(__FILE__
,__LINE__
,_("Invalid total number of accesses in file \"%s\"\n"),wdir
);
951 if (getword_atoll(tbytes
,&gwarea
,sep
)<0) {
952 debuga(__FILE__
,__LINE__
,_("Invalid number of bytes in file \"%s\"\n"),wdir
);
957 if (FileObject_Close(fp_in
)) {
958 debuga(__FILE__
,__LINE__
,_("Read error in \"%s\": %s\n"),wdir
,FileObject_GetLastCloseError());
961 longline_destroy(&line
);
966 *media
=*tbytes
/ nuser
;
970 int getperiod_fromsarglog(const char *arqtt
,struct periodstruct
*period
)
973 int day0
, month0
, year0
, hour0
, minute0
;
974 int day1
, month1
, year1
, hour1
, minute1
;
977 memset(period
,0,sizeof(*period
));
980 while((str
=strstr(str
,"sarg-"))!=NULL
) {
982 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
983 day0
=(str
[0]-'0')*10+(str
[1]-'0');
984 if (day0
<1 || day0
>31) continue;
986 month0
=(str
[0]-'0')*10+(str
[1]-'0')-1;
987 if (month0
<0 || month0
>11) continue;
990 for (i
=0 ; isdigit(str
[i
]) && i
<4 ; i
++) year0
=year0
*10+(str
[i
]-'0');
993 if (str
[0]!='_') continue;
996 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
997 hour0
=(str
[0]-'0')*10+(str
[1]-'0');
999 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
1000 minute0
=(str
[0]-'0')*10+(str
[1]-'0');
1003 if (*str
!= '-') continue;
1006 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
1007 day1
=(str
[0]-'0')*10+(str
[1]-'0');
1008 if (day1
<1 || day1
>31) continue;
1010 month1
=(str
[0]-'0')*10+(str
[1]-'0')-1;
1011 if (month1
<0 || month1
>11) continue;
1014 for (i
=0 ; isdigit(str
[i
]) && i
<4 ; i
++) year1
=year1
*10+(str
[i
]-'0');
1018 if (str
[0]!='_') continue;
1021 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
1022 hour1
=(str
[0]-'0')*10+(str
[1]-'0');
1024 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
1025 minute1
=(str
[0]-'0')*10+(str
[1]-'0');
1028 period
->start
.tm_mday
=day0
;
1029 period
->start
.tm_mon
=month0
;
1030 period
->start
.tm_year
=year0
-1900;
1031 period
->start
.tm_hour
=hour0
;
1032 period
->start
.tm_min
=minute0
;
1033 period
->end
.tm_mday
=day1
;
1034 period
->end
.tm_mon
=month1
;
1035 period
->end
.tm_year
=year1
-1900;
1036 period
->end
.tm_hour
=hour1
;
1037 period
->end
.tm_min
=minute1
;
1044 Fill the period with the specified range.
1046 \param period The period to change.
1047 \param dfrom The start date in the form year*10000+month*100+day.
1048 \param duntil The end date in the form year*10000+month*100+day.
1050 void getperiod_fromrange(struct periodstruct
*period
,int dfrom
,int duntil
)
1052 memset(&period
->start
,0,sizeof(period
->start
));
1053 period
->start
.tm_mday
=dfrom
%100;
1054 period
->start
.tm_mon
=(dfrom
/100)%100-1;
1055 period
->start
.tm_year
=(dfrom
/10000)-1900;
1057 memset(&period
->end
,0,sizeof(period
->end
));
1058 period
->end
.tm_mday
=duntil
%100;
1059 period
->end
.tm_mon
=(duntil
/100)%100-1;
1060 period
->end
.tm_year
=(duntil
/10000)-1900;
1064 Get the range from a period.
1066 \param period The period to convert to a range.
1067 \param dfrom The variable to store the range beginning. It can be NULL.
1068 \param duntil The variable to store the range end. It can be NULL.
1070 void getperiod_torange(const struct periodstruct
*period
,int *dfrom
,int *duntil
)
1073 *dfrom
=(period
->start
.tm_year
+1900)*10000+(period
->start
.tm_mon
+1)*100+period
->start
.tm_mday
;
1075 *duntil
=(period
->end
.tm_year
+1900)*10000+(period
->end
.tm_mon
+1)*100+period
->end
.tm_mday
;
1079 Update the \a main period to encompass the period in \a candidate.
1081 void getperiod_merge(struct periodstruct
*main
,struct periodstruct
*candidate
)
1086 mdate
=(main
->start
.tm_year
)*10000+(main
->start
.tm_mon
)*100+main
->start
.tm_mday
;
1087 cdate
=(candidate
->start
.tm_year
)*10000+(candidate
->start
.tm_mon
)*100+candidate
->start
.tm_mday
;
1088 if (cdate
<mdate
) memcpy(&main
->start
,&candidate
->start
,sizeof(struct tm
));
1090 mdate
=(main
->end
.tm_year
)*10000+(main
->end
.tm_mon
)*100+main
->end
.tm_mday
;
1091 cdate
=(candidate
->end
.tm_year
)*10000+(candidate
->end
.tm_mon
)*100+candidate
->end
.tm_mday
;
1092 if (cdate
>mdate
) memcpy(&main
->end
,&candidate
->end
,sizeof(struct tm
));
1095 int getperiod_buildtext(struct periodstruct
*period
)
1099 char text1
[40], text2
[40];
1102 i
=strftime(text1
, sizeof(text1
), "%Y %b %d", &period
->start
);
1103 } else if(df
=='e') {
1104 i
=strftime(text1
, sizeof(text1
), "%d %b %Y", &period
->start
);
1105 } else /*if (df=='w')*/ {
1106 IndexTree
=INDEX_TREE_FILE
;
1107 i
=strftime(text1
, sizeof(text1
), "%Y.%U", &period
->start
);
1109 if (i
== 0) return(-1);
1111 range
=(period
->start
.tm_year
!=period
->end
.tm_year
||
1112 period
->start
.tm_mon
!=period
->end
.tm_mon
||
1113 period
->start
.tm_mday
!=period
->end
.tm_mday
);
1116 i
=strftime(text2
, sizeof(text2
)-i
, "%Y %b %d", &period
->end
);
1117 } else if (df
=='e') {
1118 i
=strftime(text2
, sizeof(text2
)-i
, "%d %b %Y", &period
->end
);
1120 i
=strftime(text2
, sizeof(text2
)-i
, "%Y.%U", &period
->end
);
1122 if (i
== 0) return(-1);
1126 snprintf(period
->text
,sizeof(period
->text
),"%s-%s",text1
,text2
);
1127 snprintf(period
->html
,sizeof(period
->html
),"%s—%s",text1
,text2
);
1129 safe_strcpy(period
->text
,text1
,sizeof(period
->text
));
1130 safe_strcpy(period
->html
,text1
,sizeof(period
->html
));
1135 static void copy_images(void)
1137 FILE *img_in
, *img_ou
;
1139 char srcfile
[MAXLEN
];
1140 char dstfile
[MAXLEN
];
1142 struct dirent
*direntp
;
1143 char buffer
[MAXLEN
];
1147 if (snprintf(images
,sizeof(images
),"%simages",outdir
)>=sizeof(images
)) {
1148 debuga(__FILE__
,__LINE__
,_("Cannot copy images to target directory %simages\n"),outdir
);
1151 if (access(images
,R_OK
)!=0) {
1152 if (PortableMkDir(images
,0755)) {
1153 debuga(__FILE__
,__LINE__
,_("Cannot create directory \"%s\": %s\n"),images
,strerror(errno
));
1158 dirp
= opendir(ImageDir
);
1160 debuga(__FILE__
,__LINE__
,_("Cannot open directory \"%s\": %s\n"),ImageDir
,strerror(errno
));
1163 while ((direntp
= readdir( dirp
)) != NULL
){
1164 if(direntp
->d_name
[0]=='.')
1166 if (snprintf(srcfile
,sizeof(srcfile
),"%s/%s",ImageDir
,direntp
->d_name
)>=sizeof(srcfile
)) {
1167 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
1168 debuga_more("%s/%s",ImageDir
,direntp
->d_name
);
1171 if (stat(srcfile
,&info
)) {
1172 debuga(__FILE__
,__LINE__
,_("Cannot stat \"%s\": %s\n"),srcfile
,strerror(errno
));
1175 if (S_ISREG(info
.st_mode
)) {
1176 if (snprintf(dstfile
,sizeof(dstfile
),"%s/%s",images
,direntp
->d_name
)>=sizeof(dstfile
)) {
1177 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
1178 debuga_more("%s/%s",images
,direntp
->d_name
);
1181 img_in
= fopen(srcfile
, "rb");
1183 img_ou
= fopen(dstfile
, "wb");
1185 while ((nread
= fread(buffer
,1,sizeof(buffer
),img_in
))>0) {
1186 if (fwrite(buffer
,1,nread
,img_ou
)!=nread
) {
1187 debuga(__FILE__
,__LINE__
,_("Failed to copy image %s to %s\n"),srcfile
,dstfile
);
1191 if (fclose(img_ou
)==EOF
) {
1192 debuga(__FILE__
,__LINE__
,_("Write error in \"%s\": %s\n"),dstfile
,strerror(errno
));
1196 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"), dstfile
, strerror(errno
));
1197 if (fclose(img_in
)==EOF
) {
1198 debuga(__FILE__
,__LINE__
,_("Read error in \"%s\": %s\n"),srcfile
,strerror(errno
));
1202 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"), srcfile
, strerror(errno
));
1205 (void) closedir(dirp
);
1211 * Check if the proposed file name conforms to the directory structure layed out
1212 * as a file tree. It is used to check if the file name enumerated while scanning
1213 * a directory content may have been created by sarg running with IndexTree set to
1216 bool IsTreeFileDirName(const char *Name
)
1221 // start year (date format u) or start day (date format e)
1222 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1224 if (isdigit(Name
[2]) && isdigit(Name
[3]))
1226 // date format is either u or w
1230 if (!isdigit(Name
[5]) || !isdigit(Name
[6])) return(false);
1231 return(true);//date format w is confirmed
1238 if (!isalpha(Name
[0]) || !isalpha(Name
[1]) || !isalpha(Name
[2])) return(false);
1239 for (i
=11 ; i
>=0 && memcmp(mtab1
[i
],Name
,3) ; i
--);
1240 if (i
<0) return(false);
1244 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1249 else if (isalpha(Name
[2]) && isalpha(Name
[3]) && isalpha(Name
[4]))
1255 if (!isalpha(Name
[0]) || !isalpha(Name
[1]) || !isalpha(Name
[2])) return(false);
1256 for (i
=11 ; i
>=0 && memcmp(mtab1
[i
],Name
,3) ; i
--);
1257 if (i
<0) return(false);
1261 if (!isdigit(Name
[0]) || !isdigit(Name
[1]) || !isdigit(Name
[2]) || !isdigit(Name
[3])) return(false);
1269 if (Name
[0]!='-') return(false);
1272 if (DateFormat
=='u')
1274 if (!isdigit(Name
[0]) || !isdigit(Name
[1]) || !isdigit(Name
[2]) || !isdigit(Name
[3])) return(false);
1277 if (!isalpha(Name
[0]) || !isalpha(Name
[1]) || !isalpha(Name
[2])) return(false);
1278 for (i
=11 ; i
>=0 && memcmp(mtab1
[i
],Name
,3) ; i
--);
1279 if (i
<0) return(false);
1282 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1285 else //DateFormat=='e'
1287 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1290 if (!isalpha(Name
[0]) || !isalpha(Name
[1]) || !isalpha(Name
[2])) return(false);
1291 for (i
=11 ; i
>=0 && memcmp(mtab1
[i
],Name
,3) ; i
--);
1292 if (i
<0) return(false);
1295 if (!isdigit(Name
[0]) || !isdigit(Name
[1]) || !isdigit(Name
[2]) || !isdigit(Name
[3])) return(false);
1299 * The directory name may contains additional characters such as a counter if
1300 * a previous report is never overwritten.
1306 * Check if the proposed file name can be the year part of a report tree build with
1307 * IndexTree set to INDEX_TREE_DATE.
1309 bool IsTreeYearFileName(const char *Name
)
1311 if (!isdigit(Name
[0]) || !isdigit(Name
[1]) || !isdigit(Name
[2]) || !isdigit(Name
[3])) return(false);
1316 if (!isdigit(Name
[0]) || !isdigit(Name
[1]) || !isdigit(Name
[2]) || !isdigit(Name
[3])) return(false);
1319 if (Name
[0]) return(false);
1324 * Check if the proposed file name can be the month part of a report tree build with
1325 * IndexTree set to INDEX_TREE_DATE.
1327 bool IsTreeMonthFileName(const char *Name
)
1331 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1332 m
=(Name
[0]-'0')*10+(Name
[1]-'0');
1333 if (m
<1 || m
>12) return(false);
1338 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1339 m
=(Name
[0]-'0')*10+(Name
[1]-'0');
1340 if (m
<1 || m
>12) return(false);
1343 if (Name
[0]) return(false);
1348 * Check if the proposed file name can be the day part of a report tree build with
1349 * IndexTree set to INDEX_TREE_DATE.
1351 bool IsTreeDayFileName(const char *Name
)
1355 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1356 d
=(Name
[0]-'0')*10+(Name
[1]-'0');
1357 if (d
<1 || d
>31) return(false);
1361 if (!isdigit(Name
[0]) || !isdigit(Name
[1])) return(false);
1362 d
=(Name
[0]-'0')*10+(Name
[1]-'0');
1363 if (d
<1 || d
>31) return(false);
1366 * The directory name may contains additional characters such as a counter if
1367 * a previous report is never overwritten.
1373 * Create a directory to generate a report for the specified connection data
1374 * and populate it with the a <tt>sarg-date</tt> file containing the current
1377 * The function also create an <tt>images</tt> directory in \a dir and copy all
1378 * the files from the <tt>SYSCONFDIR/images</tt> into that directory.
1380 * \param per1 The date range in the form: YYYYMMMDD-YYYYMMMDD or DDMMMYYYY-DDMMMYYYY depending on the value of
1382 * \param addr The ip address or host name to which the report is limited. If the string is empty, all the addresses are accepted.
1383 * \param site The destination site to which the report is limited. If the string is empty, all the sites are accepted.
1384 * \param us The user to whom the report is limited. It is an empty string if all the users are accepted.
1386 int vrfydir(const struct periodstruct
*per1
, const char *addr
, const char *site
, const char *us
)
1397 strcpy(wdir
,outdir
);
1399 y1
=per1
->start
.tm_year
+1900;
1400 y2
=per1
->end
.tm_year
+1900;
1401 m1
=per1
->start
.tm_mon
+1;
1402 m2
=per1
->end
.tm_mon
+1;
1403 d1
=per1
->start
.tm_mday
;
1404 d2
=per1
->end
.tm_mday
;
1405 if(IndexTree
== INDEX_TREE_DATE
) {
1406 wlen
+=sprintf(wdir
+wlen
,"%04d",y1
);
1407 if(y1
!=y2
) wlen
+=sprintf(wdir
+wlen
,"-%04d",y2
);
1408 if(access(wdir
, R_OK
) != 0)
1411 wlen
+=sprintf(wdir
+wlen
,"/%02d",m1
);
1412 if(m1
!= m2
) wlen
+=sprintf(wdir
+wlen
,"-%02d",m2
);
1413 if(access(wdir
, R_OK
) != 0)
1416 wlen
+=sprintf(wdir
+wlen
,"/%02d",d1
);
1417 if(d1
!=d2
) wlen
+=sprintf(wdir
+wlen
,"-%02d",d2
);
1420 wlen
=snprintf(wdir
+wlen
,sizeof(wdir
)-wlen
,"%04d%s%02d-%04d%s%02d",y1
,
1421 conv_month_name(m1
),d1
,y2
,conv_month_name(m2
),d2
);
1422 } else if (df
== 'e') {
1423 wlen
=snprintf(wdir
+wlen
,sizeof(wdir
)-wlen
,"%02d%s%04d-%02d%s%04d",d1
,
1424 conv_month_name(m1
),y1
,d2
,conv_month_name(m2
),y2
);
1425 } else if (df
== 'w') {
1426 wlen2
=strftime(wdir
+wlen
, sizeof(wdir
)-wlen
, "%Y.%U", &per1
->start
);
1427 if (wlen2
==0) return(-1);
1433 struct userinfostruct
*uinfo
=userinfo_find_from_id(us
);
1436 strcat(wdir
,uinfo
->filename
);
1439 if(addr
[0] != '\0') {
1443 if(site
[0] != '\0') {
1448 strcpy(outdirname
,wdir
);
1450 // manufacture a new unique name if configured to keep old reports or overwrite old report if configured to do so
1451 if (!OverwriteReport
) {
1454 while (access(wdir
,R_OK
)==0 || errno
==EACCES
) //file exist or can't be read
1456 sprintf(wdir
,"%s.%d",outdirname
,num
);
1461 debuga(__FILE__
,__LINE__
,_("File %s already exists, moved to %s\n"),outdirname
,wdir
);
1462 rename(outdirname
,wdir
);
1465 if(access(outdirname
,R_OK
) == 0) {
1466 unlinkdir(outdirname
,1);
1469 my_mkdir(outdirname
);
1471 // create sarg-date to keep track of the report creation date
1472 if (snprintf(wdir
,sizeof(wdir
),"%s/sarg-date",outdirname
)>=sizeof(wdir
)) {
1473 debuga(__FILE__
,__LINE__
,_("Buffer too small to store "));
1474 debuga_more("%s/sarg-date",outdirname
);
1477 if ((fp_ou
= fopen(wdir
, "wt")) == 0) {
1478 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"),wdir
,strerror(errno
));
1483 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1484 loctm
=localtime(&curtime
);
1485 strftime(wdir
,sizeof(wdir
),"%Y-%m-%d %H:%M:%S",loctm
);
1486 if (fprintf(fp_ou
,"%s %d\n",wdir
,loctm
->tm_isdst
)<0) {
1487 debuga(__FILE__
,__LINE__
,_("Failed to write the date in %s\n"),wdir
);
1491 if (fclose(fp_ou
)==EOF
) {
1492 debuga(__FILE__
,__LINE__
,_("Write error in \"%s\": %s\n"),wdir
,strerror(errno
));
1501 Copy a string without overflowing the buffer. The copied string
1502 is properly terminated by an ASCII zero.
1504 \param dest The destination buffer.
1505 \param src The source buffer.
1506 \param length The size of the destination buffer. The program is aborted
1507 if the length is negative or zero.
1509 void safe_strcpy(char *dest
,const char *src
,int length
)
1512 debuga(__FILE__
,__LINE__
,_("Invalid buffer length passed to the function to safely copy a string\n"));
1515 strncpy(dest
,src
,length
-1);
1516 dest
[length
-1]='\0';
1519 void strip_latin(char *line
)
1526 for (i
=0;line
[i
];i
++){
1528 if (line
[i
]==';') skip
=0;
1540 void zdate(char *ftime
,int ftimesize
, char DateFormat
)
1546 local
= localtime(&t
);
1547 if (DateFormat
=='u')
1548 strftime(ftime
, ftimesize
, "%b/%d/%Y %H:%M", local
);
1549 else if (DateFormat
=='e')
1550 strftime(ftime
, ftimesize
, "%d/%b/%Y-%H:%M", local
);
1551 else if (DateFormat
=='w')
1552 strftime(ftime
, ftimesize
, "%W-%H-%M", local
);
1557 char *fixtime(long long int elap
)
1559 long int num
= elap
/ 1000LL;
1563 static char buf
[20];
1566 min
=(num
% 3600L) / 60L;
1569 if(hor
==0 && min
==0 && sec
==0)
1572 snprintf(buf
,sizeof(buf
),"%d:%02d:%02d",hor
,min
,sec
);
1578 void date_from(char *date
,int date_size
, int *dfrom
, int *duntil
)
1587 if (isdigit(date
[0])) {
1590 if (sscanf(date
,"%d/%d/%d%n",&d0
,&m0
,&y0
,&next
)!=3 || y0
<100 || m0
<1 || m0
>12 || d0
<1 || d0
>31 || next
<0) {
1591 debuga(__FILE__
,__LINE__
,_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1594 if (date
[next
]=='-') {
1595 if (sscanf(date
+next
+1,"%d/%d/%d",&d1
,&m1
,&y1
)!=3 || y1
<100 || m1
<1 || m1
>12 || d1
<1 || d1
>31) {
1596 debuga(__FILE__
,__LINE__
,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1599 } else if (date
[next
]!='\0') {
1600 debuga(__FILE__
,__LINE__
,_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1610 struct tm
*Date0
,Date1
;
1612 if (time(&Today
)==(time_t)-1) {
1613 debuga(__FILE__
,__LINE__
,_("Failed to get the current time\n"));
1616 if (sscanf(date
,"day-%d",&i
)==1) {
1618 debuga(__FILE__
,__LINE__
,_("Invalid number of days in -d parameter\n"));
1622 Date0
=localtime(&Today
);
1624 debuga(__FILE__
,__LINE__
,_("Cannot convert local time: %s\n"),strerror(errno
));
1627 y0
=y1
=Date0
->tm_year
+1900;
1628 m0
=m1
=Date0
->tm_mon
+1;
1629 d0
=d1
=Date0
->tm_mday
;
1630 } else if (sscanf(date
,"week-%d",&i
)==1) {
1632 There is no portable way to find the first day of the week even though the
1633 information is available in the locale. nl_langinfo has the unofficial
1634 parameters _NL_TIME_FIRST_WEEKDAY and _NL_TIME_WEEK_1STDAY but they are
1635 undocumented as is their return value and it is discouraged to use them.
1636 Beside, nl_langinfo isn't available on windows and the first day of the
1637 week isn't available at all on that system.
1639 const int FirstWeekDay
=1;
1643 debuga(__FILE__
,__LINE__
,_("Invalid number of weeks in -d parameter\n"));
1646 Date0
=localtime(&Today
);
1648 debuga(__FILE__
,__LINE__
,_("Cannot convert local time: %s\n"),strerror(errno
));
1651 WeekBegin
=Today
-((Date0
->tm_wday
-FirstWeekDay
+7)%7)*24*60*60;
1652 WeekBegin
-=i
*7*24*60*60;
1653 Date0
=localtime(&WeekBegin
);
1655 debuga(__FILE__
,__LINE__
,_("Cannot convert local time: %s\n"),strerror(errno
));
1658 y0
=Date0
->tm_year
+1900;
1661 WeekBegin
+=6*24*60*60;
1662 Date0
=localtime(&WeekBegin
);
1664 debuga(__FILE__
,__LINE__
,_("Cannot convert local time: %s\n"),strerror(errno
));
1667 y1
=Date0
->tm_year
+1900;
1670 } else if (sscanf(date
,"month-%d",&i
)==1) {
1672 debuga(__FILE__
,__LINE__
,_("Invalid number of months in -d parameter\n"));
1675 Date0
=localtime(&Today
);
1677 debuga(__FILE__
,__LINE__
,_("Cannot convert local time: %s\n"),strerror(errno
));
1680 if (Date0
->tm_mon
<i
%12) {
1681 y0
=Date0
->tm_year
+1900-i
/12-1;
1682 m0
=(Date0
->tm_mon
+12-i
%12)%12+1;
1685 y0
=Date0
->tm_year
+1900-i
/12;
1686 m0
=Date0
->tm_mon
-i
%12+1;
1689 memcpy(&Date1
,Date0
,sizeof(struct tm
));
1694 Date1
.tm_year
=y0
-1900;
1697 Date1
.tm_year
=y0
-1900+1;
1701 Date0
=localtime(&t1
);
1702 y1
=Date0
->tm_year
+1900;
1706 debuga(__FILE__
,__LINE__
,_("Invalid date range passed on command line\n"));
1711 *dfrom
=y0
*10000+m0
*100+d0
;
1712 *duntil
=y1
*10000+m1
*100+d1
;
1713 snprintf(date
,date_size
,"%02d/%02d/%04d-%02d/%02d/%04d",d0
,m0
,y0
,d1
,m1
,y1
);
1718 char *strlow(char *string
)
1724 for (s
= string
; *s
; ++s
)
1734 char *strup(char *string
)
1740 for (s
= string
; *s
; ++s
)
1748 void removetmp(const char *outdir
)
1753 if(!RemoveTempFiles
)
1757 debuga(__FILE__
,__LINE__
,_("Purging temporary file sarg-general\n"));
1759 if (snprintf(filename
,sizeof(filename
),"%s/sarg-general",outdir
)>=sizeof(filename
)) {
1760 debuga(__FILE__
,__LINE__
,_("Path too long: "));
1761 debuga_more("%s/sarg-period\n",outdir
);
1764 if((fp_gen
=fopen(filename
,"w"))==NULL
){
1765 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"),filename
,strerror(errno
));
1768 totalger(fp_gen
,filename
);
1769 if (fclose(fp_gen
)==EOF
) {
1770 debuga(__FILE__
,__LINE__
,_("Write error in \"%s\": %s\n"),filename
,strerror(errno
));
1775 void load_excludecodes(const char *ExcludeCodes
)
1783 if(ExcludeCodes
[0] == '\0')
1786 if((fp_in
=fopen(ExcludeCodes
,"r"))==NULL
) {
1787 debuga(__FILE__
,__LINE__
,_("Cannot open file \"%s\": %s\n"),ExcludeCodes
,strerror(errno
));
1791 if (fseek(fp_in
, 0, SEEK_END
)==-1) {
1792 debuga(__FILE__
,__LINE__
,_("Failed to move till the end of file \"%s\": %s\n"),ExcludeCodes
,strerror(errno
));
1795 MemSize
= ftell(fp_in
);
1797 debuga(__FILE__
,__LINE__
,_("Cannot get the size of file \"%s\"\n"),ExcludeCodes
);
1800 if (fseek(fp_in
, 0, SEEK_SET
)==-1) {
1801 debuga(__FILE__
,__LINE__
,_("Failed to rewind file \"%s\": %s\n"),ExcludeCodes
,strerror(errno
));
1806 if((excludecode
=(char *) malloc(MemSize
))==NULL
) {
1807 debuga(__FILE__
,__LINE__
,_("malloc error (%ld bytes required)\n"),MemSize
);
1810 memset(excludecode
,0,MemSize
);
1813 while(fgets(data
,sizeof(data
),fp_in
)!=NULL
) {
1814 if (data
[0]=='#') continue;
1815 for (i
=strlen(data
)-1 ; i
>=0 && (unsigned char)data
[i
]<=' ' ; i
--) data
[i
]='\0';
1817 if (Stored
+i
+2>=MemSize
) {
1818 debuga(__FILE__
,__LINE__
,_("Too many codes to exclude in file \"%s\"\n"),ExcludeCodes
);
1821 strcat(excludecode
,data
);
1822 strcat(excludecode
,";");
1826 if (fclose(fp_in
)==EOF
) {
1827 debuga(__FILE__
,__LINE__
,_("Read error in \"%s\": %s\n"),ExcludeCodes
,strerror(errno
));
1833 void free_excludecodes(void)
1841 int vercode(const char *code
)
1846 if (excludecode
&& excludecode
[0]!='\0') {
1850 if (strncmp(code
,cod
,clen
)==0 && cod
[clen
]==';')
1852 cod
=strchr(cod
,';');
1859 void fixnone(char *str
)
1863 for (i
=strlen(str
)-1 ; i
>=0 && (unsigned char)str
[i
]<=' ' ; i
--);
1864 if(i
==3 && strncmp(str
,"none",4) == 0)
1870 void fixendofline(char *str
)
1874 for (i
=strlen(str
)-1 ; i
>=0 && (unsigned char)str
[i
]<=' ' ; i
--) str
[i
]=0;
1877 #ifdef LEGACY_TESTVALIDUSERCHAR
1878 int testvaliduserchar(const char *user
)
1883 for (y
=0; y
<strlen(UserInvalidChar
); y
++) {
1884 for (x
=0; x
<strlen(user
); x
++) {
1885 if(user
[x
] == UserInvalidChar
[y
])
1892 int testvaliduserchar(const char *user
)
1894 char * p_UserInvalidChar
= UserInvalidChar
;
1895 const char * p_user
;
1897 while( *p_UserInvalidChar
) {
1900 if( *p_UserInvalidChar
== *p_user
)
1904 p_UserInvalidChar
++ ;
1910 int compar( const void *a
, const void *b
)
1912 if( *(int *)a
> *(int *)b
) return 1;
1913 if( *(int *)a
< *(int *)b
) return -1;
1917 int getnumlist( char *buf
, numlist
*list
, const int len
, const int maxvalue
)
1919 int i
, j
, d
, flag
, r1
, r2
;
1920 char *pbuf
, **bp
, *strbufs
[ 24 ];
1923 strtok( buf
, " \t" );
1924 for( *bp
= strtok( NULL
, "," ), list
->len
= 0; *bp
; *bp
= strtok( NULL
, "," ) ) {
1925 if( ++bp
>= &strbufs
[ 24 ] )
1932 for( i
= 0; i
< list
->len
; i
++ ) {
1933 if( strchr( strbufs
[ i
], '-' ) != 0 ) {
1934 pbuf
= strbufs
[ i
];
1935 strtok( pbuf
, "-" );
1936 pbuf
= strtok( NULL
, "\0" );
1937 r1
= atoi( strbufs
[ i
] );
1938 if( ( r2
= atoi( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1940 if( i
+ d
+ ( r2
- r1
) + 1 <= len
) {
1941 for( j
= r1
; j
<= r2
; j
++ )
1942 list
->list
[ i
+ d
++ ] = j
;
1947 if( ( list
->list
[ i
+ d
] = atoi( strbufs
[ i
] ) ) >= maxvalue
)
1951 qsort( list
->list
, list
->len
, sizeof( int ), compar
);
1954 for( i
= 0; i
< list
->len
- 1; i
++ )
1955 if( list
->list
[ i
] == list
->list
[ i
+ 1 ] ) {
1956 for( j
= i
+ 1; j
< list
->len
; j
++ )
1957 list
->list
[ j
- 1 ] = list
->list
[ j
];
1966 void show_info(FILE *fp_ou
)
1970 if(!ShowSargInfo
) return;
1971 zdate(ftime
, sizeof(ftime
), df
);
1972 fputs("<div class=\"info\">",fp_ou
);
1973 fprintf(fp_ou
,_("Generated by <a href=\"%s\">%s-%s</a> on %s"),URL
,PGM
,VERSION
,ftime
);
1974 fputs("</div>\n",fp_ou
);
1977 void show_sarg(FILE *fp_ou
, int depth
)
1981 if(!ShowSargLogo
) return;
1982 fputs("<div class=\"logo\"><a href=\"http://sarg.sourceforge.net\"><img src=\"",fp_ou
);
1983 for (i
=0 ; i
<depth
; i
++)
1985 fputs("images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a> Squid Analysis Report Generator</div>\n",fp_ou
);
1988 void write_logo_image(FILE *fp_ou
)
1990 if(LogoImage
[0]!='\0')
1991 fprintf(fp_ou
, "<div class=\"logo\"><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\"> %s</div>\n",LogoImage
,Width
,Height
,LogoText
);
1994 void write_html_head(FILE *fp_ou
, int depth
, const char *page_title
,int javascript
)
1998 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n",fp_ou
);
1999 fprintf(fp_ou
, "<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
2000 if (page_title
) fprintf(fp_ou
,"<title>%s</title>\n",page_title
);
2002 if ((javascript
& HTML_JS_SORTTABLE
)!=0 && SortTableJs
[0]) {
2003 fputs("<script type=\"text/javascript\" src=\"",fp_ou
);
2004 if (strncmp(SortTableJs
,"../",3)==0) {
2005 for (i
=0 ; i
<depth
; i
++) fputs("../",fp_ou
);
2007 fputs(SortTableJs
,fp_ou
);
2008 fputs("\"></script>\n",fp_ou
);
2010 fputs("</head>\n<body>\n",fp_ou
);
2013 void write_html_header(FILE *fp_ou
, int depth
, const char *page_title
,int javascript
)
2015 write_html_head(fp_ou
,depth
,page_title
,javascript
);
2016 write_logo_image(fp_ou
);
2017 show_sarg(fp_ou
, depth
);
2018 fprintf(fp_ou
,"<div class=\"title\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title_c\">%s</th></tr>\n",Title
);
2021 void close_html_header(FILE *fp_ou
)
2023 fputs("</table></div>\n",fp_ou
);
2026 void write_html_trailer(FILE *fp_ou
)
2029 fputs("</body>\n</html>\n",fp_ou
);
2032 void output_html_string(FILE *fp_ou
,const char *str
,int maxlen
)
2036 while (*str
&& (maxlen
<=0 || i
<maxlen
)) {
2039 fputs("&",fp_ou
);
2042 fputs("<",fp_ou
);
2045 fputs(">",fp_ou
);
2048 fputs(""",fp_ou
);
2051 fputs("'",fp_ou
);
2059 if (maxlen
>0 && i
>=maxlen
)
2060 fputs("…",fp_ou
);
2063 void output_html_url(FILE *fp_ou
,const char *url
)
2067 fputs("&",fp_ou
);
2075 Write a host name inside an A tag of a HTML file. If the host name starts
2076 with a star, it is assumed to be an alias that cannot be put inside a link
2077 so the A tag is not written around the host name.
2079 \param fp_ou The handle of the HTML file.
2080 \param url The host to display in the HTML file.
2081 \param maxlen The maximum number of characters to print into the host name.
2083 void output_html_link(FILE *fp_ou
,const char *url
,int maxlen
)
2085 if (url
[0]==ALIAS_PREFIX
) {
2086 // this is an alias, no need for a A tag
2087 output_html_string(fp_ou
,url
+1,100);
2089 if (skip_scheme(url
)==url
)
2090 fputs("<a href=\"http://",fp_ou
);//no scheme in the url, assume http:// to make the link clickable
2092 fputs("<a href=\"",fp_ou
);//the scheme is in the url, no need to add one
2093 output_html_url(fp_ou
,url
);
2095 output_html_string(fp_ou
,url
,100);
2096 fputs("</a>",fp_ou
);
2100 void url_module(const char *url
, char *w2
)
2106 for(x
=strlen(url
)-1; x
>=0; x
--) {
2107 if(url
[x
] == '/' || y
>=sizeof(w
)-1) break;
2116 for(y
=y
-1; y
>=0; y
--) {
2123 Mangle an URL to produce a part that can be used as an anchor in
2124 a html <a name=""> tag.
2126 \param url The URL to mangle.
2127 \param anchor The buffer to write the mangled URL.
2128 \param size The size of the buffer.
2130 void url_to_anchor(const char *url
,char *anchor
,int size
)
2136 for (i
=0 ; url
[i
] && url
[i
]!='/' && url
[i
]!='?' ; i
++);
2143 // only keep really safe characters
2149 if(isalnum(url
[i
]) || url
[i
]=='-' || url
[i
]=='_' || url
[i
]=='.') {
2153 if (!skip
) anchor
[--j
]='_';
2171 printf(_("SARG Version: %s\n"),VERSION
);
2172 #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H)
2174 printf(_("\nFor the translation to work, a valid message file should be copied to "
2175 "\"%s/<Locale>/LC_MESSAGES/%s.mo\" where <Locale> is derived from the effective locale.\n"),LOCALEDIR
,PACKAGE_NAME
);
2176 if (CurrentLocale
) {
2177 printf(_("Currently effective locale is \"%s\".\n"),CurrentLocale
);
2179 printf(_("Locale is not set in the environment variable.\n"));
2181 // TRANSLATORS: You may change this message to tell the reader that the language is correctly supported.
2182 printf(_("If this message is in English, then your language is not supported or not correctly installed.\n"));
2187 printf(_("File globbing compiled in.\n"));
2189 printf(_("File globbing NOT compiled in.\n"));
2195 char *get_param_value(const char *param
,char *line
)
2199 while (*line
==' ' || *line
=='\t') line
++;
2201 if (strncasecmp(line
,param
,plen
)) return(NULL
);
2202 if (line
[plen
]!=' ' && line
[plen
]!='\t') return(NULL
);
2204 while (*line
==' ' || *line
=='\t') line
++;
2208 void unlinkdir(const char *dir
,bool contentonly
)
2212 struct dirent
*direntp
;
2218 while ((direntp
= readdir(dirp
)) != NULL
) {
2219 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
2220 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
2222 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
2223 debuga(__FILE__
,__LINE__
,_("Path too long: "));
2224 debuga_more("%s/%s\n",dir
,direntp
->d_name
);
2228 err
=lstat(dname
,&st
);
2230 err
=stat(dname
,&st
);
2233 debuga(__FILE__
,__LINE__
,_("Cannot stat \"%s\": %s\n"),dname
,strerror(errno
));
2236 if (S_ISREG(st
.st_mode
)) {
2237 if (unlink(dname
)) {
2238 debuga(__FILE__
,__LINE__
,_("Cannot delete \"%s\": %s\n"),dname
,strerror(errno
));
2241 } else if (S_ISDIR(st
.st_mode
)) {
2244 debuga(__FILE__
,__LINE__
,_("Don't know how to delete \"%s\" (not a regular file nor a directory)\n"),dname
);
2251 debuga(__FILE__
,__LINE__
,_("Cannot delete \"%s\": %s\n"),dir
,strerror(errno
));
2258 Delete every file from the temporary directory where sarg is told to store its
2261 As any stray file left over by a previous run would be included in the report, we
2262 must delete every file from the temporary directory before we start processing the logs.
2264 But the temporary directory is given by the user either in the configuration file or
2265 on the command line. We check that the user didn't give a wrong directory by looking
2266 at the files stored in the directory. If a single file is not one of ours, we abort.
2268 \param dir The temporary directory to purge.
2270 void emptytmpdir(const char *dir
)
2274 struct dirent
*direntp
;
2280 static const char *TmpExt
[]=
2298 // make sure the temporary directory contains only our files
2299 while ((direntp
= readdir(dirp
)) != NULL
) {
2300 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
2301 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
2304 // is it one of our files
2305 dlen
=strlen(direntp
->d_name
);
2306 for (i
=sizeof(TmpExt
)/sizeof(TmpExt
[0])-1 ; i
>=0 ; i
--) {
2307 elen
=strlen(TmpExt
[i
]);
2308 if (dlen
>=elen
&& strcasecmp(direntp
->d_name
+dlen
-elen
,TmpExt
[i
])==0) break;
2311 debuga(__FILE__
,__LINE__
,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
2312 "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
2313 "directory or manually delete the content of \"%s\"\n"),direntp
->d_name
,dir
,dir
);
2317 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
2318 debuga(__FILE__
,__LINE__
,_("Path too long: "));
2319 debuga_more("%s/%s\n",dir
,direntp
->d_name
);
2324 err
=lstat(dname
,&st
);
2326 err
=stat(dname
,&st
);
2329 debuga(__FILE__
,__LINE__
,_("Cannot stat \"%s\": %s\n"),dname
,strerror(errno
));
2332 if (!S_ISDIR(st
.st_mode
) && !S_ISREG(st
.st_mode
)) {
2333 debuga(__FILE__
,__LINE__
,_("Unknown path type for \"%s\". Check temporary directory\n"),dname
);
2339 // now delete our files
2340 while ((direntp
= readdir(dirp
)) != NULL
) {
2341 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
2342 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
2345 // is it one of our files
2346 dlen
=strlen(direntp
->d_name
);
2347 for (i
=sizeof(TmpExt
)/sizeof(TmpExt
[0])-1 ; i
>=0 ; i
--) {
2348 elen
=strlen(TmpExt
[i
]);
2349 if (dlen
>=elen
&& strcasecmp(direntp
->d_name
+dlen
-elen
,TmpExt
[i
])==0) break;
2352 debuga(__FILE__
,__LINE__
,_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
2353 "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
2354 "directory or manually delete the content of \"%s\"\n"),direntp
->d_name
,dir
,dir
);
2358 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
2359 debuga(__FILE__
,__LINE__
,_("Path too long: "));
2360 debuga_more("%s/%s\n",dir
,direntp
->d_name
);
2364 err
=lstat(dname
,&st
);
2366 err
=stat(dname
,&st
);
2369 debuga(__FILE__
,__LINE__
,_("Cannot stat \"%s\": %s\n"),dname
,strerror(errno
));
2372 if (S_ISDIR(st
.st_mode
)) {
2374 } else if (S_ISREG(st
.st_mode
)) {
2375 if (unlink(dname
)) {
2376 debuga(__FILE__
,__LINE__
,_("Cannot delete \"%s\": %s\n"),dname
,strerror(errno
));
2380 debuga(__FILE__
,__LINE__
,_("Don't know how to delete \"%s\" (not a regular file)\n"),dname
);
2387 Extract an url, IPv4 or IPv6 from a buffer. The IP addresses may end with a
2390 \param buf The buffer to parse.
2391 \param text A pointer to set to the beginning of the string pattern. No terminating zero is inserted.
2392 The pointer may be NULL.
2393 \param ipv4 A 4 bytes buffer to store the bytes of the IPv4 address.
2394 \param ipv6 A 8 short integers buffer to store the values of the IPv6 address.
2395 \param nbits The number of prefix bits for an IP address.
2396 \param next The content of the line after the extracted address.
2398 \retval 3 The pattern is a IPv6 address.
2399 \retval 2 The pattern is a IPv4 address.
2400 \retval 1 The patter is a string.
2401 \retval 0 Empty pattern.
2403 int extract_address_mask(const char *buf
,const char **text
,unsigned char *ipv4
,unsigned short int *ipv6
,int *nbits
,const char **next
)
2408 unsigned int value4
, value6
;
2409 unsigned short int addr
[8];
2419 // skip leading spaces and tabs
2420 while (*buf
&& (*buf
==' ' || *buf
=='\t')) buf
++;
2422 // find out the nature of the pattern
2423 ip_size
=0x60 | 0x04;
2434 for (i
=0 ; (unsigned char)buf
[i
]>' ' && buf
[i
]!='/' && buf
[i
]!='?' && (!bracket
|| buf
[i
]!=']') && ip_size
; i
++) {
2435 if (ip_size
& 0x04) {
2436 if (isdigit(buf
[i
])) {
2438 port_num
=port_num
*10+(buf
[i
]-'0');
2439 if (port_num
>65535) ip_size
&=~0x04;
2441 value4
=value4
*10+(buf
[i
]-'0');
2442 if (value4
>0xFFU
) ip_size
&=~0x04;
2444 } else if (buf
[i
]=='.' && addr_len
<4) {
2445 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
2447 } else if (!port
&& buf
[i
]==':') {
2453 if (ip_size
& 0x60) {
2454 if (isdigit(buf
[i
])) {
2455 value6
=(value6
<<4)+(buf
[i
]-'0');
2457 if (value6
>0xFFFFU
) ip_size
&=~0x60;
2458 } else if (toupper(buf
[i
])>='A' && toupper(buf
[i
])<='F') {
2459 value6
=(value6
<<4)+(toupper(buf
[i
])-'A'+10);
2461 if (value6
>0xFFFFU
) ip_size
&=~0x60;
2462 } else if (buf
[i
]==':' && addr_len
<8) {
2463 if (nibble6_len
>0) {
2464 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
2468 if (buf
[i
+1]==':') {
2477 if (i
==0) return(0);
2478 if (ip_size
& 0x04) {
2482 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
2484 if (ip_size
& 0x60) {
2485 if (pad_pos
<0 && addr_len
!=7) {
2487 } else if (pad_pos
>=0 && addr_len
>=7)
2489 else if (nibble6_len
>0)
2490 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
2495 if (bracket
) (*text
)--;
2497 while ((unsigned char)buf
[i
]>' ') i
++;
2498 if (next
) *next
=buf
+i
;
2501 max_mask
=(ip_size
& 0x04) ? 4*8 : 8*16;
2505 while (isdigit(buf
[i
])) i
++;
2506 if (mask
<0 || mask
>max_mask
) mask
=max_mask
;
2509 if (ip_size
& 0x60 && bracket
&& buf
[i
]==']') i
++;
2510 if (next
) *next
=buf
+i
;
2511 if (ip_size
& 0x04) {
2512 if (nbits
) *nbits
=mask
;
2513 for (i
=0 ; i
<addr_len
; i
++)
2514 ipv4
[i
]=(unsigned char)addr
[i
];
2519 if (nbits
) *nbits
=mask
;
2524 ipv6
[j
++]=(unsigned short int)addr
[i
++];
2526 while (j
<pad_pos
+pad_len
)
2530 ipv6
[j
++]=(unsigned short int)addr
[i
++];