From 695843c79edcbda68b05cc67efb4f38f967d09b4 Mon Sep 17 00:00:00 2001 From: leitner Date: Thu, 23 Apr 2015 15:15:08 +0000 Subject: [PATCH] small buffer optimization fix botched noipv6 case in buffer_tcp6 --- Makefile | 31 +++++++++++++++++-------------- array/iarray_allocate.c | 5 ++++- buffer.h | 6 ++++-- buffer/buffer_put.c | 17 ++++++++++++++--- socket/socket_fastopen.c | 2 ++ socket/socket_fastopen_connect4.c | 4 ++++ socket/socket_fastopen_connect6.c | 6 +++++- socket/socket_quickack.c | 2 ++ socket/socket_tcp6.c | 13 ++++++++++++- 9 files changed, 64 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 2143ce0..a445edc 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ MAN3DIR=${prefix}/man/man3 LIBS=byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a \ buffer.a mmap.a taia.a tai.a dns.a case.a mult.a array.a io.a \ -textcode.a cdb.a +textcode.a cdb.a critbit.a all: ent $(LIBS) libowfat.a libsocket t @@ -136,7 +136,7 @@ cdb.o: cdb/cdb.c byte.h cdb.h uint32.h uint64.h cdb_hash.o: cdb/cdb_hash.c cdb.h uint32.h uint64.h cdb_make.o: cdb/cdb_make.c cdb.h uint32.h uint64.h cdb_make.h buffer.h cdb_traverse.o: cdb/cdb_traverse.c cdb.h uint32.h uint64.h -critbit.o: critbit/critbit.c +critbit.o: critbit/critbit.c critbit.h dns_dfd.o: dns/dns_dfd.c byte.h dns.h stralloc.h iopause.h taia.h tai.h \ uint64.h uint32.h dns_domain.o: dns/dns_domain.c case.h byte.h dns.h stralloc.h iopause.h \ @@ -609,8 +609,8 @@ fmt_yenc.o: textcode/fmt_yenc.c fmt.h byte.h textcode.h scan_base64.o: textcode/scan_base64.c textcode.h haveinline.h scan_cescape.o: textcode/scan_cescape.c fmt.h byte.h textcode.h scan.h scan_hexdump.o: textcode/scan_hexdump.c fmt.h byte.h textcode.h scan.h -scan_html.o: textcode/scan_html.c fmt.h byte.h textcode.h haveinline.h \ - scan.h case.h str.h +scan_html.o: textcode/scan_html.c entities.h fmt.h byte.h textcode.h \ + haveinline.h scan.h case.h str.h scan_jsonescape.o: textcode/scan_jsonescape.c fmt.h byte.h textcode.h \ scan.h scan_ldapescape.o: textcode/scan_ldapescape.c fmt.h byte.h textcode.h \ @@ -652,7 +652,8 @@ winsock2errno.o: unix/winsock2errno.c t.o: t.c fmt.h byte.h scan.h str.h uint16.h uint32.h stralloc.h socket.h \ buffer.h ip4.h ip6.h mmap.h open.h textcode.h dns.h iopause.h taia.h \ tai.h uint64.h case.h errmsg.h iob.h io.h array.h safemult.h iarray.h \ - CAS.h io_internal.h haveepoll.h havekqueue.h havedevpoll.h havesigio.h + critbit.h CAS.h io_internal.h haveepoll.h havekqueue.h havedevpoll.h \ + havesigio.h BYTE_OBJS=byte_chr.o byte_copy.o byte_copyr.o byte_diff.o byte_equal_notimingattack.o byte_rchr.o byte_zero.o FMT_OBJS=fmt_8long.o fmt_8longlong.o fmt_asn1derlength.o fmt_asn1dertag.o fmt_double.o fmt_escapecharc.o fmt_escapecharhtml.o fmt_escapecharjson.o fmt_escapecharquotedprintable.o fmt_escapecharquotedprintableutf8.o fmt_escapecharxml.o fmt_fill.o fmt_httpdate.o fmt_human.o fmt_humank.o fmt_long.o fmt_longlong.o fmt_minus.o fmt_pad.o fmt_plusminus.o fmt_str.o fmt_strm_internal.o fmt_strn.o fmt_tohex.o fmt_ulong.o fmt_ulong0.o fmt_ulonglong.o fmt_utf8.o fmt_xlong.o fmt_xlonglong.o fmt_xmlescape.o SCAN_OBJS=scan_8int.o scan_8long.o scan_8longlong.o scan_8longn.o scan_8short.o scan_asn1derlength.o scan_asn1dertag.o scan_charsetnskip.o scan_double.o scan_fromhex.o scan_httpdate.o scan_int.o scan_long.o scan_longlong.o scan_longn.o scan_netstring.o scan_noncharsetnskip.o scan_nonwhitenskip.o scan_plusminus.o scan_short.o scan_uint.o scan_ulong.o scan_ulonglong.o scan_ulongn.o scan_ushort.o scan_utf8.o scan_whitenskip.o scan_xint.o scan_xlong.o scan_xlonglong.o scan_xlongn.o scan_xshort.o @@ -661,7 +662,7 @@ UINT_OBJS=uint16_pack.o uint16_pack_big.o uint16_read.o uint16_read_big.o uint16 OPEN_OBJS=open_append.o open_excl.o open_read.o open_rw.o open_trunc.o open_write.o openreadclose.o readclose.o STRALLOC_OBJS=stralloc_append.o stralloc_cat.o stralloc_catb.o stralloc_catlong0.o stralloc_catm_internal.o stralloc_cats.o stralloc_catulong0.o stralloc_chomp.o stralloc_chop.o stralloc_copy.o stralloc_copyb.o stralloc_copys.o stralloc_diff.o stralloc_diffs.o stralloc_free.o stralloc_init.o stralloc_ready.o stralloc_readyplus.o stralloc_starts.o stralloc_zero.o UNIX_OBJS=iopause.o ndelay_off.o ndelay_on.o winsock2errno.o -SOCKET_OBJS=fmt_ip4.o fmt_ip6.o fmt_ip6_flat.o fmt_ip6c.o fmt_ip6if.o fmt_ip6ifc.o init.o scan_ip4.o scan_ip6.o scan_ip6_flat.o scan_ip6if.o socket_accept4.o socket_accept6.o socket_bind4.o socket_bind4_reuse.o socket_bind6.o socket_bind6_reuse.o socket_broadcast.o socket_connect4.o socket_connect6.o socket_connected.o socket_deferaccept.o socket_getifidx.o socket_getifname.o socket_ip4loopback.o socket_listen.o socket_local4.o socket_local6.o socket_mchopcount6.o socket_mcjoin4.o socket_mcjoin6.o socket_mcleave4.o socket_mcleave6.o socket_mcloop4.o socket_mcloop6.o socket_mcttl4.o socket_noipv6.o socket_recv4.o socket_recv6.o socket_remote4.o socket_remote6.o socket_sctp4.o socket_sctp4b.o socket_sctp6.o socket_sctp6b.o socket_send4.o socket_send6.o socket_tcp4.o socket_tcp4b.o socket_tcp6.o socket_tcp6b.o socket_tryreservein.o socket_udp4.o socket_udp6.o socket_v4mappedprefix.o socket_v6any.o socket_v6loopback.o +SOCKET_OBJS=fmt_ip4.o fmt_ip6.o fmt_ip6_flat.o fmt_ip6c.o fmt_ip6if.o fmt_ip6ifc.o init.o scan_ip4.o scan_ip6.o scan_ip6_flat.o scan_ip6if.o socket_accept4.o socket_accept6.o socket_bind4.o socket_bind4_reuse.o socket_bind6.o socket_bind6_reuse.o socket_broadcast.o socket_connect4.o socket_connect6.o socket_connected.o socket_deferaccept.o socket_fastopen.o socket_fastopen_connect4.o socket_fastopen_connect6.o socket_getifidx.o socket_getifname.o socket_ip4loopback.o socket_listen.o socket_local4.o socket_local6.o socket_mchopcount6.o socket_mcjoin4.o socket_mcjoin6.o socket_mcleave4.o socket_mcleave6.o socket_mcloop4.o socket_mcloop6.o socket_mcttl4.o socket_noipv6.o socket_quickack.o socket_recv4.o socket_recv6.o socket_remote4.o socket_remote6.o socket_sctp4.o socket_sctp4b.o socket_sctp6.o socket_sctp6b.o socket_send4.o socket_send6.o socket_tcp4.o socket_tcp4b.o socket_tcp6.o socket_tcp6b.o socket_tryreservein.o socket_udp4.o socket_udp6.o socket_v4mappedprefix.o socket_v6any.o socket_v6loopback.o BUFFER_OBJS=buffer_0.o buffer_0small.o buffer_1.o buffer_1small.o buffer_2.o buffer_close.o buffer_feed.o buffer_flush.o buffer_free.o buffer_fromarray.o buffer_frombuf.o buffer_fromsa.o buffer_get.o buffer_get_new_token_sa.o buffer_get_new_token_sa_pred.o buffer_get_token.o buffer_get_token_pred.o buffer_get_token_sa.o buffer_get_token_sa_pred.o buffer_getc.o buffer_getline.o buffer_getline_sa.o buffer_getn.o buffer_getnewline_sa.o buffer_init.o buffer_init_free.o buffer_mmapread.o buffer_munmap.o buffer_peek.o buffer_put.o buffer_put8long.o buffer_putalign.o buffer_puterror.o buffer_puterror2.o buffer_putflush.o buffer_putlong.o buffer_putlonglong.o buffer_putm_internal.o buffer_putm_internal_flush.o buffer_putnlflush.o buffer_puts.o buffer_putsa.o buffer_putsaflush.o buffer_putsalign.o buffer_putsflush.o buffer_putspace.o buffer_putulong.o buffer_putulonglong.o buffer_putxlong.o buffer_seek.o buffer_stubborn.o buffer_stubborn2.o buffer_tosa.o errmsg_iam.o errmsg_info.o errmsg_infosys.o errmsg_puts.o errmsg_warn.o errmsg_warnsys.o errmsg_write.o MMAP_OBJS=mmap_private.o mmap_read.o mmap_shared.o mmap_unmap.o TAIA_OBJS=taia_add.o taia_addsec.o taia_approx.o taia_frac.o taia_half.o taia_less.o taia_now.o taia_pack.o taia_sub.o taia_tai.o taia_uint.o taia_unpack.o @@ -669,10 +670,11 @@ TAI_OBJS=tai_add.o tai_now.o tai_pack.o tai_sub.o tai_uint.o tai_unpack.o DNS_OBJS=dns_dfd.o dns_domain.o dns_dtda.o dns_ip.o dns_ip6.o dns_ipq.o dns_ipq6.o dns_mx.o dns_name.o dns_nd.o dns_nd6.o dns_packet.o dns_random.o dns_rcip.o dns_rcrw.o dns_resolve.o dns_sortip.o dns_sortip6.o dns_transmit.o dns_txt.o CASE_OBJS=case_diffb.o case_diffs.o case_lowerb.o case_lowers.o case_starts.o MULT_OBJS=imult16.o imult32.o imult64.o range_arrayinbuf.o range_str2inbuf.o range_str4inbuf.o range_strinbuf.o umult16.o umult32.o umult64.o -ARRAY_OBJS=array_allocate.o array_bytes.o array_cat.o array_cat0.o array_catb.o array_cate.o array_cats.o array_cats0.o array_equal.o array_fail.o array_get.o array_length.o array_reset.o array_start.o array_trunc.o array_truncate.o iarray_allocate.o iarray_free.o iarray_get.o iarray_init.o -IO_OBJS=io_appendfile.o io_block.o io_canread.o io_canwrite.o io_check.o io_close.o io_closeonexec.o io_createfile.o io_dontwantread.o io_dontwantwrite.o io_eagain.o io_fd.o io_finishandshutdown.o io_getcookie.o io_mmapwritefile.o io_nonblock.o io_passfd.o io_pipe.o io_readfile.o io_readwritefile.o io_receivefd.o io_sendfile.o io_setcookie.o io_sigpipe.o io_socketpair.o io_timeout.o io_timeouted.o io_tryread.o io_tryreadtimeout.o io_trywrite.o io_trywritetimeout.o io_wait.o io_waitread.o io_waituntil.o io_waituntil2.o io_waitwrite.o io_wantread.o io_wantwrite.o iob_addbuf.o iob_addbuf_free.o iob_addbuf_internal.o iob_addbuf_munmap.o iob_addfile.o iob_addfile_close.o iob_adds.o iob_adds_free.o iob_bytesleft.o iob_free.o iob_new.o iob_prefetch.o iob_reset.o iob_send.o iob_write.o -TEXTCODE_OBJS=base64.o fmt_base64.o fmt_cescape.o fmt_foldwhitespace.o fmt_hexdump.o fmt_html.o fmt_jsonescape.o fmt_ldapescape.o fmt_ldapescape2.o fmt_quotedprintable.o fmt_to_array.o fmt_to_sa.o fmt_tofrom_array.o fmt_urlencoded.o fmt_uuencoded.o fmt_yenc.o scan_base64.o scan_cescape.o scan_hexdump.o scan_html.o scan_jsonescape.o scan_ldapescape.o scan_quotedprintable.o scan_to_array.o scan_to_sa.o scan_tofrom_array.o scan_urlencoded.o scan_uuencoded.o scan_yenc.o +ARRAY_OBJS=array_allocate.o array_bytes.o array_cat.o array_cat0.o array_catb.o array_cate.o array_cats.o array_cats0.o array_equal.o array_fail.o array_get.o array_length.o array_reset.o array_start.o array_trunc.o array_truncate.o iarray_allocate.o iarray_free.o iarray_get.o iarray_init.o iarray_length.o +IO_OBJS=io_appendfile.o io_block.o io_canread.o io_canwrite.o io_check.o io_close.o io_closeonexec.o io_createfile.o io_debugstring.o io_dontwantread.o io_dontwantwrite.o io_eagain.o io_eagain_read.o io_eagain_write.o io_fd.o io_finishandshutdown.o io_getcookie.o io_mmapwritefile.o io_nonblock.o io_passfd.o io_pipe.o io_readfile.o io_readwritefile.o io_receivefd.o io_sendfile.o io_setcookie.o io_sigpipe.o io_socketpair.o io_timedout.o io_timeout.o io_timeouted.o io_tryread.o io_tryreadtimeout.o io_trywrite.o io_trywritetimeout.o io_wait.o io_waitread.o io_waituntil.o io_waituntil2.o io_waitwrite.o io_wantread.o io_wantwrite.o iob_addbuf.o iob_addbuf_free.o iob_addbuf_internal.o iob_addbuf_munmap.o iob_addfile.o iob_addfile_close.o iob_adds.o iob_adds_free.o iob_bytesleft.o iob_free.o iob_new.o iob_prefetch.o iob_reset.o iob_send.o iob_write.o +TEXTCODE_OBJS=base64.o fmt_base64.o fmt_cescape.o fmt_foldwhitespace.o fmt_hexdump.o fmt_html.o fmt_html_tagarg.o fmt_jsonescape.o fmt_ldapescape.o fmt_ldapescape2.o fmt_quotedprintable.o fmt_to_array.o fmt_to_sa.o fmt_tofrom_array.o fmt_urlencoded.o fmt_uuencoded.o fmt_xml.o fmt_yenc.o scan_base64.o scan_cescape.o scan_hexdump.o scan_html.o scan_jsonescape.o scan_ldapescape.o scan_quotedprintable.o scan_to_array.o scan_to_sa.o scan_tofrom_array.o scan_urlencoded.o scan_uuencoded.o scan_yenc.o CDB_OBJS=cdb.o cdb_hash.o cdb_make.o cdb_traverse.o +CRITBIT_OBJS=critbit.o byte.a: $(BYTE_OBJS) fmt.a: $(FMT_OBJS) @@ -694,12 +696,13 @@ array.a: $(ARRAY_OBJS) mult.a: $(MULT_OBJS) io.a: $(IO_OBJS) cdb.a: $(CDB_OBJS) +critbit.a: $(CRITBIT_OBJS) ALL_OBJS=$(DNS_OBJS) $(BYTE_OBJS) $(FMT_OBJS) $(SCAN_OBJS) \ $(STR_OBJS) $(UINT_OBJS) $(OPEN_OBJS) $(STRALLOC_OBJS) $(UNIX_OBJS) \ $(SOCKET_OBJS) $(BUFFER_OBJS) $(MMAP_OBJS) $(TEXTCODE_OBJS) \ $(TAIA_OBJS) $(TAI_OBJS) $(CASE_OBJS) $(ARRAY_OBJS) $(MULT_OBJS) \ -$(IO_OBJS) $(CDB_OBJS) +$(IO_OBJS) $(CDB_OBJS) $(CRITBIT_OBJS) libowfat.a: $(ALL_OBJS) $(CROSS)ar cru $@ $(ALL_OBJS) @@ -734,7 +737,7 @@ INCLUDES=buffer.h byte.h fmt.h ip4.h ip6.h mmap.h scan.h socket.h str.h stralloc uint16.h uint32.h uint64.h open.h textcode.h tai.h taia.h dns.h iopause.h case.h \ openreadclose.h readclose.h ndelay.h array.h io.h safemult.h iob.h havealloca.h \ errmsg.h cdb.h cdb_make.h rangecheck.h iarray.h va_narg.h isset.h \ -compiletimeassert.h +compiletimeassert.h critbit.h install-inc: install -d $(INCLUDEDIR) @@ -755,8 +758,8 @@ uninstall: rm -f $(patsubst %.3,$(MAN3DIR)/%.3,$(notdir $(wildcard */*.3))) rm -f $(LIBDIR)/libowfat.a -VERSION=libowfat-0.30 -CURNAME=libowfat-0.30 +VERSION=libowfat-0.31 +CURNAME=libowfat-0.31 tar: clean rename rm -f dep libdep @@ -859,7 +862,7 @@ libsocket: trysocket.c socket_accept6.o socket_bind6.o socket_connect6.o socket_local6.o \ socket_mchopcount6.o socket_mcjoin6.o socket_mcleave6.o socket_mcloop6.o \ socket_recv6.o socket_remote6.o socket_sctp6b.o socket_send6.o \ -socket_tcp6b.o socket_udp6.o: haveip6.h +socket_tcp6b.o socket_tcp6.o socket_udp6.o: haveip6.h socket_accept6.o socket_connect6.o socket_local6.o socket_recv6.o \ socket_remote6.o socket_send6.o: havescope.h diff --git a/array/iarray_allocate.c b/array/iarray_allocate.c index 2dd58f3..e2d85a6 100644 --- a/array/iarray_allocate.c +++ b/array/iarray_allocate.c @@ -1,8 +1,10 @@ #include "likely.h" #include +#ifndef __MINGW32__ #include #include #include +#endif #include "iarray.h" #ifdef __dietlibc__ #include @@ -13,10 +15,11 @@ static iarray_page* new_page(size_t pagesize) { #ifdef __MINGW32__ void* x=malloc(pagesize); + if (x==0) return 0; #else void* x=mmap(0,pagesize,PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_PRIVATE,-1,0); -#endif if (x==MAP_FAILED) return 0; +#endif return (iarray_page*)x; } diff --git a/buffer.h b/buffer.h index e3dca0b..54bed5c 100644 --- a/buffer.h +++ b/buffer.h @@ -6,6 +6,8 @@ #include /* for ssize_t: */ #include +/* for strlen */ +#include #ifdef __cplusplus extern "C" { @@ -47,8 +49,8 @@ int buffer_putsflush(buffer* b,const char* x); /* as a little gcc-specific hack, if somebody calls buffer_puts with a * constant string, where we know its length at compile-time, call * buffer_put with the known length instead */ -#define buffer_puts(b,s) (__builtin_constant_p(s) ? buffer_put(b,s,sizeof(s)-1) : buffer_puts(b,s)) -#define buffer_putsflush(b,s) (__builtin_constant_p(s) ? buffer_putflush(b,s,sizeof(s)-1) : buffer_putsflush(b,s)) +#define buffer_puts(b,s) (__builtin_constant_p(s) ? buffer_put(b,s,strlen(s)) : buffer_puts(b,s)) +#define buffer_putsflush(b,s) (__builtin_constant_p(s) ? buffer_putflush(b,s,strlen(s)) : buffer_putsflush(b,s)) #endif int buffer_putm_internal(buffer*b,...); diff --git a/buffer/buffer_put.c b/buffer/buffer_put.c index ec43b57..d090b88 100644 --- a/buffer/buffer_put.c +++ b/buffer/buffer_put.c @@ -1,17 +1,28 @@ -#include "byte.h" +#include #include "buffer.h" +#ifdef __x86_64__ +#include +#endif extern int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); +#ifndef __unlikely +#ifdef __GNUC__ +#define __unlikely(x) __builtin_expect((x),0) +#else +#define __unlikely(x) (x) +#endif +#endif + int buffer_put(buffer* b,const char* buf,size_t len) { - if (len>b->a-b->p) { /* doesn't fit */ + if (__unlikely(len>b->a-b->p)) { /* doesn't fit */ if (buffer_flush(b)==-1) return -1; if (len>b->a) { if (buffer_stubborn(b->op,b->fd,buf,len,b)<0) return -1; return 0; } } - byte_copy(b->x+b->p, len, buf); + memcpy(b->x+b->p, buf, len); b->p+=len; return 0; } diff --git a/socket/socket_fastopen.c b/socket/socket_fastopen.c index 3363c2c..5c877b3 100644 --- a/socket/socket_fastopen.c +++ b/socket/socket_fastopen.c @@ -1,7 +1,9 @@ #include "socket.h" +#ifndef __MINGW32__ #include #include #include +#endif #include int socket_fastopen(int s) { diff --git a/socket/socket_fastopen_connect4.c b/socket/socket_fastopen_connect4.c index 621f58c..188c0b3 100644 --- a/socket/socket_fastopen_connect4.c +++ b/socket/socket_fastopen_connect4.c @@ -1,7 +1,11 @@ #include "socket.h" +#ifdef __MINGW32__ +#include +#else #include #include #include +#endif #include ssize_t socket_fastopen_connect4(int s,const char* ip,uint16 port,const char* buf,size_t len) { diff --git a/socket/socket_fastopen_connect6.c b/socket/socket_fastopen_connect6.c index 32d0340..33ae21f 100644 --- a/socket/socket_fastopen_connect6.c +++ b/socket/socket_fastopen_connect6.c @@ -1,7 +1,11 @@ #include "socket.h" +#ifdef __MINGW32__ +#include +#else #include -#include #include +#include +#endif #include ssize_t socket_fastopen_connect6(int s,const char* ip,uint16 port,uint32_t scope_id,const char* buf,size_t len) { diff --git a/socket/socket_quickack.c b/socket/socket_quickack.c index cd027f8..3dc1766 100644 --- a/socket/socket_quickack.c +++ b/socket/socket_quickack.c @@ -1,7 +1,9 @@ #include "socket.h" +#ifndef __MINGW32__ #include #include #include +#endif #include int socket_quickack(int s,int value) { diff --git a/socket/socket_tcp6.c b/socket/socket_tcp6.c index 1e6689c..a7c1975 100644 --- a/socket/socket_tcp6.c +++ b/socket/socket_tcp6.c @@ -9,10 +9,21 @@ #include "socket.h" #include "ndelay.h" +#ifndef EAFNOSUPPORT +#define EAFNOSUPPORT EINVAL +#endif +#ifndef EPFNOSUPPORT +#define EPFNOSUPPORT EAFNOSUPPORT +#endif +#ifndef EPROTONOSUPPORT +#define EPROTONOSUPPORT EAFNOSUPPORT +#endif + int socket_tcp6(void) { int s; #if defined(LIBC_HAS_IP6) && defined(SOCK_NONBLOCK) - if ((s=socket(noipv6?PF_INET:PF_INET6,SOCK_STREAM|SOCK_NONBLOCK,IPPROTO_TCP))>-1 || errno!=EINVAL) return s; + if ((s=socket(noipv6?PF_INET:PF_INET6,SOCK_STREAM|SOCK_NONBLOCK,IPPROTO_TCP))>-1 || + (errno!=EINVAL && errno!=EAFNOSUPPORT && errno!=EPFNOSUPPORT && errno!=EPROTONOSUPPORT)) return s; #endif s=socket_tcp6b(); if (s==-1) return -1;