diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:52:39 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:52:39 +0200 |
commit | a6a7d6ce079cabdaf2fa502b2e2cf15e5428ac6f (patch) | |
tree | b88cc7a8457658d67e0321718556ac807f6bccf3 /src/tcpto.c | |
parent | 00be7622c428f279872f84569f098ce16150f8a8 (diff) |
format files
Diffstat (limited to 'src/tcpto.c')
-rw-r--r-- | src/tcpto.c | 116 |
1 files changed, 70 insertions, 46 deletions
diff --git a/src/tcpto.c b/src/tcpto.c index 92c33ea..77e565c 100644 --- a/src/tcpto.c +++ b/src/tcpto.c @@ -1,21 +1,24 @@ +#include "tcpto.h" + #include <sys/socket.h> #include <unistd.h> -#include "tcpto.h" -#include "open.h" + +#include "byte.h" +#include "ip.h" #include "lock.h" +#include "open.h" #include "seek.h" -#include "now.h" -#include "ip.h" -#include "ipalloc.h" -#include "byte.h" + #include "datetime.h" +#include "ipalloc.h" +#include "now.h" char tcpto_buf[1024]; static int flagwasthere; static int fdlock; -static int getbuf() +static int getbuf() { int r; int fd; @@ -23,17 +26,27 @@ static int getbuf() fdlock = open_write("queue/lock/tcpto"); if (fdlock == -1) return 0; fd = open_read("queue/lock/tcpto"); - if (fd == -1) { close(fdlock); return 0; } - if (lock_ex(fdlock) == -1) { close(fdlock); close(fd); return 0; } - r = read(fd,tcpto_buf,sizeof(tcpto_buf)); + if (fd == -1) { + close(fdlock); + return 0; + } + if (lock_ex(fdlock) == -1) { + close(fdlock); + close(fd); + return 0; + } + r = read(fd, tcpto_buf, sizeof(tcpto_buf)); close(fd); - if (r < 0) { close(fdlock); return 0; } + if (r < 0) { + close(fdlock); + return 0; + } r >>= 5; if (!r) close(fdlock); return r; } -int tcpto(struct ip_mx *ix) +int tcpto(struct ip_mx *ix) { int af = ix->af; struct ip_address *ip = &ix->addr; @@ -51,13 +64,13 @@ int tcpto(struct ip_mx *ix) record = tcpto_buf; for (i = 0; i < n; ++i) { - if (af == record[0] && byte_equal(ip->d,af == AF_INET ? 4 : 16,record + 16)) { + if (af == record[0] && byte_equal(ip->d, af == AF_INET ? 4 : 16, record + 16)) { flagwasthere = 1; if (record[4] >= 2) { - when = (unsigned long) (unsigned char) record[11]; - when = (when << 8) + (unsigned long) (unsigned char) record[10]; - when = (when << 8) + (unsigned long) (unsigned char) record[9]; - when = (when << 8) + (unsigned long) (unsigned char) record[8]; + when = (unsigned long)(unsigned char)record[11]; + when = (when << 8) + (unsigned long)(unsigned char)record[10]; + when = (when << 8) + (unsigned long)(unsigned char)record[9]; + when = (when << 8) + (unsigned long)(unsigned char)record[8]; if (now() - when < ((60 + (getpid() & 31)) << 6)) return 1; } @@ -68,7 +81,7 @@ int tcpto(struct ip_mx *ix) return 0; } -void tcpto_err(struct ip_mx *ix,int flagerr) +void tcpto_err(struct ip_mx *ix, int flagerr) { int af = ix->af; struct ip_address *ip = &ix->addr; @@ -81,8 +94,7 @@ void tcpto_err(struct ip_mx *ix,int flagerr) datetime_sec lastwhen; if (!flagerr) - if (!flagwasthere) - return; /* could have been added, but not worth the effort to check */ + if (!flagwasthere) return; /* could have been added, but not worth the effort to check */ n = getbuf(); if (!n) return; @@ -90,26 +102,32 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record = tcpto_buf; for (i = 0; i < n; ++i) { - if (af == record[0] && byte_equal(ip->d,af == AF_INET ? 4 : 16,record + 16)) { + if (af == record[0] && byte_equal(ip->d, af == AF_INET ? 4 : 16, record + 16)) { if (!flagerr) - record[4] = 0; + record[4] = 0; else { - lastwhen = (unsigned long) (unsigned char) record[11]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[10]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[9]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[8]; + lastwhen = (unsigned long)(unsigned char)record[11]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[10]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[9]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[8]; when = now(); - if (record[4] && (when < 120 + lastwhen)) { close(fdlock); return; } + if (record[4] && (when < 120 + lastwhen)) { + close(fdlock); + return; + } if (++record[4] > 10) record[4] = 10; - record[8] = when; when >>= 8; - record[9] = when; when >>= 8; - record[10] = when; when >>= 8; + record[8] = when; + when >>= 8; + record[9] = when; + when >>= 8; + record[10] = when; + when >>= 8; record[11] = when; } - if (seek_set(fdlock,i << 5) == 0) - if (write(fdlock,record,32) < 32) + if (seek_set(fdlock, i << 5) == 0) + if (write(fdlock, record, 32) < 32) ; /*XXX*/ close(fdlock); return; @@ -117,7 +135,10 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record += 32; } - if (!flagerr) { close(fdlock); return; } + if (!flagerr) { + close(fdlock); + return; + } record = tcpto_buf; @@ -131,10 +152,10 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record = tcpto_buf; for (i = 0; i < n; ++i) { - when = (unsigned long) (unsigned char) record[11]; - when = (when << 8) + (unsigned long) (unsigned char) record[10]; - when = (when << 8) + (unsigned long) (unsigned char) record[9]; - when = (when << 8) + (unsigned long) (unsigned char) record[8]; + when = (unsigned long)(unsigned char)record[11]; + when = (when << 8) + (unsigned long)(unsigned char)record[10]; + when = (when << 8) + (unsigned long)(unsigned char)record[9]; + when = (when << 8) + (unsigned long)(unsigned char)record[8]; when += (record[4] << 10); if ((firstpos < 0) || (when < firstwhen)) { firstpos = i; @@ -148,20 +169,23 @@ void tcpto_err(struct ip_mx *ix,int flagerr) if (i >= 0) { record = tcpto_buf + (i << 5); record[0] = af; - if (af == AF_INET6) - byte_copy(record + 16,16,ip->d); + if (af == AF_INET6) + byte_copy(record + 16, 16, ip->d); else { - byte_copy(record + 16,4,ip->d); - byte_copy(record + 20,12,"............"); + byte_copy(record + 16, 4, ip->d); + byte_copy(record + 20, 12, "............"); } when = now(); - record[8] = when; when >>= 8; - record[9] = when; when >>= 8; - record[10] = when; when >>= 8; + record[8] = when; + when >>= 8; + record[9] = when; + when >>= 8; + record[10] = when; + when >>= 8; record[11] = when; record[4] = 1; - if (seek_set(fdlock,i << 5) == 0) - if (write(fdlock,record,32) < 32) + if (seek_set(fdlock, i << 5) == 0) + if (write(fdlock, record, 32) < 32) ; /*XXX*/ } |