diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 14:41:53 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 14:41:53 +0200 |
commit | 5fadc0cbb8577c61d66bd6f19ceaf0507c11e23b (patch) | |
tree | 684758441f5b431d0008253243034b6a4a29417c | |
parent | 249866e3d1e11dc72eaa1305f4bb479ded92ef38 (diff) |
initial clang-format
101 files changed, 2952 insertions, 2154 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..fde5d8f --- /dev/null +++ b/.clang-format @@ -0,0 +1,255 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +#AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignArrayOfStructures: Right +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: false +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +#AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +#AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: false +BinPackParameters: false +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: MultiLine + AfterEnum: false + AfterExternBlock: false + AfterFunction: true + AfterNamespace: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterAttributes: Never +#BreakAfterJavaFieldAnnotations: false +#BreakArrays: true +BreakBeforeBinaryOperators: NonAssignment +#BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Custom +#BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: AfterColon +#BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 105 +CommentPragmas: '^ IWYU pragma:' +#CompactNamespaces: false +#ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +#Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +#EmptyLineAfterAccessModifier: Never +#EmptyLineBeforeAccessModifier: LogicalBlock +#ExperimentalAutoDetectBinPacking: false +#FixNamespaceComments: true +#ForEachMacros: +# - foreach +# - Q_FOREACH +# - BOOST_FOREACH +#IfMacros: +# - KJ_IF_MAYBE +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<(aio|arpa/inet|cpio|dirent|fcntl|fmtmsg|fnmatch|ftw|glob|grp|iconv|langinfo|libgen|monetray|mqueue|ndbm|net/if|netdb|netinet/in|netinet/tcp|nl_types|poll|pthread|pwd|regex|sched|search|semaphores|spawn|strings|stropts|sys/[a-z]*|syslog|tar|termios|trace|ulimit|unistd|utime|utmpx|wordexp)\.h>$' + Priority: 2 + SortPriority: 0 + CaseSensitive: true + - Regex: '^<(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|std[a-z]+|string|tgmath|threads|time|uchar|wchar|wctype)\.h>$' + Priority: 3 + SortPriority: 0 + CaseSensitive: true + - Regex: '^"(alloc|base64|buffer|byte|case|cdbmake|cdbread|close|constmap|direntry|dnsresolv|env|error|exit|fd|fifo|fmt|genalloc|getln|getoptb|iopause|ip|lock|logmsg|ndelay|open|pathexec|prot|readclose|readwrite|rename|scan|seek|select|sig|socket_if|str|stralloc|taia|tai|timeoutconn|timeout|uint_t|wait)\.h"$' + Priority: 6 + SortPriority: 0 + CaseSensitive: true + - Regex: '^<openssl/[a-z_/]+\.h>$' + Priority: 5 + SortPriority: 0 + CaseSensitive: true + - Regex: '^<crypt\.h>$' + Priority: 5 + SortPriority: 0 + CaseSensitive: true + - Regex: '^<[a-z_/]+>$' + Priority: 4 + SortPriority: 0 + CaseSensitive: true + - Regex: '^<.*>$' + Priority: 1 + SortPriority: 0 + CaseSensitive: true + - Regex: '^".*"$' + Priority: 7 + SortPriority: 0 + CaseSensitive: true +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +#IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: BeforeHash +#IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: true +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +#JavaScriptQuotes: Leave +#JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +KeepEmptyLinesAtEOF: false +#LambdaBodyIndentation: Signature +LineEnding: LF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 2 +#NamespaceIndentation: None +#ObjCBinPackProtocolList: Auto +#ObjCBlockIndentWidth: 2 +#ObjCBreakBeforeNestedBlockParam: true +#ObjCSpaceAfterProperty: false +#ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: Never +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 1 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 1 +PenaltyReturnTypeOnItsOwnLine: 1000 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Left +ReferenceAlignment: Left +ReflowComments: false +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: true +#RequiresClausePosition: OwnLine +#RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseInsensitive +#SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +#SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +#SpaceBeforeCpp11BracedList: false +#SpaceBeforeCtorInitializerColon: true +#SpaceBeforeInheritanceColon: true +#SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +#SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 2 +#SpacesInAngles: Never +SpacesInContainerLiterals: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: c++20 +#StatementAttributeLikeMacros: +# - Q_EMIT +#StatementMacros: +# - Q_UNUSED +# - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +#VerilogBreakBetweenInstancePorts: true +#WhitespaceSensitiveMacros: +# - BOOST_PP_STRINGIZE +# - CF_SWIFT_NAME +# - NS_SWIFT_NAME +# - PP_STRINGIZE +# - STRINGIZE +... + diff --git a/include/alloc.h b/include/alloc.h index 56a4a52..4fec2c2 100644 --- a/include/alloc.h +++ b/include/alloc.h @@ -1,12 +1,12 @@ #ifndef ALLOC_H #define ALLOC_H -extern /*@null@*//*@out@*/char *alloc(); +extern /*@null@*/ /*@out@*/ char *alloc(); extern void alloc_free(); extern int alloc_re(); /* use these names in the future */ -#define qfree alloc_free +#define qfree alloc_free #define qrealloc alloc_re #endif diff --git a/include/buffer.h b/include/buffer.h index 57cd960..4f513e7 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -1,6 +1,6 @@ #ifndef BUFFER_H #define BUFFER_H -#include <sys/types.h> /* need type: ssize_t */ +#include <sys/types.h> /* need type: ssize_t */ typedef struct buffer { char *x; @@ -10,48 +10,41 @@ typedef struct buffer { ssize_t (*op)(); } buffer; -#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, (len), (fd), (op) } -#define BUFFER_SMALL 256 -#define BUFFER_INSIZE 8192 -#define BUFFER_OUTSIZE 8192 -#define BUFFER_MTUSIZE 1450 +#define BUFFER_INIT(op, fd, buf, len) {(buf), 0, (len), (fd), (op)} +#define BUFFER_SMALL 256 +#define BUFFER_INSIZE 8192 +#define BUFFER_OUTSIZE 8192 +#define BUFFER_MTUSIZE 1450 -extern void buffer_init(buffer *,ssize_t (*op)(),int,char *,size_t); +extern void buffer_init(buffer *, ssize_t (*op)(), int, char *, size_t); extern int buffer_flush(buffer *); -extern int buffer_put(buffer *,const char *,size_t); -extern int buffer_putalign(buffer *,const char *,size_t); -extern int buffer_putflush(buffer *,const char *,size_t); -extern int buffer_puts(buffer *,const char *); -extern int buffer_putsalign(buffer *,const char *); -extern int buffer_putsflush(buffer *,const char *); - -#define buffer_PUTC(s,c) \ - ( ((s)->n != (s)->p) \ - ? ( (s)->x[(s)->p++] = (c), 0 ) \ - : buffer_put((s),&(c),1) \ - ) - -extern int buffer_get(buffer *,char *,size_t); -extern int buffer_bget(buffer *,char *,size_t); +extern int buffer_put(buffer *, const char *, size_t); +extern int buffer_putalign(buffer *, const char *, size_t); +extern int buffer_putflush(buffer *, const char *, size_t); +extern int buffer_puts(buffer *, const char *); +extern int buffer_putsalign(buffer *, const char *); +extern int buffer_putsflush(buffer *, const char *); + +#define buffer_PUTC(s, c) (((s)->n != (s)->p) ? ((s)->x[(s)->p++] = (c), 0) : buffer_put((s), &(c), 1)) + +extern int buffer_get(buffer *, char *, size_t); +extern int buffer_bget(buffer *, char *, size_t); extern int buffer_feed(buffer *); extern char *buffer_peek(buffer *); -extern void buffer_seek(buffer *,size_t); +extern void buffer_seek(buffer *, size_t); -#define buffer_PEEK(s) ( (s)->x + (s)->n ) -#define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) ) +#define buffer_PEEK(s) ((s)->x + (s)->n) +#define buffer_SEEK(s, len) (((s)->p -= (len)), ((s)->n += (len))) -#define buffer_GETC(s,c) \ - ( ((s)->p > 0) \ - ? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \ - : buffer_get((s),(c),1) \ - ) +#define buffer_GETC(s, c) \ + (((s)->p > 0) ? (*(c) = (s)->x[(s)->n], buffer_SEEK((s), 1), 1) : buffer_get((s), (c), 1)) -extern int buffer_copy(buffer *,buffer *); +extern int buffer_copy(buffer *, buffer *); -extern ssize_t buffer_unixread(int,char *,size_t); -extern ssize_t buffer_unixwrite(int,char *,size_t); +extern ssize_t buffer_unixread(int, char *, size_t); +extern ssize_t buffer_unixwrite(int, char *, size_t); extern buffer *buffer_0; extern buffer *buffer_1; diff --git a/include/byte.h b/include/byte.h index f437341..04337e8 100644 --- a/include/byte.h +++ b/include/byte.h @@ -16,6 +16,6 @@ extern int byte_diff(); extern void byte_zero(); extern void byte_fill(); -#define byte_equal(s,n,t) (!byte_diff((s),(n),(t))) +#define byte_equal(s, n, t) (!byte_diff((s), (n), (t))) #endif diff --git a/include/case.h b/include/case.h index d4cead2..f63747b 100644 --- a/include/case.h +++ b/include/case.h @@ -2,16 +2,16 @@ #define CASE_H extern void case_lowers(char *); -extern void case_lowerb(char *,unsigned int); +extern void case_lowerb(char *, unsigned int); extern void case_uppers(char *); -extern void case_upperb(char *,unsigned int); -extern int case_diffs(char *,char *); -extern int case_diffrs(char *,char *); -extern int case_diffb(char *,unsigned int,char *); -extern int case_starts(char *,char *); -extern int case_startb(char *,unsigned int,char *); +extern void case_upperb(char *, unsigned int); +extern int case_diffs(char *, char *); +extern int case_diffrs(char *, char *); +extern int case_diffb(char *, unsigned int, char *); +extern int case_starts(char *, char *); +extern int case_startb(char *, unsigned int, char *); -#define case_equals(s,t) (!case_diffs((s),(t))) -#define case_equalrs(s,t) (!case_diffrs((s),(t))) +#define case_equals(s, t) (!case_diffs((s), (t))) +#define case_equalrs(s, t) (!case_diffrs((s), (t))) #endif diff --git a/include/cdbmake.h b/include/cdbmake.h index 9c20d2d..1fa3699 100644 --- a/include/cdbmake.h +++ b/include/cdbmake.h @@ -8,13 +8,16 @@ #define CDB_HPLIST 1000 -struct cdb_hp { uint32 h; uint32 p; } ; +struct cdb_hp { + uint32 h; + uint32 p; +}; struct cdb_hplist { struct cdb_hp hp[CDB_HPLIST]; struct cdb_hplist *next; int num; -} ; +}; struct cdb_make { char bspace[8192]; @@ -28,12 +31,12 @@ struct cdb_make { buffer b; uint32 pos; int fd; -} ; +}; -extern int cdb_make_start(struct cdb_make *,int); -extern int cdb_make_addbegin(struct cdb_make *,unsigned int,unsigned int); -extern int cdb_make_addend(struct cdb_make *,unsigned int,unsigned int,uint32); -extern int cdb_make_add(struct cdb_make *,char *,unsigned int,char *,unsigned int); +extern int cdb_make_start(struct cdb_make *, int); +extern int cdb_make_addbegin(struct cdb_make *, unsigned int, unsigned int); +extern int cdb_make_addend(struct cdb_make *, unsigned int, unsigned int, uint32); +extern int cdb_make_add(struct cdb_make *, char *, unsigned int, char *, unsigned int); extern int cdb_make_finish(struct cdb_make *); #endif diff --git a/include/cdbread.h b/include/cdbread.h index bee4fd4..6f510c3 100644 --- a/include/cdbread.h +++ b/include/cdbread.h @@ -6,31 +6,31 @@ #include "uint_t.h" #define CDB_HASHSTART 5381ULL -extern uint32 cdb_hashadd(uint32,unsigned char); -extern uint32 cdb_hash(char *,unsigned int); +extern uint32 cdb_hashadd(uint32, unsigned char); +extern uint32 cdb_hash(char *, unsigned int); extern uint32 cdb_unpack(unsigned char *); struct cdb { char *map; /* 0 if no map is available */ int fd; - uint32 size; /* initialized if map is nonzero */ - uint32 loop; /* number of hash slots searched under this key */ - uint32 khash; /* initialized if loop is nonzero */ - uint32 kpos; /* initialized if loop is nonzero */ - uint32 hpos; /* initialized if loop is nonzero */ + uint32 size; /* initialized if map is nonzero */ + uint32 loop; /* number of hash slots searched under this key */ + uint32 khash; /* initialized if loop is nonzero */ + uint32 kpos; /* initialized if loop is nonzero */ + uint32 hpos; /* initialized if loop is nonzero */ uint32 hslots; /* initialized if loop is nonzero */ - uint32 dpos; /* initialized if cdb_findnext() returns 1 */ - uint32 dlen; /* initialized if cdb_findnext() returns 1 */ -} ; + uint32 dpos; /* initialized if cdb_findnext() returns 1 */ + uint32 dlen; /* initialized if cdb_findnext() returns 1 */ +}; extern void cdb_free(struct cdb *); -extern void cdb_init(struct cdb *,int fd); +extern void cdb_init(struct cdb *, int fd); -extern int cdb_read(struct cdb *,char *,unsigned int,uint32); +extern int cdb_read(struct cdb *, char *, unsigned int, uint32); extern void cdb_findstart(struct cdb *); -extern int cdb_findnext(struct cdb *,char *,unsigned int); -extern int cdb_find(struct cdb *,char *,unsigned int); +extern int cdb_findnext(struct cdb *, char *, unsigned int); +extern int cdb_find(struct cdb *, char *, unsigned int); #define cdb_datapos(c) ((c)->dpos) #define cdb_datalen(c) ((c)->dlen) diff --git a/include/constmap.h b/include/constmap.h index 750702e..0a43e98 100644 --- a/include/constmap.h +++ b/include/constmap.h @@ -11,10 +11,10 @@ struct constmap { int *next; char **input; int *inputlen; -} ; +}; -int constmap_init(struct constmap *,char *,int,int); -int constmap_init_char(struct constmap *,char *,int,int,char); +int constmap_init(struct constmap *, char *, int, int); +int constmap_init_char(struct constmap *, char *, int, int, char); void constmap_free(); char *constmap(); diff --git a/include/direntry.h b/include/direntry.h index d1628a9..0a7d9d8 100644 --- a/include/direntry.h +++ b/include/direntry.h @@ -3,8 +3,8 @@ /* sysdep: +dirent */ -#include <sys/types.h> #include <dirent.h> +#include <sys/types.h> #define direntry struct dirent #endif diff --git a/include/dnsresolv.h b/include/dnsresolv.h index 406af50..0d01a87 100644 --- a/include/dnsresolv.h +++ b/include/dnsresolv.h @@ -33,71 +33,71 @@ * - added more DNS RR definitions */ -#include "stralloc.h" #include "iopause.h" +#include "stralloc.h" #include "taia.h" /* Note: The conventions are subject of change in forthcoming versions */ -#define DNS_NXD 0 /* NXDOMAIN, NODATA */ -#define DNS_MEM -1 /* out of memory; fatal */ -#define DNS_ERR -2 /* parsing errors and others */ -#define DNS_COM -3 /* (socket) communication errors: SERVFAIL */ -#define DNS_INT -4 /* internal errors */ -#define DNS_SOFT -5 /* DNS_ERR or DNS_COM */ -#define DNS_HARD -6 /* DNS loop problem */ +#define DNS_NXD 0 /* NXDOMAIN, NODATA */ +#define DNS_MEM -1 /* out of memory; fatal */ +#define DNS_ERR -2 /* parsing errors and others */ +#define DNS_COM -3 /* (socket) communication errors: SERVFAIL */ +#define DNS_INT -4 /* internal errors */ +#define DNS_SOFT -5 /* DNS_ERR or DNS_COM */ +#define DNS_HARD -6 /* DNS loop problem */ -#define MSGSIZE MTUSIZE /* todays default */ +#define MSGSIZE MTUSIZE /* todays default */ // #define MSGSIZE 512 /* RFC 1035 */ -#define MAXMSGSIZE 4096 /* 4069 seen with EDNS0 */ -#define MAXSEGMENT 65535 /* Max TCP buffer size */ +#define MAXMSGSIZE 4096 /* 4069 seen with EDNS0 */ +#define MAXSEGMENT 65535 /* Max TCP buffer size */ -#define QUERY_MAXNS 32 /* 16 IPv4 + 16 IPv6 NS */ -#define QUERY_MAXIPLEN 512 /* QUERY_MAXNS * 16 */ +#define QUERY_MAXNS 32 /* 16 IPv4 + 16 IPv6 NS */ +#define QUERY_MAXIPLEN 512 /* QUERY_MAXNS * 16 */ /* Note: These following definitions are subject of change */ -#define DNS_C_IN "\0\1" +#define DNS_C_IN "\0\1" #define DNS_C_ANY "\0\377" -#define DNS_T_A "\0\1" -#define DNS_T_NS "\0\2" -#define DNS_T_CNAME "\0\5" -#define DNS_T_SOA "\0\6" -#define DNS_T_PTR "\0\14" -#define DNS_T_HINFO "\0\15" -#define DNS_T_MX "\0\17" -#define DNS_T_TXT "\0\20" -#define DNS_T_RP "\0\21" -#define DNS_T_SIG "\0\30" -#define DNS_T_KEY "\0\31" -#define DNS_T_AAAA "\0\34" -#define DNS_T_SRV "\0\41" -#define DNS_T_NAPTR "\0\43" -#define DNS_T_CERT "\0\45" -#define DNS_T_OPT "\0\51" -#define DNS_T_DS "\0\53" -#define DNS_T_SSHFP "\0\54" -#define DNS_T_IPSECKEY "\0\55" -#define DNS_T_RRSIG "\0\56" -#define DNS_T_NSEC "\0\57" -#define DNS_T_DNSKEY "\0\60" -#define DNS_T_NSEC3 "\0\62" +#define DNS_T_A "\0\1" +#define DNS_T_NS "\0\2" +#define DNS_T_CNAME "\0\5" +#define DNS_T_SOA "\0\6" +#define DNS_T_PTR "\0\14" +#define DNS_T_HINFO "\0\15" +#define DNS_T_MX "\0\17" +#define DNS_T_TXT "\0\20" +#define DNS_T_RP "\0\21" +#define DNS_T_SIG "\0\30" +#define DNS_T_KEY "\0\31" +#define DNS_T_AAAA "\0\34" +#define DNS_T_SRV "\0\41" +#define DNS_T_NAPTR "\0\43" +#define DNS_T_CERT "\0\45" +#define DNS_T_OPT "\0\51" +#define DNS_T_DS "\0\53" +#define DNS_T_SSHFP "\0\54" +#define DNS_T_IPSECKEY "\0\55" +#define DNS_T_RRSIG "\0\56" +#define DNS_T_NSEC "\0\57" +#define DNS_T_DNSKEY "\0\60" +#define DNS_T_NSEC3 "\0\62" #define DNS_T_NSEC3PARAM "\0\63" -#define DNS_T_TLSA "\0\64" -#define DNS_T_HIP "\0\67" +#define DNS_T_TLSA "\0\64" +#define DNS_T_HIP "\0\67" #define DNS_T_OPENPGPKEY "\0\75" -#define DNS_T_SPF "\0\143" -#define DNS_T_AXFR "\0\374" -#define DNS_T_ANY "\0\377" -#define DNS_T_CAA "\1\1" +#define DNS_T_SPF "\0\143" +#define DNS_T_AXFR "\0\374" +#define DNS_T_ANY "\0\377" +#define DNS_T_CAA "\1\1" -#define LOCALHOST "localhost" /* no clear distinction IPv4/IPv6 */ +#define LOCALHOST "localhost" /* no clear distinction IPv4/IPv6 */ #define IP4_LOOPBACK "ip4-loopback" #define IP6_LOOPBACK "ip6-loopback" struct dns_transmit { - char *query; /* 0, or dynamically allocated */ + char *query; /* 0, or dynamically allocated */ unsigned int querylen; char *packet; /* 0, or dynamically allocated */ unsigned int packetlen; @@ -111,7 +111,7 @@ struct dns_transmit { uint32 scope_id; char localip[16]; char qtype[2]; -} ; +}; /* General */ @@ -119,71 +119,73 @@ extern void dns_random_init(const char *); extern unsigned int dns_random(unsigned int); extern void dns_domain_free(char **); -extern int dns_domain_copy(char **,const char *); +extern int dns_domain_copy(char **, const char *); extern unsigned int dns_domain_length(const char *); -extern int dns_domain_equal(const char *,const char *); -extern int dns_domain_suffix(const char *,const char *); -extern unsigned int dns_domain_suffixpos(const char *,const char *); -extern int dns_domain_fromdot(char **,const char *,unsigned int); -extern int dns_domain_todot_cat(stralloc *,const char *); -extern int dns_ip_qualify(stralloc *,stralloc *,const stralloc *); -extern int dns_ip_qualify_rules(stralloc *,stralloc *,const stralloc *,const stralloc *); -extern int dns_ip_qualify_localhost(stralloc *,stralloc *,const stralloc *); - -extern unsigned int dns_packet_copy(const char *,unsigned int,unsigned int,char *,unsigned int); -extern unsigned int dns_packet_getname(const char *,unsigned int,unsigned int,char **); -extern unsigned int dns_packet_skipname(const char *,unsigned int,unsigned int); +extern int dns_domain_equal(const char *, const char *); +extern int dns_domain_suffix(const char *, const char *); +extern unsigned int dns_domain_suffixpos(const char *, const char *); +extern int dns_domain_fromdot(char **, const char *, unsigned int); +extern int dns_domain_todot_cat(stralloc *, const char *); +extern int dns_ip_qualify(stralloc *, stralloc *, const stralloc *); +extern int dns_ip_qualify_rules(stralloc *, stralloc *, const stralloc *, const stralloc *); +extern int dns_ip_qualify_localhost(stralloc *, stralloc *, const stralloc *); + +extern unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int); +extern unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **); +extern unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int); extern struct dns_transmit dns_resolve_tx; -extern int dns_transmit_start(struct dns_transmit *,const char *,int,const char *,const char *,const char *); +extern int dns_transmit_start( + struct dns_transmit *, const char *, int, const char *, const char *, const char *); extern void dns_transmit_free(struct dns_transmit *); -extern void dns_transmit_io(struct dns_transmit *,iopause_fd *,struct taia *); -extern int dns_transmit_get(struct dns_transmit *,const iopause_fd *,const struct taia *); +extern void dns_transmit_io(struct dns_transmit *, iopause_fd *, struct taia *); +extern int dns_transmit_get(struct dns_transmit *, const iopause_fd *, const struct taia *); /* Common IPv4 + IPv6 */ -extern int dns_resolvconfip(char *,uint32 *); +extern int dns_resolvconfip(char *, uint32 *); extern int dns_resolvconfrewrite(stralloc *); -extern int dns_resolve(const char *,const char *); +extern int dns_resolve(const char *, const char *); -extern int dns_name(stralloc *,const char *); -extern int dns_name_packet(stralloc *,const char *,unsigned int); -extern int dns_txt_packet(stralloc *,const char *,unsigned int); -extern int dns_txt(stralloc *,const stralloc *); -extern int dns_mx_packet(stralloc *,const char *,unsigned int); -extern int dns_mx(stralloc *,const stralloc *); +extern int dns_name(stralloc *, const char *); +extern int dns_name_packet(stralloc *, const char *, unsigned int); +extern int dns_txt_packet(stralloc *, const char *, unsigned int); +extern int dns_txt(stralloc *, const stralloc *); +extern int dns_mx_packet(stralloc *, const char *, unsigned int); +extern int dns_mx(stralloc *, const stralloc *); /* IPv4 specific */ -extern int dns_ip4_packet(stralloc *,const char *,unsigned int); -extern int dns_ip4(stralloc *,stralloc *); -extern void dns_sortip4(char *,unsigned int); +extern int dns_ip4_packet(stralloc *, const char *, unsigned int); +extern int dns_ip4(stralloc *, stralloc *); +extern void dns_sortip4(char *, unsigned int); -extern int dns_ip4_qualify_rules(stralloc *,stralloc *,const stralloc *,const stralloc *); -extern int dns_ip4_qualify(stralloc *,stralloc *,const stralloc *); +extern int dns_ip4_qualify_rules(stralloc *, stralloc *, const stralloc *, const stralloc *); +extern int dns_ip4_qualify(stralloc *, stralloc *, const stralloc *); #define DNS_NAME4_DOMAIN 31 -extern int dns_name4_domain(char *,const char *); -extern int dns_name4(stralloc *,const char *); +extern int dns_name4_domain(char *, const char *); +extern int dns_name4(stralloc *, const char *); extern int randombind4(struct dns_transmit *); /* IPv6 specific */ -extern int dns_ip6_packet(stralloc *,const char *,unsigned int); -extern int dns_ip6(stralloc *,stralloc *); -extern void dns_sortip6(char *,unsigned int); +extern int dns_ip6_packet(stralloc *, const char *, unsigned int); +extern int dns_ip6(stralloc *, stralloc *); +extern void dns_sortip6(char *, unsigned int); -extern int dns_ip6_qualify_rules(stralloc *,stralloc *,const stralloc *,const stralloc *); -extern int dns_ip6_qualify(stralloc *,stralloc *,const stralloc *); +extern int dns_ip6_qualify_rules(stralloc *, stralloc *, const stralloc *, const stralloc *); +extern int dns_ip6_qualify(stralloc *, stralloc *, const stralloc *); -#define DNS_NAME6_DOMAIN (4*16+11) -extern int dns_name6_domain(char *,const char *); -extern int dns_name6(stralloc *,const char *); +#define DNS_NAME6_DOMAIN (4 * 16 + 11) +extern int dns_name6_domain(char *, const char *); +extern int dns_name6(stralloc *, const char *); -extern int dns_cname_packet(stralloc *,const char *,unsigned int); -extern int dns_cname(stralloc *,stralloc *); +extern int dns_cname_packet(stralloc *, const char *, unsigned int); +extern int dns_cname(stralloc *, stralloc *); -extern int dns_transmit_start6(struct dns_transmit *,const char *,int,const char *,const char *,const char *,const uint32 *); +extern int dns_transmit_start6( + struct dns_transmit *, const char *, int, const char *, const char *, const char *, const uint32 *); extern int randombind6(struct dns_transmit *); /* General */ @@ -191,9 +193,9 @@ extern int randombind6(struct dns_transmit *); extern void socketfree(struct dns_transmit *); extern void queryfree(struct dns_transmit *); extern void packetfree(struct dns_transmit *); -extern int serverwantstcp(const char *,unsigned int); -extern int serverfailed(const char *,unsigned int); -extern int getscopeid(const struct dns_transmit *,const char *); +extern int serverwantstcp(const char *, unsigned int); +extern int serverfailed(const char *, unsigned int); +extern int getscopeid(const struct dns_transmit *, const char *); extern int firstudp(struct dns_transmit *); extern int nextudp(struct dns_transmit *); extern int firsttcp(struct dns_transmit *); diff --git a/include/env.h b/include/env.h index 8f6ea87..88566cf 100644 --- a/include/env.h +++ b/include/env.h @@ -14,8 +14,8 @@ extern char **environ; extern int env_isinit; extern int env_init(); -extern /*@null@*/char *env_get(char *); -extern int env_put(char *,char *); +extern /*@null@*/ char *env_get(char *); +extern int env_put(char *, char *); extern int env_puts(char *); extern int env_set(char *); extern int env_unset(char *); diff --git a/include/error.h b/include/error.h index 7685c9e..63132ff 100644 --- a/include/error.h +++ b/include/error.h @@ -3,8 +3,8 @@ #include <errno.h> -#ifndef EPROTO /* OpenBSD compat */ -#define EPROTO EINTR +#ifndef EPROTO /* OpenBSD compat */ + #define EPROTO EINTR #endif #define error_str(i) errstr(i) @@ -16,42 +16,42 @@ extern char *error_str(int); */ /* djb backwards compatibility - deprecated form of system errors */ - /* Comparison of error codes and constants: +/* Comparison of error codes and constants: intern Linux FreeBSD OmniOS */ -#define error_intr EINTR /* -1 4 4 4 */ -#define error_nomem ENOMEM /* -2 12 12 12 */ -#define error_noent ENOENT /* -3 2 2 2 */ -#define error_txtbsy ETXTBSY /* -4 26 26 26 */ -#define error_io EIO /* -5 5 5 5 */ -#define error_exist EEXIST /* -6 17 17 17 */ -#define error_timeout ETIMEDOUT /* -7 110 60 145 */ -#define error_inprogress EINPROGRESS /* -8 115 36 160 */ -#define error_wouldblock EWOULDBLOCK /* -9 EAGAIN EAGAIN EAGAIN */ -#define error_again EAGAIN /* -10 11 35 11 */ -#define error_pipe EPIPE /* -11 32 32 32 */ -#define error_perm EPERM /* -12 1 1 1 */ -#define error_acces EACCES /* -13 13 13 13 */ -#define error_nodevice ENODEV /* -14 (6) (6) 19 */ -#define error_proto EPROTO /* -15 71 92 71 */ -#define error_isdir EISDIR /* -16 21 21 21 */ -#define error_connrefused ECONNREFUSED /* -17 111 61 146 */ +#define error_intr EINTR /* -1 4 4 4 */ +#define error_nomem ENOMEM /* -2 12 12 12 */ +#define error_noent ENOENT /* -3 2 2 2 */ +#define error_txtbsy ETXTBSY /* -4 26 26 26 */ +#define error_io EIO /* -5 5 5 5 */ +#define error_exist EEXIST /* -6 17 17 17 */ +#define error_timeout ETIMEDOUT /* -7 110 60 145 */ +#define error_inprogress EINPROGRESS /* -8 115 36 160 */ +#define error_wouldblock EWOULDBLOCK /* -9 EAGAIN EAGAIN EAGAIN */ +#define error_again EAGAIN /* -10 11 35 11 */ +#define error_pipe EPIPE /* -11 32 32 32 */ +#define error_perm EPERM /* -12 1 1 1 */ +#define error_acces EACCES /* -13 13 13 13 */ +#define error_nodevice ENODEV /* -14 (6) (6) 19 */ +#define error_proto EPROTO /* -15 71 92 71 */ +#define error_isdir EISDIR /* -16 21 21 21 */ +#define error_connrefused ECONNREFUSED /* -17 111 61 146 */ //extern int error_notdir; /* -18 20 20 20 */ -#define error_rofs EROFS /* -19 30 30 30 */ -#define error_connreset ECONNRESET /* -20 104 54 131 */ +#define error_rofs EROFS /* -19 30 30 30 */ +#define error_connreset ECONNRESET /* -20 104 54 131 */ /* djb uses some internal application error and class definitions -- revised (feh) */ -#define CAT -10 /* raw message w/o terminating \n */ -#define LOG -90 /* generic logging */ -#define INFO -91 /* named logging */ -#define TEMP -97 /* (triggered) temporay alert condition */ -#define ALERT -98 /* (triggered) alert condition */ -#define WARN -99 /* exception condition */ -#define ESOFT -100 /* soft error, reversed negative */ -#define EHARD -111 /* hard error, reversed negative */ -#define USAGE 100 /* usage error on call -- explicit usage() */ -#define SYNTAX 101 /* usage/syntax error on call -- explicit syntaxerror() */ -#define DROP 110 /* connection dropped -- explicit dropped() */ -#define FATAL 111 /* internal error -- all */ -#define ERROR 112 /* application error */ +#define CAT -10 /* raw message w/o terminating \n */ +#define LOG -90 /* generic logging */ +#define INFO -91 /* named logging */ +#define TEMP -97 /* (triggered) temporay alert condition */ +#define ALERT -98 /* (triggered) alert condition */ +#define WARN -99 /* exception condition */ +#define ESOFT -100 /* soft error, reversed negative */ +#define EHARD -111 /* hard error, reversed negative */ +#define USAGE 100 /* usage error on call -- explicit usage() */ +#define SYNTAX 101 /* usage/syntax error on call -- explicit syntaxerror() */ +#define DROP 110 /* connection dropped -- explicit dropped() */ +#define FATAL 111 /* internal error -- all */ +#define ERROR 112 /* application error */ #endif diff --git a/include/fd.h b/include/fd.h index 21047c9..d0c447c 100644 --- a/include/fd.h +++ b/include/fd.h @@ -1,8 +1,8 @@ #ifndef FD_H #define FD_H -extern int fd_copy(int,int); -extern int fd_move(int,int); +extern int fd_copy(int, int); +extern int fd_move(int, int); extern int fd_coe(int fd); #endif diff --git a/include/fifo.h b/include/fifo.h index 3c1bf68..7e7e0f1 100644 --- a/include/fifo.h +++ b/include/fifo.h @@ -7,6 +7,6 @@ @source s/qmail */ -int fifo_make(char *,int); +int fifo_make(char *, int); #endif diff --git a/include/fmt.h b/include/fmt.h index 6e68d60..e6de428 100644 --- a/include/fmt.h +++ b/include/fmt.h @@ -8,15 +8,15 @@ @brief conversion function declarations */ -#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ -#define FMT_LEN ((char *) 0) /* convenient abbreviation */ +#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */ +#define FMT_LEN ((char *)0) /* convenient abbreviation */ -extern unsigned int fmt_str(char *,char *); -extern unsigned int fmt_strn(char *,char *,unsigned int); -extern unsigned int fmt_uint(char *,unsigned int); -extern unsigned int fmt_uint0(char *,unsigned int,unsigned int); -extern unsigned int fmt_ulong(char *,unsigned long); -extern unsigned int fmt_xlong(char *,unsigned long); +extern unsigned int fmt_str(char *, char *); +extern unsigned int fmt_strn(char *, char *, unsigned int); +extern unsigned int fmt_uint(char *, unsigned int); +extern unsigned int fmt_uint0(char *, unsigned int, unsigned int); +extern unsigned int fmt_ulong(char *, unsigned long); +extern unsigned int fmt_xlong(char *, unsigned long); extern int fromhex(unsigned char); extern char tohex(char); diff --git a/include/genalloc.h b/include/genalloc.h index ee489af..d27c6b1 100644 --- a/include/genalloc.h +++ b/include/genalloc.h @@ -36,39 +36,64 @@ Macros: */ /* file: gen_alloc.h */ -#define GEN_ALLOC_typedef(ta,type,field,len,a) \ - typedef struct ta { type *field; unsigned int len; unsigned int a; } ta; +#define GEN_ALLOC_typedef(ta, type, field, len, a) \ + typedef struct ta { \ + type *field; \ + unsigned int len; \ + unsigned int a; \ + } ta; /* file: gen_allocdefs.h (deprecated) */ // used in: ipalloc, prioq, qmail-remote, qmail-inject, token822 -#define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \ -int ta_ready(x,n) register ta *x; register unsigned int n; \ -{ register unsigned int i; \ - if (x->field) { \ - i = x->a; \ - if (n > i) { \ - x->a = base + n + (n >> 3); \ - if (alloc_re(&x->field,i * sizeof(type),x->a * sizeof(type))) return 1; \ - x->a = i; return 0; } \ - return 1; } \ - x->len = 0; \ - return !!(x->field = (type *) alloc((x->a = n) * sizeof(type))); } +#define GEN_ALLOC_ready(ta, type, field, len, a, i, n, x, base, ta_ready) \ + int ta_ready(x, n) \ + register ta *x; \ + register unsigned int n; \ + { \ + register unsigned int i; \ + if (x->field) { \ + i = x->a; \ + if (n > i) { \ + x->a = base + n + (n >> 3); \ + if (alloc_re(&x->field, i * sizeof(type), x->a * sizeof(type))) return 1; \ + x->a = i; \ + return 0; \ + } \ + return 1; \ + } \ + x->len = 0; \ + return !!(x->field = (type *)alloc((x->a = n) * sizeof(type))); \ + } -#define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \ -int ta_rplus(x,n) register ta *x; register unsigned int n; \ -{ register unsigned int i; \ - if (x->field) { \ - i = x->a; n += x->len; \ - if (n > i) { \ - x->a = base + n + (n >> 3); \ - if (alloc_re(&x->field,i * sizeof(type),x->a * sizeof(type))) return 1; \ - x->a = i; return 0; } \ - return 1; } \ - x->len = 0; \ - return !!(x->field = (type *) alloc((x->a = n) * sizeof(type))); } +#define GEN_ALLOC_readyplus(ta, type, field, len, a, i, n, x, base, ta_rplus) \ + int ta_rplus(x, n) \ + register ta *x; \ + register unsigned int n; \ + { \ + register unsigned int i; \ + if (x->field) { \ + i = x->a; \ + n += x->len; \ + if (n > i) { \ + x->a = base + n + (n >> 3); \ + if (alloc_re(&x->field, i * sizeof(type), x->a * sizeof(type))) return 1; \ + x->a = i; \ + return 0; \ + } \ + return 1; \ + } \ + x->len = 0; \ + return !!(x->field = (type *)alloc((x->a = n) * sizeof(type))); \ + } -#define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \ -int ta_append(x,i) register ta *x; register type *i; \ -{ if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; } +#define GEN_ALLOC_append(ta, type, field, len, a, i, n, x, base, ta_rplus, ta_append) \ + int ta_append(x, i) \ + register ta *x; \ + register type *i; \ + { \ + if (!ta_rplus(x, 1)) return 0; \ + x->field[x->len++] = *i; \ + return 1; \ + } #endif diff --git a/include/getln.h b/include/getln.h index 23eb58b..60b5dcc 100644 --- a/include/getln.h +++ b/include/getln.h @@ -4,7 +4,7 @@ #include "buffer.h" #include "stralloc.h" -extern int getln(buffer *,stralloc *,int *,int); -extern int sgetln(buffer *,stralloc *,char **,unsigned int *,int); +extern int getln(buffer *, stralloc *, int *, int); +extern int sgetln(buffer *, stralloc *, char **, unsigned int *, int); #endif diff --git a/include/getoptb.h b/include/getoptb.h index 0338a15..61fa7aa 100644 --- a/include/getoptb.h +++ b/include/getoptb.h @@ -6,16 +6,16 @@ * - consolidated 'sgetopt.h' and 'subgetopt.h' into '(b)getopt.h' */ -#define optarg subgetoptarg -#define optind subgetoptind -#define optpos subgetoptpos -#define optproblem subgetoptproblem +#define optarg subgetoptarg +#define optind subgetoptind +#define optpos subgetoptpos +#define optproblem subgetoptproblem #define optprogname subgetoptprogname -#define opteof subgetoptdone +#define opteof subgetoptdone #define SUBGETOPTDONE -1 -extern int subgetopt(int,char **,char *); +extern int subgetopt(int, char **, char *); extern char *subgetoptarg; extern int subgetoptind; extern int subgetoptpos; diff --git a/include/iopause.h b/include/iopause.h index e3cfa01..871c8eb 100644 --- a/include/iopause.h +++ b/include/iopause.h @@ -5,24 +5,24 @@ #include <sys/types.h> #ifdef HAS_POLL_H -#include <poll.h> + #include <poll.h> typedef struct pollfd iopause_fd; -#define IOPAUSE_READ POLLIN -#define IOPAUSE_WRITE POLLOUT + #define IOPAUSE_READ POLLIN + #define IOPAUSE_WRITE POLLOUT #else typedef struct { int fd; short events; short revents; } iopause_fd; -#define IOPAUSE_READ 1 -#define IOPAUSE_WRITE 4 + #define IOPAUSE_READ 1 + #define IOPAUSE_WRITE 4 #endif #include "taia.h" -extern int iopause(iopause_fd *,unsigned int,struct taia *,struct taia *); +extern int iopause(iopause_fd *, unsigned int, struct taia *, struct taia *); extern unsigned long pollmax; #endif diff --git a/include/ip.h b/include/ip.h index 3f0c299..6088813 100644 --- a/include/ip.h +++ b/include/ip.h @@ -30,35 +30,39 @@ #define V4MAPPREFIX "::ffff:" #define HOSTNAMELEN 1025 -#define IP4MLMTU 512 -#define IP6MLMTU 1280 /* RFC 8200 */ -#define MTUSIZE IP6MLMTU +#define IP4MLMTU 512 +#define IP6MLMTU 1280 /* RFC 8200 */ +#define MTUSIZE IP6MLMTU -#define IP4_FMT 20 /* backwards compatibility */ -#define IP6_FMT 40 /* " */ -#define IPFMT 72 /* used in qmail-remote only (temp?) */ +#define IP4_FMT 20 /* backwards compatibility */ +#define IP6_FMT 40 /* " */ +#define IPFMT 72 /* used in qmail-remote only (temp?) */ /* may be better: */ //#define IP4_FMT IPFMT /* backwards compatibility */ //#define IP6_FMT IPFMT /* " */ /* deprecated: */ -#define FMT_IP4 IP4_FMT /* more backwards compatibility */ -#define FMT_IP6 IP6_FMT /* " */ +#define FMT_IP4 IP4_FMT /* more backwards compatibility */ +#define FMT_IP6 IP6_FMT /* " */ /* these structs are going deprecated (should replaced by socket lib) */ -struct ip4_address { unsigned char d[4]; }; /* 4 decimal pieces */ -struct ip6_address { unsigned char d[16]; }; /* 16 hex pieces */ -#define ip_address ip4_address /* backward compatibility */ +struct ip4_address { + unsigned char d[4]; +}; /* 4 decimal pieces */ +struct ip6_address { + unsigned char d[16]; +}; /* 16 hex pieces */ +#define ip_address ip4_address /* backward compatibility */ -unsigned int ip4_bytestring(stralloc *,char [4],int); -unsigned int ip4_cidr(char *,char [4],unsigned long *); -unsigned int ip4_fmt(char *,char [4]); -unsigned int ip4_scan(const char *,char [4]); -unsigned int ip4_scanbracket(const char *,char [4]); -unsigned int ia4_fmt(char *,char [4]); +unsigned int ip4_bytestring(stralloc *, char[4], int); +unsigned int ip4_cidr(char *, char[4], unsigned long *); +unsigned int ip4_fmt(char *, char[4]); +unsigned int ip4_scan(const char *, char[4]); +unsigned int ip4_scanbracket(const char *, char[4]); +unsigned int ia4_fmt(char *, char[4]); -const static char V4loopback[4] = {127,0,0,1}; -const static char V4localnet[4] = {0,0,0,0}; -const static char V4broadcast[4] = {-1,-1,-1,-1}; // all bits 1 +const static char V4loopback[4] = {127, 0, 0, 1}; +const static char V4localnet[4] = {0, 0, 0, 0}; +const static char V4broadcast[4] = {-1, -1, -1, -1}; // all bits 1 /*** * Compactified IPv6 addresses are really ugly to parse. @@ -82,26 +86,26 @@ const static char V4broadcast[4] = {-1,-1,-1,-1}; // all bits 1 * */ -unsigned int ip6_bytestring(stralloc *,char *,int); -unsigned int ip6_cidr(char *,char [16],unsigned long *); -unsigned int ip6_fmt(char *,char [16]); -unsigned int ip6_fmt_flat(char *,char [16]); -unsigned int ip6_ifscan(char *,char [16],stralloc *); -unsigned int ip6_scan(const char *,char [16]); -unsigned int ip6_scanbracket(const char *,char [16]); -unsigned int ip6_scan_flat(const char *,char [16]); -unsigned int ia6_fmt(char *,char [16]); +unsigned int ip6_bytestring(stralloc *, char *, int); +unsigned int ip6_cidr(char *, char[16], unsigned long *); +unsigned int ip6_fmt(char *, char[16]); +unsigned int ip6_fmt_flat(char *, char[16]); +unsigned int ip6_ifscan(char *, char[16], stralloc *); +unsigned int ip6_scan(const char *, char[16]); +unsigned int ip6_scanbracket(const char *, char[16]); +unsigned int ip6_scan_flat(const char *, char[16]); +unsigned int ia6_fmt(char *, char[16]); -static const unsigned char V4mappedprefix[12] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff}; -static const unsigned char V46loopback[16] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff, 127,0,0,1}; -static const unsigned char V6loopback[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1}; -static const unsigned char V6localnet[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; -static const unsigned char V6linklocal[2] = {0xfe,0x80}; +static const unsigned char V4mappedprefix[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; +static const unsigned char V46loopback[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127, 0, 0, 1}; +static const unsigned char V6loopback[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; +static const unsigned char V6localnet[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +static const unsigned char V6linklocal[2] = {0xfe, 0x80}; int fromhex(unsigned char); char tohex(char); -#define V6any V6localnet /* backward compatibility */ -#define ip6_isv4mapped(ip) (byte_equal(ip,12,V4mappedprefix)) +#define V6any V6localnet /* backward compatibility */ +#define ip6_isv4mapped(ip) (byte_equal(ip, 12, V4mappedprefix)) #endif diff --git a/include/logmsg.h b/include/logmsg.h index c480c8b..2f47a94 100644 --- a/include/logmsg.h +++ b/include/logmsg.h @@ -3,29 +3,29 @@ #include <errno.h> #include <stdlib.h> + #include "error.h" -extern void logmsg(const char *who, int ecode, unsigned int class, - const char *msg); +extern void logmsg(const char *who, int ecode, unsigned int class, const char *msg); /* useful combinations of params */ /* class: FATAL (hard) - system error */ -#define err_sys(w,e) logmsg(w,e,FATAL,"") -#define err_sys_plus(w,e,m) logmsg(w,e,FATAL,m) +#define err_sys(w, e) logmsg(w, e, FATAL, "") +#define err_sys_plus(w, e, m) logmsg(w, e, FATAL, m) /* class: WARN (application) - temporary error */ -#define err_tmp(w,e,m) logmsg(w,e,WARN,m) -#define err_tmp_plus(w,e,m) logmsg(w,e,WARN,m) +#define err_tmp(w, e, m) logmsg(w, e, WARN, m) +#define err_tmp_plus(w, e, m) logmsg(w, e, WARN, m) /* class: facultative ('int'ernal definition */ -#define err_int(w,e,c) logmsg(w,e,c,"") -#define err_int_plus(w,e,c,m) logmsg(w,e,c,m) +#define err_int(w, e, c) logmsg(w, e, c, "") +#define err_int_plus(w, e, c, m) logmsg(w, e, c, m) /* log messages */ /* #define log(w,m) logmsg(w,0,LOG,m) // obsoleted by */ -#define log_who(w,m) logmsg(w,0,LOG,m) -#define log_anon(m) logmsg("",0,LOG,m) -#define log_cat(n) logmsg("",0,CAT,m) +#define log_who(w, m) logmsg(w, 0, LOG, m) +#define log_anon(m) logmsg("", 0, LOG, m) +#define log_cat(n) logmsg("", 0, CAT, m) /* build log message from multiple partial strings */ extern char *build_log_msg(const char *[]); -#define B(...) build_log_msg((const char *[]){__VA_ARGS__,NULL}) // K/R sect. 7.3 +#define B(...) build_log_msg((const char *[]){__VA_ARGS__, NULL}) // K/R sect. 7.3 #endif diff --git a/include/pathexec.h b/include/pathexec.h index 9db6fb4..7ecc0b2 100644 --- a/include/pathexec.h +++ b/include/pathexec.h @@ -2,8 +2,8 @@ #define PATHEXEC_H #include "stralloc.h" -extern void pathexec_run(const char *,char *const *,char *const *); -extern int pathexec_env(const char *,const char *); +extern void pathexec_run(const char *, char *const *, char *const *); +extern int pathexec_env(const char *, const char *); extern int pathexec_multienv(stralloc *); extern void pathexec(char *const *); diff --git a/include/readclose.h b/include/readclose.h index a95f4f5..69264a1 100644 --- a/include/readclose.h +++ b/include/readclose.h @@ -1,14 +1,14 @@ #ifndef READCLOSE_H #define READCLOSE_H -#include "stralloc.h" #include "close.h" +#include "stralloc.h" -extern int readclose_append(int,stralloc *,unsigned int); -extern int readclose(int,stralloc *,unsigned int); +extern int readclose_append(int, stralloc *, unsigned int); +extern int readclose(int, stralloc *, unsigned int); -#define slurpclose readclose /* backwards compatibility */ +#define slurpclose readclose /* backwards compatibility */ -extern int openreadclose(const char *,stralloc *,unsigned int); +extern int openreadclose(const char *, stralloc *, unsigned int); #endif diff --git a/include/scan.h b/include/scan.h index f1d4030..1a55066 100644 --- a/include/scan.h +++ b/include/scan.h @@ -1,12 +1,12 @@ #ifndef SCAN_H #define SCAN_H -extern unsigned int scan_0x(const char *,unsigned int *); -extern unsigned int scan_xint(const char *,unsigned int *); -extern unsigned int scan_8long(const char *,unsigned long *); -extern unsigned int scan_uint(const char *,unsigned int *); -extern unsigned int scan_long(const char *,long *); -extern unsigned int scan_ulong(const char *,unsigned long *); -extern unsigned int scan_xlong(const char *,unsigned long *); +extern unsigned int scan_0x(const char *, unsigned int *); +extern unsigned int scan_xint(const char *, unsigned int *); +extern unsigned int scan_8long(const char *, unsigned long *); +extern unsigned int scan_uint(const char *, unsigned int *); +extern unsigned int scan_long(const char *, long *); +extern unsigned int scan_ulong(const char *, unsigned long *); +extern unsigned int scan_xlong(const char *, unsigned long *); #endif diff --git a/include/seek.h b/include/seek.h index 06aad97..a1d837b 100644 --- a/include/seek.h +++ b/include/seek.h @@ -5,11 +5,11 @@ typedef unsigned long seek_pos; extern seek_pos seek_cur(int); -extern int seek_set(int,seek_pos); +extern int seek_set(int, seek_pos); extern int seek_end(int); -extern int seek_trunc(int,seek_pos); +extern int seek_trunc(int, seek_pos); -#define seek_begin(fd) (seek_set((fd),(seek_pos) 0)) +#define seek_begin(fd) (seek_set((fd), (seek_pos)0)) #endif diff --git a/include/select.h b/include/select.h index 646dd50..7257ddd 100644 --- a/include/select.h +++ b/include/select.h @@ -1,11 +1,11 @@ #ifndef SELECT_H #define SELECT_H -#include <sys/types.h> #include <sys/time.h> +#include <sys/types.h> #ifdef HAS_SELECT_H -#include <sys/select.h> + #include <sys/select.h> #endif extern int select(); diff --git a/include/sig.h b/include/sig.h index 3efdd32..4d1743d 100644 --- a/include/sig.h +++ b/include/sig.h @@ -17,9 +17,9 @@ extern int sig_term; extern void (*sig_defaulthandler)(); extern void (*sig_ignorehandler)(); -extern void sig_catch(int,void (*)()); -#define sig_ignore(s) (sig_catch((s),sig_ignorehandler)) -#define sig_uncatch(s) (sig_catch((s),sig_defaulthandler)) +extern void sig_catch(int, void (*)()); +#define sig_ignore(s) (sig_catch((s), sig_ignorehandler)) +#define sig_uncatch(s) (sig_catch((s), sig_defaulthandler)) extern void sig_block(int); extern void sig_unblock(int); diff --git a/include/socket_if.h b/include/socket_if.h index 54cfa5f..3681d29 100644 --- a/include/socket_if.h +++ b/include/socket_if.h @@ -18,52 +18,52 @@ * - enlarged usage for ipv4socket -> dual bind IPv4/IPv6 */ -#define __APPLE_USE_RFC_3542 /* MacOS Anycast support */ +#define __APPLE_USE_RFC_3542 /* MacOS Anycast support */ #include "uint_t.h" /* IPv4 only */ -extern int socket_accept4(int,char [4],uint16 *); -extern int socket_bind4(int,const char [4],uint16); -extern int socket_bind4_reuse(int,const char [4],uint16); -extern int socket_connect4(int,const char [4],uint16); -extern int socket_send4(int,const char *,unsigned int,const char [4],uint16); -extern int socket_broadcast(int,const char *,unsigned int,uint16); +extern int socket_accept4(int, char[4], uint16 *); +extern int socket_bind4(int, const char[4], uint16); +extern int socket_bind4_reuse(int, const char[4], uint16); +extern int socket_connect4(int, const char[4], uint16); +extern int socket_send4(int, const char *, unsigned int, const char[4], uint16); +extern int socket_broadcast(int, const char *, unsigned int, uint16); /* Backward compatibility */ -#define socket_local4 socket_local +#define socket_local4 socket_local #define socket_remote4 socket_remote /* IPv6 only */ -extern int socket_bind6(int,const char [16],uint16,uint32); -extern int socket_bind6_reuse(int,const char [16],uint16,uint32); -extern int socket_connect6(int,const char [16],uint16,uint32); -extern int socket_send6(int,const char *,unsigned int,const char [16],uint16,uint32); -extern const char* socket_getifname(uint32); +extern int socket_bind6(int, const char[16], uint16, uint32); +extern int socket_bind6_reuse(int, const char[16], uint16, uint32); +extern int socket_connect6(int, const char[16], uint16, uint32); +extern int socket_send6(int, const char *, unsigned int, const char[16], uint16, uint32); +extern const char *socket_getifname(uint32); extern uint32 socket_getifidx(const char *); extern int socket_ip6optionskill(int); extern int socket_ip6anycast(int); /* Common IPv4 & IPv6 */ -extern int socket_accept(int,char [16],uint16 *,uint32 *); -extern int socket_bind(int,const char [16],uint16,uint32); -extern int socket_bind_reuse(int,const char [16],uint16,uint32); -extern int socket_connect(int,const char [16],uint16,uint32); +extern int socket_accept(int, char[16], uint16 *, uint32 *); +extern int socket_bind(int, const char[16], uint16, uint32); +extern int socket_bind_reuse(int, const char[16], uint16, uint32); +extern int socket_connect(int, const char[16], uint16, uint32); extern int socket_connected(int); -extern int socket_listen(int,int); -extern int socket_local(int,char [16],uint16 *,uint32 *); -extern int socket_recv(int,char *,unsigned int,char [16],uint16 *,uint32 *); -extern int socket_remote(int,char [16],uint16 *,uint32 *); -extern int socket_send(int,const char *,unsigned int,const char [16],uint16,uint32); -extern void socket_tryreservein(int,int); +extern int socket_listen(int, int); +extern int socket_local(int, char[16], uint16 *, uint32 *); +extern int socket_recv(int, char *, unsigned int, char[16], uint16 *, uint32 *); +extern int socket_remote(int, char[16], uint16 *, uint32 *); +extern int socket_send(int, const char *, unsigned int, const char[16], uint16, uint32); +extern void socket_tryreservein(int, int); extern int socket_ipoptionskill(int); extern int socket_dualstack(int); extern int socket_nodualstack(int); /* Backward compatibility */ #define socket_accept6 socket_accept -#define socket_local6 socket_local -#define socket_recv6 socket_recv +#define socket_local6 socket_local +#define socket_recv6 socket_recv #define socket_remote6 socket_remote /* TCP */ diff --git a/include/str.h b/include/str.h index fe36d0b..0d74812 100644 --- a/include/str.h +++ b/include/str.h @@ -8,18 +8,18 @@ * - added '*str_append' and 'str_cat' */ -extern unsigned int str_copy(char *,const char *); -extern unsigned int str_copyb(char *,const char *,unsigned int); -extern int str_diff(const char *,const char *); -extern int str_diffn(const char *,const char *,unsigned int); +extern unsigned int str_copy(char *, const char *); +extern unsigned int str_copyb(char *, const char *, unsigned int); +extern int str_diff(const char *, const char *); +extern int str_diffn(const char *, const char *, unsigned int); //extern unsigned int str_len(char *); // --> this produces lot of warnings !!! extern unsigned int str_len(); -extern unsigned int str_chr(const char *,int); -extern unsigned int str_rchr(const char *,int); -extern int str_start(const char *,const char *); -extern char *str_append(char *out,const char *s); +extern unsigned int str_chr(const char *, int); +extern unsigned int str_rchr(const char *, int); +extern int str_start(const char *, const char *); +extern char *str_append(char *out, const char *s); -#define str_equal(s,t) (!str_diff((s),(t))) -#define str_cat(s,t) str_append(s,t) +#define str_equal(s, t) (!str_diff((s), (t))) +#define str_cat(s, t) str_append(s, t) #endif diff --git a/include/stralloc.h b/include/stralloc.h index 0f4a7d2..b472891 100644 --- a/include/stralloc.h +++ b/include/stralloc.h @@ -15,35 +15,35 @@ */ typedef struct stralloc { - char* s; + char *s; size_t len; size_t a; } stralloc; //extern int stralloc_ready(stralloc *,unsigned int); -extern int stralloc_ready(stralloc *sa,size_t len); +extern int stralloc_ready(stralloc *sa, size_t len); //extern int stralloc_readyplus(stralloc *,unsigned int); -extern int stralloc_readyplus(stralloc *sa,size_t len); -extern int stralloc_copy(stralloc *,stralloc *); -extern int stralloc_cat(stralloc *,stralloc *); -extern int stralloc_copys(stralloc *,const char *); -extern int stralloc_cats(stralloc *,const char *); -extern int stralloc_copyb(stralloc *,const char *,unsigned int); -extern int stralloc_catb(stralloc *,const char *,unsigned int); +extern int stralloc_readyplus(stralloc *sa, size_t len); +extern int stralloc_copy(stralloc *, stralloc *); +extern int stralloc_cat(stralloc *, stralloc *); +extern int stralloc_copys(stralloc *, const char *); +extern int stralloc_cats(stralloc *, const char *); +extern int stralloc_copyb(stralloc *, const char *, unsigned int); +extern int stralloc_catb(stralloc *, const char *, unsigned int); //extern int stralloc_append(stralloc *,char *); /* beware: this takes a pointer to 1 char */ -extern int stralloc_append(stralloc *sa,const char *in); /* beware: this takes a pointer to 1 char */ -extern int stralloc_starts(stralloc *,const char *); +extern int stralloc_append(stralloc *sa, const char *in); /* beware: this takes a pointer to 1 char */ +extern int stralloc_starts(stralloc *, const char *); -#define stralloc_0(sa) stralloc_append(sa,"") +#define stralloc_0(sa) stralloc_append(sa, "") -extern int stralloc_catulong0(stralloc *,unsigned long,unsigned int); -extern int stralloc_catlong0(stralloc *,long,unsigned int); +extern int stralloc_catulong0(stralloc *, unsigned long, unsigned int); +extern int stralloc_catlong0(stralloc *, long, unsigned int); extern void stralloc_free(stralloc *); -#define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0)) -#define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n))) -#define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n))) -#define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0)) +#define stralloc_catlong(sa, l) (stralloc_catlong0((sa), (l), 0)) +#define stralloc_catuint0(sa, i, n) (stralloc_catulong0((sa), (i), (n))) +#define stralloc_catint0(sa, i, n) (stralloc_catlong0((sa), (i), (n))) +#define stralloc_catint(sa, i) (stralloc_catlong0((sa), (i), 0)) #endif diff --git a/include/tai.h b/include/tai.h index 37d24e0..99b1a40 100644 --- a/include/tai.h +++ b/include/tai.h @@ -32,7 +32,7 @@ typedef struct tai { } tai64; -#define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64) (u))) +#define tai_unix(t, u) ((void)((t)->x = 4611686018427387914ULL + (uint64)(u))) /* tai_now puts the current time into t. More precisely: tai_now puts * into t its best guess as to the TAI64 label for the 1-second interval @@ -46,26 +46,26 @@ void tai_now(struct tai *); /* tai_approx returns a double-precision approximation to t. The result * of tai_approx is always nonnegative. */ -#define tai_approx(t) ((double) ((t)->x)) +#define tai_approx(t) ((double)((t)->x)) /* tai_add adds a to b modulo 2^64 and puts the result into t. The * inputs and output may overlap. */ -void tai_add(struct tai *,const struct tai *,const struct tai *); +void tai_add(struct tai *, const struct tai *, const struct tai *); /* tai_sub subtracts b from a modulo 2^64 and puts the result into t. * The inputs and output may overlap. */ -void tai_sub(struct tai *,const struct tai *,const struct tai *); +void tai_sub(struct tai *, const struct tai *, const struct tai *); /* tai_less returns 1 if a is less than b, 0 otherwise. */ -#define tai_less(t,u) ((t)->x < (u)->x) +#define tai_less(t, u) ((t)->x < (u)->x) #define TAI_PACK 8 /* tai_pack converts a TAI64 label from internal format in t to external * TAI64 format in buf. */ -void tai_pack(char *,const struct tai *); +void tai_pack(char *, const struct tai *); /* tai_unpack converts a TAI64 label from external TAI64 format in buf * to internal format in t. */ -void tai_unpack(const char *,struct tai *); +void tai_unpack(const char *, struct tai *); -void tai_uint(struct tai *,unsigned int); +void tai_uint(struct tai *, unsigned int); #ifdef __cplusplus } diff --git a/include/taia.h b/include/taia.h index 03f455c..2aaad06 100644 --- a/include/taia.h +++ b/include/taia.h @@ -14,27 +14,27 @@ struct taia { struct tai sec; unsigned long nano; /* 0...999999999 */ unsigned long atto; /* 0...999999999 */ -} ; +}; -extern void taia_tai(struct taia *,struct tai *); +extern void taia_tai(struct taia *, struct tai *); extern int taia_now(struct taia *); extern double taia_approx(struct taia *); extern double taia_frac(struct taia *); -extern void taia_add(struct taia *,struct taia *,struct taia *); -extern void taia_sub(struct taia *,struct taia *,struct taia *); -extern void taia_half(struct taia *,struct taia *); -extern int taia_less(struct taia *,struct taia *); +extern void taia_add(struct taia *, struct taia *, struct taia *); +extern void taia_sub(struct taia *, struct taia *, struct taia *); +extern void taia_half(struct taia *, struct taia *); +extern int taia_less(struct taia *, struct taia *); #define TAIA_PACK 16 -extern void taia_pack(char *,struct taia *); -extern void taia_unpack(char *,struct taia *); +extern void taia_pack(char *, struct taia *); +extern void taia_unpack(char *, struct taia *); #define TAIA_FMTFRAC 19 -extern unsigned int taia_fmtfrac(char *,struct taia *); +extern unsigned int taia_fmtfrac(char *, struct taia *); -extern void taia_uint(struct taia *,unsigned int); +extern void taia_uint(struct taia *, unsigned int); #endif diff --git a/include/timeout.h b/include/timeout.h index 1b45e9f..7b1d6b1 100644 --- a/include/timeout.h +++ b/include/timeout.h @@ -1,7 +1,7 @@ #ifndef TIMEOUT_H #define TIMEOUT_H -extern int timeoutread(int,int,char *,int); -extern int timeoutwrite(int,int,char *,int); +extern int timeoutread(int, int, char *, int); +extern int timeoutwrite(int, int, char *, int); #endif diff --git a/include/timeoutconn.h b/include/timeoutconn.h index 308ec5a..6462488 100644 --- a/include/timeoutconn.h +++ b/include/timeoutconn.h @@ -3,8 +3,8 @@ #include "uint_t.h" -extern int timeoutconn4(int,char *,uint16,unsigned int); -extern int timeoutconn6(int,char *,uint16,unsigned int,uint32); -extern int timeoutconn(int,char *,uint16,unsigned int,uint32); +extern int timeoutconn4(int, char *, uint16, unsigned int); +extern int timeoutconn6(int, char *, uint16, unsigned int, uint32); +extern int timeoutconn(int, char *, uint16, unsigned int, uint32); #endif diff --git a/include/uint_t.h b/include/uint_t.h index 84eb06e..aba0f38 100644 --- a/include/uint_t.h +++ b/include/uint_t.h @@ -9,68 +9,67 @@ */ #ifndef UINT8_H -#define UINT8_H + #define UINT8_H -#ifdef HAS_UINT8_H + #ifdef HAS_UINT8_H typedef uint8_t uint8; -#else + #else typedef unsigned char uint8; -#endif + #endif #endif #ifndef UINT16_H -#define UINT16_H + #define UINT16_H typedef uint16_t uint16; -extern void uint16_pack(char *,uint16); -extern void uint16_pack_big(char *,uint16); -extern void uint16_unpack(char *,uint16 *); -extern void uint16_unpack_big(char *,uint16 *); +extern void uint16_pack(char *, uint16); +extern void uint16_pack_big(char *, uint16); +extern void uint16_unpack(char *, uint16 *); +extern void uint16_unpack_big(char *, uint16 *); #endif #ifndef UINT32_H -#define UINT32_H + #define UINT32_H typedef uint32_t uint32; -extern void uint32_pack(char *,uint32); -extern void uint32_pack_big(char *,uint32); -extern void uint32_unpack(char *,uint32 *); -extern void uint32_unpack_big(char *,uint32 *); +extern void uint32_pack(char *, uint32); +extern void uint32_pack_big(char *, uint32); +extern void uint32_unpack(char *, uint32 *); +extern void uint32_unpack_big(char *, uint32 *); #endif #ifndef UINT64_H -#define UINT64_H + #define UINT64_H -#ifdef HAS_UINT64_H + #ifdef HAS_UINT64_H typedef uint64_t uint64; -#else + #else typedef unsigned long long uint64; -#endif + #endif -extern void uint64_pack(char *,uint64); -extern void uint64_pack_big(char *,uint64); -extern void uint64_unpack(char *,uint64 *); -extern void uint64_unpack_big(char *,uint64 *); +extern void uint64_pack(char *, uint64); +extern void uint64_pack_big(char *, uint64); +extern void uint64_unpack(char *, uint64 *); +extern void uint64_unpack_big(char *, uint64 *); #endif #ifndef UINT128_H -#define UINT128_H + #define UINT128_H /* uint128 used for native IPv6 address presentation */ -struct uint128_t -{ - uint64_t hi; /* routing area */ - uint64_t lo; /* local area */ +struct uint128_t { + uint64_t hi; /* routing area */ + uint64_t lo; /* local area */ }; typedef struct uint128_t uint128; -extern void uint128_pack(char *,uint128); -extern void uint128_pack_big(char *,uint128); -extern void uint128_unpack(char *,uint128 *); -extern void uint128_unpack_big(char *,uint128 *); +extern void uint128_pack(char *, uint128); +extern void uint128_pack_big(char *, uint128); +extern void uint128_unpack(char *, uint128 *); +extern void uint128_unpack_big(char *, uint128 *); #endif diff --git a/include/wait.h b/include/wait.h index f7cfc60..af8a979 100644 --- a/include/wait.h +++ b/include/wait.h @@ -7,14 +7,14 @@ @source s/qmail */ -int wait_pid(int *,int); +int wait_pid(int *, int); int wait_nohang(int *); int wait_stop(); int wait_stopnohang(); -#define wait_crashed(w) ((w) & 127) +#define wait_crashed(w) ((w) & 127) #define wait_exitcode(w) ((w) >> 8) -#define wait_stopsig(w) ((w) >> 8) -#define wait_stopped(w) (((w) & 127) == 127) +#define wait_stopsig(w) ((w) >> 8) +#define wait_stopped(w) (((w) & 127) == 127) #endif diff --git a/src/alloc.c b/src/alloc.c index 8ec0a7e..cee09ac 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1,21 +1,27 @@ -#include <stdlib.h> +#include "alloc.h" + #include <errno.h> #include <limits.h> +#include <stdlib.h> + #include "byte.h" -#include "alloc.h" -#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ -#define SPACE 4096 /* must be multiple of ALIGNMENT */ +#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ +#define SPACE 4096 /* must be multiple of ALIGNMENT */ -typedef union { char irrelevant[ALIGNMENT]; double d; } aligned; +typedef union { + char irrelevant[ALIGNMENT]; + double d; +} aligned; static aligned realspace[SPACE / ALIGNMENT]; -#define space ((char *) realspace) +#define space ((char *)realspace) static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ -/*@null@*//*@out@*/char *alloc(unsigned int n) { +/*@null@*/ /*@out@*/ char *alloc(unsigned int n) +{ char *x; -/* Guninski exploit + patch from Qualys (CVE-2005-1513) */ + /* Guninski exploit + patch from Qualys (CVE-2005-1513) */ if (n >= (INT_MAX >> 3)) { errno = ENOMEM; @@ -23,27 +29,29 @@ static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ } n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */ - if (n <= avail) { avail -= n; return space + avail; } + if (n <= avail) { + avail -= n; + return space + avail; + } x = malloc(n); if (!x) errno = ENOMEM; return x; } -void alloc_free(char *x) +void alloc_free(char *x) { if (x >= space) - if (x < space + SPACE) - return; /* XXX: assuming that pointers are flat */ + if (x < space + SPACE) return; /* XXX: assuming that pointers are flat */ free(x); } -int alloc_re(char **x,unsigned int m,unsigned int n) +int alloc_re(char **x, unsigned int m, unsigned int n) { char *y; y = alloc(n); if (!y) return 0; - byte_copy(y,m,*x); + byte_copy(y, m, *x); qfree(*x); *x = y; return 1; diff --git a/src/base64.c b/src/base64.c index 986201e..636874e 100644 --- a/src/base64.c +++ b/src/base64.c @@ -1,6 +1,7 @@ #include "base64.h" -#include "stralloc.h" + #include "str.h" +#include "stralloc.h" /** @file base64.c @@ -9,13 +10,12 @@ @brief base64 en+decoding of strings */ -static char *b64alpha = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static char *b64alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; #define B64PAD '=' /* returns 0 ok, 1 illegal, -1 problem */ -int b64decode(const unsigned char *in,int l,stralloc *out) /* not null terminated */ +int b64decode(const unsigned char *in, int l, stralloc *out) /* not null terminated */ { int p = 0; int n; @@ -25,83 +25,89 @@ int b64decode(const unsigned char *in,int l,stralloc *out) /* not null terminat unsigned char b[3]; if (l == 0) { - if (!stralloc_copys(out,"")) return -1; + if (!stralloc_copys(out, "")) return -1; return 0; } - while (in[l-1] == B64PAD) { - p ++; + while (in[l - 1] == B64PAD) { + p++; l--; } n = (l + p) / 4; i = (n * 3) - p; - if (!stralloc_ready(out,i)) return -1; + if (!stralloc_ready(out, i)) return -1; out->len = i; s = out->s; - for (i = 0; i < n - 1 ; i++) { + for (i = 0; i < n - 1; i++) { x = 0; for (j = 0; j < 4; j++) { - if(in[j] >= 'A' && in[j] <= 'Z') + if (in[j] >= 'A' && in[j] <= 'Z') x = (x << 6) + (unsigned int)(in[j] - 'A' + 0); - else if(in[j] >= 'a' && in[j] <= 'z') + else if (in[j] >= 'a' && in[j] <= 'z') x = (x << 6) + (unsigned int)(in[j] - 'a' + 26); - else if(in[j] >= '0' && in[j] <= '9') + else if (in[j] >= '0' && in[j] <= '9') x = (x << 6) + (unsigned int)(in[j] - '0' + 52); - else if(in[j] == '+') + else if (in[j] == '+') x = (x << 6) + 62; - else if(in[j] == '/') + else if (in[j] == '/') x = (x << 6) + 63; - else if(in[j] == '=') + else if (in[j] == '=') x = (x << 6); } - s[2] = (unsigned char)(x & 255); x >>= 8; - s[1] = (unsigned char)(x & 255); x >>= 8; - s[0] = (unsigned char)(x & 255); x >>= 8; - s += 3; in += 4; + s[2] = (unsigned char)(x & 255); + x >>= 8; + s[1] = (unsigned char)(x & 255); + x >>= 8; + s[0] = (unsigned char)(x & 255); + x >>= 8; + s += 3; + in += 4; } x = 0; for (j = 0; j < 4; j++) { - if(in[j] >= 'A' && in[j] <= 'Z') + if (in[j] >= 'A' && in[j] <= 'Z') x = (x << 6) + (unsigned int)(in[j] - 'A' + 0); - else if(in[j] >= 'a' && in[j] <= 'z') + else if (in[j] >= 'a' && in[j] <= 'z') x = (x << 6) + (unsigned int)(in[j] - 'a' + 26); - else if(in[j] >= '0' && in[j] <= '9') + else if (in[j] >= '0' && in[j] <= '9') x = (x << 6) + (unsigned int)(in[j] - '0' + 52); - else if(in[j] == '+') + else if (in[j] == '+') x = (x << 6) + 62; - else if(in[j] == '/') + else if (in[j] == '/') x = (x << 6) + 63; - else if(in[j] == '=') + else if (in[j] == '=') x = (x << 6); } - b[2] = (unsigned char)(x & 255); x >>= 8; - b[1] = (unsigned char)(x & 255); x >>= 8; - b[0] = (unsigned char)(x & 255); x >>= 8; + b[2] = (unsigned char)(x & 255); + x >>= 8; + b[1] = (unsigned char)(x & 255); + x >>= 8; + b[0] = (unsigned char)(x & 255); + x >>= 8; - for (i = 0; i < 3 - p; i++) - s[i] = b[i]; + for (i = 0; i < 3 - p; i++) s[i] = b[i]; return 0; } -int b64encode(stralloc *in,stralloc *out) /* not null terminated */ +int b64encode(stralloc *in, stralloc *out) /* not null terminated */ { unsigned char a, b, c; int i; char *s; if (in->len == 0) { - if (!stralloc_copys(out,"")) return -1; + if (!stralloc_copys(out, "")) return -1; return 0; } - i = in->len / 3 * 4 + 4; - if (!stralloc_ready(out,i)) return -1; + i = in->len / 3 * 4 + 4; + if (!stralloc_ready(out, i)) return -1; s = out->s; for (i = 0; i < in->len; i += 3) { @@ -110,13 +116,17 @@ int b64encode(stralloc *in,stralloc *out) /* not null terminated */ c = i + 2 < in->len ? in->s[i + 2] : 0; *s++ = b64alpha[a >> 2]; - *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)]; + *s++ = b64alpha[((a & 3) << 4) | (b >> 4)]; - if (i + 1 >= in->len) *s++ = B64PAD; - else *s++ = b64alpha[((b & 15) << 2) | (c >> 6)]; + if (i + 1 >= in->len) + *s++ = B64PAD; + else + *s++ = b64alpha[((b & 15) << 2) | (c >> 6)]; - if (i + 2 >= in->len) *s++ = B64PAD; - else *s++ = b64alpha[c & 63]; + if (i + 2 >= in->len) + *s++ = B64PAD; + else + *s++ = b64alpha[c & 63]; } out->len = s - out->s; diff --git a/src/buffer.c b/src/buffer.c index 92d72b1..c30caf2 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,8 +1,10 @@ -#include <unistd.h> #include "buffer.h" -#include "str.h" + +#include <unistd.h> + #include "byte.h" #include "error.h" +#include "str.h" /** @file buffer.c @@ -10,7 +12,7 @@ @brief input/output routines */ -void buffer_init(buffer *s,ssize_t (*op)(),int fd,char *buf,size_t len) +void buffer_init(buffer *s, ssize_t (*op)(), int fd, char *buf, size_t len) { s->x = buf; s->fd = fd; @@ -19,47 +21,47 @@ void buffer_init(buffer *s,ssize_t (*op)(),int fd,char *buf,size_t len) s->n = len; } -ssize_t buffer_0_read(int fd,char *buf,size_t len) +ssize_t buffer_0_read(int fd, char *buf, size_t len) { if (buffer_flush(buffer_1) == -1) return -1; - return read(fd,buf,len); + return read(fd, buf, len); } char buffer_0_space[BUFFER_INSIZE]; -static buffer it0 = BUFFER_INIT(buffer_0_read,0,buffer_0_space,sizeof(buffer_0_space)); +static buffer it0 = BUFFER_INIT(buffer_0_read, 0, buffer_0_space, sizeof(buffer_0_space)); buffer *buffer_0 = &it0; char buffer_1_space[BUFFER_OUTSIZE]; -static buffer it1 = BUFFER_INIT(write,1,buffer_1_space,sizeof(buffer_1_space)); +static buffer it1 = BUFFER_INIT(write, 1, buffer_1_space, sizeof(buffer_1_space)); buffer *buffer_1 = &it1; char buffer_2_space[BUFFER_OUTSIZE]; -static buffer it2 = BUFFER_INIT(write,2,buffer_2_space,sizeof(buffer_2_space)); +static buffer it2 = BUFFER_INIT(write, 2, buffer_2_space, sizeof(buffer_2_space)); buffer *buffer_2 = &it2; char buffer_0_small[BUFFER_SMALL]; -static buffer is0 = BUFFER_INIT(buffer_0_read,0,buffer_0_small,sizeof(buffer_0_small)); +static buffer is0 = BUFFER_INIT(buffer_0_read, 0, buffer_0_small, sizeof(buffer_0_small)); buffer *buffer_0small = &is0; char buffer_1_small[BUFFER_SMALL]; -static buffer is1 = BUFFER_INIT(write,1,buffer_1_small,sizeof(buffer_1_small)); +static buffer is1 = BUFFER_INIT(write, 1, buffer_1_small, sizeof(buffer_1_small)); buffer *buffer_1small = &is1; char buffer_2_small[BUFFER_SMALL]; -static buffer is2 = BUFFER_INIT(write,2,buffer_2_small,sizeof(buffer_2_small)); +static buffer is2 = BUFFER_INIT(write, 2, buffer_2_small, sizeof(buffer_2_small)); buffer *buffer_2small = &is2; -ssize_t buffer_unixread(int fd,char *buf,size_t len) +ssize_t buffer_unixread(int fd, char *buf, size_t len) { - return read(fd,buf,len); + return read(fd, buf, len); } -ssize_t buffer_unixwrite(int fd,char *buf,size_t len) +ssize_t buffer_unixwrite(int fd, char *buf, size_t len) { - return write(fd,buf,len); + return write(fd, buf, len); } -int buffer_copy(buffer *bout,buffer *bin) +int buffer_copy(buffer *bout, buffer *bin) { int n; char *x; @@ -69,89 +71,90 @@ int buffer_copy(buffer *bout,buffer *bin) if (n < 0) return -2; if (!n) return 0; x = buffer_PEEK(bin); - if (buffer_put(bout,x,n) == -1) return -3; - buffer_SEEK(bin,n); + if (buffer_put(bout, x, n) == -1) return -3; + buffer_SEEK(bin, n); } } -static int oneread(ssize_t (*op)(),int fd,char *buf,size_t len) +static int oneread(ssize_t (*op)(), int fd, char *buf, size_t len) { int r; for (;;) { - r = op(fd,buf,len); - if (r == -1) if (errno == EINTR) continue; + r = op(fd, buf, len); + if (r == -1) + if (errno == EINTR) continue; return r; } } -static int getthis(buffer *s,char *buf,size_t len) +static int getthis(buffer *s, char *buf, size_t len) { if (len > s->p) len = s->p; s->p -= len; - byte_copy(buf,len,s->x + s->n); + byte_copy(buf, len, s->x + s->n); s->n += len; return len; } -int buffer_feed(buffer *s) +int buffer_feed(buffer *s) { int r; if (s->p) return s->p; - r = oneread(s->op,s->fd,s->x,s->n); + r = oneread(s->op, s->fd, s->x, s->n); if (r <= 0) return r; s->p = r; s->n -= r; - if (s->n > 0) byte_copyr(s->x + s->n,r,s->x); + if (s->n > 0) byte_copyr(s->x + s->n, r, s->x); return r; } -int buffer_bget(buffer *s,char *buf,size_t len) +int buffer_bget(buffer *s, char *buf, size_t len) { int r; - if (s->p > 0) return getthis(s,buf,len); - if (s->n <= len) return oneread(s->op,s->fd,buf,s->n); - r = buffer_feed(s); - if (r <= 0) return r; - return getthis(s,buf,len); + if (s->p > 0) return getthis(s, buf, len); + if (s->n <= len) return oneread(s->op, s->fd, buf, s->n); + r = buffer_feed(s); + if (r <= 0) return r; + return getthis(s, buf, len); } -int buffer_get(buffer *s,char *buf,size_t len) +int buffer_get(buffer *s, char *buf, size_t len) { int r; - if (s->p > 0) return getthis(s,buf,len); - if (s->n <= len) return oneread(s->op,s->fd,buf,len); - r = buffer_feed(s); + if (s->p > 0) return getthis(s, buf, len); + if (s->n <= len) return oneread(s->op, s->fd, buf, len); + r = buffer_feed(s); if (r <= 0) return r; - return getthis(s,buf,len); + return getthis(s, buf, len); } -char *buffer_peek(buffer *s) +char *buffer_peek(buffer *s) { return s->x + s->n; } -void buffer_seek(buffer *s,size_t len) +void buffer_seek(buffer *s, size_t len) { s->n += len; s->p -= len; } -static int allwrite(ssize_t (*op)(),int fd,const char *buf,size_t len) +static int allwrite(ssize_t (*op)(), int fd, const char *buf, size_t len) { int w; while (len) { - w = op(fd,buf,len); + w = op(fd, buf, len); if (w == -1) { if (errno == EINTR) continue; return -1; /* note that some data may have been written */ } if (w == 0) /* luser's fault */ - ; + ; buf += w; len -= w; } @@ -165,24 +168,27 @@ int buffer_flush(buffer *s) p = s->p; if (!p) return 0; s->p = 0; - return allwrite(s->op,s->fd,s->x,p); + return allwrite(s->op, s->fd, s->x, p); } -int buffer_putalign(buffer *s,const char *buf,size_t len) +int buffer_putalign(buffer *s, const char *buf, size_t len) { unsigned int n; while (len > (n = s->n - s->p)) { - byte_copy(s->x + s->p,n,buf); s->p += n; buf += n; len -= n; + byte_copy(s->x + s->p, n, buf); + s->p += n; + buf += n; + len -= n; if (buffer_flush(s) == -1) return -1; } /* now len <= s->n - s->p */ - byte_copy(s->x + s->p,len,buf); + byte_copy(s->x + s->p, len, buf); s->p += len; return 0; } -int buffer_put(buffer *s,const char *buf,size_t len) +int buffer_put(buffer *s, const char *buf, size_t len) { unsigned int n; @@ -193,34 +199,34 @@ int buffer_put(buffer *s,const char *buf,size_t len) if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE; while (len > s->n) { if (n > len) n = len; - if (allwrite(s->op,s->fd,buf,n) == -1) return -1; + if (allwrite(s->op, s->fd, buf, n) == -1) return -1; buf += n; len -= n; } } /* now len <= s->n - s->p */ - byte_copy(s->x + s->p,len,buf); + byte_copy(s->x + s->p, len, buf); s->p += len; return 0; } -int buffer_putflush(buffer *s,const char *buf,size_t len) +int buffer_putflush(buffer *s, const char *buf, size_t len) { if (buffer_flush(s) == -1) return -1; - return allwrite(s->op,s->fd,buf,len); + return allwrite(s->op, s->fd, buf, len); } -int buffer_putsalign(buffer *s,const char *buf) +int buffer_putsalign(buffer *s, const char *buf) { - return buffer_putalign(s,buf,str_len(buf)); + return buffer_putalign(s, buf, str_len(buf)); } -int buffer_puts(buffer *s,const char *buf) +int buffer_puts(buffer *s, const char *buf) { - return buffer_put(s,buf,str_len(buf)); + return buffer_put(s, buf, str_len(buf)); } -int buffer_putsflush(buffer *s,const char *buf) +int buffer_putsflush(buffer *s, const char *buf) { - return buffer_putflush(s,buf,str_len(buf)); + return buffer_putflush(s, buf, str_len(buf)); } @@ -6,7 +6,7 @@ @brief byte manipulation functions */ -unsigned int byte_chr(char *s,register unsigned int n,int c) +unsigned int byte_chr(char *s, register unsigned int n, int c) { register char ch; register char *t; @@ -14,49 +14,57 @@ unsigned int byte_chr(char *s,register unsigned int n,int c) ch = c; t = s; for (;;) { + // clang-format off if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; if (!n) { break; } if (*t == ch) { break; } ++t; --n; + // clang-format on } return t - s; } -void byte_copy(register char *to,register unsigned int n,register char *from) +void byte_copy(register char *to, register unsigned int n, register char *from) { for (;;) { + // clang-format off if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; if (!n) { return; } *to++ = *from++; --n; + // clang-format on } } -void byte_copyr(register char *to,register unsigned int n,register char *from) +void byte_copyr(register char *to, register unsigned int n, register char *from) { to += n; from += n; + for (;;) { + // clang-format off if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; if (!n) { return; } *--to = *--from; --n; + // clang-format on } } -int byte_diff(register char *s,register unsigned int n,register char *t) +int byte_diff(register char *s, register unsigned int n, register char *t) { for (;;) { + // clang-format off if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n; + // clang-format on } - return ((int)(unsigned int)(unsigned char) *s) - - ((int)(unsigned int)(unsigned char) *t); + return ((int)(unsigned int)(unsigned char)*s) - ((int)(unsigned int)(unsigned char)*t); } -unsigned int byte_rchr(char *s,register unsigned int n,int c) +unsigned int byte_rchr(char *s, register unsigned int n, int c) { register char ch; register char *t; @@ -66,34 +74,42 @@ unsigned int byte_rchr(char *s,register unsigned int n,int c) t = s; u = 0; for (;;) { + // clang-format off if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; + // clang-format on + } + if (!u) { + u = t; } - if (!u) { u = t; } return u - s; } -void byte_zero(char *s,register unsigned int n) +void byte_zero(char *s, register unsigned int n) { for (;;) { + // clang-format off if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; if (!n) { break; } *s++ = 0; --n; + // clang-format on } } -void byte_fill(char *s,register unsigned int n,const int c) +void byte_fill(char *s, register unsigned int n, const int c) { register char ch; - + ch = c; for (;;) { + // clang-format off if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; if (!n) { break; } *s++ = ch; --n; + // clang-format on } } @@ -1,4 +1,5 @@ #include "case.h" + #include "str.h" /** @@ -7,7 +8,7 @@ @brief string comparison and helper functions; case insensitive */ -int case_diffb(register char *s,unsigned int len,register char *t) +int case_diffb(register char *s, unsigned int len, register char *t) { register unsigned char x; register unsigned char y; @@ -15,32 +16,43 @@ int case_diffb(register char *s,unsigned int len,register char *t) while (len > 0) { --len; x = *s++ - 'A'; - if (x <= 'Z' - 'A') x += 'a'; else x += 'A'; + if (x <= 'Z' - 'A') + x += 'a'; + else + x += 'A'; y = *t++ - 'A'; - if (y <= 'Z' - 'A') y += 'a'; else y += 'A'; - if (x != y) - return ((int)(unsigned int) x) - ((int)(unsigned int) y); + if (y <= 'Z' - 'A') + y += 'a'; + else + y += 'A'; + if (x != y) return ((int)(unsigned int)x) - ((int)(unsigned int)y); } return 0; } -int case_diffs(register char *s,register char *t) +int case_diffs(register char *s, register char *t) { register unsigned char x; register unsigned char y; for (;;) { x = *s++ - 'A'; - if (x <= 'Z' - 'A') x += 'a'; else x += 'A'; + if (x <= 'Z' - 'A') + x += 'a'; + else + x += 'A'; y = *t++ - 'A'; - if (y <= 'Z' - 'A') y += 'a'; else y += 'A'; + if (y <= 'Z' - 'A') + y += 'a'; + else + y += 'A'; if (x != y) break; if (!x) break; } - return ((int)(unsigned int) x) - ((int)(unsigned int) y); + return ((int)(unsigned int)x) - ((int)(unsigned int)y); } -int case_diffrs(register char *s,register char *t) +int case_diffrs(register char *s, register char *t) { register unsigned char x = 0; register unsigned char y = 0; @@ -49,17 +61,23 @@ int case_diffrs(register char *s,register char *t) while (lens > 0 && lent > 0) { x = s[--lens] - 'A'; - if (x <= 'Z' - 'A') x += 'a'; else x += 'A'; + if (x <= 'Z' - 'A') + x += 'a'; + else + x += 'A'; y = t[--lent] - 'A'; - if (y <= 'Z' - 'A') y += 'a'; else y += 'A'; + if (y <= 'Z' - 'A') + y += 'a'; + else + y += 'A'; if (x != y) break; if (!x) break; if (!y) break; } - return ((int)(unsigned int) x) - ((int)(unsigned int) y); + return ((int)(unsigned int)x) - ((int)(unsigned int)y); } -void case_lowerb(char *s,unsigned int len) +void case_lowerb(char *s, unsigned int len) { unsigned char x; while (len > 0) { @@ -80,7 +98,7 @@ void case_lowers(char *s) } } -void case_upperb(char *s,unsigned int len) +void case_upperb(char *s, unsigned int len) { unsigned char x; while (len > 0) { @@ -101,33 +119,45 @@ void case_uppers(char *s) } } -int case_startb(register char *s,unsigned int len,register char *t) +int case_startb(register char *s, unsigned int len, register char *t) { register unsigned char x; register unsigned char y; for (;;) { y = *t++ - 'A'; - if (y <= 'Z' - 'A') y += 'a'; else y += 'A'; + if (y <= 'Z' - 'A') + y += 'a'; + else + y += 'A'; if (!y) return 1; if (!len) return 0; --len; x = *s++ - 'A'; - if (x <= 'Z' - 'A') x += 'a'; else x += 'A'; + if (x <= 'Z' - 'A') + x += 'a'; + else + x += 'A'; if (x != y) return 0; } } -int case_starts(register char *s,register char *t) +int case_starts(register char *s, register char *t) { register unsigned char x; register unsigned char y; for (;;) { x = *s++ - 'A'; - if (x <= 'Z' - 'A') x += 'a'; else x += 'A'; + if (x <= 'Z' - 'A') + x += 'a'; + else + x += 'A'; y = *t++ - 'A'; - if (y <= 'Z' - 'A') y += 'a'; else y += 'A'; + if (y <= 'Z' - 'A') + y += 'a'; + else + y += 'A'; if (!y) return 1; if (x != y) return 0; } diff --git a/src/cdbmake.c b/src/cdbmake.c index 28e170c..47bb420 100644 --- a/src/cdbmake.c +++ b/src/cdbmake.c @@ -1,9 +1,11 @@ +#include "cdbmake.h" + #include <unistd.h> -#include "seek.h" -#include "error.h" + #include "alloc.h" #include "cdbread.h" -#include "cdbmake.h" +#include "error.h" +#include "seek.h" /** @file cdbmake.c @@ -12,7 +14,7 @@ @brief constant data base (cdb) generation */ -int cdb_make_start(struct cdb_make *c,int fd) +int cdb_make_start(struct cdb_make *c, int fd) { c->head = 0; c->split = 0; @@ -20,25 +22,28 @@ int cdb_make_start(struct cdb_make *c,int fd) c->numentries = 0; c->fd = fd; c->pos = sizeof(c->final); - buffer_init(&c->b,write,fd,c->bspace,sizeof(c->bspace)); - return seek_set(fd,c->pos); + buffer_init(&c->b, write, fd, c->bspace, sizeof(c->bspace)); + return seek_set(fd, c->pos); } -static int posplus(struct cdb_make *c,uint32 len) +static int posplus(struct cdb_make *c, uint32 len) { uint32 newpos = c->pos + len; - if (newpos < len) { errno = ENOMEM; return -1; } + if (newpos < len) { + errno = ENOMEM; + return -1; + } c->pos = newpos; return 0; } -int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h) +int cdb_make_addend(struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h) { struct cdb_hplist *head; head = c->head; if (!head || (head->num >= CDB_HPLIST)) { - head = (struct cdb_hplist *) alloc(sizeof(struct cdb_hplist)); + head = (struct cdb_hplist *)alloc(sizeof(struct cdb_hplist)); if (!head) return -1; head->num = 0; head->next = c->head; @@ -48,31 +53,37 @@ int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen, head->hp[head->num].p = c->pos; ++head->num; ++c->numentries; - if (posplus(c,8) == -1) return -1; - if (posplus(c,keylen) == -1) return -1; - if (posplus(c,datalen) == -1) return -1; + if (posplus(c, 8) == -1) return -1; + if (posplus(c, keylen) == -1) return -1; + if (posplus(c, datalen) == -1) return -1; return 0; } -int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen) +int cdb_make_addbegin(struct cdb_make *c, unsigned int keylen, unsigned int datalen) { char buf[8]; - if (keylen > 0xffffffff) { errno = ENOMEM; return -1; } - if (datalen > 0xffffffff) { errno = ENOMEM; return -1; } + if (keylen > 0xffffffff) { + errno = ENOMEM; + return -1; + } + if (datalen > 0xffffffff) { + errno = ENOMEM; + return -1; + } - uint32_pack(buf,keylen); - uint32_pack(buf + 4,datalen); - if (buffer_putalign(&c->b,buf,8) == -1) return -1; + uint32_pack(buf, keylen); + uint32_pack(buf + 4, datalen); + if (buffer_putalign(&c->b, buf, 8) == -1) return -1; return 0; } -int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen) +int cdb_make_add(struct cdb_make *c, char *key, unsigned int keylen, char *data, unsigned int datalen) { - if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1; - if (buffer_putalign(&c->b,key,keylen) == -1) return -1; - if (buffer_putalign(&c->b,data,datalen) == -1) return -1; - return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen)); + if (cdb_make_addbegin(c, keylen, datalen) == -1) return -1; + if (buffer_putalign(&c->b, key, keylen) == -1) return -1; + if (buffer_putalign(&c->b, data, datalen) == -1) return -1; + return cdb_make_addend(c, keylen, datalen, cdb_hash(key, keylen)); } int cdb_make_finish(struct cdb_make *c) @@ -87,28 +98,28 @@ int cdb_make_finish(struct cdb_make *c) struct cdb_hplist *x; struct cdb_hp *hp; - for (i = 0; i < 256; ++i) - c->count[i] = 0; + for (i = 0; i < 256; ++i) c->count[i] = 0; for (x = c->head; x; x = x->next) { i = x->num; - while (i--) - ++c->count[255 & x->hp[i].h]; + while (i--) ++c->count[255 & x->hp[i].h]; } memsize = 1; for (i = 0; i < 256; ++i) { u = c->count[i] * 2; - if (u > memsize) - memsize = u; + if (u > memsize) memsize = u; } memsize += c->numentries; /* no overflow possible up to now */ - u = (uint32) 0 - (uint32) 1; + u = (uint32)0 - (uint32)1; u /= sizeof(struct cdb_hp); - if (memsize > u) { errno = ENOMEM; return -1; } + if (memsize > u) { + errno = ENOMEM; + return -1; + } - c->split = (struct cdb_hp *) alloc(memsize * sizeof(struct cdb_hp)); + c->split = (struct cdb_hp *)alloc(memsize * sizeof(struct cdb_hp)); if (!c->split) return -1; c->hash = c->split + c->numentries; @@ -121,38 +132,35 @@ int cdb_make_finish(struct cdb_make *c) for (x = c->head; x; x = x->next) { i = x->num; - while (i--) - c->split[--c->start[255 & x->hp[i].h]] = x->hp[i]; + while (i--) c->split[--c->start[255 & x->hp[i].h]] = x->hp[i]; } for (i = 0; i < 256; ++i) { count = c->count[i]; len = count + count; /* no overflow possible */ - uint32_pack(c->final + 8 * i,c->pos); - uint32_pack(c->final + 8 * i + 4,len); + uint32_pack(c->final + 8 * i, c->pos); + uint32_pack(c->final + 8 * i + 4, len); - for (u = 0; u < len; ++u) - c->hash[u].h = c->hash[u].p = 0; + for (u = 0; u < len; ++u) c->hash[u].h = c->hash[u].p = 0; hp = c->split + c->start[i]; for (u = 0; u < count; ++u) { where = (hp->h >> 8) % len; while (c->hash[where].p) - if (++where == len) - where = 0; + if (++where == len) where = 0; c->hash[where] = *hp++; } for (u = 0; u < len; ++u) { - uint32_pack(buf,c->hash[u].h); - uint32_pack(buf + 4,c->hash[u].p); - if (buffer_putalign(&c->b,buf,8) == -1) return -1; - if (posplus(c,8) == -1) return -1; + uint32_pack(buf, c->hash[u].h); + uint32_pack(buf + 4, c->hash[u].p); + if (buffer_putalign(&c->b, buf, 8) == -1) return -1; + if (posplus(c, 8) == -1) return -1; } } if (buffer_flush(&c->b) == -1) return -1; if (seek_begin(c->fd) == -1) return -1; - return buffer_putflush(&c->b,c->final,sizeof(c->final)); + return buffer_putflush(&c->b, c->final, sizeof(c->final)); } diff --git a/src/cdbread.c b/src/cdbread.c index c8ffa42..802c726 100644 --- a/src/cdbread.c +++ b/src/cdbread.c @@ -1,11 +1,13 @@ -#include <sys/types.h> -#include <sys/stat.h> +#include "cdbread.h" + #include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> + +#include "byte.h" #include "error.h" #include "seek.h" -#include "byte.h" -#include "cdbread.h" /** @file cdbread.c @@ -17,9 +19,12 @@ uint32 cdb_unpack(unsigned char *buf) { uint32 num; - num = buf[3]; num <<= 8; - num += buf[2]; num <<= 8; - num += buf[1]; num <<= 8; + num = buf[3]; + num <<= 8; + num += buf[2]; + num <<= 8; + num += buf[1]; + num <<= 8; num += buf[0]; return num; } @@ -27,7 +32,7 @@ uint32 cdb_unpack(unsigned char *buf) void cdb_free(struct cdb *c) { if (c->map) { - munmap(c->map,c->size); + munmap(c->map, c->size); c->map = 0; } } @@ -37,7 +42,7 @@ void cdb_findstart(struct cdb *c) c->loop = 0; } -void cdb_init(struct cdb *c,int fd) +void cdb_init(struct cdb *c, int fd) { struct stat st; char *x; @@ -46,9 +51,9 @@ void cdb_init(struct cdb *c,int fd) cdb_findstart(c); c->fd = fd; - if (fstat(fd,&st) == 0) + if (fstat(fd, &st) == 0) if (st.st_size <= 0xffffffff) { - x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0); + x = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (x + 1) { c->size = st.st_size; c->map = x; @@ -56,18 +61,16 @@ void cdb_init(struct cdb *c,int fd) } } -int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos) +int cdb_read(struct cdb *c, char *buf, unsigned int len, uint32 pos) { if (c->map) { if ((pos > c->size) || (c->size - pos < len)) goto FORMAT; - byte_copy(buf,len,c->map + pos); - } - else { - if (seek_set(c->fd,pos) == -1) return -1; + byte_copy(buf, len, c->map + pos); + } else { + if (seek_set(c->fd, pos) == -1) return -1; while (len > 0) { int r; - do - r = read(c->fd,buf,len); + do r = read(c->fd, buf, len); while ((r == -1) && (errno == EINTR)); if (r == -1) return -1; if (r == 0) goto FORMAT; @@ -77,12 +80,12 @@ int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos) } return 0; - FORMAT: +FORMAT: errno = EPROTO; return -1; } -static int match(struct cdb *c,char *key,unsigned int len,uint32 pos) +static int match(struct cdb *c, char *key, unsigned int len, uint32 pos) { char buf[32]; int n; @@ -90,8 +93,8 @@ static int match(struct cdb *c,char *key,unsigned int len,uint32 pos) while (len > 0) { n = sizeof(buf); if (n > len) n = len; - if (cdb_read(c,buf,n,pos) == -1) return -1; - if (byte_diff(buf,n,key)) return 0; + if (cdb_read(c, buf, n, pos) == -1) return -1; + if (byte_diff(buf, n, key)) return 0; pos += n; key += n; len -= n; @@ -99,18 +102,18 @@ static int match(struct cdb *c,char *key,unsigned int len,uint32 pos) return 1; } -int cdb_findnext(struct cdb *c,char *key,unsigned int len) +int cdb_findnext(struct cdb *c, char *key, unsigned int len) { char buf[8]; uint32 pos; uint32 u; if (!c->loop) { - u = cdb_hash(key,len); - if (cdb_read(c,buf,8,(u << 3) & 2047) == -1) return -1; - uint32_unpack(buf + 4,&c->hslots); + u = cdb_hash(key, len); + if (cdb_read(c, buf, 8, (u << 3) & 2047) == -1) return -1; + uint32_unpack(buf + 4, &c->hslots); if (!c->hslots) return 0; - uint32_unpack(buf,&c->hpos); + uint32_unpack(buf, &c->hpos); c->khash = u; u >>= 8; u %= c->hslots; @@ -119,50 +122,49 @@ int cdb_findnext(struct cdb *c,char *key,unsigned int len) } while (c->loop < c->hslots) { - if (cdb_read(c,buf,8,c->kpos) == -1) return -1; - uint32_unpack(buf + 4,&pos); + if (cdb_read(c, buf, 8, c->kpos) == -1) return -1; + uint32_unpack(buf + 4, &pos); if (!pos) return 0; c->loop += 1; c->kpos += 8; if (c->kpos == c->hpos + (c->hslots << 3)) c->kpos = c->hpos; - uint32_unpack(buf,&u); + uint32_unpack(buf, &u); if (u == c->khash) { - if (cdb_read(c,buf,8,pos) == -1) return -1; - uint32_unpack(buf,&u); - if (u == len) - switch(match(c,key,len,pos + 8)) { - case -1: - return -1; - case 1: - uint32_unpack(buf + 4,&c->dlen); - c->dpos = pos + 8 + len; - return 1; - } + if (cdb_read(c, buf, 8, pos) == -1) return -1; + uint32_unpack(buf, &u); + if (u == len) switch (match(c, key, len, pos + 8)) + { + case -1: return -1; + case 1: + uint32_unpack(buf + 4, &c->dlen); + c->dpos = pos + 8 + len; + return 1; + } } } return 0; } -int cdb_find(struct cdb *c,char *key,unsigned int len) +int cdb_find(struct cdb *c, char *key, unsigned int len) { cdb_findstart(c); - return cdb_findnext(c,key,len); + return cdb_findnext(c, key, len); } -uint32 cdb_hashadd(uint32 h,unsigned char c) +uint32 cdb_hashadd(uint32 h, unsigned char c) { h += (h << 5); return h ^ c; } -uint32 cdb_hash(char *buf,unsigned int len) +uint32 cdb_hash(char *buf, unsigned int len) { uint32 h; h = CDB_HASHSTART; while (len) { - h = cdb_hashadd(h,*buf++); + h = cdb_hashadd(h, *buf++); --len; } return h; diff --git a/src/constmap.c b/src/constmap.c index ecd5a92..360d6f5 100644 --- a/src/constmap.c +++ b/src/constmap.c @@ -1,8 +1,9 @@ #include "constmap.h" + #include "alloc.h" #include "case.h" -static constmap_hash hash(char *s,int len) +static constmap_hash hash(char *s, int len) { unsigned char ch; constmap_hash h; @@ -16,49 +17,48 @@ static constmap_hash hash(char *s,int len) return h; } -char *constmap(struct constmap *cm,char *s,int len) +char *constmap(struct constmap *cm, char *s, int len) { constmap_hash h; int pos; - h = hash(s,len); + h = hash(s, len); pos = cm->first[h & cm->mask]; while (pos != -1) { if (h == cm->hash[pos]) if (len == cm->inputlen[pos]) - if (!case_diffb(cm->input[pos],len,s)) - return cm->input[pos] + cm->inputlen[pos] + 1; + if (!case_diffb(cm->input[pos], len, s)) return cm->input[pos] + cm->inputlen[pos] + 1; pos = cm->next[pos]; } return 0; } -int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) +int constmap_init(struct constmap *cm, char *s, int len, int flagcolon) { int i; int j; int k; int pos; constmap_hash h; - + cm->num = 0; - for (j = 0; j < len; ++j) if (!s[j]) ++cm->num; - + for (j = 0; j < len; ++j) + if (!s[j]) ++cm->num; + h = 64; while (h && (h < cm->num)) h += h; cm->mask = h - 1; - - cm->first = (int *) alloc(sizeof(int) * h); + + cm->first = (int *)alloc(sizeof(int) * h); if (cm->first) { - cm->input = (char **) alloc(sizeof(char *) * cm->num); + cm->input = (char **)alloc(sizeof(char *) * cm->num); if (cm->input) { - cm->inputlen = (int *) alloc(sizeof(int) * cm->num); + cm->inputlen = (int *)alloc(sizeof(int) * cm->num); if (cm->inputlen) { - cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num); + cm->hash = (constmap_hash *)alloc(sizeof(constmap_hash) * cm->num); if (cm->hash) { - cm->next = (int *) alloc(sizeof(int) * cm->num); + cm->next = (int *)alloc(sizeof(int) * cm->num); if (cm->next) { - for (h = 0; h <= cm->mask; ++h) - cm->first[h] = -1; + for (h = 0; h <= cm->mask; ++h) cm->first[h] = -1; pos = 0; i = 0; for (j = 0; j < len; ++j) @@ -67,12 +67,15 @@ int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) if (flagcolon) { for (k = i; k < j; ++k) if (s[k] == ':') break; - if (k >= j) { i = j + 1; continue; } + if (k >= j) { + i = j + 1; + continue; + } k -= i; } cm->input[pos] = s + i; cm->inputlen[pos] = k; - h = hash(s + i,k); + h = hash(s + i, k); cm->hash[pos] = h; h &= cm->mask; cm->next[pos] = cm->first[h]; @@ -93,7 +96,7 @@ int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) return 0; } -int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char flagchar) +int constmap_init_char(struct constmap *cm, char *s, int len, int flagcolon, char flagchar) { int i; int j; @@ -106,24 +109,24 @@ int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char fl } cm->num = 0; - for (j = 0; j < len; ++j) if (!s[j]) ++cm->num; + for (j = 0; j < len; ++j) + if (!s[j]) ++cm->num; h = 64; while (h && (h < cm->num)) h += h; cm->mask = h - 1; - cm->first = (int *) alloc(sizeof(int) * h); + cm->first = (int *)alloc(sizeof(int) * h); if (cm->first) { - cm->input = (char **) alloc(sizeof(char *) * cm->num); + cm->input = (char **)alloc(sizeof(char *) * cm->num); if (cm->input) { - cm->inputlen = (int *) alloc(sizeof(int) * cm->num); + cm->inputlen = (int *)alloc(sizeof(int) * cm->num); if (cm->inputlen) { - cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num); + cm->hash = (constmap_hash *)alloc(sizeof(constmap_hash) * cm->num); if (cm->hash) { - cm->next = (int *) alloc(sizeof(int) * cm->num); + cm->next = (int *)alloc(sizeof(int) * cm->num); if (cm->next) { - for (h = 0; h <= cm->mask; ++h) - cm->first[h] = -1; + for (h = 0; h <= cm->mask; ++h) cm->first[h] = -1; pos = 0; i = 0; for (j = 0; j < len; ++j) { @@ -132,12 +135,15 @@ int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char fl if (flagcolon) { for (k = i; k < j; ++k) if (s[k] == flagchar) break; - if (k >= j) { i = j + 1; continue; } + if (k >= j) { + i = j + 1; + continue; + } k -= i; } cm->input[pos] = s + i; cm->inputlen[pos] = k; - h = hash(s + i,k); + h = hash(s + i, k); cm->hash[pos] = h; h &= cm->mask; cm->next[pos] = cm->first[h]; diff --git a/src/dnsstub/dns_cname.c b/src/dnsstub/dns_cname.c index 408949a..d1ea110 100644 --- a/src/dnsstub/dns_cname.c +++ b/src/dnsstub/dns_cname.c @@ -1,9 +1,9 @@ -#include "stralloc.h" -#include "uint_t.h" #include "byte.h" -#include "ip.h" #include "case.h" #include "dnsresolv.h" +#include "ip.h" +#include "stralloc.h" +#include "uint_t.h" /** @file dns_cname.c @@ -13,7 +13,7 @@ static char *q = 0; -int dns_cname_packet(stralloc *out,const char *buf,unsigned int len) +int dns_cname_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -21,21 +21,25 @@ int dns_cname_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_CNAME)) - if (byte_equal(header + 2,2,DNS_C_IN)) { - if (!dns_packet_getname(buf,len,pos,&q)) return DNS_ERR; - if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR; + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_CNAME)) + if (byte_equal(header + 2, 2, DNS_C_IN)) { + if (!dns_packet_getname(buf, len, pos, &q)) return DNS_ERR; + if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR; } pos += datalen; ++ranswers; @@ -44,16 +48,16 @@ int dns_cname_packet(stralloc *out,const char *buf,unsigned int len) return ranswers; } -int dns_cname(stralloc *out,stralloc *fqdn) +int dns_cname(stralloc *out, stralloc *fqdn) { - int rc; + int rc; - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; - if (dns_resolve(q,DNS_T_CNAME) < 0) return DNS_ERR; - if ((rc = dns_cname_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; + if (dns_resolve(q, DNS_T_CNAME) < 0) return DNS_ERR; + if ((rc = dns_cname_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); - if (case_equals(out->s,fqdn->s)) rc = -6; // loop DNS_HARD + if (case_equals(out->s, fqdn->s)) rc = -6; // loop DNS_HARD return rc; } diff --git a/src/dnsstub/dns_dfd.c b/src/dnsstub/dns_dfd.c index 756a1f8..645d068 100644 --- a/src/dnsstub/dns_dfd.c +++ b/src/dnsstub/dns_dfd.c @@ -1,7 +1,7 @@ -#include "error.h" #include "alloc.h" #include "byte.h" #include "dnsresolv.h" +#include "error.h" /** @file dns_dfd.c @@ -10,7 +10,7 @@ @brief domain name qualification (domain from dot) */ -int dns_domain_fromdot(char **out,const char *buf,unsigned int n) +int dns_domain_fromdot(char **out, const char *buf, unsigned int n) { char label[63]; unsigned int labellen = 0; /* <= sizeof label */ @@ -23,30 +23,34 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n) for (;;) { if (!n) break; - ch = *buf++; --n; + ch = *buf++; + --n; if (ch == '.') { if (labellen) { if (namelen + labellen + 1 > sizeof(name)) return 0; name[namelen++] = labellen; - byte_copy(name + namelen,labellen,label); + byte_copy(name + namelen, labellen, label); namelen += labellen; labellen = 0; } continue; } - if (ch == '\\') { // octal -> decimal + if (ch == '\\') { // octal -> decimal if (!n) break; - ch = *buf++; --n; + ch = *buf++; + --n; if ((ch >= '0') && (ch <= '7')) { ch -= '0'; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; - ++buf; --n; + ++buf; + --n; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; - ++buf; --n; + ++buf; + --n; } } } @@ -58,7 +62,7 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n) if (labellen) { if (namelen + labellen + 1 > sizeof(name)) return 0; name[namelen++] = labellen; - byte_copy(name + namelen,labellen,label); + byte_copy(name + namelen, labellen, label); namelen += labellen; labellen = 0; } @@ -68,7 +72,7 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n) x = alloc(namelen); if (!x) return DNS_MEM; - byte_copy(x,namelen,name); + byte_copy(x, namelen, name); if (*out) alloc_free(*out); *out = x; diff --git a/src/dnsstub/dns_domain.c b/src/dnsstub/dns_domain.c index 654a827..4ee7ba3 100644 --- a/src/dnsstub/dns_domain.c +++ b/src/dnsstub/dns_domain.c @@ -1,6 +1,6 @@ #include "alloc.h" -#include "case.h" #include "byte.h" +#include "case.h" #include "dnsresolv.h" /** @@ -16,8 +16,7 @@ unsigned int dns_domain_length(const char *dn) unsigned char c; x = dn; - while ((c = *x++)) - x += (unsigned int) c; + while ((c = *x++)) x += (unsigned int)c; return x - dn; } @@ -29,7 +28,7 @@ void dns_domain_free(char **out) } } -int dns_domain_copy(char **out,const char *in) +int dns_domain_copy(char **out, const char *in) { unsigned int len; char *x; @@ -37,42 +36,42 @@ int dns_domain_copy(char **out,const char *in) len = dns_domain_length(in); x = alloc(len); if (!x) return 0; - byte_copy(x,len,in); + byte_copy(x, len, in); if (*out) alloc_free(*out); *out = x; return 1; } -int dns_domain_equal(const char *dn1,const char *dn2) +int dns_domain_equal(const char *dn1, const char *dn2) { unsigned int len; len = dns_domain_length(dn1); if (len != dns_domain_length(dn2)) return 0; - if (case_diffb((char *)dn1,len,(char *)dn2)) return 0; /* safe since 63 < 'A' */ + if (case_diffb((char *)dn1, len, (char *)dn2)) return 0; /* safe since 63 < 'A' */ return 1; } -int dns_domain_suffix(const char *big,const char *little) +int dns_domain_suffix(const char *big, const char *little) { unsigned char c; for (;;) { - if (dns_domain_equal(big,little)) return 1; + if (dns_domain_equal(big, little)) return 1; c = *big++; if (!c) return 0; big += c; } } -unsigned int dns_domain_suffixpos(const char *big,const char *little) +unsigned int dns_domain_suffixpos(const char *big, const char *little) { const char *orig = big; unsigned char c; for (;;) { - if (dns_domain_equal(big,little)) return big - orig; + if (dns_domain_equal(big, little)) return big - orig; c = *big++; if (!c) return 0; big += c; diff --git a/src/dnsstub/dns_dtda.c b/src/dnsstub/dns_dtda.c index 38358a2..13e6051 100644 --- a/src/dnsstub/dns_dtda.c +++ b/src/dnsstub/dns_dtda.c @@ -1,5 +1,5 @@ -#include "stralloc.h" #include "dnsresolv.h" +#include "stralloc.h" /** @file dns_dtda.c @@ -8,36 +8,36 @@ @brief domain to dot append */ -int dns_domain_todot_cat(stralloc *out,const char *d) +int dns_domain_todot_cat(stralloc *out, const char *d) { char ch; char ch2; unsigned char ch3; char buf[4]; - if (!*d) - return stralloc_append(out,"."); + if (!*d) return stralloc_append(out, "."); for (;;) { ch = *d++; while (ch--) { ch2 = *d++; - if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32; // FQDN -> lowercase - if (((ch2 >= 'a') && (ch2 <= 'z')) || - ((ch2 >= '0') && (ch2 <= '9')) || - (ch2 == '-') || (ch2 == '_')) { - if (!stralloc_append(out,&ch2)) return DNS_MEM; - } - else { // decimal -> octal + if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32; // FQDN -> lowercase + if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') + || (ch2 == '_')) + { + if (!stralloc_append(out, &ch2)) return DNS_MEM; + } else { // decimal -> octal ch3 = ch2; - buf[3] = '0' + (ch3 & 7); ch3 >>= 3; - buf[2] = '0' + (ch3 & 7); ch3 >>= 3; + buf[3] = '0' + (ch3 & 7); + ch3 >>= 3; + buf[2] = '0' + (ch3 & 7); + ch3 >>= 3; buf[1] = '0' + (ch3 & 7); buf[0] = '\\'; - if (!stralloc_catb(out,buf,4)) return DNS_MEM; + if (!stralloc_catb(out, buf, 4)) return DNS_MEM; } } if (!*d) return 1; - if (!stralloc_append(out,".")) return DNS_MEM; + if (!stralloc_append(out, ".")) return DNS_MEM; } } diff --git a/src/dnsstub/dns_ip.c b/src/dnsstub/dns_ip.c index f89728c..0a63022 100644 --- a/src/dnsstub/dns_ip.c +++ b/src/dnsstub/dns_ip.c @@ -1,8 +1,8 @@ -#include "stralloc.h" -#include "uint_t.h" #include "byte.h" -#include "ip.h" #include "dnsresolv.h" +#include "ip.h" +#include "stralloc.h" +#include "uint_t.h" /** @file dns_ip.c @@ -13,7 +13,7 @@ static char *q = 0; -int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len) +int dns_ip4_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -21,53 +21,60 @@ int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_A)) - if (byte_equal(header + 2,2,DNS_C_IN)) + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_A)) + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 4) { - if (!dns_packet_copy(buf,len,pos,header,4)) return DNS_ERR; - if (!stralloc_catb(out,header,4)) return DNS_MEM; - } + if (!dns_packet_copy(buf, len, pos, header, 4)) return DNS_ERR; + if (!stralloc_catb(out, header, 4)) return DNS_MEM; + } pos += datalen; ++ranswers; } - dns_sortip4(out->s,out->len); + dns_sortip4(out->s, out->len); return ranswers; } -int dns_ip4(stralloc *out,stralloc *fqdn) +int dns_ip4(stralloc *out, stralloc *fqdn) { unsigned int i; char code = 0; - int dot = 0; - char ch; + int dot = 0; + char ch; char ip[4]; int r; int rc = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; - if (!stralloc_readyplus(fqdn,1)) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; + if (!stralloc_readyplus(fqdn, 1)) return DNS_MEM; - fqdn->s[fqdn->len] = 0; /* test FQDN string */ - for (i = 1; i < fqdn->len; i++) { - if (fqdn->s[i] >= '_') { code = 127; break; } - if (fqdn->s[i] == '.') dot++; - } + fqdn->s[fqdn->len] = 0; /* test FQDN string */ + for (i = 1; i < fqdn->len; i++) { + if (fqdn->s[i] >= '_') { + code = 127; + break; + } + if (fqdn->s[i] == '.') dot++; + } - if (code != 127 && dot == 3) /* if FQDN is just IPv4 */ - if (ip4_scan(fqdn->s,ip) || ip4_scanbracket(fqdn->s,ip)) { - if (!stralloc_copyb(out,ip,4)) return DNS_MEM; + if (code != 127 && dot == 3) /* if FQDN is just IPv4 */ + if (ip4_scan(fqdn->s, ip) || ip4_scanbracket(fqdn->s, ip)) { + if (!stralloc_copyb(out, ip, 4)) return DNS_MEM; return 1; } @@ -80,7 +87,7 @@ int dns_ip4(stralloc *out,stralloc *fqdn) if ((ch == '[') || (ch == ']')) continue; if (ch == '.') { - if (!stralloc_append(out,&code)) return DNS_MEM; + if (!stralloc_append(out, &code)) return DNS_MEM; code = 0; continue; } @@ -90,22 +97,22 @@ int dns_ip4(stralloc *out,stralloc *fqdn) continue; } - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fdqn -> A query -> response - if (dns_resolve(q,DNS_T_A) >= 0) { - if ((r = dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; // fdqn -> A query -> response + if (dns_resolve(q, DNS_T_A) >= 0) { + if ((r = dns_ip4_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); rc += r; } - return rc; + return rc; } out->len &= ~3; return 0; } -int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len) +int dns_ip6_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[16]; @@ -113,39 +120,43 @@ int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_AAAA)) { - if (byte_equal(header + 2,2,DNS_C_IN)) + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_AAAA)) { + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 16) { - if (!dns_packet_copy(buf,len,pos,header,16)) return DNS_ERR; - if (!stralloc_catb(out,header,16)) return DNS_MEM; + if (!dns_packet_copy(buf, len, pos, header, 16)) return DNS_ERR; + if (!stralloc_catb(out, header, 16)) return DNS_MEM; } - } else if (byte_equal(header,2,DNS_T_A)) - if (byte_equal(header + 2,2,DNS_C_IN)) + } else if (byte_equal(header, 2, DNS_T_A)) + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 4) { - byte_copy(header,12,V4mappedprefix); - if (!dns_packet_copy(buf,len,pos,header + 12,4)) return DNS_ERR; - if (!stralloc_catb(out,header,16)) return DNS_MEM; + byte_copy(header, 12, V4mappedprefix); + if (!dns_packet_copy(buf, len, pos, header + 12, 4)) return DNS_ERR; + if (!stralloc_catb(out, header, 16)) return DNS_MEM; } pos += datalen; ++ranswers; } - dns_sortip6(out->s,out->len); + dns_sortip6(out->s, out->len); return ranswers; } -int dns_ip6(stralloc *out,stralloc *fqdn) +int dns_ip6(stralloc *out, stralloc *fqdn) { unsigned int i; char code; @@ -154,12 +165,12 @@ int dns_ip6(stralloc *out,stralloc *fqdn) int r; int rc = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; - if (!stralloc_readyplus(fqdn,1)) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; + if (!stralloc_readyplus(fqdn, 1)) return DNS_MEM; - fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv6 */ - if (ip6_scan(fqdn->s,ip) || ip6_scanbracket(fqdn->s,ip)) { - if (!stralloc_copyb(out,ip,16)) return DNS_MEM; + fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv6 */ + if (ip6_scan(fqdn->s, ip) || ip6_scanbracket(fqdn->s, ip)) { + if (!stralloc_copyb(out, ip, 16)) return DNS_MEM; return 1; } @@ -172,7 +183,7 @@ int dns_ip6(stralloc *out,stralloc *fqdn) if ((ch == '[') || (ch == ']')) continue; if (ch == '.') { - if (!stralloc_append(out,&code)) return DNS_MEM; + if (!stralloc_append(out, &code)) return DNS_MEM; code = 0; continue; } @@ -182,9 +193,10 @@ int dns_ip6(stralloc *out,stralloc *fqdn) continue; } - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fqdn -> AAAA query -> response - if (dns_resolve(q,DNS_T_AAAA) >= 0) { - if ((r = dns_ip6_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) + return DNS_ERR; // fqdn -> AAAA query -> response + if (dns_resolve(q, DNS_T_AAAA) >= 0) { + if ((r = dns_ip6_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); rc += r; @@ -192,7 +204,7 @@ int dns_ip6(stralloc *out,stralloc *fqdn) return rc; } - + out->len &= ~3; return 0; } diff --git a/src/dnsstub/dns_ipq.c b/src/dnsstub/dns_ipq.c index 26c3818..d1a40e5 100644 --- a/src/dnsstub/dns_ipq.c +++ b/src/dnsstub/dns_ipq.c @@ -1,10 +1,10 @@ -#include "case.h" #include "byte.h" -#include "str.h" -#include "stralloc.h" +#include "case.h" #include "dnsresolv.h" -#include "socket_if.h" #include "ip.h" +#include "socket_if.h" +#include "str.h" +#include "stralloc.h" /** @file dns_ipq.c @@ -16,7 +16,7 @@ /** @fn int doit -> @return number of added chars to name */ -static int doit(stralloc *work,const char *rule) +static int doit(stralloc *work, const char *rule) { char ch; unsigned int colon; @@ -24,27 +24,27 @@ static int doit(stralloc *work,const char *rule) ch = *rule++; if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1; - colon = str_chr((char *)rule,':'); + colon = str_chr((char *)rule, ':'); if (!rule[colon]) return 1; if (work->len < colon) return 1; prefixlen = work->len - colon; if ((ch == '=') && prefixlen) return 1; - if (case_diffb((char *)rule,colon,work->s + prefixlen)) return 1; + if (case_diffb((char *)rule, colon, work->s + prefixlen)) return 1; if (ch == '?') { - if (byte_chr(work->s,prefixlen,'.') < prefixlen) return 1; - if (byte_chr(work->s,prefixlen,'[') < prefixlen) return 1; - if (byte_chr(work->s,prefixlen,']') < prefixlen) return 1; + if (byte_chr(work->s, prefixlen, '.') < prefixlen) return 1; + if (byte_chr(work->s, prefixlen, '[') < prefixlen) return 1; + if (byte_chr(work->s, prefixlen, ']') < prefixlen) return 1; } work->len = prefixlen; if (ch == '-') work->len = 0; - return stralloc_cats(work,rule + colon + 1); + return stralloc_cats(work, rule + colon + 1); } /** @fn int dns_ip4_qualify_rules -> @return number of IPv4 addresss with rules */ -int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules) +int dns_ip4_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules) { unsigned int i; unsigned int j; @@ -52,25 +52,24 @@ int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons unsigned int fqdnlen; int rc = 0; - if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM; + if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM; for (j = i = 0; j < rules->len; ++j) if (!rules->s[j]) { - if (!doit(fqdn,rules->s + i)) return DNS_INT; + if (!doit(fqdn, rules->s + i)) return DNS_INT; i = j + 1; } fqdnlen = fqdn->len; - plus = byte_chr(fqdn->s,fqdnlen,'+'); - if (plus >= fqdnlen) - return dns_ip4(ipout,fqdn); + plus = byte_chr(fqdn->s, fqdnlen, '+'); + if (plus >= fqdnlen) return dns_ip4(ipout, fqdn); i = plus + 1; for (;;) { - j = byte_chr(fqdn->s + i,fqdnlen - i,'+'); - byte_copy(fqdn->s + plus,j,fqdn->s + i); + j = byte_chr(fqdn->s + i, fqdnlen - i, '+'); + byte_copy(fqdn->s + plus, j, fqdn->s + i); fqdn->len = plus + j; - if (rc += dns_ip4(ipout,fqdn) < 0) return DNS_ERR; + if (rc += dns_ip4(ipout, fqdn) < 0) return DNS_ERR; i += j; if (i >= fqdnlen) return rc; ++i; @@ -80,45 +79,44 @@ int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons /** @fn int dns_ip4_qualify -> @return number of IPv4 addresss qualified */ -int dns_ip4_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in) +int dns_ip4_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in) { int r; static stralloc rules; - if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r; + if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r; if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT; - return dns_ip4_qualify_rules(ipout,fqdn,in,&rules); + return dns_ip4_qualify_rules(ipout, fqdn, in, &rules); } /** @fn int dns_ip4_qualify_rules -> @return number of IPv6 addresss with rules */ -int dns_ip6_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules) +int dns_ip6_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules) { unsigned int i; unsigned int j; unsigned int plus; unsigned int fqdnlen; - int rc = 0; + int rc = 0; - if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM; + if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM; for (j = i = 0; j < rules->len; ++j) if (!rules->s[j]) { - if (!doit(fqdn,rules->s + i)) return DNS_INT; + if (!doit(fqdn, rules->s + i)) return DNS_INT; i = j + 1; } fqdnlen = fqdn->len; - plus = byte_chr(fqdn->s,fqdnlen,'+'); - if (plus >= fqdnlen) - return dns_ip6(ipout,fqdn); + plus = byte_chr(fqdn->s, fqdnlen, '+'); + if (plus >= fqdnlen) return dns_ip6(ipout, fqdn); i = plus + 1; for (;;) { - j = byte_chr(fqdn->s + i,fqdnlen - i,'+'); - byte_copy(fqdn->s + plus,j,fqdn->s + i); + j = byte_chr(fqdn->s + i, fqdnlen - i, '+'); + byte_copy(fqdn->s + plus, j, fqdn->s + i); fqdn->len = plus + j; - if ((rc += dns_ip6(ipout,fqdn)) < 0) return DNS_ERR; + if ((rc += dns_ip6(ipout, fqdn)) < 0) return DNS_ERR; i += j; if (i >= fqdnlen) return rc; ++i; @@ -128,19 +126,19 @@ int dns_ip6_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons /** @fn int dns_ip6_qualify -> @return number of IPv6 addresss qualified */ -int dns_ip6_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in) +int dns_ip6_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in) { - int r; + int r; static stralloc rules; - - if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0) return r; + + if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r; if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT; - return dns_ip6_qualify_rules(ipout,fqdn,in,&rules); + return dns_ip6_qualify_rules(ipout, fqdn, in, &rules); } /** @fn int dns_ip_qualify_rules -> @return number of IPv6+IPv4 addresss with rules */ -int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules) +int dns_ip_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules) { unsigned int i; unsigned int j; @@ -148,25 +146,25 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const unsigned int plus; unsigned int fqdnlen; stralloc tmp = {0}; - int rc = 0; + int rc = 0; - if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM; - if (!stralloc_copys(ipout,"")) return DNS_MEM; + if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM; + if (!stralloc_copys(ipout, "")) return DNS_MEM; for (j = i = 0; j < rules->len; ++j) if (!rules->s[j]) { - if (!doit(fqdn,rules->s + i)) return DNS_INT; + if (!doit(fqdn, rules->s + i)) return DNS_INT; i = j + 1; } fqdnlen = fqdn->len; - plus = byte_chr(fqdn->s,fqdnlen,'+'); + plus = byte_chr(fqdn->s, fqdnlen, '+'); if (plus >= fqdnlen) { - rc = dns_ip6(ipout,fqdn); - if (dns_ip4(&tmp,fqdn) > 0) { + rc = dns_ip6(ipout, fqdn); + if (dns_ip4(&tmp, fqdn) > 0) { for (k = 0; k < tmp.len; k += 4) { - if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM; - if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM; + if (!stralloc_catb(ipout, (const char *)V4mappedprefix, 12)) return DNS_MEM; + if (!stralloc_catb(ipout, tmp.s + k, 4)) return DNS_MEM; rc++; } } @@ -175,20 +173,21 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const i = plus + 1; for (;;) { - j = byte_chr(fqdn->s + i,fqdnlen - i,'+'); - byte_copy(fqdn->s + plus,j,fqdn->s + i); + j = byte_chr(fqdn->s + i, fqdnlen - i, '+'); + byte_copy(fqdn->s + plus, j, fqdn->s + i); fqdn->len = plus + j; - if (!stralloc_copys(ipout,"")) return DNS_MEM; - rc = dns_ip6(&tmp,fqdn); - if (rc) if (!stralloc_cat(ipout,&tmp)) return DNS_MEM; - if (dns_ip4(&tmp,fqdn) > 0) { + if (!stralloc_copys(ipout, "")) return DNS_MEM; + rc = dns_ip6(&tmp, fqdn); + if (rc) + if (!stralloc_cat(ipout, &tmp)) return DNS_MEM; + if (dns_ip4(&tmp, fqdn) > 0) { for (k = 0; k < tmp.len; k += 4) { - if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM; - if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM; + if (!stralloc_catb(ipout, (const char *)V4mappedprefix, 12)) return DNS_MEM; + if (!stralloc_catb(ipout, tmp.s + k, 4)) return DNS_MEM; rc++; } } - + if (rc < 0) return DNS_ERR; i += j; if (i >= fqdnlen) return rc; @@ -199,38 +198,38 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const /** @fn int dns_ip_qualify_localhost -> @return number of IP addresss */ -int dns_ip_qualify_localhost(stralloc *ipout,stralloc *fqdn,const stralloc *in) +int dns_ip_qualify_localhost(stralloc *ipout, stralloc *fqdn, const stralloc *in) { - if (!stralloc_copys(ipout,"")) return DNS_MEM; - if (!stralloc_copys(fqdn,"")) return DNS_MEM; + if (!stralloc_copys(ipout, "")) return DNS_MEM; + if (!stralloc_copys(fqdn, "")) return DNS_MEM; ipout->len = 0; - if (byte_equal(in->s,9,LOCALHOST)) { - if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM; - if (!stralloc_catb(ipout,(const char *) V46loopback,16)) return DNS_MEM; - if (!stralloc_copys(fqdn,"localhost.localhost.")) return DNS_MEM; + if (byte_equal(in->s, 9, LOCALHOST)) { + if (!stralloc_copyb(ipout, (const char *)V6loopback, 16)) return DNS_MEM; + if (!stralloc_catb(ipout, (const char *)V46loopback, 16)) return DNS_MEM; + if (!stralloc_copys(fqdn, "localhost.localhost.")) return DNS_MEM; } - if (byte_equal(in->s,13,IP4_LOOPBACK)) { - if (!stralloc_copyb(ipout,(const char *) V46loopback,16)) return DNS_MEM; - if (!stralloc_copys(fqdn,"ip4-loopback.localhost.")) return DNS_MEM; + if (byte_equal(in->s, 13, IP4_LOOPBACK)) { + if (!stralloc_copyb(ipout, (const char *)V46loopback, 16)) return DNS_MEM; + if (!stralloc_copys(fqdn, "ip4-loopback.localhost.")) return DNS_MEM; } - if (byte_equal(in->s,13,IP6_LOOPBACK)) { - if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM; - if (!stralloc_copys(fqdn,"ip6-loopback.localhost.")) return DNS_MEM; + if (byte_equal(in->s, 13, IP6_LOOPBACK)) { + if (!stralloc_copyb(ipout, (const char *)V6loopback, 16)) return DNS_MEM; + if (!stralloc_copys(fqdn, "ip6-loopback.localhost.")) return DNS_MEM; } -// if (!stralloc_0(fqdn)) return DNS_MEM; // don't do it + // if (!stralloc_0(fqdn)) return DNS_MEM; // don't do it return ipout->len ? ipout->len % 15 : 0; } /** @fn int dns_ip_qualify -> @return number of IP addresss */ -int dns_ip_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in) +int dns_ip_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in) { int r; static stralloc rules; - if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r; + if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r; if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT; - return dns_ip_qualify_rules(ipout,fqdn,in,&rules); + return dns_ip_qualify_rules(ipout, fqdn, in, &rules); } diff --git a/src/dnsstub/dns_mx.c b/src/dnsstub/dns_mx.c index c0845ef..3a140d2 100644 --- a/src/dnsstub/dns_mx.c +++ b/src/dnsstub/dns_mx.c @@ -1,7 +1,7 @@ -#include "stralloc.h" #include "byte.h" -#include "uint_t.h" #include "dnsresolv.h" +#include "stralloc.h" +#include "uint_t.h" /** @file dns_mx.c @@ -13,7 +13,7 @@ static char *q = 0; -int dns_mx_packet(stralloc *out,const char *buf,unsigned int len) +int dns_mx_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -22,23 +22,27 @@ int dns_mx_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_MX)) - if (byte_equal(header + 2,2,DNS_C_IN)) { - if (!dns_packet_copy(buf,len,pos,pref,2)) return DNS_ERR; - if (!dns_packet_getname(buf,len,pos + 2,&q)) return DNS_ERR; - if (!stralloc_catb(out,pref,2)) return DNS_MEM; - if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR; + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_MX)) + if (byte_equal(header + 2, 2, DNS_C_IN)) { + if (!dns_packet_copy(buf, len, pos, pref, 2)) return DNS_ERR; + if (!dns_packet_getname(buf, len, pos + 2, &q)) return DNS_ERR; + if (!stralloc_catb(out, pref, 2)) return DNS_MEM; + if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR; if (!stralloc_0(out)) return DNS_MEM; } pos += datalen; @@ -48,16 +52,16 @@ int dns_mx_packet(stralloc *out,const char *buf,unsigned int len) return ranswers; } -int dns_mx(stralloc *out,const stralloc *fqdn) +int dns_mx(stralloc *out, const stralloc *fqdn) { int rc = 0; - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; - if (dns_resolve(q,DNS_T_MX) >= 0) { - if ((rc = dns_mx_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; + if (dns_resolve(q, DNS_T_MX) >= 0) { + if ((rc = dns_mx_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); - dns_domain_free(&q); + dns_domain_free(&q); } return rc; -} +} diff --git a/src/dnsstub/dns_name.c b/src/dnsstub/dns_name.c index 0723a8f..db4565a 100644 --- a/src/dnsstub/dns_name.c +++ b/src/dnsstub/dns_name.c @@ -1,8 +1,8 @@ -#include "stralloc.h" -#include "uint_t.h" #include "byte.h" -#include "ip.h" #include "dnsresolv.h" +#include "ip.h" +#include "stralloc.h" +#include "uint_t.h" /** @file dns_name.c @@ -13,28 +13,32 @@ static char *q = 0; -int dns_name_packet(stralloc *out,const char *buf,unsigned int len) +int dns_name_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; uint16 numanswers; uint16 datalen; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_PTR)) - if (byte_equal(header + 2,2,DNS_C_IN)) { - if (!dns_packet_getname(buf,len,pos,&q)) return DNS_ERR; - if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR; + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_PTR)) + if (byte_equal(header + 2, 2, DNS_C_IN)) { + if (!dns_packet_getname(buf, len, pos, &q)) return DNS_ERR; + if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR; return 1; } pos += datalen; @@ -43,38 +47,38 @@ int dns_name_packet(stralloc *out,const char *buf,unsigned int len) return 0; } -int dns_name4(stralloc *out,const char ip[4]) +int dns_name4(stralloc *out, const char ip[4]) { int rc; char name[DNS_NAME4_DOMAIN]; - dns_name4_domain(name,ip); - if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR; - if ((rc = dns_name_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + dns_name4_domain(name, ip); + if (dns_resolve(name, DNS_T_PTR) < 0) return DNS_ERR; + if ((rc = dns_name_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); return rc; } -int dns_name6(stralloc *out,const char ip[16]) +int dns_name6(stralloc *out, const char ip[16]) { int rc; char name[DNS_NAME6_DOMAIN]; - dns_name6_domain(name,ip); - if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR; - if ((rc = dns_name_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + dns_name6_domain(name, ip); + if (dns_resolve(name, DNS_T_PTR) < 0) return DNS_ERR; + if ((rc = dns_name_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); return rc; } -int dns_name(stralloc *out,const char ip[16]) +int dns_name(stralloc *out, const char ip[16]) { if (ip6_isv4mapped(ip)) - return dns_name4(out,ip+12); + return dns_name4(out, ip + 12); else - return dns_name6(out,ip); + return dns_name6(out, ip); } diff --git a/src/dnsstub/dns_nd.c b/src/dnsstub/dns_nd.c index 6ce8ed9..3bbbeca 100644 --- a/src/dnsstub/dns_nd.c +++ b/src/dnsstub/dns_nd.c @@ -1,7 +1,7 @@ #include "byte.h" +#include "dnsresolv.h" #include "fmt.h" #include "ip.h" -#include "dnsresolv.h" /** @file dns_nd.c @@ -10,29 +10,29 @@ @brief DNS domain name for ip (wire format) */ -int dns_name4_domain(char name[DNS_NAME4_DOMAIN],const char ip[4]) +int dns_name4_domain(char name[DNS_NAME4_DOMAIN], const char ip[4]) { unsigned int namelen; unsigned int i; namelen = 0; - i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[3]); + i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[3]); name[namelen++] = i; namelen += i; - i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[2]); + i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[2]); name[namelen++] = i; namelen += i; - i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[1]); + i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[1]); name[namelen++] = i; namelen += i; - i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[0]); + i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[0]); name[namelen++] = i; namelen += i; - byte_copy(name + namelen,14,"\7in-addr\4arpa\0"); - return namelen+14; + byte_copy(name + namelen, 14, "\7in-addr\4arpa\0"); + return namelen + 14; } -int dns_name6_domain(char name[DNS_NAME6_DOMAIN],const char ip[16]) +int dns_name6_domain(char name[DNS_NAME6_DOMAIN], const char ip[16]) { unsigned int j; @@ -42,7 +42,6 @@ int dns_name6_domain(char name[DNS_NAME6_DOMAIN],const char ip[16]) name[j * 4 + 2] = 1; name[j * 4 + 3] = tohex((unsigned char)ip[15 - j] >> 4); } - byte_copy(name + 4 * 16,10,"\3ip6\4arpa\0"); + byte_copy(name + 4 * 16, 10, "\3ip6\4arpa\0"); return 4 * 16 + 10; } - diff --git a/src/dnsstub/dns_packet.c b/src/dnsstub/dns_packet.c index ce322ea..2b2040f 100644 --- a/src/dnsstub/dns_packet.c +++ b/src/dnsstub/dns_packet.c @@ -1,5 +1,5 @@ -#include "error.h" #include "dnsresolv.h" +#include "error.h" /** @file dns_packet.c @@ -9,17 +9,22 @@ @brief DNS should have used LZ77 instead of its own sophomoric compression algorithm. */ -unsigned int dns_packet_copy(const char *buf,unsigned int len,unsigned int pos,char *out,unsigned int outlen) +unsigned int dns_packet_copy( + const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen) { while (outlen) { - if (pos >= len) { errno = EPROTO; return 0; } + if (pos >= len) { + errno = EPROTO; + return 0; + } *out = buf[pos++]; - ++out; --outlen; + ++out; + --outlen; } return pos; } -unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int pos) +unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos) { unsigned char ch; @@ -36,7 +41,7 @@ unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int p return 0; } -unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int pos,char **d) +unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d) { unsigned int loop = 0; unsigned int state = 0; @@ -47,39 +52,41 @@ unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int po unsigned int namelen = 0; for (;;) { - if (pos >= len) goto PROTO; + if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; if (state) { - if (namelen + 1 > sizeof(name)) goto PROTO; + if (namelen + 1 > sizeof(name)) goto PROTO; name[namelen++] = ch; --state; } else { while (ch >= 192) { - where = ch; where -= 192; where <<= 8; - if (pos >= len) goto PROTO; + where = ch; + where -= 192; + where <<= 8; + if (pos >= len) goto PROTO; ch = buf[pos++]; if (!firstcompress) firstcompress = pos; pos = where + ch; - if (pos >= len) goto PROTO; + if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; } if (ch >= 64) goto PROTO; - if (namelen + 1 > sizeof(name)) goto PROTO; + if (namelen + 1 > sizeof(name)) goto PROTO; name[namelen++] = ch; if (!ch) break; state = ch; } } - if (!dns_domain_copy(d,name)) return 0; + if (!dns_domain_copy(d, name)) return 0; if (firstcompress) return firstcompress; return pos; - PROTO: +PROTO: errno = EPROTO; return 0; } diff --git a/src/dnsstub/dns_random.c b/src/dnsstub/dns_random.c index 200cd6c..8b89244 100644 --- a/src/dnsstub/dns_random.c +++ b/src/dnsstub/dns_random.c @@ -1,7 +1,8 @@ #include <unistd.h> + +#include "dnsresolv.h" #include "taia.h" #include "uint_t.h" -#include "dnsresolv.h" /** @file dns_random.c @@ -15,13 +16,17 @@ static uint32 in[12]; static uint32 out[8]; static int outleft = 0; -#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b)))) -#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b)); +#define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b)))) +#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b)) static void surf(void) { - uint32 t[12]; uint32 x; uint32 sum = 0; - int r; int i; int loop; + uint32 t[12]; + uint32 x; + uint32 sum = 0; + int r; + int i; + int loop; for (i = 0; i < 12; ++i) t[i] = in[i] ^ seed[12 + i]; for (i = 0; i < 8; ++i) out[i] = seed[24 + i]; @@ -29,9 +34,21 @@ static void surf(void) for (loop = 0; loop < 2; ++loop) { for (r = 0; r < 16; ++r) { sum += 0x9e3779b9; - MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13) - MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13) - MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13) + + MUSH(0, 5); + MUSH(1, 7); + MUSH(2, 9); + MUSH(3, 13); + + MUSH(4, 5); + MUSH(5, 7); + MUSH(6, 9); + MUSH(7, 13); + + MUSH(8, 5); + MUSH(9, 7); + MUSH(10, 9); + MUSH(11, 13); } for (i = 0; i < 8; ++i) out[i] ^= t[i + 4]; } @@ -43,13 +60,11 @@ void dns_random_init(const char data[128]) struct taia t; char tpack[16]; - for (i = 0; i < 32; ++i) - uint32_unpack((char *)data + 4 * i,seed + i); + for (i = 0; i < 32; ++i) uint32_unpack((char *)data + 4 * i, seed + i); taia_now(&t); - taia_pack(tpack,&t); - for (i = 0; i < 4; ++i) - uint32_unpack(tpack + 4 * i,in + 4 + i); + taia_pack(tpack, &t); + for (i = 0; i < 4; ++i) uint32_unpack(tpack + 4 * i, in + 4 + i); in[8] = getpid(); in[9] = getppid(); @@ -61,7 +76,9 @@ unsigned int dns_random(unsigned int n) if (!n) return 0; if (!outleft) { - if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3]; + if (!++in[0]) + if (!++in[1]) + if (!++in[2]) ++in[3]; surf(); outleft = 8; } diff --git a/src/dnsstub/dns_rcip.c b/src/dnsstub/dns_rcip.c index 93b0daa..fd86df9 100644 --- a/src/dnsstub/dns_rcip.c +++ b/src/dnsstub/dns_rcip.c @@ -1,10 +1,10 @@ -#include "taia.h" -#include "readclose.h" #include "byte.h" -#include "ip.h" -#include "env.h" #include "dnsresolv.h" +#include "env.h" +#include "ip.h" +#include "readclose.h" #include "socket_if.h" +#include "taia.h" /** @file dns_rcip.c @@ -16,7 +16,7 @@ static stralloc data = {0}; static stralloc ifname = {0}; -static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS]) +static int init(char ip[QUERY_MAXIPLEN], uint32 sid[QUERY_MAXNS]) { int i; int j; @@ -25,7 +25,7 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS]) char *x; char ip4[4]; -/* Read (compactified) IPv4|v6 addresses of resolvers + /* Read (compactified) IPv4|v6 addresses of resolvers Store them in array IP with fixed length : ip(64) -> 16 IPv4 addresses (not used anymore) ip(512) -> 16*2 IPv6 addresses (we use IPv4 mapped IPv6 addresses) @@ -36,39 +36,40 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS]) x = env_get("DNSCACHEIP"); if (x) while (iplen <= 240 && *x != '\0') { - if (*x == ' ') - ++x; - else - if ((i = ip6_ifscan(x,ip + iplen,&ifname))) { - if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s); - iplen += 16; k++; - if (*(x += i) == '\0') break; - } + if (*x == ' ') + ++x; + else if ((i = ip6_ifscan(x, ip + iplen, &ifname))) { + if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s); + iplen += 16; + k++; + if (*(x += i) == '\0') break; + } } if (!iplen) { - i = openreadclose("/etc/resolv.conf",&data,64); + i = openreadclose("/etc/resolv.conf", &data, 64); if (i == -1) return DNS_INT; if (i) { - if (!stralloc_append(&data,"\n")) return DNS_MEM; + if (!stralloc_append(&data, "\n")) return DNS_MEM; i = 0; for (j = 0; j < data.len; ++j) if (data.s[j] == '\n') { - if (byte_equal("nameserver ",11,data.s + i) || byte_equal("nameserver\t",11,data.s + i)) { - i += 10; - while ((data.s[i] == ' ') || (data.s[i] == '\t')) - i++; + if (byte_equal("nameserver ", 11, data.s + i) || byte_equal("nameserver\t", 11, data.s + i)) { + i += 10; + while ((data.s[i] == ' ') || (data.s[i] == '\t')) i++; if (iplen <= 240) { data.s[j] = '\0'; /* ip6_ifscan needs terminated string on input */ - if (ip4_scan(data.s + i,ip4)) { - if (byte_equal(ip4,4,"\0\0\0\0")) - byte_copy(ip4,4,"\177\0\0\1"); - byte_copy(ip + iplen,12,V4mappedprefix); - byte_copy(ip + iplen + 12,4,ip4); - sid[k] = 0; iplen += 16; k++; - } else if (ip6_ifscan(data.s + i,ip + iplen,&ifname)) { + if (ip4_scan(data.s + i, ip4)) { + if (byte_equal(ip4, 4, "\0\0\0\0")) byte_copy(ip4, 4, "\177\0\0\1"); + byte_copy(ip + iplen, 12, V4mappedprefix); + byte_copy(ip + iplen + 12, 4, ip4); + sid[k] = 0; + iplen += 16; + k++; + } else if (ip6_ifscan(data.s + i, ip + iplen, &ifname)) { if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s); - iplen += 16; k++; + iplen += 16; + k++; } } } @@ -78,10 +79,10 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS]) } if (!iplen) { - byte_copy(ip,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"); + byte_copy(ip, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"); iplen = 16; } - byte_zero(ip + iplen,QUERY_MAXIPLEN - iplen); + byte_zero(ip + iplen, QUERY_MAXIPLEN - iplen); return 0; } @@ -89,26 +90,26 @@ static int ok = 0; static unsigned int uses; static struct taia deadline; static char ip[QUERY_MAXIPLEN]; /* defined if ok */ -static uint32 scopes[QUERY_MAXNS]; +static uint32 scopes[QUERY_MAXNS]; -int dns_resolvconfip(char s[QUERY_MAXIPLEN],uint32 scope[QUERY_MAXNS]) +int dns_resolvconfip(char s[QUERY_MAXIPLEN], uint32 scope[QUERY_MAXNS]) { struct taia now; taia_now(&now); - if (taia_less(&deadline,&now)) ok = 0; + if (taia_less(&deadline, &now)) ok = 0; if (!uses) ok = 0; if (!ok) { - if (init(ip,scopes) < 0) return DNS_INT; - taia_uint(&deadline,600); - taia_add(&deadline,&now,&deadline); + if (init(ip, scopes) < 0) return DNS_INT; + taia_uint(&deadline, 600); + taia_add(&deadline, &now, &deadline); uses = 10000; ok = 1; } --uses; - byte_copy(s,QUERY_MAXIPLEN,ip); - byte_copy(scope,128,scopes); + byte_copy(s, QUERY_MAXIPLEN, ip); + byte_copy(scope, 128, scopes); return 0; } diff --git a/src/dnsstub/dns_rcrw.c b/src/dnsstub/dns_rcrw.c index 4633fed..8c15591 100644 --- a/src/dnsstub/dns_rcrw.c +++ b/src/dnsstub/dns_rcrw.c @@ -1,10 +1,11 @@ #include <unistd.h> -#include "taia.h" -#include "env.h" + #include "byte.h" -#include "str.h" -#include "readclose.h" #include "dnsresolv.h" +#include "env.h" +#include "readclose.h" +#include "str.h" +#include "taia.h" /** @file dns_rcrw.c @@ -23,25 +24,24 @@ static int init(stralloc *rules) int j; int k; - if (!stralloc_copys(rules,"")) return DNS_MEM; + if (!stralloc_copys(rules, "")) return DNS_MEM; x = env_get("DNSREWRITEFILE"); if (!x) x = "/etc/dnsrewrite"; - i = openreadclose(x,&data,64); + i = openreadclose(x, &data, 64); if (i == -1) return DNS_INT; if (i) { - if (!stralloc_append(&data,"\n")) return DNS_MEM; + if (!stralloc_append(&data, "\n")) return DNS_MEM; i = 0; for (j = 0; j < data.len; ++j) if (data.s[j] == '\n') { - if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM; + if (!stralloc_catb(rules, data.s + i, j - i)) return DNS_MEM; while (rules->len) { if (rules->s[rules->len - 1] != ' ') - if (rules->s[rules->len - 1] != '\t') - if (rules->s[rules->len - 1] != '\r') - break; + if (rules->s[rules->len - 1] != '\t') + if (rules->s[rules->len - 1] != '\r') break; --rules->len; } if (!stralloc_0(rules)) return DNS_MEM; @@ -52,46 +52,48 @@ static int init(stralloc *rules) x = env_get("LOCALDOMAIN"); if (x) { - if (!stralloc_copys(&data,x)) return DNS_MEM; - if (!stralloc_append(&data," ")) return DNS_MEM; - if (!stralloc_copys(rules,"?:")) return DNS_MEM; + if (!stralloc_copys(&data, x)) return DNS_MEM; + if (!stralloc_append(&data, " ")) return DNS_MEM; + if (!stralloc_copys(rules, "?:")) return DNS_MEM; i = 0; for (j = 0; j < data.len; ++j) if (data.s[j] == ' ') { - if (!stralloc_cats(rules,"+.")) return DNS_MEM; - if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM; + if (!stralloc_cats(rules, "+.")) return DNS_MEM; + if (!stralloc_catb(rules, data.s + i, j - i)) return DNS_MEM; i = j + 1; } if (!stralloc_0(rules)) return DNS_MEM; - if (!stralloc_cats(rules,"*.:")) return DNS_MEM; + if (!stralloc_cats(rules, "*.:")) return DNS_MEM; if (!stralloc_0(rules)) return DNS_MEM; return 0; } - i = openreadclose("/etc/resolv.conf",&data,64); + i = openreadclose("/etc/resolv.conf", &data, 64); if (i == -1) return DNS_INT; if (i) { - if (!stralloc_append(&data,"\n")) return DNS_MEM; + if (!stralloc_append(&data, "\n")) return DNS_MEM; i = 0; for (j = 0; j < data.len; ++j) if (data.s[j] == '\n') { - if (byte_equal("search ",7,data.s + i) || - byte_equal("search\t",7,data.s + i) || - byte_equal("domain ",7,data.s + i) || - byte_equal("domain\t",7,data.s + i)) { - if (!stralloc_copys(rules,"?:")) return DNS_MEM; + if (byte_equal("search ", 7, data.s + i) || byte_equal("search\t", 7, data.s + i) + || byte_equal("domain ", 7, data.s + i) || byte_equal("domain\t", 7, data.s + i)) + { + if (!stralloc_copys(rules, "?:")) return DNS_MEM; i += 7; while (i < j) { - k = byte_chr(data.s + i,j - i,' '); - k = byte_chr(data.s + i,k,'\t'); - if (!k) { ++i; continue; } - if (!stralloc_cats(rules,"+.")) return DNS_MEM; - if (!stralloc_catb(rules,data.s + i,k)) return DNS_MEM; + k = byte_chr(data.s + i, j - i, ' '); + k = byte_chr(data.s + i, k, '\t'); + if (!k) { + ++i; + continue; + } + if (!stralloc_cats(rules, "+.")) return DNS_MEM; + if (!stralloc_catb(rules, data.s + i, k)) return DNS_MEM; i += k; } if (!stralloc_0(rules)) return DNS_MEM; - if (!stralloc_cats(rules,"*.:")) return DNS_MEM; + if (!stralloc_cats(rules, "*.:")) return DNS_MEM; if (!stralloc_0(rules)) return DNS_MEM; return 0; } @@ -100,15 +102,15 @@ static int init(stralloc *rules) } host[0] = 0; - if (gethostname(host,sizeof(host)) == -1) return DNS_ERR; + if (gethostname(host, sizeof(host)) == -1) return DNS_ERR; host[(sizeof(host)) - 1] = 0; - i = str_chr(host,'.'); + i = str_chr(host, '.'); if (host[i]) { - if (!stralloc_copys(rules,"?:")) return DNS_MEM; - if (!stralloc_cats(rules,host + i)) return DNS_MEM; + if (!stralloc_copys(rules, "?:")) return DNS_MEM; + if (!stralloc_cats(rules, host + i)) return DNS_MEM; if (!stralloc_0(rules)) return DNS_MEM; } - if (!stralloc_cats(rules,"*.:")) return DNS_MEM; + if (!stralloc_cats(rules, "*.:")) return DNS_MEM; if (!stralloc_0(rules)) return DNS_MEM; return 0; @@ -124,18 +126,18 @@ int dns_resolvconfrewrite(stralloc *out) struct taia now; taia_now(&now); - if (taia_less(&deadline,&now)) ok = 0; + if (taia_less(&deadline, &now)) ok = 0; if (!uses) ok = 0; if (!ok) { if (init(&rules) < 0) return DNS_INT; - taia_uint(&deadline,600); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, 600); + taia_add(&deadline, &now, &deadline); uses = 10000; ok = 1; } --uses; - if (!stralloc_copy(out,&rules)) return DNS_MEM; + if (!stralloc_copy(out, &rules)) return DNS_MEM; return 0; } diff --git a/src/dnsstub/dns_resolve.c b/src/dnsstub/dns_resolve.c index bcc4308..90c5615 100644 --- a/src/dnsstub/dns_resolve.c +++ b/src/dnsstub/dns_resolve.c @@ -1,8 +1,8 @@ -#include "iopause.h" -#include "taia.h" #include "byte.h" -#include "ip.h" #include "dnsresolv.h" +#include "iopause.h" +#include "ip.h" +#include "taia.h" /** @file dns_resolve.c @@ -13,7 +13,7 @@ struct dns_transmit dns_resolve_tx = {0}; -int dns_resolve(const char *q,const char qtype[2]) +int dns_resolve(const char *q, const char qtype[2]) { struct taia stamp; struct taia deadline; @@ -22,17 +22,18 @@ int dns_resolve(const char *q,const char qtype[2]) iopause_fd x[1]; int r; - if (dns_resolvconfip(servers,scopes) < 0) return DNS_INT; + if (dns_resolvconfip(servers, scopes) < 0) return DNS_INT; - if (dns_transmit_start6(&dns_resolve_tx,servers,1,q,qtype,(const char *)V6localnet,scopes) < 0) return DNS_COM; + if (dns_transmit_start6(&dns_resolve_tx, servers, 1, q, qtype, (const char *)V6localnet, scopes) < 0) + return DNS_COM; for (;;) { taia_now(&stamp); - taia_uint(&deadline,120); - taia_add(&deadline,&deadline,&stamp); - dns_transmit_io(&dns_resolve_tx,x,&deadline); - iopause(x,1,&deadline,&stamp); - r = dns_transmit_get(&dns_resolve_tx,x,&stamp); + taia_uint(&deadline, 120); + taia_add(&deadline, &deadline, &stamp); + dns_transmit_io(&dns_resolve_tx, x, &deadline); + iopause(x, 1, &deadline, &stamp); + r = dns_transmit_get(&dns_resolve_tx, x, &stamp); if (r < 0) return DNS_COM; if (r == 1) return 0; } diff --git a/src/dnsstub/dns_sortip.c b/src/dnsstub/dns_sortip.c index 56742e0..886e989 100644 --- a/src/dnsstub/dns_sortip.c +++ b/src/dnsstub/dns_sortip.c @@ -1,6 +1,6 @@ #include "byte.h" -#include "ip.h" #include "dnsresolv.h" +#include "ip.h" /** @file dns_sortip.c @@ -14,32 +14,32 @@ /* XXX: pay attention to qualification (DNSSec, DNSCurve) of each server? */ /* YYY: we use a randomly sorted list of NS; not depending on answer */ -void dns_sortip4(char *s,unsigned int n) +void dns_sortip4(char *s, unsigned int n) { unsigned int i; char tmp[4]; - n >>= 2; /* 4 byte per IPv4 address */ + n >>= 2; /* 4 byte per IPv4 address */ while (n > 1) { i = dns_random(n); --n; - byte_copy(tmp,4,s + (i << 2)); - byte_copy(s + (i << 2),4,s + (n << 2)); - byte_copy(s + (n << 2),4,tmp); + byte_copy(tmp, 4, s + (i << 2)); + byte_copy(s + (i << 2), 4, s + (n << 2)); + byte_copy(s + (n << 2), 4, tmp); } } -void dns_sortip6(char *s,unsigned int n) +void dns_sortip6(char *s, unsigned int n) { unsigned int i; char tmp[16]; - n >>= 4; /* 16 byte per IPv4 address */ + n >>= 4; /* 16 byte per IPv4 address */ while (n > 1) { i = dns_random(n); --n; - byte_copy(tmp,16,s + (i << 4)); - byte_copy(s + (i << 4),16,s + (n << 4)); - byte_copy(s + (n << 4),16,tmp); + byte_copy(tmp, 16, s + (i << 4)); + byte_copy(s + (i << 4), 16, s + (n << 4)); + byte_copy(s + (n << 4), 16, tmp); } } diff --git a/src/dnsstub/dns_transmit.c b/src/dnsstub/dns_transmit.c index 2513565..e041312 100644 --- a/src/dnsstub/dns_transmit.c +++ b/src/dnsstub/dns_transmit.c @@ -1,13 +1,14 @@ -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <unistd.h> -#include "socket_if.h" + #include "alloc.h" -#include "error.h" #include "byte.h" -#include "uint_t.h" -#include "ip.h" #include "dnsresolv.h" +#include "error.h" +#include "ip.h" +#include "socket_if.h" +#include "uint_t.h" /** @file dns_transmit.c @@ -21,62 +22,70 @@ uint32 scope_ids[QUERY_MAXNS]; -static const int timeouts[5] = { 1, 2, 4, 8, 16 }; /* quadratic, not exponentially */ - -int getscopeid(const struct dns_transmit *d,const char *ip) +static const int timeouts[5] = {1, 2, 4, 8, 16}; /* quadratic, not exponentially */ + +int getscopeid(const struct dns_transmit *d, const char *ip) { int i; - if (byte_diff(ip,2,V6linklocal)) return 0; - for (i = 0; i < QUERY_MAXNS; ++i) - if (byte_equal(d->servers + 16 * i,16,ip)) - return scope_ids[i]; + if (byte_diff(ip, 2, V6linklocal)) return 0; + for (i = 0; i < QUERY_MAXNS; ++i) + if (byte_equal(d->servers + 16 * i, 16, ip)) return scope_ids[i]; return 0; } -int serverwantstcp(const char *buf,unsigned int len) +int serverwantstcp(const char *buf, unsigned int len) { char out[12]; - if (!dns_packet_copy(buf,len,0,out,12)) return 1; + if (!dns_packet_copy(buf, len, 0, out, 12)) return 1; if (out[2] & 2) return 1; return 0; } -int serverfailed(const char *buf,unsigned int len) +int serverfailed(const char *buf, unsigned int len) { char out[12]; unsigned int rcode; - if (!dns_packet_copy(buf,len,0,out,12)) return 1; + if (!dns_packet_copy(buf, len, 0, out, 12)) return 1; rcode = out[3]; rcode &= 15; - if (rcode && (rcode != 3)) { errno = EAGAIN; return 1; } + if (rcode && (rcode != 3)) { + errno = EAGAIN; + return 1; + } return 0; } -int irrelevant(const struct dns_transmit *d,const char *buf,unsigned int len) +int irrelevant(const struct dns_transmit *d, const char *buf, unsigned int len) { char out[12]; char *dn; unsigned int pos; - pos = dns_packet_copy(buf,len,0,out,12); if (!pos) return 1; - if (byte_diff(out,2,d->query + 2)) return 1; + pos = dns_packet_copy(buf, len, 0, out, 12); + if (!pos) return 1; + if (byte_diff(out, 2, d->query + 2)) return 1; if (out[4] != 0) return 1; if (out[5] != 1) return 1; dn = 0; - pos = dns_packet_getname(buf,len,pos,&dn); if (!pos) return 1; - if (!dns_domain_equal(dn,d->query + 14)) { alloc_free(dn); return 1; } + pos = dns_packet_getname(buf, len, pos, &dn); + if (!pos) return 1; + if (!dns_domain_equal(dn, d->query + 14)) { + alloc_free(dn); + return 1; + } alloc_free(dn); - pos = dns_packet_copy(buf,len,pos,out,4); if (!pos) return 1; - if (byte_diff(out,2,d->qtype)) return 1; - if (byte_diff(out + 2,2,DNS_C_IN)) return 1; + pos = dns_packet_copy(buf, len, pos, out, 4); + if (!pos) return 1; + if (byte_diff(out, 2, d->qtype)) return 1; + if (byte_diff(out + 2, 2, DNS_C_IN)) return 1; return 0; } @@ -112,13 +121,11 @@ void dns_transmit_free(struct dns_transmit *d) int randombind6(struct dns_transmit *d) { int j; - + for (j = 0; j < 10; ++j) { - if (socket_bind6(d->s1 - 1,d->localip,1025 + dns_random(64510),d->scope_id) == 0) - return 0; - } - if (socket_bind6(d->s1 - 1,d->localip,0,d->scope_id) == 0) - return 0; + if (socket_bind6(d->s1 - 1, d->localip, 1025 + dns_random(64510), d->scope_id) == 0) return 0; + } + if (socket_bind6(d->s1 - 1, d->localip, 0, d->scope_id) == 0) return 0; return DNS_COM; } @@ -126,13 +133,11 @@ int randombind6(struct dns_transmit *d) int randombind4(struct dns_transmit *d) { int j; - + for (j = 0; j < 10; ++j) { - if (socket_bind4(d->s1 - 1,d->localip + 12,1025 + dns_random(64510)) == 0) - return 0; - } - if (socket_bind4(d->s1 - 1,d->localip + 12,0) == 0) - return 0; + if (socket_bind4(d->s1 - 1, d->localip + 12, 1025 + dns_random(64510)) == 0) return 0; + } + if (socket_bind4(d->s1 - 1, d->localip + 12, 0) == 0) return 0; return DNS_COM; } @@ -146,28 +151,39 @@ int thisudp(struct dns_transmit *d) while (d->udploop < 5) { for (; d->curserver < QUERY_MAXNS; ++d->curserver) { ip = d->servers + 16 * d->curserver; - if (byte_diff(ip,16,V6localnet)) { + if (byte_diff(ip, 16, V6localnet)) { d->query[2] = dns_random(256); d->query[3] = dns_random(256); - + if (ip6_isv4mapped(ip)) { - d->s1 = 1 + socket_udp4(); - if (!d->s1) { dns_transmit_free(d); return DNS_COM; } - if (randombind4(d) < 0) { dns_transmit_free(d); return DNS_COM; } + d->s1 = 1 + socket_udp4(); + if (!d->s1) { + dns_transmit_free(d); + return DNS_COM; + } + if (randombind4(d) < 0) { + dns_transmit_free(d); + return DNS_COM; + } } else { - d->s1 = 1 + socket_udp6(); - if (!d->s1) { dns_transmit_free(d); return DNS_COM; } - if (randombind6(d) < 0) { dns_transmit_free(d); return DNS_COM; } - } - - if (byte_equal(ip,2,V6linklocal) && !d->scope_id) - d->scope_id = getscopeid(d,ip); - if (socket_connect(d->s1 - 1,ip,DNSPORT,d->scope_id) == 0) - if (send(d->s1 - 1,d->query + 2,d->querylen - 2,0) == d->querylen - 2) { + d->s1 = 1 + socket_udp6(); + if (!d->s1) { + dns_transmit_free(d); + return DNS_COM; + } + if (randombind6(d) < 0) { + dns_transmit_free(d); + return DNS_COM; + } + } + + if (byte_equal(ip, 2, V6linklocal) && !d->scope_id) d->scope_id = getscopeid(d, ip); + if (socket_connect(d->s1 - 1, ip, DNSPORT, d->scope_id) == 0) + if (send(d->s1 - 1, d->query + 2, d->querylen - 2, 0) == d->querylen - 2) { struct taia now; taia_now(&now); - taia_uint(&d->deadline,timeouts[d->udploop]); - taia_add(&d->deadline,&d->deadline,&now); + taia_uint(&d->deadline, timeouts[d->udploop]); + taia_add(&d->deadline, &d->deadline, &now); d->tcpstate = 0; return 0; } @@ -178,7 +194,8 @@ int thisudp(struct dns_transmit *d) d->curserver = 0; } - dns_transmit_free(d); return DNS_COM; + dns_transmit_free(d); + return DNS_COM; } int firstudp(struct dns_transmit *d) @@ -203,27 +220,38 @@ int thistcp(struct dns_transmit *d) for (; d->curserver < QUERY_MAXNS; ++d->curserver) { ip = d->servers + 16 * d->curserver; - if (byte_diff(ip,16,V6localnet)) { + if (byte_diff(ip, 16, V6localnet)) { d->query[2] = dns_random(256); d->query[3] = dns_random(256); if (ip6_isv4mapped(ip)) { d->s1 = 1 + socket_tcp4(); - if (!d->s1) { dns_transmit_free(d); return DNS_COM; } - if (randombind4(d) < 0) { dns_transmit_free(d); return DNS_COM; } + if (!d->s1) { + dns_transmit_free(d); + return DNS_COM; + } + if (randombind4(d) < 0) { + dns_transmit_free(d); + return DNS_COM; + } } else { - d->s1 = 1 + socket_tcp6(); - if (!d->s1) { dns_transmit_free(d); return DNS_COM; } - if (randombind6(d) < 0) { dns_transmit_free(d); return DNS_COM; } - } - + d->s1 = 1 + socket_tcp6(); + if (!d->s1) { + dns_transmit_free(d); + return DNS_COM; + } + if (randombind6(d) < 0) { + dns_transmit_free(d); + return DNS_COM; + } + } + taia_now(&now); - taia_uint(&d->deadline,10); - taia_add(&d->deadline,&d->deadline,&now); + taia_uint(&d->deadline, 10); + taia_add(&d->deadline, &d->deadline, &now); - if (byte_equal(ip,2,V6linklocal) && !d->scope_id) - d->scope_id = getscopeid(d,ip); - if (socket_connect(d->s1 - 1,ip,DNSPORT,d->scope_id) == 0) { + if (byte_equal(ip, 2, V6linklocal) && !d->scope_id) d->scope_id = getscopeid(d, ip); + if (socket_connect(d->s1 - 1, ip, DNSPORT, d->scope_id) == 0) { d->tcpstate = 2; return 0; } @@ -231,12 +259,12 @@ int thistcp(struct dns_transmit *d) d->tcpstate = 1; return 0; } - + socketfree(d); } } - dns_transmit_free(d); + dns_transmit_free(d); return DNS_COM; } @@ -252,8 +280,13 @@ int nexttcp(struct dns_transmit *d) return thistcp(d); } -int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN], \ - int flagrecursive,const char *q,const char qtype[2],const char localip[16]) +int dns_transmit_start( + struct dns_transmit *d, + const char servers[QUERY_MAXIPLEN], + int flagrecursive, + const char *q, + const char qtype[2], + const char localip[16]) { unsigned int len; @@ -264,17 +297,19 @@ int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN] d->querylen = len + 18; d->query = alloc(d->querylen); if (!d->query) return DNS_COM; - - uint16_pack_big(d->query,len + 16); - byte_copy(d->query + 2,12,flagrecursive ? "\0\0\1\0\0\1\0\0\0\0\0\0" : \ - "\0\0\0\0\0\1\0\0\0\0\0\0gcc-bug-workaround"); - byte_copy(d->query + 14,len,q); - byte_copy(d->query + 14 + len,2,qtype); - byte_copy(d->query + 16 + len,2,DNS_C_IN); - - byte_copy(d->qtype,2,(char *) qtype); + + uint16_pack_big(d->query, len + 16); + byte_copy( + d->query + 2, + 12, + flagrecursive ? "\0\0\1\0\0\1\0\0\0\0\0\0" : "\0\0\0\0\0\1\0\0\0\0\0\0gcc-bug-workaround"); + byte_copy(d->query + 14, len, q); + byte_copy(d->query + 14 + len, 2, qtype); + byte_copy(d->query + 16 + len, 2, DNS_C_IN); + + byte_copy(d->qtype, 2, (char *)qtype); d->servers = servers; - byte_copy(d->localip,16,(char *) localip); + byte_copy(d->localip, 16, (char *)localip); d->udploop = flagrecursive ? 1 : 0; @@ -282,33 +317,37 @@ int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN] return firstudp(d); } -int dns_transmit_start6(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN], \ - int flagrecursive,const char *q,const char qtype[2], \ - const char localip[16],const uint32 scopes[QUERY_MAXNS]) +int dns_transmit_start6( + struct dns_transmit *d, + const char servers[QUERY_MAXIPLEN], + int flagrecursive, + const char *q, + const char qtype[2], + const char localip[16], + const uint32 scopes[QUERY_MAXNS]) { - byte_copy(scope_ids,128,(char *) scopes); + byte_copy(scope_ids, 128, (char *)scopes); - return dns_transmit_start(d,servers,flagrecursive,q,qtype,localip); + return dns_transmit_start(d, servers, flagrecursive, q, qtype, localip); } -void dns_transmit_io(struct dns_transmit *d,iopause_fd *x,struct taia *deadline) +void dns_transmit_io(struct dns_transmit *d, iopause_fd *x, struct taia *deadline) { x->fd = d->s1 - 1; switch (d->tcpstate) { - case 0: case 3: case 4: case 5: - x->events = IOPAUSE_READ; - break; - case 1: case 2: - x->events = IOPAUSE_WRITE; - break; + case 0: + case 3: + case 4: + case 5: x->events = IOPAUSE_READ; break; + case 1: + case 2: x->events = IOPAUSE_WRITE; break; } - if (taia_less(&d->deadline,deadline)) - *deadline = d->deadline; + if (taia_less(&d->deadline, deadline)) *deadline = d->deadline; } -int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct taia *when) +int dns_transmit_get(struct dns_transmit *d, const iopause_fd *x, const struct taia *when) { char udpbuf[MSGSIZE + 1]; unsigned char ch; @@ -319,27 +358,28 @@ int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct tai fd = d->s1 - 1; if (!x->revents) { - if (taia_less((struct taia *)when,&d->deadline)) return 0; + if (taia_less((struct taia *)when, &d->deadline)) return 0; errno = ETIMEDOUT; if (d->tcpstate == 0) return nextudp(d); return nexttcp(d); } -/* + /* have attempted to send UDP query to each server udploop times have sent query to curserver on UDP socket s */ if (d->tcpstate == 0) { - r = recv(fd,udpbuf,sizeof(udpbuf),0); + r = recv(fd, udpbuf, sizeof(udpbuf), 0); if (r <= 0) { - if (errno == ECONNREFUSED) if (d->udploop == 2) return 0; + if (errno == ECONNREFUSED) + if (d->udploop == 2) return 0; return nextudp(d); } if (r + 1 > sizeof(udpbuf)) return 0; - if (irrelevant(d,udpbuf,r)) return 0; - if (serverwantstcp(udpbuf,r)) return firsttcp(d); - if (serverfailed(udpbuf,r)) { + if (irrelevant(d, udpbuf, r)) return 0; + if (serverwantstcp(udpbuf, r)) return firsttcp(d); + if (serverfailed(udpbuf, r)) { if (d->udploop == 2) return 0; return nextudp(d); } @@ -347,13 +387,16 @@ have sent query to curserver on UDP socket s d->packetlen = r; d->packet = alloc(d->packetlen); - if (!d->packet) { dns_transmit_free(d); return DNS_COM; } - byte_copy(d->packet,d->packetlen,udpbuf); + if (!d->packet) { + dns_transmit_free(d); + return DNS_COM; + } + byte_copy(d->packet, d->packetlen, udpbuf); queryfree(d); return 1; } -/* + /* have sent connection attempt to curserver on TCP socket s pos not defined */ @@ -364,69 +407,72 @@ pos not defined return 0; } -/* + /* have connection to curserver on TCP socket s have sent pos bytes of query */ if (d->tcpstate == 2) { - r = write(fd,d->query + d->pos,d->querylen - d->pos); + r = write(fd, d->query + d->pos, d->querylen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos == d->querylen) { struct taia now; taia_now(&now); - taia_uint(&d->deadline,10); - taia_add(&d->deadline,&d->deadline,&now); + taia_uint(&d->deadline, 10); + taia_add(&d->deadline, &d->deadline, &now); d->tcpstate = 3; } return 0; } -/* + /* have sent entire query to curserver on TCP socket s pos not defined */ if (d->tcpstate == 3) { - r = read(fd,&ch,1); + r = read(fd, &ch, 1); if (r <= 0) return nexttcp(d); d->packetlen = ch; d->tcpstate = 4; return 0; } -/* + /* have sent entire query to curserver on TCP socket s pos not defined have received one byte of packet length into packetlen */ if (d->tcpstate == 4) { - r = read(fd,&ch,1); + r = read(fd, &ch, 1); if (r <= 0) return nexttcp(d); d->packetlen <<= 8; d->packetlen += ch; d->tcpstate = 5; d->pos = 0; d->packet = alloc(d->packetlen); - if (!d->packet) { dns_transmit_free(d); return DNS_COM; } + if (!d->packet) { + dns_transmit_free(d); + return DNS_COM; + } return 0; } -/* + /* have sent entire query to curserver on TCP socket s have received entire packet length into packetlen packet is allocated have received pos bytes of packet */ if (d->tcpstate == 5) { - r = read(fd,d->packet + d->pos,d->packetlen - d->pos); + r = read(fd, d->packet + d->pos, d->packetlen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos < d->packetlen) return 0; socketfree(d); - if (irrelevant(d,d->packet,d->packetlen)) return nexttcp(d); - if (serverwantstcp(d->packet,d->packetlen)) return nexttcp(d); - if (serverfailed(d->packet,d->packetlen)) return nexttcp(d); + if (irrelevant(d, d->packet, d->packetlen)) return nexttcp(d); + if (serverwantstcp(d->packet, d->packetlen)) return nexttcp(d); + if (serverfailed(d->packet, d->packetlen)) return nexttcp(d); queryfree(d); return 1; diff --git a/src/dnsstub/dns_txt.c b/src/dnsstub/dns_txt.c index 9a1b56a..ce0afc4 100644 --- a/src/dnsstub/dns_txt.c +++ b/src/dnsstub/dns_txt.c @@ -1,9 +1,9 @@ -#include "stralloc.h" -#include "uint_t.h" #include "byte.h" #include "dnsresolv.h" +#include "stralloc.h" +#include "uint_t.h" -int dns_txt_packet(stralloc *out,const char *buf,unsigned int len) +int dns_txt_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -14,35 +14,40 @@ int dns_txt_packet(stralloc *out,const char *buf,unsigned int len) int i; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_TXT)) - if (byte_equal(header + 2,2,DNS_C_IN)) { + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_TXT)) + if (byte_equal(header + 2, 2, DNS_C_IN)) { if (pos + datalen > len) return DNS_ERR; - txtlen = (unsigned char) buf[pos]; + txtlen = (unsigned char)buf[pos]; for (i = 1; i < datalen; ++i) { ch = buf[pos + i]; - if (i == txtlen + 1) // next label - txtlen += (unsigned char) ch + 1; + if (i == txtlen + 1) // next label + txtlen += (unsigned char)ch + 1; else { if (ch < 32) ch = '?'; if (ch > 126) ch = '?'; - if (!stralloc_append(out,&ch)) return DNS_MEM; + if (!stralloc_append(out, &ch)) return DNS_MEM; } } } pos += datalen; ++ranswers; - if (numanswers) if (!stralloc_append(out,"\n")) return DNS_MEM; + if (numanswers) + if (!stralloc_append(out, "\n")) return DNS_MEM; } return ranswers; @@ -50,13 +55,13 @@ int dns_txt_packet(stralloc *out,const char *buf,unsigned int len) static char *q = 0; -int dns_txt(stralloc *out,const stralloc *fqdn) +int dns_txt(stralloc *out, const stralloc *fqdn) { - int rc; + int rc; - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; - if (dns_resolve(q,DNS_T_TXT) < 0) return DNS_ERR; - if ((rc = dns_txt_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; + if (dns_resolve(q, DNS_T_TXT) < 0) return DNS_ERR; + if ((rc = dns_txt_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); @@ -1,7 +1,8 @@ -#include "str.h" -#include "alloc.h" #include "env.h" +#include "alloc.h" +#include "str.h" + /** @file env.c @author djb @@ -9,7 +10,7 @@ @brief setting up environment after fork */ -extern /*@null@*/char *env_get(char *s) +extern /*@null@*/ char *env_get(char *s) { int i; unsigned int len; @@ -17,12 +18,11 @@ extern /*@null@*/char *env_get(char *s) if (!s) return 0; len = str_len(s); for (i = 0; environ[i]; ++i) - if (str_start(environ[i],s) && (environ[i][len] == '=')) - return environ[i] + len + 1; + if (str_start(environ[i], s) && (environ[i][len] == '=')) return environ[i] + len + 1; return 0; } -extern char *env_findeq(char *s) +extern char *env_findeq(char *s) { for (; *s; ++s) if (*s == '=') return s; @@ -33,91 +33,102 @@ int env_isinit = 0; /* if env_isinit: */ static int ea; /* environ is a pointer to ea+1 char*'s. */ static int en; /* the first en of those are ALLOCATED. environ[en] is 0. */ -static void env_del(int i) { +static void env_del(int i) +{ alloc_free(environ[i]); environ[i] = environ[--en]; environ[en] = 0; } -static void env_unsetlen(char *s,int len) +static void env_unsetlen(char *s, int len) { int i; for (i = en - 1; i >= 0; --i) - if (!str_diffn(s,environ[i],len)) - if (environ[i][len] == '=') - env_del(i); + if (!str_diffn(s, environ[i], len)) + if (environ[i][len] == '=') env_del(i); } int env_unset(char *s) { - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; - env_unsetlen(s,str_len(s)); + env_unsetlen(s, str_len(s)); return 1; } -int env_set(char *s) { +int env_set(char *s) +{ char *t; t = env_findeq(s); - if (t) env_unsetlen(s,t - s); + if (t) env_unsetlen(s, t - s); if (en == ea) { ea += 30; - if (!alloc_re(&environ,(en + 1) * sizeof(char *),(ea + 1) * sizeof(char *))) - { ea = en; return 0; } + if (!alloc_re(&environ, (en + 1) * sizeof(char *), (ea + 1) * sizeof(char *))) { + ea = en; + return 0; + } } environ[en++] = s; environ[en] = 0; return 1; } -int env_puts(char *s) { +int env_puts(char *s) +{ char *u; - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; u = alloc(str_len(s) + 1); if (!u) return 0; - str_copy(u,s); - if (!env_set(u)) { alloc_free(u); return 0; } + str_copy(u, s); + if (!env_set(u)) { + alloc_free(u); + return 0; + } return 1; } -int env_put(char *name,char *value) { +int env_put(char *name, char *value) +{ char *ln; int len; - if (!env_isinit) + if (!env_isinit) if (!env_init()) return 0; len = str_len(name); ln = alloc(len + str_len(value) + 2); if (!ln) return 0; - str_copy(ln,name); + str_copy(ln, name); ln[len] = '='; - str_copy(ln + len + 1,value); - if (!env_set(ln)) { alloc_free(ln); return 0; } + str_copy(ln + len + 1, value); + if (!env_set(ln)) { + alloc_free(ln); + return 0; + } return 1; } -int env_init() { +int env_init() +{ char **newenviron; int i; - for (en = 0; environ[en]; ++en) - ; + for (en = 0; environ[en]; ++en); ea = en + 10; - newenviron = (char **) alloc((ea + 1) * sizeof(char *)); + newenviron = (char **)alloc((ea + 1) * sizeof(char *)); if (!newenviron) return 0; for (en = 0; environ[en]; ++en) { newenviron[en] = alloc(str_len(environ[en]) + 1); if (!newenviron[en]) { for (i = 0; i < en; ++i) alloc_free(newenviron[i]); - alloc_free(newenviron); - return 0; + alloc_free(newenviron); + return 0; } - str_copy(newenviron[en],environ[en]); + str_copy(newenviron[en], environ[en]); } newenviron[en] = 0; @@ -128,9 +139,14 @@ int env_init() { static char *null = 0; -void env_clear() { - if (env_isinit) +void env_clear() +{ + if (env_isinit) while (en) env_del(0); - else environ = &null; + else + environ = &null; +} +extern char *env_pick() +{ + return environ[0]; } -extern char *env_pick() { return environ[0]; } diff --git a/src/errstr.c b/src/errstr.c index e2290fb..47b5aef 100644 --- a/src/errstr.c +++ b/src/errstr.c @@ -7,157 +7,158 @@ @brief error output to log for different conditions and OS */ -#define X(e,s) if (code == e) return s; +#define X(e, s) \ + if (code == e) return s; extern char *error_str(int); char *errstr(int code) { - X(0,"") // NOERROR - X(error_intr,"interrupted system call") // EINTR - X(EINTR, "interrupted system call") - X(error_nomem,"out of memory") // ENOMEM - X(ENOMEM, "out of memory") - X(error_noent,"file does not exist") // ENOENT - X(ENOENT, "file does not exist") - X(error_txtbsy,"text busy") // ETXTBSY - X(ETXTBSY, "text busy") - X(error_io,"input/output error") // EIO - X(EIO, "input/output error") - X(error_exist,"file already exists") // EEXISTS - X(EEXIST, "file already exists") - X(error_timeout,"timed out") // ETIMEDOUT - X(ETIMEDOUT, "timed out") - X(error_inprogress,"operation in progress") // EINPROGRESS - X(EINPROGRESS, "operation in progress") - X(error_again,"temporary failure") // EAGAIN - X(EAGAIN, "temporary failure") - X(error_wouldblock,"input/output would block") // EWOULDBLOCK (intern EAGAIN) - X(EWOULDBLOCK, "input/output would block") - X(error_pipe,"broken pipe") // EPIPE - X(EPIPE, "broken pipe") - X(error_perm,"permission denied") // EPERM - X(EPERM, "permission denied") - X(error_acces,"access denied") // EACCES - X(EACCES, "access denied") - - X(ESRCH, "no such process") - -// X(error_nodevice,"device not configured") // ENXIO - X(ENXIO, "device not configured") - - X(E2BIG, "argument list too long") - X(ENOEXEC, "exec format error") - X(EBADF, "file descriptor not open") - X(ECHILD, "no child processes") - X(EDEADLK, "operation would cause deadlock") - X(EFAULT, "bad address") - X(ENOTBLK, "not a block device") - X(EBUSY, "device busy") - X(EXDEV, "cross-device link") - X(ENODEV, "device does not support operation") -// X(error_notdir,"not a directory") // ENOTDIR - X(ENOTDIR, "not a directory") - X(error_isdir,"is a directory") // EISDIR - X(EISDIR, "is a directory") - X(EINVAL, "invalid argument") - X(ENFILE, "system cannot open more files") - X(EMFILE, "process cannot open more files") - X(ENOTTY, "not a tty") - X(EFBIG, "file too big") - X(ENOSPC, "out of disk space") - X(ESPIPE, "unseekable descriptor") -// X(error_rofs,"read-only file system") // EROFS - X(EROFS, "read-only file system") - X(EMLINK, "too many links") - X(EDOM, "input out of range") - X(ERANGE, "output out of range") - X(EALREADY, "operation already in progress") - X(ENOTSOCK, "not a socket") - X(EDESTADDRREQ, "destination address required") - X(EMSGSIZE, "message too long") - X(EPROTOTYPE, "incorrect protocol type") - X(ENOPROTOOPT, "protocol not available") - X(EPROTONOSUPPORT, "protocol not supported") - X(ESOCKTNOSUPPORT, "socket type not supported") - X(EOPNOTSUPP, "operation not supported") - X(EPFNOSUPPORT, "protocol family not supported") - X(EAFNOSUPPORT, "address family not supported") - X(EADDRINUSE, "address already used") - X(EADDRNOTAVAIL, "address not available") - X(ENETDOWN, "network down") - X(ENETUNREACH, "network unreachable") - X(ENETRESET, "network reset") - X(ECONNABORTED, "connection aborted") - X(error_connreset, "connection reset") // ECONNRESET - X(ECONNRESET, "connection reset") - X(ENOBUFS, "out of buffer space") - X(EISCONN, "already connected") - X(ENOTCONN, "not connected") - X(ESHUTDOWN, "socket shut down") - X(ETOOMANYREFS, "too many references") - X(error_connrefused,"connection refused") // ECONNREFUSED - X(ECONNREFUSED, "connection refused") - X(ELOOP, "symbolic link loop") - X(ENAMETOOLONG, "file name too long") - X(EHOSTDOWN, "host down") - X(EHOSTUNREACH, "host unreachable") - X(ENOTEMPTY, "directory not empty") - X(EUSERS, "too many users") - X(EDQUOT, "disk quota exceeded") - X(ESTALE, "stale NFS file handle") + X(0, "") // NOERROR + X(error_intr, "interrupted system call") // EINTR + X(EINTR, "interrupted system call") + X(error_nomem, "out of memory") // ENOMEM + X(ENOMEM, "out of memory") + X(error_noent, "file does not exist") // ENOENT + X(ENOENT, "file does not exist") + X(error_txtbsy, "text busy") // ETXTBSY + X(ETXTBSY, "text busy") + X(error_io, "input/output error") // EIO + X(EIO, "input/output error") + X(error_exist, "file already exists") // EEXISTS + X(EEXIST, "file already exists") + X(error_timeout, "timed out") // ETIMEDOUT + X(ETIMEDOUT, "timed out") + X(error_inprogress, "operation in progress") // EINPROGRESS + X(EINPROGRESS, "operation in progress") + X(error_again, "temporary failure") // EAGAIN + X(EAGAIN, "temporary failure") + X(error_wouldblock, "input/output would block") // EWOULDBLOCK (intern EAGAIN) + X(EWOULDBLOCK, "input/output would block") + X(error_pipe, "broken pipe") // EPIPE + X(EPIPE, "broken pipe") + X(error_perm, "permission denied") // EPERM + X(EPERM, "permission denied") + X(error_acces, "access denied") // EACCES + X(EACCES, "access denied") + + X(ESRCH, "no such process") + + // X(error_nodevice,"device not configured") // ENXIO + X(ENXIO, "device not configured") + + X(E2BIG, "argument list too long") + X(ENOEXEC, "exec format error") + X(EBADF, "file descriptor not open") + X(ECHILD, "no child processes") + X(EDEADLK, "operation would cause deadlock") + X(EFAULT, "bad address") + X(ENOTBLK, "not a block device") + X(EBUSY, "device busy") + X(EXDEV, "cross-device link") + X(ENODEV, "device does not support operation") + // X(error_notdir,"not a directory") // ENOTDIR + X(ENOTDIR, "not a directory") + X(error_isdir, "is a directory") // EISDIR + X(EISDIR, "is a directory") + X(EINVAL, "invalid argument") + X(ENFILE, "system cannot open more files") + X(EMFILE, "process cannot open more files") + X(ENOTTY, "not a tty") + X(EFBIG, "file too big") + X(ENOSPC, "out of disk space") + X(ESPIPE, "unseekable descriptor") + // X(error_rofs,"read-only file system") // EROFS + X(EROFS, "read-only file system") + X(EMLINK, "too many links") + X(EDOM, "input out of range") + X(ERANGE, "output out of range") + X(EALREADY, "operation already in progress") + X(ENOTSOCK, "not a socket") + X(EDESTADDRREQ, "destination address required") + X(EMSGSIZE, "message too long") + X(EPROTOTYPE, "incorrect protocol type") + X(ENOPROTOOPT, "protocol not available") + X(EPROTONOSUPPORT, "protocol not supported") + X(ESOCKTNOSUPPORT, "socket type not supported") + X(EOPNOTSUPP, "operation not supported") + X(EPFNOSUPPORT, "protocol family not supported") + X(EAFNOSUPPORT, "address family not supported") + X(EADDRINUSE, "address already used") + X(EADDRNOTAVAIL, "address not available") + X(ENETDOWN, "network down") + X(ENETUNREACH, "network unreachable") + X(ENETRESET, "network reset") + X(ECONNABORTED, "connection aborted") + X(error_connreset, "connection reset") // ECONNRESET + X(ECONNRESET, "connection reset") + X(ENOBUFS, "out of buffer space") + X(EISCONN, "already connected") + X(ENOTCONN, "not connected") + X(ESHUTDOWN, "socket shut down") + X(ETOOMANYREFS, "too many references") + X(error_connrefused, "connection refused") // ECONNREFUSED + X(ECONNREFUSED, "connection refused") + X(ELOOP, "symbolic link loop") + X(ENAMETOOLONG, "file name too long") + X(EHOSTDOWN, "host down") + X(EHOSTUNREACH, "host unreachable") + X(ENOTEMPTY, "directory not empty") + X(EUSERS, "too many users") + X(EDQUOT, "disk quota exceeded") + X(ESTALE, "stale NFS file handle") /* BSD only (all BSD's, NOT on Linux) */ -// X(EPROCLIM, "too many processes") // -L +FB +OB +NB -// X(EBADRPC, "RPC structure is bad") // -L +FB +OB +NB + // X(EPROCLIM, "too many processes") // -L +FB +OB +NB + // X(EBADRPC, "RPC structure is bad") // -L +FB +OB +NB -// X(ERPCMISMATCH, "RPC version mismatch") // -L +FB +OB +NB -// X(EPROGUNAVAIL, "RPC program unavailable") // -L +FB +OB +NB -// X(EPROGMISMATCH, "program version mismatch") // -L +FB +OB +NB -// X(EPROCUNAVAIL, "bad procedure for program") // -L +FB +OB +NB -// X(EFTYPE, "bad file type") // -L +FB +OB +NB + // X(ERPCMISMATCH, "RPC version mismatch") // -L +FB +OB +NB + // X(EPROGUNAVAIL, "RPC program unavailable") // -L +FB +OB +NB + // X(EPROGMISMATCH, "program version mismatch") // -L +FB +OB +NB + // X(EPROCUNAVAIL, "bad procedure for program") // -L +FB +OB +NB + // X(EFTYPE, "bad file type") // -L +FB +OB +NB - X(ENOLCK, "no locks available") - X(ENOSYS, "system call not available") - X(ENOMSG, "no message of desired type") - X(EIDRM, "identifier removed") + X(ENOLCK, "no locks available") + X(ENOSYS, "system call not available") + X(ENOMSG, "no message of desired type") + X(EIDRM, "identifier removed") -// X(ERREMOTE, "object not local") // -L -FB -OB -NB - X(EREMOTE, "object not local") // Linux: "Object is remote" -// X(EREMOTE, "too many levels of remote in path") + // X(ERREMOTE, "object not local") // -L -FB -OB -NB + X(EREMOTE, "object not local") // Linux: "Object is remote" + // X(EREMOTE, "too many levels of remote in path") /* Linux only */ -// X(ENONET, "machine not on network") // +L -FB -OB -NB -// X(EADV, "advertise error") // +L -FB -OB -NB -// X(ESRMNT, "srmount error") // +L -FB -OB -NB -// X(ECOMM, "communication error") // +L -FB -OB -NB -// X(EREMCHG, "remote address changed") // +L -FB -OB -NB + // X(ENONET, "machine not on network") // +L -FB -OB -NB + // X(EADV, "advertise error") // +L -FB -OB -NB + // X(ESRMNT, "srmount error") // +L -FB -OB -NB + // X(ECOMM, "communication error") // +L -FB -OB -NB + // X(EREMCHG, "remote address changed") // +L -FB -OB -NB - X(error_proto,"protocol error") // EPROTO + X(error_proto, "protocol error") // EPROTO /* EPROTO: see 'error.h' for OpenBSD compat */ - X(EPROTO, "protocol error") // +L +FB -OB +NB + X(EPROTO, "protocol error") // +L +FB -OB +NB /* Linux and NetBSD */ -// X(ENOSTR, "not a stream device") // +L -FB -OB +NB -// X(ETIME, "timer expired") // +L -FB -OB +NB -// X(ENOSR, "out of stream resources") // +L -FB -OB +NB + // X(ENOSTR, "not a stream device") // +L -FB -OB +NB + // X(ETIME, "timer expired") // +L -FB -OB +NB + // X(ENOSR, "out of stream resources") // +L -FB -OB +NB /* FreeBSD and NetBSD */ -// X(EAUTH, "authentication error") // -L +FB -OB +NB -// X(ENEEDAUTH, "not authenticated") // -L +FB -OB +NB + // X(EAUTH, "authentication error") // -L +FB -OB +NB + // X(ENEEDAUTH, "not authenticated") // -L +FB -OB +NB /* NOT on OpenBSD */ -// X(EBADMSG, "bad message type") // +L +FB -OB +NB -// X(ENOLINK, "link severed") // +L +FB -OB +NB -// X(EMULTIHOP, "multihop attempted") // +L +FB -OB +NB + // X(EBADMSG, "bad message type") // +L +FB -OB +NB + // X(ENOLINK, "link severed") // +L +FB -OB +NB + // X(EMULTIHOP, "multihop attempted") // +L +FB -OB +NB /* Application/DJB specific */ - X(EHARD, " ") - X(ESOFT, " ") - X(USAGE, "usage error") // qmail; explicit - X(SYNTAX, "syntax error") // djbdns, qmail; explicit - X(DROP, "connection dropped") // ucspi-tcp - X(FATAL, "unable to continue") // all - - return "unknown error"; /* worst case */ + X(EHARD, " ") + X(ESOFT, " ") + X(USAGE, "usage error") // qmail; explicit + X(SYNTAX, "syntax error") // djbdns, qmail; explicit + X(DROP, "connection dropped") // ucspi-tcp + X(FATAL, "unable to continue") // all + + return "unknown error"; /* worst case */ } @@ -1,6 +1,7 @@ -#include <fcntl.h> #include "fd.h" +#include <fcntl.h> + /** @file fd.c @autor djb @@ -8,23 +9,26 @@ @brief file descriptor manipulation */ -int close(int __fd); /* we won't use unistd.h here */ +int close(int __fd); /* we won't use unistd.h here */ -int fd_copy(int to,int from) +int fd_copy(int to, int from) { if (to == from) return 0; - if (fcntl(from,F_GETFL,0) == -1) return -1; + if (fcntl(from, F_GETFL, 0) == -1) return -1; close(to); - if (fcntl(from,F_DUPFD,to) == -1) return -1; + if (fcntl(from, F_DUPFD, to) == -1) return -1; return 0; } -int fd_move(int to,int from) +int fd_move(int to, int from) { if (to == from) return 0; - if (fd_copy(to,from) == -1) return -1; + if (fd_copy(to, from) == -1) return -1; close(from); return 0; } -int fd_coe(int fd) {return fcntl(fd,F_SETFD,1); } +int fd_coe(int fd) +{ + return fcntl(fd, F_SETFD, 1); +} @@ -7,65 +7,91 @@ @brief formating differnt inputs format for output printing */ -unsigned int fmt_str(register char *s,register char *t) +unsigned int fmt_str(register char *s, register char *t) { register unsigned int len; char ch; len = 0; - if (s) { while ((ch = t[len])) s[len++] = ch; } - else while (t[len]) len++; + if (s) { + while ((ch = t[len])) s[len++] = ch; + } else + while (t[len]) len++; return len; } -unsigned int fmt_strn(register char *s,register char *t,register unsigned int n) +unsigned int fmt_strn(register char *s, register char *t, register unsigned int n) { register unsigned int len; char ch; len = 0; - if (s) { while (n-- && (ch = t[len])) s[len++] = ch; } - else while (n-- && t[len]) len++; + if (s) { + while (n-- && (ch = t[len])) s[len++] = ch; + } else + while (n-- && t[len]) len++; return len; } -unsigned int fmt_uint(register char *s,register unsigned int u) +unsigned int fmt_uint(register char *s, register unsigned int u) { - register unsigned long l; l = u; return fmt_ulong(s,l); + register unsigned long l; + l = u; + return fmt_ulong(s, l); } -unsigned int fmt_uint0(char *s,unsigned int u,unsigned int n) +unsigned int fmt_uint0(char *s, unsigned int u, unsigned int n) { unsigned int len; - len = fmt_uint(FMT_LEN,u); - while (len < n) { if (s) *s++ = '0'; ++len; } - if (s) fmt_uint(s,u); + len = fmt_uint(FMT_LEN, u); + while (len < n) { + if (s) *s++ = '0'; + ++len; + } + if (s) fmt_uint(s, u); return len; } -unsigned int fmt_ulong(register char *s,register unsigned long u) +unsigned int fmt_ulong(register char *s, register unsigned long u) { - register unsigned int len; register unsigned long q; - len = 1; q = u; - while (q > 9) { ++len; q /= 10; } + register unsigned int len; + register unsigned long q; + len = 1; + q = u; + while (q > 9) { + ++len; + q /= 10; + } if (s) { s += len; - do { *--s = '0' + (u % 10); u /= 10; } while(u); /* handles u == 0 */ + do { + *--s = '0' + (u % 10); + u /= 10; + } while (u); /* handles u == 0 */ } return len; } -unsigned int fmt_xlong(register char *s,register unsigned long u) +unsigned int fmt_xlong(register char *s, register unsigned long u) { - register unsigned int len; register unsigned long q; - len = 1; q = u; - while (q > 15) { ++len; q /= 16; } + register unsigned int len; + register unsigned long q; + len = 1; + q = u; + while (q > 15) { + ++len; + q /= 16; + } if (s) { s += len; - do { *--s = tohex(u % 16); u /= 16; } while(u); /* handles u == 0 */ + do { + *--s = tohex(u % 16); + u /= 16; + } while (u); /* handles u == 0 */ } return len; } -char tohex(char num) { +char tohex(char num) +{ if (num < 10) return num + '0'; else if (num < 16) @@ -74,11 +100,12 @@ char tohex(char num) { return -1; } -int fromhex(unsigned char c) { +int fromhex(unsigned char c) +{ if (c >= '0' && c <= '9') - return c-'0'; + return c - '0'; else if (c >= 'A' && c <= 'F') - return c -'A' + 10; + return c - 'A' + 10; else if (c >= 'a' && c <= 'f') return c - 'a' + 10; return -1; diff --git a/src/getln.c b/src/getln.c index f83d3c2..64189fe 100644 --- a/src/getln.c +++ b/src/getln.c @@ -1,6 +1,7 @@ -#include "byte.h" #include "getln.h" +#include "byte.h" + /** @file getln.c @author djb @@ -8,36 +9,46 @@ @brief evaluting tokenized input arguments */ -int getln(buffer *b,stralloc *sa,int *match,int sep) +int getln(buffer *b, stralloc *sa, int *match, int sep) { char *cont; unsigned int clen; - if (sgetln(b,sa,&cont,&clen,sep) == -1) return -1; - if (!clen) { *match = 0; return 0; } - if (!stralloc_catb(sa,cont,clen)) return -1; + if (sgetln(b, sa, &cont, &clen, sep) == -1) return -1; + if (!clen) { + *match = 0; + return 0; + } + if (!stralloc_catb(sa, cont, clen)) return -1; *match = 1; return 0; } -int sgetln(buffer *b,stralloc *sa,char **cont,unsigned int *clen,int sep) +int sgetln(buffer *b, stralloc *sa, char **cont, unsigned int *clen, int sep) { register char *x; register unsigned int i; int n; - if (!stralloc_ready(sa,0)) return -1; + if (!stralloc_ready(sa, 0)) return -1; sa->len = 0; for (;;) { n = buffer_feed(b); if (n < 0) return -1; - if (n == 0) { *clen = 0; return 0; } + if (n == 0) { + *clen = 0; + return 0; + } x = buffer_PEEK(b); - i = byte_chr(x,n,sep); - if (i < n) { buffer_SEEK(b,*clen = i + 1); *cont = x; return 0; } - if (!stralloc_readyplus(sa,n)) return -1; + i = byte_chr(x, n, sep); + if (i < n) { + buffer_SEEK(b, *clen = i + 1); + *cont = x; + return 0; + } + if (!stralloc_readyplus(sa, n)) return -1; i = sa->len; - sa->len = i + buffer_get(b,sa->s + i,n); + sa->len = i + buffer_get(b, sa->s + i, n); } } diff --git a/src/getoptb.c b/src/getoptb.c index edee6b8..9859102 100644 --- a/src/getoptb.c +++ b/src/getoptb.c @@ -1,6 +1,7 @@ -#include "buffer.h" #include "getoptb.h" +#include "buffer.h" + /** @file getoptb.c @author djb @@ -8,13 +9,13 @@ @brief 'getopt' version w/o stdlib */ -#define optind subgetoptind +#define optind subgetoptind #define optproblem subgetoptproblem int opterr = 1; char *optprogname = 0; -int getopt(int argc,char **argv,char *opts) +int getopt(int argc, char **argv, char *opts) { int c; char *s; @@ -22,25 +23,28 @@ int getopt(int argc,char **argv,char *opts) if (!optprogname) { optprogname = *argv; if (!optprogname) optprogname = ""; - for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1; + for (s = optprogname; *s; ++s) + if (*s == '/') optprogname = s + 1; } - c = subgetopt(argc,argv,opts); + c = subgetopt(argc, argv, opts); if (opterr) if (c == '?') { - char chp[2]; chp[0] = optproblem; chp[1] = '\n'; - buffer_puts(buffer_2,optprogname); + char chp[2]; + chp[0] = optproblem; + chp[1] = '\n'; + buffer_puts(buffer_2, optprogname); if (argv[optind] && (optind < argc)) - buffer_puts(buffer_2,": illegal option -- "); + buffer_puts(buffer_2, ": illegal option -- "); else - buffer_puts(buffer_2,": option requires an argument -- "); - buffer_put(buffer_2,chp,2); + buffer_puts(buffer_2, ": option requires an argument -- "); + buffer_put(buffer_2, chp, 2); buffer_flush(buffer_2); } return c; } -#define optpos subgetoptpos -#define optarg subgetoptarg +#define optpos subgetoptpos +#define optarg subgetoptarg #define optdone subgetoptdone int optind = 1; @@ -49,7 +53,7 @@ char *optarg = 0; int optproblem = 0; int optdone = SUBGETOPTDONE; -int subgetopt(int argc,char **argv,char *opts) +int subgetopt(int argc, char **argv, char *opts) { int c; char *s; @@ -87,7 +91,7 @@ int subgetopt(int argc,char **argv,char *opts) optproblem = c; return '?'; } - ++optind; + ++optind; } } return c; diff --git a/src/iopause.c b/src/iopause.c index 94a6fea..0baad2a 100644 --- a/src/iopause.c +++ b/src/iopause.c @@ -1,7 +1,9 @@ +#include "iopause.h" + #include <poll.h> -#include "taia.h" + #include "select.h" -#include "iopause.h" +#include "taia.h" /** @file iopause.c @@ -11,29 +13,28 @@ @return > 0 if successful */ -int iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *stamp) +int iopause(iopause_fd *x, unsigned int len, struct taia *deadline, struct taia *stamp) { struct taia t; int millisecs; double d; int i, r; - if (taia_less(deadline,stamp)) + if (taia_less(deadline, stamp)) millisecs = 0; else { t = *stamp; - taia_sub(&t,deadline,&t); + taia_sub(&t, deadline, &t); d = taia_approx(&t); if (d > 1000.0) d = 1000.0; millisecs = d * 1000.0 + 20.0; if (millisecs < 0) millisecs = 20.0; } - for (i = 0; i < len; ++i) - x[i].revents = 0; + for (i = 0; i < len; ++i) x[i].revents = 0; #ifdef IOPAUSE_POLL - r = poll(x,len,millisecs); + r = poll(x, len, millisecs); /* XXX: some kernels apparently need x[0] even if len is 0 */ /* XXX: how to handle EAGAIN? are kernels really this dumb? */ @@ -56,14 +57,14 @@ int iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *st if (fd >= 8 * sizeof(fd_set)) continue; /*XXX*/ if (fd >= nfds) nfds = fd + 1; - if (x[i].events & IOPAUSE_READ) FD_SET(fd,&rfds); - if (x[i].events & IOPAUSE_WRITE) FD_SET(fd,&wfds); + if (x[i].events & IOPAUSE_READ) FD_SET(fd, &rfds); + if (x[i].events & IOPAUSE_WRITE) FD_SET(fd, &wfds); } tv.tv_sec = millisecs / 1000; tv.tv_usec = 1000 * (millisecs % 1000); - r = select(nfds,&rfds,&wfds,(fd_set *) 0,&tv); + r = select(nfds, &rfds, &wfds, (fd_set *)0, &tv); if (r <= 0) return r; /* XXX: for EBADF, could seek out and destroy the bad descriptor */ @@ -74,9 +75,9 @@ int iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *st if (fd >= 8 * sizeof(fd_set)) continue; /*XXX*/ if (x[i].events & IOPAUSE_READ) - if (FD_ISSET(fd,&rfds)) x[i].revents |= IOPAUSE_READ; + if (FD_ISSET(fd, &rfds)) x[i].revents |= IOPAUSE_READ; if (x[i].events & IOPAUSE_WRITE) - if (FD_ISSET(fd,&wfds)) x[i].revents |= IOPAUSE_WRITE; + if (FD_ISSET(fd, &wfds)) x[i].revents |= IOPAUSE_WRITE; } #endif @@ -1,7 +1,7 @@ #include "fmt.h" +#include "ip.h" #include "scan.h" #include "str.h" -#include "ip.h" /** @file ip4.c @@ -18,12 +18,13 @@ @return int length of address (ok > 0) */ -unsigned int ip4_fmt(char *s,char ip[4]) +unsigned int ip4_fmt(char *s, char ip[4]) { unsigned int len; unsigned int i; len = 0; + // clang-format off i = fmt_ulong(s,(unsigned long) (unsigned char) ip[0]); len += i; if (s) s += i; if (s) { *s++ = '.'; } ++len; i = fmt_ulong(s,(unsigned long) (unsigned char) ip[1]); len += i; if (s) s += i; @@ -31,6 +32,7 @@ unsigned int ip4_fmt(char *s,char ip[4]) i = fmt_ulong(s,(unsigned long) (unsigned char) ip[2]); len += i; if (s) s += i; if (s) { *s++ = '.'; } ++len; i = fmt_ulong(s,(unsigned long) (unsigned char) ip[3]); len += i; if (s) s += i; + // clang-format on return len; } @@ -42,12 +44,13 @@ unsigned int ip4_fmt(char *s,char ip[4]) @return int length of address (ok > 0) */ -unsigned int ia4_fmt(char *s,char ip[4]) +unsigned int ia4_fmt(char *s, char ip[4]) { unsigned int i; unsigned int len; len = 0; + // clang-format off i = fmt_ulong(s,(unsigned long) ip[3]); len += i; if (s) s += i; i = fmt_str(s,"."); len += i; if (s) s += i; i = fmt_ulong(s,(unsigned long) ip[2]); len += i; if (s) s += i; @@ -56,6 +59,7 @@ unsigned int ia4_fmt(char *s,char ip[4]) i = fmt_str(s,"."); len += i; if (s) s += i; i = fmt_ulong(s,(unsigned long) ip[0]); len += i; if (s) s += i; i = fmt_str(s,".in-addr.arpa."); len += i; if (s) s += i; + // clang-format on return len; } @@ -67,14 +71,15 @@ unsigned int ia4_fmt(char *s,char ip[4]) @return int length of ip_address (ok > 0) */ -unsigned int ip4_scan(const char *s,char ip[4]) +unsigned int ip4_scan(const char *s, char ip[4]) { unsigned int i; unsigned int len; unsigned long u; - byte_zero(ip,4); + byte_zero(ip, 4); len = 0; + // clang-format off i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[0] = u; s += i; len += i; if (*s != '.') { return 0; } ++s; ++len; i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[1] = u; s += i; len += i; @@ -82,6 +87,7 @@ unsigned int ip4_scan(const char *s,char ip[4]) i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[2] = u; s += i; len += i; if (*s != '.') { return 0; } ++s; ++len; i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[3] = u; s += i; len += i; + // clang-format on return len; } @@ -93,12 +99,12 @@ unsigned int ip4_scan(const char *s,char ip[4]) @return int length of ip_address (ok > 0) */ -unsigned int ip4_scanbracket(const char *s,char ip[4]) +unsigned int ip4_scanbracket(const char *s, char ip[4]) { unsigned int len; if (*s != '[') return 0; - len = ip4_scan(s + 1,ip); + len = ip4_scan(s + 1, ip); if (!len) return 0; if (s[len + 1] != ']') return 0; return len + 2; @@ -113,17 +119,17 @@ unsigned int ip4_scanbracket(const char *s,char ip[4]) @return int length of ip6_address/ip */ -unsigned int ip4_cidr(char *s,char ip[4],unsigned long *plen) +unsigned int ip4_cidr(char *s, char ip[4], unsigned long *plen) { unsigned int j = 0; *plen = 32UL; - j = str_chr(s,'/'); + j = str_chr(s, '/'); if (s[j] == '/') { - s[j] = 0; - j = scan_ulong(s + j + 1,plen); - } - return ip4_scan((const char *)s,ip); + s[j] = 0; + j = scan_ulong(s + j + 1, plen); + } + return ip4_scan((const char *)s, ip); } /** @@ -134,28 +140,28 @@ unsigned int ip4_cidr(char *s,char ip[4],unsigned long *plen) @return n: number of bytes, if ok; -1: memory shortage; -2: input error */ -unsigned int ip4_bytestring(stralloc *ipstring,char ip[4],int prefix) +unsigned int ip4_bytestring(stralloc *ipstring, char ip[4], int prefix) { int i, j, n = 0; unsigned char number; - if (!stralloc_readyplus(ipstring,32)) return -1; - if (!stralloc_copys(ipstring,"")) return -1; + if (!stralloc_readyplus(ipstring, 32)) return -1; + if (!stralloc_copys(ipstring, "")) return -1; for (i = 0; i < 4; i++) { - number = (unsigned char) ip[i]; + number = (unsigned char)ip[i]; if (number > 255) return -2; for (j = 7; j >= 0; j--) { if (number & (1 << j)) { - n++; - if (!stralloc_cats(ipstring,"1")) return -1; + n++; + if (!stralloc_cats(ipstring, "1")) return -1; } else { - n++; - if (!stralloc_cats(ipstring,"0")) return -1; + n++; + if (!stralloc_cats(ipstring, "0")) return -1; } prefix--; - if (!prefix) goto DONE; + if (!prefix) goto DONE; } } @@ -1,7 +1,7 @@ -#include "fmt.h" #include "byte.h" -#include "scan.h" +#include "fmt.h" #include "ip.h" +#include "scan.h" #include "str.h" /** @@ -19,7 +19,7 @@ @return int length of address (ok > 0) */ -unsigned int ip6_fmt(char *s,char ip[16]) +unsigned int ip6_fmt(char *s, char ip[16]) { unsigned int len; unsigned int i; @@ -34,17 +34,15 @@ unsigned int ip6_fmt(char *s,char ip[16]) for (j = 0; j < 16; j += 2) { if (j == 12 && ip6_isv4mapped(ip)) { - len += ip4_fmt(s,ip+12); + len += ip4_fmt(s, ip + 12); break; } - temp = ((unsigned long) (unsigned char) ip[j] << 8) + - (unsigned long) (unsigned char) ip[j+1]; + temp = ((unsigned long)(unsigned char)ip[j] << 8) + (unsigned long)(unsigned char)ip[j + 1]; temp0 = 0; if (!compressing && j < 16) - temp0 = ((unsigned long) (unsigned char) ip[j+2] << 8) + - (unsigned long) (unsigned char) ip[j+3]; + temp0 = ((unsigned long)(unsigned char)ip[j + 2] << 8) + (unsigned long)(unsigned char)ip[j + 3]; if (temp == 0 && temp0 == 0 && !compressed) { if (!compressing) { @@ -61,7 +59,7 @@ unsigned int ip6_fmt(char *s,char ip[16]) if (s) *s++ = ':'; ++len; } - i = fmt_xlong(s,temp); + i = fmt_xlong(s, temp); len += i; if (s) s += i; if (j < 14) { @@ -70,7 +68,10 @@ unsigned int ip6_fmt(char *s,char ip[16]) } } } - if (compressing) { *s++ = ':'; ++len; } + if (compressing) { + *s++ = ':'; + ++len; + } return len; } @@ -83,7 +84,7 @@ unsigned int ip6_fmt(char *s,char ip[16]) @return int length of address (ok > 0) */ -unsigned int ip6_fmt_flat(char *s,char ip[16]) +unsigned int ip6_fmt_flat(char *s, char ip[16]) { int i; for (i = 0; i < 16; i++) { @@ -102,7 +103,7 @@ unsigned int ip6_fmt_flat(char *s,char ip[16]) @return int length of address */ -unsigned int ia6_fmt(char *s,char ip[16]) +unsigned int ia6_fmt(char *s, char ip[16]) { unsigned int i; unsigned int len; @@ -112,12 +113,22 @@ unsigned int ia6_fmt(char *s,char ip[16]) len = 0; for (j = 15; j >= 0; j--) { - i = fmt_str(s,&data[ip[j] & 0x0f]); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; - i = fmt_str(s,&data[ip[j] >> 4 & 0x0f]); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; + i = fmt_str(s, &data[ip[j] & 0x0f]); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; + i = fmt_str(s, &data[ip[j] >> 4 & 0x0f]); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; } - i = fmt_str(s,"ip6.arpa."); len += i; if (s) s += i; + i = fmt_str(s, "ip6.arpa."); + len += i; + if (s) s += i; return len; } @@ -131,7 +142,7 @@ unsigned int ia6_fmt(char *s,char ip[16]) @return int length of address (ok > 0) */ -unsigned int ip6_scan_flat(const char *s,char ip[16]) +unsigned int ip6_scan_flat(const char *s, char ip[16]) { int i, tmp; @@ -154,7 +165,7 @@ unsigned int ip6_scan_flat(const char *s,char ip[16]) @return int length of ip6_address/ip */ -unsigned int ip6_scan(const char *s,char ip[16]) +unsigned int ip6_scan(const char *s, char ip[16]) { unsigned int i, j; unsigned int len = 0; @@ -164,61 +175,63 @@ unsigned int ip6_scan(const char *s,char ip[16]) int prefixlen = 0; int suffixlen = 0; - /* Always return IPv4 as IPv4-mapped IPv6 address */ - if ((i = ip4_scan(s,ip+12))) { - for (len = 0; len < 12; ++len) - ip[len] = V4mappedprefix[len]; + /* Always return IPv4 as IPv4-mapped IPv6 address */ + if ((i = ip4_scan(s, ip + 12))) { + for (len = 0; len < 12; ++len) ip[len] = V4mappedprefix[len]; return i; - if (byte_equal(ip+12,4,V4localnet)) { - byte_copy(ip,16,V6localnet); + if (byte_equal(ip + 12, 4, V4localnet)) { + byte_copy(ip, 16, V6localnet); return 16; } } - byte_zero(ip,16); + byte_zero(ip, 16); for (;;) { if (*s == ':') { len++; - if (s[1] == ':') { /* Found "::", skip to part 2 */ - s += 2; len++; + if (s[1] == ':') { /* Found "::", skip to part 2 */ + s += 2; + len++; break; } s++; } - i = scan_xlong((char *)s,&u); + i = scan_xlong((char *)s, &u); if (!i) return 0; if (prefixlen == 12 && s[i] == '.') { /* the last 4 bytes may be written as IPv4 address */ - i = ip4_scan(s,ip+12); - if (i) - return i+len; + i = ip4_scan(s, ip + 12); + if (i) + return i + len; else return 0; } ip[prefixlen++] = (u >> 8); ip[prefixlen++] = (u & 255); - s += i; len += i; + s += i; + len += i; if (prefixlen == 16) return len; } -/* part 2, after "::" */ + /* part 2, after "::" */ for (;;) { if (*s == ':') { if (suffixlen == 0) break; s++; len++; - } else if (suffixlen != 0) break; + } else if (suffixlen != 0) + break; - i = scan_xlong((char *)s,&u); + i = scan_xlong((char *)s, &u); if (!i) { len--; break; } if (suffixlen + prefixlen <= 12 && s[i] == '.') { - j = ip4_scan(s,suffix+suffixlen); + j = ip4_scan(s, suffix + suffixlen); if (j) { suffixlen += 4; len += j; @@ -229,12 +242,12 @@ unsigned int ip6_scan(const char *s,char ip[16]) suffix[suffixlen++] = (u >> 8); suffix[suffixlen++] = (u & 255); - s += i; len += i; + s += i; + len += i; if (prefixlen + suffixlen == 16) break; } - for (i = 0; i < suffixlen; i++) - ip[16 - suffixlen + i] = suffix[i]; + for (i = 0; i < suffixlen; i++) ip[16 - suffixlen + i] = suffix[i]; return len; } @@ -247,12 +260,12 @@ unsigned int ip6_scan(const char *s,char ip[16]) @return int length of ip_address (ok > 0) */ -unsigned int ip6_scanbracket(const char *s,char ip[16]) +unsigned int ip6_scanbracket(const char *s, char ip[16]) { unsigned int len; if (*s != '[') return 0; - len = ip6_scan(s + 1,ip); + len = ip6_scan(s + 1, ip); if (!len) return 0; if (s[len + 1] != ']') return 0; return len + 2; @@ -267,24 +280,27 @@ unsigned int ip6_scanbracket(const char *s,char ip[16]) @return int length of ip6_address/ip */ -unsigned int ip6_ifscan(char *s,char ip[16],stralloc *ifname) +unsigned int ip6_ifscan(char *s, char ip[16], stralloc *ifname) { - int i; + int i; int j = 0; - int k = 0; - if (!stralloc_copys(ifname,"0")) return 0; - - if ((j = str_chr(s,'%'))) { - if ((i = str_chr(s+j+1,' '))) k = i; - else if ((i = str_chr(s+j+1,'\n'))) k = i; - else if ((i = str_chr(s+j+1,'\t'))) k = i; - if (k) s[j+k+1] = '\0'; /* input might contain trailing chars */ - if (!stralloc_copys(ifname,s+j+1)) return 0; + int k = 0; + if (!stralloc_copys(ifname, "0")) return 0; + + if ((j = str_chr(s, '%'))) { + if ((i = str_chr(s + j + 1, ' '))) + k = i; + else if ((i = str_chr(s + j + 1, '\n'))) + k = i; + else if ((i = str_chr(s + j + 1, '\t'))) + k = i; + if (k) s[j + k + 1] = '\0'; /* input might contain trailing chars */ + if (!stralloc_copys(ifname, s + j + 1)) return 0; s[j] = 0; } if (!stralloc_0(ifname)) return 0; - return ip6_scan(s,ip); + return ip6_scan(s, ip); } /** @@ -296,17 +312,17 @@ unsigned int ip6_ifscan(char *s,char ip[16],stralloc *ifname) @return int length of ip6_address/ip */ -unsigned int ip6_cidr(char *s,char ip[16],unsigned long *plen) +unsigned int ip6_cidr(char *s, char ip[16], unsigned long *plen) { unsigned int j = 0; *plen = 128UL; - j = str_chr(s,'/'); + j = str_chr(s, '/'); if (s[j] == '/') { - s[j] = 0; - j = scan_ulong(s+j+1,plen); + s[j] = 0; + j = scan_ulong(s + j + 1, plen); } - return ip6_scan((const char *)s,ip); + return ip6_scan((const char *)s, ip); } /** @@ -317,39 +333,39 @@ unsigned int ip6_cidr(char *s,char ip[16],unsigned long *plen) @return n: number of bytes, if ok; -1: memory shortage */ -unsigned int ip6_bytestring(stralloc *ipstring,char ip[16],int prefix) +unsigned int ip6_bytestring(stralloc *ipstring, char ip[16], int prefix) { int i, j, n = 0; unsigned char lowbyte, highbyte; - if (!stralloc_readyplus(ipstring,128)) return -1; - if (!stralloc_copys(ipstring,"")) return -1; + if (!stralloc_readyplus(ipstring, 128)) return -1; + if (!stralloc_copys(ipstring, "")) return -1; for (i = 0; i < 16; i++) { - lowbyte = (unsigned char) (ip[i]) & 0x0f; - highbyte = (unsigned char) (ip[i] >> 4) & 0x0f; + lowbyte = (unsigned char)(ip[i]) & 0x0f; + highbyte = (unsigned char)(ip[i] >> 4) & 0x0f; for (j = 3; j >= 0; j--) { if (highbyte & (1 << j)) { - n++; - if (!stralloc_cats(ipstring,"1")) return -1; + n++; + if (!stralloc_cats(ipstring, "1")) return -1; } else { - n++; - if (!stralloc_cats(ipstring,"0")) return -1; + n++; + if (!stralloc_cats(ipstring, "0")) return -1; } prefix--; - if (!prefix) goto DONE; + if (!prefix) goto DONE; } for (j = 3; j >= 0; j--) { if (lowbyte & (1 << j)) { - n++; - if (!stralloc_cats(ipstring,"1")) return -1; + n++; + if (!stralloc_cats(ipstring, "1")) return -1; } else { - n++; - if (!stralloc_cats(ipstring,"0")) return -1; + n++; + if (!stralloc_cats(ipstring, "0")) return -1; } prefix--; - if (!prefix) goto DONE; + if (!prefix) goto DONE; } } @@ -1,9 +1,10 @@ -#include <unistd.h> -#include <sys/types.h> -#include <sys/file.h> -#include <fcntl.h> #include "lock.h" +#include <fcntl.h> +#include <sys/file.h> +#include <sys/types.h> +#include <unistd.h> + /** @file lock.c @author djb @@ -12,11 +13,29 @@ */ #ifdef HASFLOCK -int lock_ex(int fd) { return flock(fd,LOCK_EX); } -int lock_exnb(int fd) { return flock(fd,LOCK_EX | LOCK_NB); } -int lock_un(int fd) { return flock(fd,LOCK_UN); } +int lock_ex(int fd) +{ + return flock(fd, LOCK_EX); +} +int lock_exnb(int fd) +{ + return flock(fd, LOCK_EX | LOCK_NB); +} +int lock_un(int fd) +{ + return flock(fd, LOCK_UN); +} #else -int lock_ex(int fd) { return lockf(fd,1,0); } -int lock_exnb(int fd) { return lockf(fd,2,0); } -int lock_un(int fd) { return lockf(fd,0,0); } +int lock_ex(int fd) +{ + return lockf(fd, 1, 0); +} +int lock_exnb(int fd) +{ + return lockf(fd, 2, 0); +} +int lock_un(int fd) +{ + return lockf(fd, 0, 0); +} #endif diff --git a/src/logmsg.c b/src/logmsg.c index 3f079f4..e846275 100644 --- a/src/logmsg.c +++ b/src/logmsg.c @@ -1,11 +1,14 @@ +#include "logmsg.h" + #include <unistd.h> -#include <stdarg.h> + #include <errno.h> +#include <stdarg.h> + #include "buffer.h" #include "fmt.h" -#include "str.h" -#include "stralloc.h" -#include "logmsg.h" +#include "str.h" +#include "stralloc.h" /** @file logmsg.c @@ -19,79 +22,82 @@ char *build_log_msg(const char *x[]) { stralloc sa = {0}; - stralloc_copys(&sa,""); + stralloc_copys(&sa, ""); + + while (*x) { + if (!stralloc_cats(&sa, *x++)) err_sys(WHO, errno); + } /* concatenate *x */ - while(*x) { if (!stralloc_cats(&sa,*x++)) err_sys(WHO,errno); } /* concatenate *x */ - - if (!stralloc_0(&sa)) err_sys(WHO,errno); - return(sa.s); + if (!stralloc_0(&sa)) err_sys(WHO, errno); + return (sa.s); } -void logmsg(const char *who,int ecode,unsigned int class,const char *msg) +void logmsg(const char *who, int ecode, unsigned int class, const char *msg) { char strnum[FMT_ULONG]; char *codestr = ""; char *classstr = ""; char *errmsg = ""; - errno = 0; /* re-initialize errno, value is in 'code' now */ + errno = 0; /* re-initialize errno, value is in 'code' now */ -/* Part 1: obtain the (error) code -- perhaps received from OS */ + /* Part 1: obtain the (error) code -- perhaps received from OS */ if (ecode != 0) { codestr = ""; - if (ecode < 0) { // check for negative error codes - ecode = (ecode^-1) + 1; + if (ecode < 0) { // check for negative error codes + ecode = (ecode ^ -1) + 1; codestr = "-"; } - strnum[fmt_ulong(strnum,ecode)] = 0; /* format for output */ + strnum[fmt_ulong(strnum, ecode)] = 0; /* format for output */ char *temp = strnum; - codestr = str_cat(codestr,temp); + codestr = str_cat(codestr, temp); } -/* Part 2: behavioral on error */ + /* Part 2: behavioral on error */ switch (class) { - case ERROR: classstr = "error: "; break; // info + exit - case FATAL: classstr = "fatal: "; break; // info + exit - case DROP: classstr = "drop: "; break; // info + next call/iteration - case ALERT: classstr = "alert: "; break; // info + next statement - case WARN: classstr = "warning: "; break; // info + next statement - case INFO: classstr = "info: "; break; // info + continue - case SYNTAX: classstr = "syntax: "; break; // info + exit - case USAGE: classstr = "usage: "; break; // info + exit - case TEMP: classstr = "temp: "; break; // info + exit - case CAT: classstr = ""; break; // info w/o \n - default: - class = LOG; classstr = ""; break; // custom info + continue + case ERROR: classstr = "error: "; break; // info + exit + case FATAL: classstr = "fatal: "; break; // info + exit + case DROP: classstr = "drop: "; break; // info + next call/iteration + case ALERT: classstr = "alert: "; break; // info + next statement + case WARN: classstr = "warning: "; break; // info + next statement + case INFO: classstr = "info: "; break; // info + continue + case SYNTAX: classstr = "syntax: "; break; // info + exit + case USAGE: classstr = "usage: "; break; // info + exit + case TEMP: classstr = "temp: "; break; // info + exit + case CAT: classstr = ""; break; // info w/o \n + default: + class = LOG; + classstr = ""; + break; // custom info + continue } -/* Part 3: get system error message */ + /* Part 3: get system error message */ - if (class == FATAL || class == DROP) - errmsg = error_str(errno); + if (class == FATAL || class == DROP) errmsg = error_str(errno); -/* Part 4: construct log message: Source: Class (Ecode) Message: Errmsg */ + /* Part 4: construct log message: Source: Class (Ecode) Message: Errmsg */ - buffer_puts(buffer_2,who); - buffer_puts(buffer_2,": "); - buffer_puts(buffer_2,classstr); + buffer_puts(buffer_2, who); + buffer_puts(buffer_2, ": "); + buffer_puts(buffer_2, classstr); if (class == FATAL || class == DROP || class == ERROR) { - buffer_puts(buffer_2,"("); - buffer_puts(buffer_2,codestr); - buffer_puts(buffer_2,") "); + buffer_puts(buffer_2, "("); + buffer_puts(buffer_2, codestr); + buffer_puts(buffer_2, ") "); } - buffer_puts(buffer_2,msg); - if (errno) { - buffer_puts(buffer_2,": "); - buffer_puts(buffer_2,errmsg); + buffer_puts(buffer_2, msg); + if (errno) { + buffer_puts(buffer_2, ": "); + buffer_puts(buffer_2, errmsg); } if (class != CAT) { - buffer_puts(buffer_2,"\n"); + buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); } - if (class == USAGE) _exit(USAGE); - if (class == SYNTAX) _exit(SYNTAX); + if (class == USAGE) _exit(USAGE); + if (class == SYNTAX) _exit(SYNTAX); if (class == FATAL || class == DROP || class == ERROR) _exit(ecode); } diff --git a/src/ndelay.c b/src/ndelay.c index f4b5eb8..8f6b5a9 100644 --- a/src/ndelay.c +++ b/src/ndelay.c @@ -1,7 +1,8 @@ -#include <sys/types.h> -#include <fcntl.h> #include "ndelay.h" +#include <fcntl.h> +#include <sys/types.h> + /** @file ndelay.c @author djb @@ -10,15 +11,15 @@ */ #ifndef O_NONBLOCK -#define O_NONBLOCK O_NDELAY + #define O_NONBLOCK O_NDELAY #endif int ndelay_on(int fd) { - return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NONBLOCK); + return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); } int ndelay_off(int fd) { - return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NONBLOCK); + return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); } @@ -1,7 +1,8 @@ -#include <sys/types.h> -#include <fcntl.h> #include "open.h" +#include <fcntl.h> +#include <sys/types.h> + /** @file open.c @author djb @@ -10,16 +11,26 @@ */ int open_append(const char *fn) -{ return open(fn,O_WRONLY | O_NDELAY | O_APPEND | O_CREAT,0600); } +{ + return open(fn, O_WRONLY | O_NDELAY | O_APPEND | O_CREAT, 0600); +} int open_excl(const char *fn) -{ return open(fn,O_WRONLY | O_EXCL | O_CREAT,0644); } +{ + return open(fn, O_WRONLY | O_EXCL | O_CREAT, 0644); +} int open_read(const char *fn) -{ return open(fn,O_RDONLY | O_NDELAY); } +{ + return open(fn, O_RDONLY | O_NDELAY); +} int open_trunc(const char *fn) -{ return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644); } +{ + return open(fn, O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0644); +} int open_write(const char *fn) -{ return open(fn,O_WRONLY | O_NDELAY); } +{ + return open(fn, O_WRONLY | O_NDELAY); +} diff --git a/src/pathexec.c b/src/pathexec.c index 2c1e7d1..343b4ba 100644 --- a/src/pathexec.c +++ b/src/pathexec.c @@ -1,11 +1,13 @@ +#include "pathexec.h" + #include <unistd.h> + #include "alloc.h" -#include "error.h" -#include "stralloc.h" -#include "str.h" #include "byte.h" #include "env.h" -#include "pathexec.h" +#include "error.h" +#include "str.h" +#include "stralloc.h" /** @file pathexec.c @@ -17,25 +19,25 @@ static stralloc plus; static stralloc tmp; -int pathexec_env(const char *s,const char *t) +int pathexec_env(const char *s, const char *t) { if (!s) return 1; - if (!stralloc_copys(&tmp,s)) return 0; + if (!stralloc_copys(&tmp, s)) return 0; if (t) { - if (!stralloc_cats(&tmp,"=")) return 0; - if (!stralloc_cats(&tmp,t)) return 0; + if (!stralloc_cats(&tmp, "=")) return 0; + if (!stralloc_cats(&tmp, t)) return 0; } if (!stralloc_0(&tmp)) return 0; - return stralloc_cat(&plus,&tmp); + return stralloc_cat(&plus, &tmp); } int pathexec_multienv(stralloc *sa) { if (!sa) return 1; - return stralloc_cat(&plus,sa); + return stralloc_cat(&plus, sa); } -void pathexec(char * const *argv) +void pathexec(char *const *argv) { char **e; unsigned int elen; @@ -44,51 +46,47 @@ void pathexec(char * const *argv) unsigned int split; unsigned int t; - if (!stralloc_cats(&plus,"")) return; + if (!stralloc_cats(&plus, "")) return; elen = 0; - for (i = 0; environ[i]; ++i) - ++elen; + for (i = 0; environ[i]; ++i) ++elen; for (i = 0; i < plus.len; ++i) - if (!plus.s[i]) - ++elen; + if (!plus.s[i]) ++elen; - e = (char **) alloc((elen + 1) * sizeof(char *)); + e = (char **)alloc((elen + 1) * sizeof(char *)); if (!e) return; elen = 0; - for (i = 0; environ[i]; ++i) - e[elen++] = environ[i]; + for (i = 0; environ[i]; ++i) e[elen++] = environ[i]; j = 0; for (i = 0; i < plus.len; ++i) if (!plus.s[i]) { - split = str_chr(plus.s + j,'='); + split = str_chr(plus.s + j, '='); for (t = 0; t < elen; ++t) - if (byte_equal(plus.s + j,split,e[t])) + if (byte_equal(plus.s + j, split, e[t])) if (e[t][split] == '=') { --elen; e[t] = e[elen]; break; } - if (plus.s[j + split]) - e[elen++] = plus.s + j; + if (plus.s[j + split]) e[elen++] = plus.s + j; j = i + 1; } e[elen] = 0; - pathexec_run(*argv,argv,e); + pathexec_run(*argv, argv, e); alloc_free(e); } -void pathexec_run(const char *file,char *const *argv,char *const *envp) +void pathexec_run(const char *file, char *const *argv, char *const *envp) { char *path; unsigned int split; int savederrno; - if (file[str_chr(file,'/')]) { - execve(file,argv,envp); + if (file[str_chr(file, '/')]) { + execve(file, argv, envp); return; } @@ -97,15 +95,15 @@ void pathexec_run(const char *file,char *const *argv,char *const *envp) savederrno = 0; for (;;) { - split = str_chr(path,':'); - if (!stralloc_copyb(&tmp,path,split)) return; + split = str_chr(path, ':'); + if (!stralloc_copyb(&tmp, path, split)) return; if (!split) - if (!stralloc_cats(&tmp,".")) return; - if (!stralloc_cats(&tmp,"/")) return; - if (!stralloc_cats(&tmp,file)) return; + if (!stralloc_cats(&tmp, ".")) return; + if (!stralloc_cats(&tmp, "/")) return; + if (!stralloc_cats(&tmp, file)) return; if (!stralloc_0(&tmp)) return; - execve(tmp.s,argv,envp); + execve(tmp.s, argv, envp); if (errno != ENOENT) { savederrno = errno; if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) return; @@ -1,6 +1,6 @@ +#include <grp.h> #include <sys/types.h> #include <unistd.h> -#include <grp.h> //#include "hasshsgr.h" #include "prot.h" @@ -15,17 +15,17 @@ int prot_gid(int gid) { -//#ifdef HASSHORTSETGROUPS -// short x[2]; -// x[0] = gid; x[1] = 73; /* catch errors */ -// if (setgroups(1,x) == -1) return -1; -//#else - if (setgroups(1,(gid_t *)&gid) == -1) return -1; -//#endif + //#ifdef HASSHORTSETGROUPS + // short x[2]; + // x[0] = gid; x[1] = 73; /* catch errors */ + // if (setgroups(1,x) == -1) return -1; + //#else + if (setgroups(1, (gid_t *)&gid) == -1) return -1; + //#endif return setgid(gid); /* _should_ be redundant, but on some systems it isn't */ } -int prot_uid(int uid) +int prot_uid(int uid) { return setuid(uid); } diff --git a/src/readclose.c b/src/readclose.c index b0bce7e..2804965 100644 --- a/src/readclose.c +++ b/src/readclose.c @@ -1,7 +1,9 @@ +#include "readclose.h" + #include <unistd.h> -#include "open.h" + #include "error.h" -#include "readclose.h" +#include "open.h" /** @file readclose.c @@ -12,32 +14,42 @@ 'readclose' was introduced here initial. */ -int readclose_append(int fd,stralloc *sa,unsigned int bufsize) +int readclose_append(int fd, stralloc *sa, unsigned int bufsize) { int r; for (;;) { - if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; } - r = read(fd,sa->s + sa->len,bufsize); - if (r == -1) if (errno == EINTR) continue; - if (r <= 0) { close(fd); return r; } + if (!stralloc_readyplus(sa, bufsize)) { + close(fd); + return -1; + } + r = read(fd, sa->s + sa->len, bufsize); + if (r == -1) + if (errno == EINTR) continue; + if (r <= 0) { + close(fd); + return r; + } sa->len += r; } } -int readclose(int fd,stralloc *sa,unsigned int bufsize) +int readclose(int fd, stralloc *sa, unsigned int bufsize) { - if (!stralloc_copys(sa,"")) { close(fd); return -1; } - return readclose_append(fd,sa,bufsize); + if (!stralloc_copys(sa, "")) { + close(fd); + return -1; + } + return readclose_append(fd, sa, bufsize); } -int openreadclose(const char *fn,stralloc *sa,unsigned int bufsize) +int openreadclose(const char *fn, stralloc *sa, unsigned int bufsize) { int fd; - fd = open_read((char *) fn); + fd = open_read((char *)fn); if (fd == -1) { if (errno == ENOENT) return 0; return -1; } - if (readclose(fd,sa,bufsize) == -1) return -1; + if (readclose(fd, sa, bufsize) == -1) return -1; return 1; } @@ -7,24 +7,24 @@ @brief scanning/conversion of strings to different variable types */ -static long int fromhex(unsigned char c) +static long int fromhex(unsigned char c) { - if (c>='0' && c<='9') - return c-'0'; - else if (c>='A' && c<='F') - return c-'A'+10; - else if (c>='a' && c<='f') - return c-'a'+10; + if (c >= '0' && c <= '9') + return c - '0'; + else if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + return c - 'a' + 10; return -1; } -unsigned int scan_0x(register const char *s,register unsigned int *u) +unsigned int scan_0x(register const char *s, register unsigned int *u) { register unsigned int pos = 0; register unsigned long result = 0; register long int c; - while ((c = fromhex((unsigned char) (s[pos]))) >= 0) { + while ((c = fromhex((unsigned char)(s[pos]))) >= 0) { result = (result << 4) + c; ++pos; } @@ -32,89 +32,99 @@ unsigned int scan_0x(register const char *s,register unsigned int *u) return pos; } -unsigned int scan_8long(register const char *s,register unsigned long *u) +unsigned int scan_8long(register const char *s, register unsigned long *u) { register unsigned int pos = 0; register unsigned long result = 0; register unsigned long c; - while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 8) { + while ((c = (unsigned long)(unsigned char)(s[pos] - '0')) < 8) { result = result * 8 + c; - ++pos; + ++pos; } *u = result; return pos; } -unsigned int scan_uint(register const char *s,register unsigned int *u) +unsigned int scan_uint(register const char *s, register unsigned int *u) { - register unsigned int pos; + register unsigned int pos; unsigned long result; - pos = scan_ulong(s,&result); - *u = result; + pos = scan_ulong(s, &result); + *u = result; return pos; } -unsigned int scan_plusminus(register const char *s,register int *sign) +unsigned int scan_plusminus(register const char *s, register int *sign) { - if (*s == '+') { *sign = 1; return 1; } - if (*s == '-') { *sign = -1; return 1; } - *sign = 1; return 0; + if (*s == '+') { + *sign = 1; + return 1; + } + if (*s == '-') { + *sign = -1; + return 1; + } + *sign = 1; + return 0; } -unsigned int scan_long(register const char *s,register long *i) +unsigned int scan_long(register const char *s, register long *i) { - int sign; - unsigned long u; + int sign; + unsigned long u; register unsigned int len; - len = scan_plusminus(s,&sign); s += len; - len += scan_ulong(s,&u); - if (sign < 0) *i = -u; else *i = u; + len = scan_plusminus(s, &sign); + s += len; + len += scan_ulong(s, &u); + if (sign < 0) + *i = -u; + else + *i = u; return len; } -unsigned int scan_ulong(register const char *s,register unsigned long *u) +unsigned int scan_ulong(register const char *s, register unsigned long *u) { register unsigned int pos = 0; register unsigned long result = 0; register unsigned long c; - while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) { + while ((c = (unsigned long)(unsigned char)(s[pos] - '0')) < 10) { result = result * 10 + c; - ++pos; + ++pos; } *u = result; return pos; } -unsigned int scan_xlong(const char *s,unsigned long *u) +unsigned int scan_xlong(const char *s, unsigned long *u) { register const char *t = s; register int l = 0; register unsigned char c; while ((c = fromhex(*t)) < 16) { - l = (l<<4)+c; + l = (l << 4) + c; ++t; } - *u=l; - return t-s; + *u = l; + return t - s; } -unsigned int scan_xint(const char *s,unsigned int *i) +unsigned int scan_xint(const char *s, unsigned int *i) { register const char *t = s; register unsigned int l = 0; register unsigned char c; - while ((l >> (sizeof(l)*8 - 4)) == 0 - && (c = (unsigned char)fromhex((unsigned char)*t))<16) { - l= (l << 4) + c; + while ((l >> (sizeof(l) * 8 - 4)) == 0 && (c = (unsigned char)fromhex((unsigned char)*t)) < 16) { + l = (l << 4) + c; ++t; } *i = l; - return (unsigned int)(t-s); + return (unsigned int)(t - s); } @@ -1,6 +1,7 @@ -#include <sys/types.h> #include "seek.h" +#include <sys/types.h> + /** @file seek.c @author djb @@ -8,23 +9,33 @@ @brief seek in an open file descritor */ -off_t lseek(int fd,off_t offset,int whence); +off_t lseek(int fd, off_t offset, int whence); int ftruncate(int fd, off_t length); #define CUR 1 /* sigh */ seek_pos seek_cur(int fd) -{ return lseek(fd,(off_t) 0,CUR); } +{ + return lseek(fd, (off_t)0, CUR); +} #define END 2 /* sigh */ int seek_end(int fd) -{ if (lseek(fd,(off_t) 0,END) == -1) return -1; return 0; } +{ + if (lseek(fd, (off_t)0, END) == -1) return -1; + return 0; +} #define SET 0 /* sigh */ -int seek_set(int fd,seek_pos pos) -{ if (lseek(fd,(off_t) pos,SET) == -1) return -1; return 0; } +int seek_set(int fd, seek_pos pos) +{ + if (lseek(fd, (off_t)pos, SET) == -1) return -1; + return 0; +} -int seek_trunc(int fd,seek_pos pos) -{ return ftruncate(fd,(off_t) pos); } +int seek_trunc(int fd, seek_pos pos) +{ + return ftruncate(fd, (off_t)pos); +} @@ -1,6 +1,7 @@ -#include <signal.h> #include "sig.h" +#include <signal.h> + /** @file sig.c @author djb @@ -8,101 +9,155 @@ @brief signal handling functions */ -void sig_alarmblock() { sig_block(SIGALRM); } -void sig_alarmunblock() { sig_unblock(SIGALRM); } -void sig_alarmcatch(f) void (*f)(); { sig_catch(SIGALRM,f); } -void sig_alarmdefault() { sig_catch(SIGALRM,SIG_DFL); } +void sig_alarmblock() +{ + sig_block(SIGALRM); +} +void sig_alarmunblock() +{ + sig_unblock(SIGALRM); +} +void sig_alarmcatch(f) void (*f)(); +{ + sig_catch(SIGALRM, f); +} +void sig_alarmdefault() +{ + sig_catch(SIGALRM, SIG_DFL); +} int sig_alarm = SIGALRM; -void sig_block(int sig) +void sig_block(int sig) { sigset_t ss; sigemptyset(&ss); - sigaddset(&ss,sig); - sigprocmask(SIG_BLOCK,&ss,(sigset_t *) 0); + sigaddset(&ss, sig); + sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0); } -void sig_unblock(int sig) +void sig_unblock(int sig) { sigset_t ss; sigemptyset(&ss); - sigaddset(&ss,sig); - sigprocmask(SIG_UNBLOCK,&ss,(sigset_t *) 0); + sigaddset(&ss, sig); + sigprocmask(SIG_UNBLOCK, &ss, (sigset_t *)0); } -void sig_blocknone() +void sig_blocknone() { sigset_t ss; sigemptyset(&ss); - sigprocmask(SIG_SETMASK,&ss,(sigset_t *) 0); + sigprocmask(SIG_SETMASK, &ss, (sigset_t *)0); } -void sig_catch(int sig,void (*f)()) +void sig_catch(int sig, void (*f)()) { struct sigaction sa; sa.sa_handler = f; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); - sigaction(sig,&sa,(struct sigaction *) 0); + sigaction(sig, &sa, (struct sigaction *)0); } -void sig_pause() +void sig_pause() { sigset_t ss; sigemptyset(&ss); sigsuspend(&ss); } -void sig_pipeignore() { sig_catch(SIGPIPE,SIG_IGN); } -void sig_pipedefault() { sig_catch(SIGPIPE,SIG_DFL); } +void sig_pipeignore() +{ + sig_catch(SIGPIPE, SIG_IGN); +} +void sig_pipedefault() +{ + sig_catch(SIGPIPE, SIG_DFL); +} int sig_pipe = SIGPIPE; -void sig_childblock() { sig_block(SIGCHLD); } -void sig_childunblock() { sig_unblock(SIGCHLD); } -void sig_childcatch(f) void (*f)(); { sig_catch(SIGCHLD,f); } -void sig_childdefault() { sig_catch(SIGCHLD,SIG_DFL); } +void sig_childblock() +{ + sig_block(SIGCHLD); +} +void sig_childunblock() +{ + sig_unblock(SIGCHLD); +} +void sig_childcatch(f) void (*f)(); +{ + sig_catch(SIGCHLD, f); +} +void sig_childdefault() +{ + sig_catch(SIGCHLD, SIG_DFL); +} int sig_child = SIGCHLD; -void sig_hangupblock() { sig_block(SIGHUP); } -void sig_hangupunblock() { sig_unblock(SIGHUP); } -void sig_hangupcatch(f) void (*f)(); { sig_catch(SIGHUP,f); } -void sig_hangupdefault() { sig_catch(SIGHUP,SIG_DFL); } +void sig_hangupblock() +{ + sig_block(SIGHUP); +} +void sig_hangupunblock() +{ + sig_unblock(SIGHUP); +} +void sig_hangupcatch(f) void (*f)(); +{ + sig_catch(SIGHUP, f); +} +void sig_hangupdefault() +{ + sig_catch(SIGHUP, SIG_DFL); +} int sig_hangup = SIGHUP; -void sig_termblock() { sig_block(SIGTERM); } -void sig_termunblock() { sig_unblock(SIGTERM); } -void sig_termcatch(f) void (*f)(); { sig_catch(SIGTERM,f); } -void sig_termdefault() { sig_catch(SIGTERM,SIG_DFL); } +void sig_termblock() +{ + sig_block(SIGTERM); +} +void sig_termunblock() +{ + sig_unblock(SIGTERM); +} +void sig_termcatch(f) void (*f)(); +{ + sig_catch(SIGTERM, f); +} +void sig_termdefault() +{ + sig_catch(SIGTERM, SIG_DFL); +} int sig_term = SIGTERM; -void sig_bugcatch(void (*f)()) +void sig_bugcatch(void (*f)()) { - sig_catch(SIGILL,f); - sig_catch(SIGABRT,f); - sig_catch(SIGFPE,f); - sig_catch(SIGBUS,f); - sig_catch(SIGSEGV,f); + sig_catch(SIGILL, f); + sig_catch(SIGABRT, f); + sig_catch(SIGFPE, f); + sig_catch(SIGBUS, f); + sig_catch(SIGSEGV, f); #ifdef SIGSYS - sig_catch(SIGSYS,f); + sig_catch(SIGSYS, f); #endif #ifdef SIGEMT - sig_catch(SIGEMT,f); + sig_catch(SIGEMT, f); #endif } void (*sig_defaulthandler)() = SIG_DFL; -void sig_miscignore() +void sig_miscignore() { - sig_catch(SIGVTALRM,SIG_IGN); - sig_catch(SIGPROF,SIG_IGN); - sig_catch(SIGQUIT,SIG_IGN); - sig_catch(SIGINT,SIG_IGN); - sig_catch(SIGHUP,SIG_IGN); + sig_catch(SIGVTALRM, SIG_IGN); + sig_catch(SIGPROF, SIG_IGN); + sig_catch(SIGQUIT, SIG_IGN); + sig_catch(SIGINT, SIG_IGN); + sig_catch(SIGHUP, SIG_IGN); #ifdef SIGXCPU - sig_catch(SIGXCPU,SIG_IGN); + sig_catch(SIGXCPU, SIG_IGN); #endif #ifdef SIGXFSZ - sig_catch(SIGXFSZ,SIG_IGN); + sig_catch(SIGXFSZ, SIG_IGN); #endif } void (*sig_ignorehandler)() = SIG_IGN; diff --git a/src/socket_bind.c b/src/socket_bind.c index b942e20..0376265 100644 --- a/src/socket_bind.c +++ b/src/socket_bind.c @@ -1,10 +1,11 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include "byte.h" -#include "socket_if.h" #include "ip.h" +#include "socket_if.h" /** @file socket_bind.c @@ -13,67 +14,65 @@ @brief binding a socket to a local resource */ -int socket_bind4(int s,const char ip[4],uint16 port) +int socket_bind4(int s, const char ip[4], uint16 port) { struct sockaddr_in sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin_family = AF_INET; - uint16_pack_big((char *)&sa.sin_port,port); - byte_copy((char *)&sa.sin_addr,4,ip); + uint16_pack_big((char *)&sa.sin_port, port); + byte_copy((char *)&sa.sin_addr, 4, ip); - return bind(s,(struct sockaddr *)&sa,sizeof(sa)); + return bind(s, (struct sockaddr *)&sa, sizeof(sa)); } -int socket_bind4_reuse(int s,const char ip[4],uint16 port) +int socket_bind4_reuse(int s, const char ip[4], uint16 port) { int opt = 1; - setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); - return socket_bind4(s,ip,port); + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + return socket_bind4(s, ip, port); } /* seems not to be used here -- djbdns requires it */ -void socket_tryreservein(int s,int size) +void socket_tryreservein(int s, int size) { while (size >= 1024) { - if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size)) == 0) return; + if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) == 0) return; size -= (size >> 5); } } -int socket_bind6(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_bind6(int s, const char ip[16], uint16 port, uint32 scope_id) { struct sockaddr_in6 sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin6_family = AF_INET6; - uint16_pack_big((char *)&sa.sin6_port,port); -/* implicit: sa.sin6_flowinfo = 0; */ - byte_copy((char *)&sa.sin6_addr,16,ip); + uint16_pack_big((char *)&sa.sin6_port, port); + /* implicit: sa.sin6_flowinfo = 0; */ + byte_copy((char *)&sa.sin6_addr, 16, ip); sa.sin6_scope_id = scope_id; - return bind(s,(struct sockaddr *)&sa,sizeof(sa)); + return bind(s, (struct sockaddr *)&sa, sizeof(sa)); } -int socket_bind6_reuse(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_bind6_reuse(int s, const char ip[16], uint16 port, uint32 scope_id) { int opt = 1; - setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); - return socket_bind6(s,ip,port,scope_id); + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + return socket_bind6(s, ip, port, scope_id); } -int socket_bind(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_bind(int s, const char ip[16], uint16 port, uint32 scope_id) { - if (ip6_isv4mapped(ip)) - return socket_bind4(s,ip + 12,port); - - return socket_bind6(s,ip,port,scope_id); + if (ip6_isv4mapped(ip)) return socket_bind4(s, ip + 12, port); + + return socket_bind6(s, ip, port, scope_id); } -int socket_bind_reuse(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_bind_reuse(int s, const char ip[16], uint16 port, uint32 scope_id) { - if (ip6_isv4mapped(ip)) - return socket_bind4_reuse(s,ip + 12,port); + if (ip6_isv4mapped(ip)) return socket_bind4_reuse(s, ip + 12, port); - return socket_bind6_reuse(s,ip,port,scope_id); + return socket_bind6_reuse(s, ip, port, scope_id); } diff --git a/src/socket_connect.c b/src/socket_connect.c index 7b20659..a5d5e8f 100644 --- a/src/socket_connect.c +++ b/src/socket_connect.c @@ -1,11 +1,13 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include <errno.h> + #include "byte.h" -#include "socket_if.h" #include "ip.h" +#include "socket_if.h" /** @file socket_connect.c @@ -14,41 +16,40 @@ @brief connection to remote IP host */ -int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_connect6(int s, const char ip[16], uint16 port, uint32 scope_id) { struct sockaddr_in6 sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin6_family = AF_INET6; - uint16_pack_big((char *)&sa.sin6_port,port); + uint16_pack_big((char *)&sa.sin6_port, port); sa.sin6_flowinfo = 0; sa.sin6_scope_id = scope_id; - byte_copy((char *)&sa.sin6_addr,16,ip); + byte_copy((char *)&sa.sin6_addr, 16, ip); - return connect(s,(struct sockaddr *)&sa,sizeof(sa)); + return connect(s, (struct sockaddr *)&sa, sizeof(sa)); } /* this explizit declaration is needed to prevent compiler warnings */ int read(int _str, void *_buf, int _b); -int socket_connect4(int s,const char ip[4],uint16 port) +int socket_connect4(int s, const char ip[4], uint16 port) { struct sockaddr_in sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin_family = AF_INET; - uint16_pack_big((char *)&sa.sin_port,port); - byte_copy((char *)&sa.sin_addr,4,ip); + uint16_pack_big((char *)&sa.sin_port, port); + byte_copy((char *)&sa.sin_addr, 4, ip); - return connect(s,(struct sockaddr *)&sa,sizeof(sa)); + return connect(s, (struct sockaddr *)&sa, sizeof(sa)); } -int socket_connect(int s,const char ip[16],uint16 port,uint32 scope_id) +int socket_connect(int s, const char ip[16], uint16 port, uint32 scope_id) { - if (ip6_isv4mapped(ip)) - return socket_connect4(s,ip + 12,port); + if (ip6_isv4mapped(ip)) return socket_connect4(s, ip + 12, port); - return socket_connect6(s,ip,port,scope_id); + return socket_connect6(s, ip, port, scope_id); } int socket_connected(int s) @@ -58,8 +59,8 @@ int socket_connected(int s) char ch; dummy = sizeof(sa); - if (getpeername(s,(struct sockaddr *)&sa,(socklen_t *)&dummy) == -1) { - read(s,&ch,1); /* sets errno */ + if (getpeername(s, (struct sockaddr *)&sa, (socklen_t *)&dummy) == -1) { + read(s, &ch, 1); /* sets errno */ return 0; } return 1; diff --git a/src/socket_if.c b/src/socket_if.c index f55af4e..e8e1d54 100644 --- a/src/socket_if.c +++ b/src/socket_if.c @@ -1,9 +1,10 @@ -#include <sys/types.h> +#include "socket_if.h" + +#include <net/if.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <net/if.h> -#include "socket_if.h" +#include <sys/types.h> /** @file socket_if.c @@ -12,23 +13,23 @@ @brief interface handling for LLU */ -const unsigned char V4loopback[4] = {127,0,0,1}; -const unsigned char V4localnet[4] = {0,0,0,0}; +const unsigned char V4loopback[4] = {127, 0, 0, 1}; +const unsigned char V4localnet[4] = {0, 0, 0, 0}; /* the 'V4mappedprefix' constant is needed by 'ip.a' too */ -const unsigned char V4mappedprefix[12] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff}; -const unsigned char V6localnet[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; -const unsigned char V6loopback[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1}; +const unsigned char V4mappedprefix[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; +const unsigned char V6localnet[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +const unsigned char V6loopback[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; -uint32 socket_getifidx(const char *ifname) +uint32 socket_getifidx(const char *ifname) { return if_nametoindex(ifname); } static char ifname[IFNAMSIZ]; -const char* socket_getifname(uint32 scope_id) +const char *socket_getifname(uint32 scope_id) { - char *tmp = if_indextoname(scope_id,ifname); + char *tmp = if_indextoname(scope_id, ifname); if (tmp) return tmp; else diff --git a/src/socket_info.c b/src/socket_info.c index e644798..9300b24 100644 --- a/src/socket_info.c +++ b/src/socket_info.c @@ -1,10 +1,11 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include "byte.h" -#include "socket_if.h" #include "ip.h" +#include "socket_if.h" /** @file socket_info.c @@ -13,44 +14,44 @@ @brief querying local and remote info for socket */ -int socket_local(int s,char ip[16],uint16 *port,uint32 *scope_id) +int socket_local(int s, char ip[16], uint16 *port, uint32 *scope_id) { struct sockaddr_in6 sa; unsigned int dummy = sizeof(sa); - if (getsockname(s,(struct sockaddr *)&sa,&dummy) == -1) return -1; - + if (getsockname(s, (struct sockaddr *)&sa, &dummy) == -1) return -1; + if (sa.sin6_family == AF_INET) { - struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa; - byte_copy(ip,12,V4mappedprefix); - byte_copy(ip+12,4,(char *)&sa4->sin_addr); - uint16_unpack_big((char *)&sa4->sin_port,port); + struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa; + byte_copy(ip, 12, V4mappedprefix); + byte_copy(ip + 12, 4, (char *)&sa4->sin_addr); + uint16_unpack_big((char *)&sa4->sin_port, port); if (scope_id) *scope_id = 0; } else { - byte_copy(ip,16,(char *)&sa.sin6_addr); - uint16_unpack_big((char *)&sa.sin6_port,port); + byte_copy(ip, 16, (char *)&sa.sin6_addr); + uint16_unpack_big((char *)&sa.sin6_port, port); if (scope_id) *scope_id = sa.sin6_scope_id; } return 0; } -int socket_remote(int s,char ip[16],uint16 *port,uint32 *scope_id) +int socket_remote(int s, char ip[16], uint16 *port, uint32 *scope_id) { struct sockaddr_in6 sa; unsigned int dummy = sizeof(sa); - if (getpeername(s,(struct sockaddr *)&sa,&dummy) == -1) return -1; + if (getpeername(s, (struct sockaddr *)&sa, &dummy) == -1) return -1; if (sa.sin6_family == AF_INET) { - struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa; - byte_copy(ip,12,V4mappedprefix); - byte_copy(ip+12,4,(char *)&sa4->sin_addr); - uint16_unpack_big((char *)&sa4->sin_port,port); + struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa; + byte_copy(ip, 12, V4mappedprefix); + byte_copy(ip + 12, 4, (char *)&sa4->sin_addr); + uint16_unpack_big((char *)&sa4->sin_port, port); *scope_id = 0; } else { - byte_copy(ip,16,(char *)&sa.sin6_addr); - uint16_unpack_big((char *)&sa.sin6_port,port); + byte_copy(ip, 16, (char *)&sa.sin6_addr); + uint16_unpack_big((char *)&sa.sin6_port, port); *scope_id = sa.sin6_scope_id; } diff --git a/src/socket_recv.c b/src/socket_recv.c index de8c856..03aa311 100644 --- a/src/socket_recv.c +++ b/src/socket_recv.c @@ -1,7 +1,8 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include "byte.h" #include "ip.h" #include "socket_if.h" @@ -13,25 +14,25 @@ @brief setup receiving socket */ -int socket_recv(int s,char *buf,unsigned int len,char ip[16],uint16 *port,uint32 *scope_id) +int socket_recv(int s, char *buf, unsigned int len, char ip[16], uint16 *port, uint32 *scope_id) { struct sockaddr_in6 sa; unsigned int dummy = sizeof(sa); int r; - byte_zero(&sa,dummy); - r = recvfrom(s,buf,len,0,(struct sockaddr *)&sa,&dummy); + byte_zero(&sa, dummy); + r = recvfrom(s, buf, len, 0, (struct sockaddr *)&sa, &dummy); if (r == -1) return -1; if (sa.sin6_family == AF_INET) { struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa; - byte_copy(ip,12,V4mappedprefix); - byte_copy(ip+12,4,(char *)&sa4->sin_addr); - uint16_unpack_big((char *)&sa4->sin_port,port); + byte_copy(ip, 12, V4mappedprefix); + byte_copy(ip + 12, 4, (char *)&sa4->sin_addr); + uint16_unpack_big((char *)&sa4->sin_port, port); if (scope_id) *scope_id = 0; } else { - byte_copy(ip,16,(char *)&sa.sin6_addr); - uint16_unpack_big((char *)&sa.sin6_port,port); + byte_copy(ip, 16, (char *)&sa.sin6_addr); + uint16_unpack_big((char *)&sa.sin6_port, port); if (scope_id) *scope_id = sa.sin6_scope_id; } diff --git a/src/socket_send.c b/src/socket_send.c index 9f09520..7c919f4 100644 --- a/src/socket_send.c +++ b/src/socket_send.c @@ -1,7 +1,8 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include "byte.h" #include "ip.h" #include "socket_if.h" @@ -13,50 +14,52 @@ @brief setup sending socket */ -int socket_send4(int s,const char *buf,unsigned int len,const char ip[4],uint16 port) +int socket_send4(int s, const char *buf, unsigned int len, const char ip[4], uint16 port) { struct sockaddr_in sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin_family = AF_INET; - uint16_pack_big((char *)&sa.sin_port,port); - byte_copy((char *)&sa.sin_addr,4,ip); + uint16_pack_big((char *)&sa.sin_port, port); + byte_copy((char *)&sa.sin_addr, 4, ip); - return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa)); + return sendto(s, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa)); } -int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id) +int socket_send6( + int s, const char *buf, unsigned int len, const char ip[16], uint16 port, uint32 scope_id) { struct sockaddr_in6 sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin6_family = AF_INET6; sa.sin6_scope_id = scope_id; - uint16_pack_big((char *)&sa.sin6_port,port); - byte_copy((char *)&sa.sin6_addr,16,ip); + uint16_pack_big((char *)&sa.sin6_port, port); + byte_copy((char *)&sa.sin6_addr, 16, ip); - return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa)); + return sendto(s, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa)); } -int socket_send(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id) -{ +int socket_send( + int s, const char *buf, unsigned int len, const char ip[16], uint16 port, uint32 scope_id) +{ if (ip6_isv4mapped(ip)) - return socket_send4(s,buf,len,ip + 12,port); - else - return socket_send6(s,buf,len,ip,port,scope_id); -} + return socket_send4(s, buf, len, ip + 12, port); + else + return socket_send6(s, buf, len, ip, port, scope_id); +} -int socket_broadcast4(int s,const char *buf,unsigned int len,uint16 port) +int socket_broadcast4(int s, const char *buf, unsigned int len, uint16 port) { struct sockaddr_in sa; - byte_zero(&sa,sizeof(sa)); + byte_zero(&sa, sizeof(sa)); sa.sin_family = AF_INET; - uint16_pack_big((char *)&sa.sin_port,port); - byte_copy((char *)&sa.sin_addr,4,V4broadcast); + uint16_pack_big((char *)&sa.sin_port, port); + byte_copy((char *)&sa.sin_addr, 4, V4broadcast); - return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa)); + return sendto(s, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa)); } diff --git a/src/socket_setup.c b/src/socket_setup.c index fb65fa2..39fc3dd 100644 --- a/src/socket_setup.c +++ b/src/socket_setup.c @@ -1,10 +1,11 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include "byte.h" -#include "socket_if.h" #include "ip.h" +#include "socket_if.h" /** @file socket_setup.c @@ -13,57 +14,57 @@ @brief setup listening socket */ -int socket_accept(int s,char ip[16],uint16 *port,uint32 *scope_id) +int socket_accept(int s, char ip[16], uint16 *port, uint32 *scope_id) { struct sockaddr_in6 sa; unsigned int dummy = sizeof(sa); int fd; - fd = accept(s,(struct sockaddr *)&sa,&dummy); + fd = accept(s, (struct sockaddr *)&sa, &dummy); if (fd == -1) return -1; if (sa.sin6_family == AF_INET) { - struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa; - byte_copy(ip,12,V4mappedprefix); - byte_copy(ip+12,4,(char *)&sa4->sin_addr); - uint16_unpack_big((char *)&sa4->sin_port,port); + struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa; + byte_copy(ip, 12, V4mappedprefix); + byte_copy(ip + 12, 4, (char *)&sa4->sin_addr); + uint16_unpack_big((char *)&sa4->sin_port, port); if (scope_id) *scope_id = 0; } else { - byte_copy(ip,16,(char *)&sa.sin6_addr); - uint16_unpack_big((char *)&sa.sin6_port,port); + byte_copy(ip, 16, (char *)&sa.sin6_addr); + uint16_unpack_big((char *)&sa.sin6_port, port); if (scope_id) *scope_id = sa.sin6_scope_id; - } + } return fd; } -int socket_accept4(int s,char ip[4],uint16 *port) +int socket_accept4(int s, char ip[4], uint16 *port) { - struct sockaddr_in sa; - unsigned int dummy = sizeof(sa); - int fd; + struct sockaddr_in sa; + unsigned int dummy = sizeof(sa); + int fd; - fd = accept(s,(struct sockaddr *) &sa,&dummy); - if (fd == -1) return -1; + fd = accept(s, (struct sockaddr *)&sa, &dummy); + if (fd == -1) return -1; - byte_copy(ip,4,(char *) &sa.sin_addr); - uint16_unpack_big((char *) &sa.sin_port,port); + byte_copy(ip, 4, (char *)&sa.sin_addr); + uint16_unpack_big((char *)&sa.sin_port, port); - return fd; + return fd; } -int socket_listen(int s,int backlog) +int socket_listen(int s, int backlog) { - return listen(s,backlog); + return listen(s, backlog); } int socket_ipoptionskill(int s) { int r; - r = setsockopt(s,IPPROTO_IP,1,(char *) 0,0); /* 1 == IP_OPTIONS */ - r = setsockopt(s,IPPROTO_IPV6,1,(char *) 0,0); - + r = setsockopt(s, IPPROTO_IP, 1, (char *)0, 0); /* 1 == IP_OPTIONS */ + r = setsockopt(s, IPPROTO_IPV6, 1, (char *)0, 0); + return r; } @@ -73,27 +74,27 @@ int socket_ip6anycast(int s) int r; #ifdef GEN_IP_PKTINFO /* Linux */ - r = setsockopt(s,IPPROTO_IP,GEN_IP_PKTINFO,&opt,sizeof(opt)); -#elif IP_PKTINFO /* Solaris */ - r = setsockopt(s,IPPROTO_IP,IP_PKTINFO,&opt,sizeof(opt)); -#elif IP_RECVDSTADDR /* BSD */ - r = setsockopt(s,IPPROTO_IP,IP_RECVDSTADDR,&opt,sizeof(opt)); + r = setsockopt(s, IPPROTO_IP, GEN_IP_PKTINFO, &opt, sizeof(opt)); +#elif IP_PKTINFO /* Solaris */ + r = setsockopt(s, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)); +#elif IP_RECVDSTADDR /* BSD */ + r = setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)); #elif IPV6_RECVDSTADDR - r = setsockopt(s,IPPROTO_IPV6,IP_RECVDSTADDR,&opt,sizeof(opt)); + r = setsockopt(s, IPPROTO_IPV6, IP_RECVDSTADDR, &opt, sizeof(opt)); #endif - return r; -} + return r; +} int socket_dualstack(int s) { int opt = 0; - return setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&opt,sizeof(opt)); + return setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); } int socket_nodualstack(int s) { int opt = 1; - return setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&opt,sizeof(opt)); + return setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); } diff --git a/src/socket_tcp.c b/src/socket_tcp.c index 1ff050e..70ada37 100644 --- a/src/socket_tcp.c +++ b/src/socket_tcp.c @@ -1,15 +1,17 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include <errno.h> -#include "close.h" /* better use unistd.h ? */ + +#include "close.h" /* better use unistd.h ? */ +#include "error.h" #include "ndelay.h" #include "socket_if.h" -#include "error.h" #ifndef EAFNOSUPPORT -#define EAFNOSUPPORT EINVAL + #define EAFNOSUPPORT EINVAL #endif /** @@ -23,9 +25,12 @@ int socket_tcp4(void) { int s; - s = socket(AF_INET,SOCK_STREAM,0); - if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + s = socket(AF_INET, SOCK_STREAM, 0); + if (s != -1) + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; } @@ -34,9 +39,12 @@ int socket_tcp6(void) { int s; - s = socket(AF_INET6,SOCK_STREAM,0); - if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + s = socket(AF_INET6, SOCK_STREAM, 0); + if (s != -1) + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; } @@ -45,13 +53,16 @@ int socket_tcp(void) { int s; - s = socket(AF_INET6,SOCK_STREAM,0); - if (s == -1) - if (errno == EINVAL || errno == EAFNOSUPPORT || errno == EPROTO || errno == EPROTONOSUPPORT) - s = socket(AF_INET,SOCK_STREAM,0); + s = socket(AF_INET6, SOCK_STREAM, 0); + if (s == -1) + if (errno == EINVAL || errno == EAFNOSUPPORT || errno == EPROTO || errno == EPROTONOSUPPORT) + s = socket(AF_INET, SOCK_STREAM, 0); - if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + if (s != -1) + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; } @@ -59,5 +70,5 @@ int socket_tcp(void) int socket_tcpnodelay(int s) { int opt = 1; - return setsockopt(s,IPPROTO_TCP,1,&opt,sizeof(opt)); /* 1 == TCP_NODELAY */ + return setsockopt(s, IPPROTO_TCP, 1, &opt, sizeof(opt)); /* 1 == TCP_NODELAY */ } diff --git a/src/socket_udp.c b/src/socket_udp.c index 743cdf1..2ac6068 100644 --- a/src/socket_udp.c +++ b/src/socket_udp.c @@ -1,15 +1,17 @@ -#include <sys/types.h> +#include <netinet/in.h> #include <sys/param.h> #include <sys/socket.h> -#include <netinet/in.h> +#include <sys/types.h> + #include <errno.h> -#include "close.h" /* better use unistd.h ? */ + +#include "close.h" /* better use unistd.h ? */ +#include "error.h" #include "ndelay.h" #include "socket_if.h" -#include "error.h" #ifndef EAFNOSUPPORT -#define EAFNOSUPPORT EINVAL + #define EAFNOSUPPORT EINVAL #endif /** @@ -23,35 +25,44 @@ int socket_udp6(void) { int s; - s = socket(AF_INET6,SOCK_DGRAM,0); + s = socket(AF_INET6, SOCK_DGRAM, 0); if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; -} +} int socket_udp4(void) { int s; - s = socket(AF_INET,SOCK_DGRAM,0); + s = socket(AF_INET, SOCK_DGRAM, 0); if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; -} +} int socket_udp(void) { int s; - s = socket(AF_INET6,SOCK_DGRAM,0); - if (s == -1) + s = socket(AF_INET6, SOCK_DGRAM, 0); + if (s == -1) if (errno == EINVAL || errno == EAFNOSUPPORT || errno == EPROTO || errno == EPROTONOSUPPORT) - s = socket(AF_INET,SOCK_DGRAM,0); + s = socket(AF_INET, SOCK_DGRAM, 0); if (s != -1) - if (ndelay_on(s) == -1) { close(s); return -1; } + if (ndelay_on(s) == -1) { + close(s); + return -1; + } return s; } @@ -1,4 +1,5 @@ #include "str.h" + #include "stralloc.h" /** @@ -8,61 +9,67 @@ @brief string handling functions */ -unsigned int str_copy(register char *s,register const char *t) +unsigned int str_copy(register char *s, register const char *t) { register int len; len = 0; for (;;) { + // clang-format off if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; + // clang-format on } } -unsigned int str_copyb(register char *s,register const char *t,unsigned int max) +unsigned int str_copyb(register char *s, register const char *t, unsigned int max) { register int len; len = 0; while (max-- > 0) { + // clang-format off if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; if (!(*s = *t)) { return len; } ++s; ++t; ++len; + // clang-format on } return len; } -int str_diff(register const char *s,register const char *t) +int str_diff(register const char *s, register const char *t) { register char x; for (;;) { + // clang-format off x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; + // clang-format on } - return ((int)(unsigned int)(unsigned char) x) - - ((int)(unsigned int)(unsigned char) *t); + return ((int)(unsigned int)(unsigned char)x) - ((int)(unsigned int)(unsigned char)*t); } -int str_diffn(register const char *s,register const char *t,unsigned int len) +int str_diffn(register const char *s, register const char *t, unsigned int len) { register char x; for (;;) { + // clang-format off if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t; + // clang-format on } - return ((int)(unsigned int)(unsigned char) x) - - ((int)(unsigned int)(unsigned char) *t); + return ((int)(unsigned int)(unsigned char)x) - ((int)(unsigned int)(unsigned char)*t); } unsigned int str_len(register const char *s) @@ -71,14 +78,16 @@ unsigned int str_len(register const char *s) t = s; for (;;) { + // clang-format off if (!*t) { return t - s; } ++t; if (!*t) { return t - s; } ++t; if (!*t) { return t - s; } ++t; if (!*t) { return t - s; } ++t; + // clang-format on } } -unsigned int str_chr(register const char *s,int c) +unsigned int str_chr(register const char *s, int c) { register char ch; register const char *t; @@ -86,15 +95,17 @@ unsigned int str_chr(register const char *s,int c) ch = c; t = s; for (;;) { + // clang-format off if (!*t) { break; } if (*t == ch) { break; } ++t; if (!*t) { break; } if (*t == ch) { break; } ++t; if (!*t) { break; } if (*t == ch) { break; } ++t; if (!*t) { break; } if (*t == ch) { break; } ++t; + // clang-format on } return t - s; } -unsigned int str_rchr(register const char *s,int c) +unsigned int str_rchr(register const char *s, int c) { register char ch; register const char *t; @@ -104,32 +115,36 @@ unsigned int str_rchr(register const char *s,int c) t = s; u = 0; for (;;) { + // clang-format off if (!*t) { break; } if (*t == ch) { u = t; } ++t; if (!*t) { break; } if (*t == ch) { u = t; } ++t; if (!*t) { break; } if (*t == ch) { u = t; } ++t; if (!*t) { break; } if (*t == ch) { u = t; } ++t; + // clang-format on } if (!u) u = t; return u - s; } -int str_start(register const char *s,register const char *t) +int str_start(register const char *s, register const char *t) { register char x; for (;;) { + // clang-format off x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; + // clang-format on } } -char *str_append(char *dest, char const *s) +char *str_append(char *dest, const char *s) { static stralloc sa = {0}; - stralloc_copys(&sa,dest); - stralloc_catb(&sa,s,sizeof(s)); + stralloc_copys(&sa, dest); + stralloc_catb(&sa, s, sizeof(s)); return sa.s; } diff --git a/src/stralloc.c b/src/stralloc.c index 8c0335e..8cd8a10 100644 --- a/src/stralloc.c +++ b/src/stralloc.c @@ -1,8 +1,10 @@ +#include "stralloc.h" + #include <stdlib.h> + +#include "alloc.h" #include "byte.h" #include "str.h" -#include "stralloc.h" -#include "alloc.h" /** @file stralloc.c @@ -11,46 +13,46 @@ @brief genious dynamic string handling */ -int stralloc_starts(stralloc *sa,const char *s) +int stralloc_starts(stralloc *sa, const char *s) { int len; len = str_len(s); - return (sa->len >= len) && byte_equal(s,len,sa->s); + return (sa->len >= len) && byte_equal(s, len, sa->s); } -int stralloc_cat(stralloc *sato,stralloc *safrom) +int stralloc_cat(stralloc *sato, stralloc *safrom) { - return stralloc_catb(sato,safrom->s,safrom->len); + return stralloc_catb(sato, safrom->s, safrom->len); } -int stralloc_catb(stralloc *sa,const char *s,unsigned int n) +int stralloc_catb(stralloc *sa, const char *s, unsigned int n) { - if (!sa->s) return stralloc_copyb(sa,s,n); - if (!stralloc_readyplus(sa,n + 1)) return 0; - byte_copy(sa->s + sa->len,n,s); + if (!sa->s) return stralloc_copyb(sa, s, n); + if (!stralloc_readyplus(sa, n + 1)) return 0; + byte_copy(sa->s + sa->len, n, s); sa->len += n; sa->s[sa->len] = 'Z'; /* ``offensive programming'' */ return 1; } -int stralloc_cats(stralloc *sa,const char *s) +int stralloc_cats(stralloc *sa, const char *s) { - return stralloc_catb(sa,s,str_len(s)); + return stralloc_catb(sa, s, str_len(s)); } -int stralloc_copy(stralloc *sato,stralloc *safrom) +int stralloc_copy(stralloc *sato, stralloc *safrom) { - return stralloc_copyb(sato,safrom->s,safrom->len); + return stralloc_copyb(sato, safrom->s, safrom->len); } -int stralloc_ready(stralloc *sa,size_t len) +int stralloc_ready(stralloc *sa, size_t len) { - register size_t wanted = len+(len>>3)+30; /* heuristic from djb */ - if (wanted<len) wanted = len; - if (!sa->s || sa->a<len) { - register char* tmp; - if (!(tmp = realloc(sa->s,wanted))) // !!! needs stdlib (realloc) + register size_t wanted = len + (len >> 3) + 30; /* heuristic from djb */ + if (wanted < len) wanted = len; + if (!sa->s || sa->a < len) { + register char *tmp; + if (!(tmp = realloc(sa->s, wanted))) // !!! needs stdlib (realloc) return 0; sa->a = wanted; sa->s = tmp; @@ -58,30 +60,30 @@ int stralloc_ready(stralloc *sa,size_t len) return 1; } -int stralloc_readyplus(stralloc *sa,size_t len) +int stralloc_readyplus(stralloc *sa, size_t len) { if (sa->s) { - if (sa->len + len < len) return 0; /* catch integer overflow */ - return stralloc_ready(sa,sa->len+len); + if (sa->len + len < len) return 0; /* catch integer overflow */ + return stralloc_ready(sa, sa->len + len); } else - return stralloc_ready(sa,len); + return stralloc_ready(sa, len); } -int stralloc_copyb(stralloc *sa,const char *s,unsigned int n) +int stralloc_copyb(stralloc *sa, const char *s, unsigned int n) { - if (!stralloc_ready(sa,n + 1)) return 0; - byte_copy(sa->s,n,s); + if (!stralloc_ready(sa, n + 1)) return 0; + byte_copy(sa->s, n, s); sa->len = n; sa->s[n] = 'Z'; /* ``offensive programming'' */ return 1; } -int stralloc_copys(stralloc *sa,const char *s) +int stralloc_copys(stralloc *sa, const char *s) { - return stralloc_copyb(sa,s,str_len(s)); + return stralloc_copyb(sa, s, str_len(s)); } -int stralloc_catulong0(stralloc *sa,unsigned long u,unsigned int n) +int stralloc_catulong0(stralloc *sa, unsigned long u, unsigned int n) { unsigned int len; unsigned long q; @@ -89,29 +91,35 @@ int stralloc_catulong0(stralloc *sa,unsigned long u,unsigned int n) len = 1; q = u; - while (q > 9) { ++len; q /= 10; } + while (q > 9) { + ++len; + q /= 10; + } if (len < n) len = n; - if (!stralloc_readyplus(sa,len)) return 0; + if (!stralloc_readyplus(sa, len)) return 0; s = sa->s + sa->len; sa->len += len; - while (len) { s[--len] = '0' + (u % 10); u /= 10; } + while (len) { + s[--len] = '0' + (u % 10); + u /= 10; + } return 1; } -int stralloc_catlong0(stralloc *sa,long l,unsigned int n) +int stralloc_catlong0(stralloc *sa, long l, unsigned int n) { if (l < 0) { - if (!stralloc_append(sa,"-")) return 0; + if (!stralloc_append(sa, "-")) return 0; l = -l; } - return stralloc_catulong0(sa,l,n); + return stralloc_catulong0(sa, l, n); } -int stralloc_append(stralloc *sa,const char *in) +int stralloc_append(stralloc *sa, const char *in) { - if (stralloc_readyplus(sa,1)) { + if (stralloc_readyplus(sa, 1)) { sa->s[sa->len] = *in; ++sa->len; return 1; @@ -119,7 +127,8 @@ int stralloc_append(stralloc *sa,const char *in) return 0; } -void stralloc_free(stralloc *sa) { +void stralloc_free(stralloc *sa) +{ if (sa->s) free(sa->s); sa->s = 0; sa->a = sa->len = 0; @@ -1,6 +1,7 @@ -#include <time.h> #include "tai.h" +#include <time.h> + /** @file tai.c @author djb @@ -8,21 +9,22 @@ @brief 'temps atomic' time handling */ -void tai_add(struct tai *t,const struct tai *u,const struct tai *v) +void tai_add(struct tai *t, const struct tai *u, const struct tai *v) { t->x = u->x + v->x; } void tai_now(struct tai *t) { - tai_unix(t,time((time_t *) 0)); + tai_unix(t, time((time_t *)0)); } -void tai_pack(char *s,const struct tai *t) +void tai_pack(char *s, const struct tai *t) { uint64 x; x = t->x; + // clang-format off s[7] = (char)x; x >>= 8; s[6] = (char)x; x >>= 8; s[5] = (char)x; x >>= 8; @@ -31,23 +33,25 @@ void tai_pack(char *s,const struct tai *t) s[2] = (char)x; x >>= 8; s[1] = (char)x; x >>= 8; s[0] = (char)x; + // clang-format on } -void tai_sub(struct tai *t,const struct tai *u,const struct tai *v) +void tai_sub(struct tai *t, const struct tai *u, const struct tai *v) { t->x = u->x - v->x; } -void tai_uint(struct tai *t,unsigned int u) +void tai_uint(struct tai *t, unsigned int u) { t->x = u; } -void tai_unpack(const char *s,struct tai *t) +void tai_unpack(const char *s, struct tai *t) { uint64 x; - x = (unsigned char) s[0]; + x = (unsigned char)s[0]; + // clang-format off x <<= 8; x += (unsigned char) s[1]; x <<= 8; x += (unsigned char) s[2]; x <<= 8; x += (unsigned char) s[3]; @@ -55,5 +59,6 @@ void tai_unpack(const char *s,struct tai *t) x <<= 8; x += (unsigned char) s[5]; x <<= 8; x += (unsigned char) s[6]; x <<= 8; x += (unsigned char) s[7]; + // clang-format on t->x = x; } @@ -1,7 +1,8 @@ -#include <sys/types.h> -#include <sys/time.h> #include "taia.h" +#include <sys/time.h> +#include <sys/types.h> + /** @file taia.c @author djb @@ -11,7 +12,7 @@ /* XXX: breaks tai encapsulation */ -void taia_add(struct taia *t,struct taia *u,struct taia *v) +void taia_add(struct taia *t, struct taia *u, struct taia *v) { t->sec.x = u->sec.x + v->sec.x; t->nano = u->nano + v->nano; @@ -36,7 +37,7 @@ double taia_frac(struct taia *t) return (t->atto * 0.000000001 + t->nano) * 0.000000001; } -int taia_less(struct taia *t,struct taia *u) +int taia_less(struct taia *t, struct taia *u) { if (t->sec.x < u->sec.x) return 1; if (t->sec.x > u->sec.x) return 0; @@ -48,8 +49,8 @@ int taia_less(struct taia *t,struct taia *u) int taia_now(struct taia *t) { struct timeval now; - if (gettimeofday(&now,(struct timezone *) 0) == 0) { - tai_unix(&t->sec,now.tv_sec); + if (gettimeofday(&now, (struct timezone *)0) == 0) { + tai_unix(&t->sec, now.tv_sec); t->nano = 1000 * now.tv_usec + 500; t->atto = 0; return 0; @@ -59,26 +60,32 @@ int taia_now(struct taia *t) return -1; } -void taia_pack(char *s,struct taia *t) +void taia_pack(char *s, struct taia *t) { unsigned long x; - tai_pack(s,&t->sec); + tai_pack(s, &t->sec); s += 8; x = t->atto; - s[7] = x & 255; x >>= 8; - s[6] = x & 255; x >>= 8; - s[5] = x & 255; x >>= 8; + s[7] = x & 255; + x >>= 8; + s[6] = x & 255; + x >>= 8; + s[5] = x & 255; + x >>= 8; s[4] = x; x = t->nano; - s[3] = x & 255; x >>= 8; - s[2] = x & 255; x >>= 8; - s[1] = x & 255; x >>= 8; + s[3] = x & 255; + x >>= 8; + s[2] = x & 255; + x >>= 8; + s[1] = x & 255; + x >>= 8; s[0] = x; } -void taia_sub(struct taia *t,struct taia *u,struct taia *v) +void taia_sub(struct taia *t, struct taia *u, struct taia *v) { unsigned long unano = u->nano; unsigned long uatto = u->atto; @@ -96,7 +103,7 @@ void taia_sub(struct taia *t,struct taia *u,struct taia *v) } } -void taia_uint(struct taia *t,unsigned int s) +void taia_uint(struct taia *t, unsigned int s) { t->sec.x = s; t->nano = 0; diff --git a/src/timeout.c b/src/timeout.c index e721b66..bdc2949 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -1,7 +1,9 @@ +#include "timeout.h" + #include <unistd.h> + #include "error.h" #include "iopause.h" -#include "timeout.h" /** @file timeout.c @@ -10,50 +12,50 @@ @brief read/write timeout handling */ -int timeoutread(int t,int fd,char *buf,int len) +int timeoutread(int t, int fd, char *buf, int len) { struct taia now; struct taia deadline; iopause_fd x; taia_now(&now); - taia_uint(&deadline,t); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, t); + taia_add(&deadline, &now, &deadline); x.fd = fd; x.events = IOPAUSE_READ; for (;;) { taia_now(&now); - iopause(&x,1,&deadline,&now); + iopause(&x, 1, &deadline, &now); if (x.revents) break; - if (taia_less(&deadline,&now)) { + if (taia_less(&deadline, &now)) { errno = ETIMEDOUT; return -1; } } - return read(fd,buf,len); + return read(fd, buf, len); } -int timeoutwrite(int t,int fd,char *buf,int len) +int timeoutwrite(int t, int fd, char *buf, int len) { struct taia now; struct taia deadline; iopause_fd x; taia_now(&now); - taia_uint(&deadline,t); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, t); + taia_add(&deadline, &now, &deadline); x.fd = fd; x.events = IOPAUSE_WRITE; for (;;) { taia_now(&now); - iopause(&x,1,&deadline,&now); + iopause(&x, 1, &deadline, &now); if (x.revents) break; - if (taia_less(&deadline,&now)) { + if (taia_less(&deadline, &now)) { errno = ETIMEDOUT; return -1; } } - return write(fd,buf,len); + return write(fd, buf, len); } diff --git a/src/timeoutconn.c b/src/timeoutconn.c index c94f600..714faef 100644 --- a/src/timeoutconn.c +++ b/src/timeoutconn.c @@ -1,9 +1,10 @@ -#include "ndelay.h" -#include "socket_if.h" -#include "iopause.h" -#include "error.h" #include "timeoutconn.h" + +#include "error.h" +#include "iopause.h" #include "ip.h" +#include "ndelay.h" +#include "socket_if.h" /** @file timeoutconn.c @@ -12,25 +13,25 @@ @brief socket read/write timeout handling; return code of iopause considered */ -int timeoutconn4(int s,char ip[4],uint16 port,unsigned int timeout) +int timeoutconn4(int s, char ip[4], uint16 port, unsigned int timeout) { struct taia now; struct taia deadline; iopause_fd x; unsigned int p = 0; - if (socket_connect4(s,ip,port) == -1) { + if (socket_connect4(s, ip, port) == -1) { if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); - taia_uint(&deadline,timeout); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, timeout); + taia_add(&deadline, &now, &deadline); for (;;) { taia_now(&now); - iopause(&x,1,&deadline,&now); - if (x.revents) break; /* 's' available */ - if (taia_less(&deadline,&now)) { + iopause(&x, 1, &deadline, &now); + if (x.revents) break; /* 's' available */ + if (taia_less(&deadline, &now)) { errno = ETIMEDOUT; /* note that connect attempt is continuing */ return -1; } @@ -43,25 +44,25 @@ int timeoutconn4(int s,char ip[4],uint16 port,unsigned int timeout) return 0; } -int timeoutconn6(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif) +int timeoutconn6(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif) { struct taia now; struct taia deadline; iopause_fd x; unsigned int p = 0; - if (socket_connect6(s,ip,port,netif) == -1) { + if (socket_connect6(s, ip, port, netif) == -1) { if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); - taia_uint(&deadline,timeout); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, timeout); + taia_add(&deadline, &now, &deadline); for (;;) { taia_now(&now); - iopause(&x,1,&deadline,&now); - if (x.revents) break; /* 's' available */ - if (taia_less(&deadline,&now)) { + iopause(&x, 1, &deadline, &now); + if (x.revents) break; /* 's' available */ + if (taia_less(&deadline, &now)) { errno = ETIMEDOUT; /* note that connect attempt is continuing */ return -1; } @@ -74,7 +75,7 @@ int timeoutconn6(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif return 0; } -int timeoutconn(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif) +int timeoutconn(int s, char ip[16], uint16 port, unsigned int timeout, uint32 netif) { struct taia now; struct taia deadline; @@ -82,23 +83,23 @@ int timeoutconn(int s,char ip[16],uint16 port,unsigned int timeout,uint32 netif) unsigned int p = 0; int r; - if (ip6_isv4mapped(ip)) - r = socket_connect4(s,ip + 12,port); + if (ip6_isv4mapped(ip)) + r = socket_connect4(s, ip + 12, port); else - r = socket_connect6(s,ip,port,netif); + r = socket_connect6(s, ip, port, netif); if (r == -1) { if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); - taia_uint(&deadline,timeout); - taia_add(&deadline,&now,&deadline); + taia_uint(&deadline, timeout); + taia_add(&deadline, &now, &deadline); for (;;) { taia_now(&now); - iopause(&x,1,&deadline,&now); - if (x.revents) break; /* 's' available */ - if (taia_less(&deadline,&now)) { + iopause(&x, 1, &deadline, &now); + if (x.revents) break; /* 's' available */ + if (taia_less(&deadline, &now)) { errno = ETIMEDOUT; /* note that connect attempt is continuing */ return -1; } diff --git a/src/uint128p.c b/src/uint128p.c index 57c713e..a06da72 100644 --- a/src/uint128p.c +++ b/src/uint128p.c @@ -6,9 +6,10 @@ @source djbdns6 @brief packing/unpacking 128 bit integer to/from char string */ - -void uint128_pack(char s[16],uint128 u) + +void uint128_pack(char s[16], uint128 u) { + // clang-format off s[0] = u.lo & 255; u.lo >>= 8; s[1] = u.lo & 255; u.lo >>= 8; s[2] = u.lo & 255; u.lo >>= 8; @@ -25,10 +26,12 @@ void uint128_pack(char s[16],uint128 u) s[12] = u.hi & 255; u.hi >>= 8; s[13] = u.hi & 255; u.hi >>= 8; s[14] = u.hi & 255; u.hi >>= 8; - s[15] = u.hi & 255; + s[15] = u.hi & 255; + // clang-format on } -void uint128_pack_big(char s[16],uint128 u) +void uint128_pack_big(char s[16], uint128 u) { + // clang-format off s[15] = u.lo & 255; u.lo >>= 8; s[14] = u.lo & 255; u.lo >>= 8; s[13] = u.lo & 255; u.lo >>= 8; @@ -45,14 +48,16 @@ void uint128_pack_big(char s[16],uint128 u) s[3] = u.hi & 255; u.hi >>= 8; s[2] = u.hi & 255; u.hi >>= 8; s[1] = u.hi & 255; u.hi >>= 8; - s[0] = u.hi & 255; + s[0] = u.hi & 255; + // clang-format on } -void uint128_unpack(char s[16],uint128 *u) +void uint128_unpack(char s[16], uint128 *u) { uint128 result; result.hi = result.lo = 0ULL; + // clang-format off result.hi = (unsigned char) s[15]; result.hi <<= 8; result.hi += (unsigned char) s[14]; result.hi <<= 8; result.hi += (unsigned char) s[13]; result.hi <<= 8; @@ -69,15 +74,17 @@ void uint128_unpack(char s[16],uint128 *u) result.lo += (unsigned char) s[3]; result.lo <<= 8; result.lo += (unsigned char) s[2]; result.lo <<= 8; result.lo += (unsigned char) s[1]; result.lo <<= 8; - result.lo += (unsigned char) s[0]; + result.lo += (unsigned char) s[0]; + // clang-format on *u = result; } -void uint128_unpack_big(char s[16],uint128 *u) +void uint128_unpack_big(char s[16], uint128 *u) { uint128 result; result.hi = result.lo = 0ULL; + // clang-format off result.hi = (unsigned char) s[0]; result.hi <<= 8; result.hi += (unsigned char) s[1]; result.hi <<= 8; result.hi += (unsigned char) s[2]; result.hi <<= 8; @@ -95,6 +102,7 @@ void uint128_unpack_big(char s[16],uint128 *u) result.lo += (unsigned char) s[13]; result.lo <<= 8; result.lo += (unsigned char) s[14]; result.lo <<= 8; result.lo += (unsigned char) s[15]; + // clang-format on *u = result; } diff --git a/src/uint16p.c b/src/uint16p.c index 5dddf21..b470e50 100644 --- a/src/uint16p.c +++ b/src/uint16p.c @@ -7,34 +7,36 @@ @brief packing/unpacking 16 bit integer to/from char string */ -void uint16_pack(char s[2],uint16 u) +void uint16_pack(char s[2], uint16 u) { s[0] = u & 255; s[1] = u >> 8; } -void uint16_pack_big(char s[2],uint16 u) +void uint16_pack_big(char s[2], uint16 u) { s[1] = u & 255; s[0] = u >> 8; } -void uint16_unpack(char s[2],uint16 *u) +void uint16_unpack(char s[2], uint16 *u) { uint16 result; - result = (unsigned char) s[1]; result <<= 8; - result += (unsigned char) s[0]; + result = (unsigned char)s[1]; + result <<= 8; + result += (unsigned char)s[0]; *u = result; } -void uint16_unpack_big(char s[2],uint16 *u) +void uint16_unpack_big(char s[2], uint16 *u) { uint16 result; - result = (unsigned char) s[0]; result <<= 8; - result += (unsigned char) s[1]; + result = (unsigned char)s[0]; + result <<= 8; + result += (unsigned char)s[1]; *u = result; } diff --git a/src/uint32p.c b/src/uint32p.c index f3f04ea..7fe5237 100644 --- a/src/uint32p.c +++ b/src/uint32p.c @@ -7,41 +7,51 @@ @brief packing/unpacking 32 bit integer to/from char string */ -void uint32_pack(char s[4],uint32 u) +void uint32_pack(char s[4], uint32 u) { - s[0] = u & 255; u >>= 8; - s[1] = u & 255; u >>= 8; + s[0] = u & 255; + u >>= 8; + s[1] = u & 255; + u >>= 8; s[2] = u & 255; s[3] = u >> 8; } -void uint32_pack_big(char s[4],uint32 u) +void uint32_pack_big(char s[4], uint32 u) { - s[3] = u & 255; u >>= 8; - s[2] = u & 255; u >>= 8; + s[3] = u & 255; + u >>= 8; + s[2] = u & 255; + u >>= 8; s[1] = u & 255; s[0] = u >> 8; } -void uint32_unpack(char s[4],uint32 *u) +void uint32_unpack(char s[4], uint32 *u) { uint32 result; - result = (unsigned char) s[3]; result <<= 8; - result += (unsigned char) s[2]; result <<= 8; - result += (unsigned char) s[1]; result <<= 8; - result += (unsigned char) s[0]; + result = (unsigned char)s[3]; + result <<= 8; + result += (unsigned char)s[2]; + result <<= 8; + result += (unsigned char)s[1]; + result <<= 8; + result += (unsigned char)s[0]; *u = result; } -void uint32_unpack_big(char s[4],uint32 *u) +void uint32_unpack_big(char s[4], uint32 *u) { uint32 result; - result = (unsigned char) s[0]; result <<= 8; - result += (unsigned char) s[1]; result <<= 8; - result += (unsigned char) s[2]; result <<= 8; - result += (unsigned char) s[3]; + result = (unsigned char)s[0]; + result <<= 8; + result += (unsigned char)s[1]; + result <<= 8; + result += (unsigned char)s[2]; + result <<= 8; + result += (unsigned char)s[3]; *u = result; } diff --git a/src/uint64p.c b/src/uint64p.c index 41b8ceb..84a4885 100644 --- a/src/uint64p.c +++ b/src/uint64p.c @@ -1,62 +1,90 @@ #include "uint_t.h" - + /** @file uint64p.c @author feh, jannis @source djbdns6 @brief packing/unpacking 64 bit integer to/from char string */ - -void uint64_pack(char s[8],uint64 u) + +void uint64_pack(char s[8], uint64 u) { - s[0] = u & 255; u >>= 8; - s[1] = u & 255; u >>= 8; - s[2] = u & 255; u >>= 8; - s[3] = u & 255; u >>= 8; - s[4] = u & 255; u >>= 8; - s[5] = u & 255; u >>= 8; - s[6] = u & 255; u >>= 8; + s[0] = u & 255; + u >>= 8; + s[1] = u & 255; + u >>= 8; + s[2] = u & 255; + u >>= 8; + s[3] = u & 255; + u >>= 8; + s[4] = u & 255; + u >>= 8; + s[5] = u & 255; + u >>= 8; + s[6] = u & 255; + u >>= 8; s[7] = u & 255; } -void uint64_pack_big(char s[8],uint64 u) +void uint64_pack_big(char s[8], uint64 u) { - s[7] = u & 255; u >>= 8; - s[6] = u & 255; u >>= 8; - s[5] = u & 255; u >>= 8; - s[4] = u & 255; u >>= 8; - s[3] = u & 255; u >>= 8; - s[2] = u & 255; u >>= 8; - s[1] = u & 255; u >>= 8; - s[0] = u & 255; + s[7] = u & 255; + u >>= 8; + s[6] = u & 255; + u >>= 8; + s[5] = u & 255; + u >>= 8; + s[4] = u & 255; + u >>= 8; + s[3] = u & 255; + u >>= 8; + s[2] = u & 255; + u >>= 8; + s[1] = u & 255; + u >>= 8; + s[0] = u & 255; } -void uint64_unpack(char s[8],uint64 *u) +void uint64_unpack(char s[8], uint64 *u) { uint64 result; - result = (unsigned char) s[7]; result <<= 8; - result += (unsigned char) s[6]; result <<= 8; - result += (unsigned char) s[5]; result <<= 8; - result += (unsigned char) s[4]; result <<= 8; - result += (unsigned char) s[3]; result <<= 8; - result += (unsigned char) s[2]; result <<= 8; - result += (unsigned char) s[1]; result <<= 8; - result += (unsigned char) s[0]; + result = (unsigned char)s[7]; + result <<= 8; + result += (unsigned char)s[6]; + result <<= 8; + result += (unsigned char)s[5]; + result <<= 8; + result += (unsigned char)s[4]; + result <<= 8; + result += (unsigned char)s[3]; + result <<= 8; + result += (unsigned char)s[2]; + result <<= 8; + result += (unsigned char)s[1]; + result <<= 8; + result += (unsigned char)s[0]; *u = result; } -void uint64_unpack_big(char s[8],uint64 *u) +void uint64_unpack_big(char s[8], uint64 *u) { uint64 result; - result = (unsigned char) s[0]; result <<= 8; - result += (unsigned char) s[1]; result <<= 8; - result += (unsigned char) s[2]; result <<= 8; - result += (unsigned char) s[3]; result <<= 8; - result += (unsigned char) s[4]; result <<= 8; - result += (unsigned char) s[5]; result <<= 8; - result += (unsigned char) s[6]; result <<= 8; - result += (unsigned char) s[7]; + result = (unsigned char)s[0]; + result <<= 8; + result += (unsigned char)s[1]; + result <<= 8; + result += (unsigned char)s[2]; + result <<= 8; + result += (unsigned char)s[3]; + result <<= 8; + result += (unsigned char)s[4]; + result <<= 8; + result += (unsigned char)s[5]; + result <<= 8; + result += (unsigned char)s[6]; + result <<= 8; + result += (unsigned char)s[7]; *u = result; } diff --git a/src/uint8p.c b/src/uint8p.c index a7a1460..71513cd 100644 --- a/src/uint8p.c +++ b/src/uint8p.c @@ -6,34 +6,36 @@ @brief packing/unpacking 8 bit int to/from char string */ -void uint8_pack(char s[2],uint8 u) +void uint8_pack(char s[2], uint8 u) { s[0] = u & 255; s[1] = u >> 4; } -void uint8_pack_big(char s[2],uint8 u) +void uint8_pack_big(char s[2], uint8 u) { s[1] = u & 255; s[0] = u >> 4; } -void uint8_unpack(char s[2],uint8 *u) +void uint8_unpack(char s[2], uint8 *u) { uint8 result; - result = (unsigned char) s[1]; result <<= 4; - result += (unsigned char) s[0]; + result = (unsigned char)s[1]; + result <<= 4; + result += (unsigned char)s[0]; *u = result; } -void uint8_unpack_big(char s[2],uint8 *u) +void uint8_unpack_big(char s[2], uint8 *u) { uint8 result; - result = (unsigned char) s[0]; result <<= 4; - result += (unsigned char) s[1]; + result = (unsigned char)s[0]; + result <<= 4; + result += (unsigned char)s[1]; *u = result; } @@ -1,5 +1,6 @@ #include <sys/types.h> #include <sys/wait.h> + #include "logmsg.h" /** @@ -11,15 +12,14 @@ int wait_nohang(int *wstat) { - return waitpid(-1,wstat,WNOHANG); + return waitpid(-1, wstat, WNOHANG); } -int wait_pid(int *wstat,int pid) +int wait_pid(int *wstat, int pid) { int r; - do - r = waitpid(pid,wstat,0); + do r = waitpid(pid, wstat, 0); while ((r == -1) && (errno == EINTR)); return r; } |