]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/formats.c
aa427760a3e8bd14bb5b727e2fd136af09be8026
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
->lastmaxuserreset
, 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
->lastmaxuserreset
, 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 tg
->id
= strtoul(id
, NULL
, 10);
140 line
= strchr(line
, ',');
146 r
= sscanf(line
, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
147 /*current, */ &tg
->trustedfor
, &tg
->mode
, &tg
->maxperident
,
148 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
150 r
= sscanf(line
, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
151 &tg
->trustedfor
, &tg
->mode
, &tg
->maxperident
,
152 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
157 tg
->expires
= (time_t)expires
;
158 tg
->lastseen
= (time_t)lastseen
;
159 tg
->lastmaxuserreset
= (time_t)lastmaxusereset
;
161 createdby
= &line
[pos
];
162 contact
= strchr(createdby
, ',');
167 comment
= strchr(contact
, ',');
172 tg
->name
= getsstring(name
, TRUSTNAMELEN
);
173 tg
->createdby
= getsstring(createdby
, NICKLEN
);
174 tg
->comment
= getsstring(comment
, COMMENTLEN
);
175 tg
->contact
= getsstring(contact
, CONTACTLEN
);
176 if(!tg
->name
|| !tg
->createdby
|| !tg
->comment
|| !tg
->contact
) {
177 freesstring(tg
->name
);
178 freesstring(tg
->createdby
);
179 freesstring(tg
->comment
);
180 freesstring(tg
->contact
);
187 int parseth(char *line
, trusthost
*th
, unsigned int *tgid
, int oformat
) {
188 unsigned long lastseen
;
189 char *ip
, xbuf
[1024], *id
;
191 /* #id,213.230.192.128/26,20,23,1222732944
192 ip ,cur,max,lastseen */
194 strlcpy(xbuf
, line
, sizeof(xbuf
));
197 line
= strchr(line
, ',');
202 *tgid
= strtoul(id
, NULL
, 10);
207 line
= strchr(line
, ',');
212 if(!trusts_str2cidr(ip
, &th
->ip
, &th
->mask
))
216 if(sscanf(line
, "%*u,%u,%lu", /*current, */&th
->maxusage
, &lastseen
) != 2)
219 if(sscanf(line
, "%u,%u,%lu", &th
->id
, &th
->maxusage
, &lastseen
) != 3)
223 th
->lastseen
= (time_t)lastseen
;
228 char *rtrim(char *buf
) {
229 static char obuf
[1024];
230 size_t len
= strlcpy(obuf
, buf
, sizeof(obuf
));
232 if((len
< sizeof(obuf
)) && (len
> 0)) {
234 for(i
=len
-1;i
>=0;i
--) {