From a771e7c0057d0f506f8cdf67be39268e1c9f0735 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Tue, 21 Apr 2009 23:48:38 +0000 Subject: *: remove check for errors on getsockaddr in cases we know they can't happen libbb: make get_sock_lsa use only one getsockaddr syscall, not two function old new delta get_sock_lsa 72 101 +29 do_iplink 1151 1137 -14 arping_main 1585 1569 -16 dolisten 789 755 -34 xrtnl_open 161 94 -67 --- libbb/xconnect.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'libbb') diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 1b4f4f7..f853e95 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -37,16 +37,21 @@ int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface) len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd) { - len_and_sockaddr *lsa; - socklen_t len = 0; + len_and_sockaddr lsa; + len_and_sockaddr *lsa_ptr; - /* Can be optimized to do only one getsockname() */ - if (getsockname(fd, NULL, &len) != 0) + lsa.len = LSA_SIZEOF_SA; + if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0) return NULL; - lsa = xzalloc(LSA_LEN_SIZE + len); - lsa->len = len; - getsockname(fd, &lsa->u.sa, &lsa->len); - return lsa; + + lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len); + if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */ + lsa_ptr->len = lsa.len; + getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len); + } else { + memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len); + } + return lsa_ptr; } void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) -- cgit v1.1