*: 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
This commit is contained in:
Denis Vlasenko 2009-04-21 23:48:38 +00:00
parent e6b10ef0a7
commit a771e7c005
6 changed files with 37 additions and 31 deletions

View File

@ -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)

View File

@ -348,9 +348,10 @@ int arping_main(int argc UNUSED_PARAM, char **argv)
if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1)
bb_perror_msg("setsockopt(SO_DONTROUTE)");
xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) {
bb_perror_msg_and_die("getsockname");
}
getsockname(probe_fd, (struct sockaddr *) &saddr, &alen);
//never happens:
//if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1)
// bb_perror_msg_and_die("getsockname");
if (saddr.sin_family != AF_INET)
bb_error_msg_and_die("no IP address configured");
src = saddr.sin_addr;
@ -365,10 +366,10 @@ int arping_main(int argc UNUSED_PARAM, char **argv)
{
socklen_t alen = sizeof(me);
if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1) {
bb_perror_msg_and_die("getsockname");
}
getsockname(sock_fd, (struct sockaddr *) &me, &alen);
//never happens:
//if (getsockname(sock_fd, (struct sockaddr *) &me, &alen) == -1)
// bb_perror_msg_and_die("getsockname");
}
if (me.sll_halen == 0) {
bb_error_msg(err_str, "is not ARPable (no ll address)");

View File

@ -112,11 +112,11 @@ static int get_address(char *dev, int *htype)
me.sll_ifindex = ifr.ifr_ifindex;
me.sll_protocol = htons(ETH_P_LOOP);
xbind(s, (struct sockaddr*)&me, sizeof(me));
alen = sizeof(me);
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
bb_perror_msg_and_die("getsockname");
}
getsockname(s, (struct sockaddr*)&me, &alen);
//never happens:
//if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
// bb_perror_msg_and_die("getsockname");
close(s);
*htype = me.sll_hatype;
return me.sll_halen;

View File

@ -26,22 +26,23 @@ int FAST_FUNC xrtnl_open(struct rtnl_handle *rth/*, unsigned subscriptions*/)
{
socklen_t addr_len;
memset(rth, 0, sizeof(rth));
memset(rth, 0, sizeof(*rth));
rth->fd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
memset(&rth->local, 0, sizeof(rth->local));
rth->local.nl_family = AF_NETLINK;
/*rth->local.nl_groups = subscriptions;*/
xbind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local));
addr_len = sizeof(rth->local);
getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len);
/* too much paranoia
if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0)
bb_perror_msg_and_die("getsockname");
if (addr_len != sizeof(rth->local))
bb_error_msg_and_die("wrong address length %d", addr_len);
if (rth->local.nl_family != AF_NETLINK)
bb_error_msg_and_die("wrong address family %d", rth->local.nl_family);
*/
rth->seq = time(NULL);
return 0;
}

View File

@ -10,8 +10,7 @@
PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
struct rtnl_handle
{
struct rtnl_handle {
int fd;
struct sockaddr_nl local;
struct sockaddr_nl peer;

View File

@ -278,9 +278,9 @@ static void dolisten(void)
random unknown port is probably not very useful without "netstat". */
if (o_verbose) {
char *addr;
rr = getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
if (rr < 0)
bb_perror_msg_and_die("getsockname after bind");
getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
//if (rr < 0)
// bb_perror_msg_and_die("getsockname after bind");
addr = xmalloc_sockaddr2dotted(&ouraddr->u.sa);
fprintf(stderr, "listening on %s ...\n", addr);
free(addr);
@ -359,9 +359,9 @@ create new one, and bind() it. TODO */
doing a listen-on-any on a multihomed machine. This allows one to
offer different services via different alias addresses, such as the
"virtual web site" hack. */
rr = getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
if (rr < 0)
bb_perror_msg_and_die("getsockname after accept");
getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
//if (rr < 0)
// bb_perror_msg_and_die("getsockname after accept");
}
if (o_verbose) {