]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/formats.c
6 #include "../lib/strlfunc.h"
9 int trusts_parsecidr(const char *host
, uint32_t *ip
, short *mask
) {
10 unsigned int octet1
= 0, octet2
= 0, octet3
= 0, octet4
= 0, umask
= 32;
12 if(sscanf(host
, "%u.%u.%u.%u/%u", &octet1
, &octet2
, &octet3
, &octet4
, &umask
) != 5)
13 if(sscanf(host
, "%u.%u.%u/%u", &octet1
, &octet2
, &octet3
, &umask
) != 4)
14 if(sscanf(host
, "%u.%u/%u", &octet1
, &octet2
, &umask
) != 3)
15 if(sscanf(host
, "%u/%u", &octet1
, &umask
) != 2)
16 if(sscanf(host
, "%u.%u.%u.%u", &octet1
, &octet2
, &octet3
, &octet4
) != 4)
19 if(octet1
> 255 || octet2
> 255 || octet3
> 255 || octet4
> 255 || umask
> 32)
22 *ip
= (octet1
<< 24) | (octet2
<< 16) | (octet3
<< 8) | octet4
;
28 /* returns mask pre-anded */
29 int trusts_str2cidr(const char *host
, uint32_t *ip
, uint32_t *mask
) {
33 if(!trusts_parsecidr(host
, &result
, &smask
))
39 *mask
= 0xffffffff << (32 - smask
);
46 char *trusts_cidr2str(uint32_t ip
, uint32_t mask
) {
51 /* count number of trailing zeros */
52 float f
= (float)(mask
& -mask
);
54 mask
= 32 - ((*(unsigned int *)&f
>> 23) - 0x7f);
58 snprintf(maskbuf
, sizeof(maskbuf
), "/%u", mask
);
63 snprintf(buf
, sizeof(buf
), "%u.%u.%u.%u%s", (ip
>> 24) & 0xff, (ip
>> 16) & 0xff, (ip
>> 8) & 0xff, ip
& 0xff, maskbuf
);
68 char *trusts_timetostr(time_t t
) {
71 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", localtime(&t
));
76 char *dumpth(trusthost
*th
, int oformat
) {
80 snprintf(buf
, sizeof(buf
), "#%u,%s,%u,%u,%jd", th
->group
->id
, trusts_cidr2str(th
->ip
, th
->mask
), th
->count
, th
->maxusage
, (intmax_t)th
->lastseen
);
82 snprintf(buf
, sizeof(buf
), "%u,%s,%u,%u,%jd", th
->group
->id
, trusts_cidr2str(th
->ip
, th
->mask
), th
->id
, th
->maxusage
, (intmax_t)th
->lastseen
);
88 char *dumptg(trustgroup
*tg
, int oformat
) {
92 snprintf(buf
, sizeof(buf
), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg
->id
, tg
->name
->content
, tg
->count
, tg
->trustedfor
, tg
->mode
, tg
->maxperident
, tg
->maxusage
, (intmax_t)tg
->expires
, (intmax_t)tg
->lastseen
, (intmax_t)tg
->lastmaxusereset
, tg
->createdby
->content
, tg
->contact
->content
, tg
->comment
->content
);
94 snprintf(buf
, sizeof(buf
), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg
->id
, tg
->name
->content
, tg
->trustedfor
, tg
->mode
, tg
->maxperident
, tg
->maxusage
, (intmax_t)tg
->expires
, (intmax_t)tg
->lastseen
, (intmax_t)tg
->lastmaxusereset
, tg
->createdby
->content
, tg
->contact
->content
, tg
->comment
->content
);
100 int parsetg(char *buf
, trustgroup
*tg
, int oformat
) {
101 char *line
, *createdby
, *contact
, *comment
, *name
, *id
;
102 unsigned long expires
, lastseen
, lastmaxusereset
;
106 /* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
112 ,expires ,lastseen ,lastmaxusereset
113 ,createdby,contact ,comment
117 strlcpy(xbuf
, buf
, sizeof(xbuf
));
130 line
= strchr(xbuf
, ',');
135 if(oformat
&& (id
[0] == '#'))
138 tg
->id
= strtoul(id
, NULL
, 10);
143 line
= strchr(line
, ',');
149 r
= sscanf(line
, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
150 /*current, */ &tg
->trustedfor
, &tg
->mode
, &tg
->maxperident
,
151 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
153 r
= sscanf(line
, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
154 &tg
->trustedfor
, &tg
->mode
, &tg
->maxperident
,
155 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
160 tg
->expires
= (time_t)expires
;
161 tg
->lastseen
= (time_t)lastseen
;
162 tg
->lastmaxusereset
= (time_t)lastmaxusereset
;
164 createdby
= &line
[pos
];
165 contact
= strchr(createdby
, ',');
170 comment
= strchr(contact
, ',');
175 tg
->name
= getsstring(name
, TRUSTNAMELEN
);
176 tg
->createdby
= getsstring(createdby
, CREATEDBYLEN
);
177 tg
->comment
= getsstring(comment
, COMMENTLEN
);
178 tg
->contact
= getsstring(contact
, CONTACTLEN
);
179 if(!tg
->name
|| !tg
->createdby
|| !tg
->comment
|| !tg
->contact
) {
180 freesstring(tg
->name
);
181 freesstring(tg
->createdby
);
182 freesstring(tg
->comment
);
183 freesstring(tg
->contact
);
190 int parseth(char *line
, trusthost
*th
, unsigned int *tgid
, int oformat
) {
191 unsigned long lastseen
;
192 char *ip
, xbuf
[1024], *id
;
194 /* #id,213.230.192.128/26,20,23,1222732944
195 ip ,cur,max,lastseen */
197 strlcpy(xbuf
, line
, sizeof(xbuf
));
200 line
= strchr(line
, ',');
205 if(oformat
&& (id
[0] == '#'))
208 *tgid
= strtoul(id
, NULL
, 10);
213 line
= strchr(line
, ',');
218 if(!trusts_str2cidr(ip
, &th
->ip
, &th
->mask
))
222 if(sscanf(line
, "%*u,%u,%lu", /*current, */&th
->maxusage
, &lastseen
) != 2)
225 if(sscanf(line
, "%u,%u,%lu", &th
->id
, &th
->maxusage
, &lastseen
) != 3)
229 th
->lastseen
= (time_t)lastseen
;
234 char *rtrim(char *buf
) {
235 static char obuf
[1024];
236 size_t len
= strlcpy(obuf
, buf
, sizeof(obuf
));
238 if((len
< sizeof(obuf
)) && (len
> 0)) {
240 for(i
=len
-1;i
>=0;i
--) {