diff options
| author | Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org> | 2011-12-07 13:30:56 +0059 |
|---|---|---|
| committer | Ricardo Cerqueira <cyanogenmod@cerqueira.org> | 2012-07-10 23:09:00 +0100 |
| commit | 826d75fb6dcefe8a6c6d4d54a7d215553945812a (patch) | |
| tree | be27e9ac12aebda33e7f3ec90b887acbb195c42d /libnetutils | |
| parent | 3853a7c04ef517d940e566c65c293193ddbcd9c1 (diff) | |
| download | system_core-826d75fb6dcefe8a6c6d4d54a7d215553945812a.tar.gz system_core-826d75fb6dcefe8a6c6d4d54a7d215553945812a.tar.bz2 system_core-826d75fb6dcefe8a6c6d4d54a7d215553945812a.zip | |
libcutils: Fix aliasing violations
Fix aliasing violtations that caused a need for the
code to be compiled with -fno-strict-aliasing
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
libnetutils: Fix aliasing violations
This allows us to build it with more compiler optimizations
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
libcutils: Fix aliasing violation
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
core: Fix build in ISO C++11 mode
Fix compatibility with ISO C++11 compilers
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
Change-Id: I8f9aa775b5681d4d8c5202a1a1935acb4efa4171
adb: Don't force -O2
Don't force -O2 over -O3 -- the O2 hardcode is there to force
optimizations, not to reduce them...
Change-Id: Ic75eeb767db4926f519580fba8f5f7b8e593df4f
Signed-off-by: Bernhard Rosenkraenzer <Bernhard.Rosenkranzer@linaro.org>
Diffstat (limited to 'libnetutils')
| -rw-r--r-- | libnetutils/ifc_utils.c | 50 | ||||
| -rw-r--r-- | libnetutils/packet.c | 17 |
2 files changed, 40 insertions, 27 deletions
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index c52268ab..a3579a4c 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -248,7 +248,11 @@ int ifc_set_addr(const char *name, in_addr_t addr) int ifc_act_on_address(int action, const char *name, const char *address, int prefixlen) { int ifindex, s, len, ret; - struct sockaddr_storage ss; + union { + struct sockaddr_storage ss; + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + } sa; void *addr; size_t addrlen; struct { @@ -260,11 +264,13 @@ int ifc_act_on_address(int action, const char *name, const char *address, NLMSG_ALIGN(INET6_ADDRLEN)]; } req; struct rtattr *rta; - struct nlmsghdr *nh; struct nlmsgerr *err; - char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + - NLMSG_ALIGN(sizeof(struct nlmsgerr)) + - NLMSG_ALIGN(sizeof(struct nlmsghdr))]; + union { + struct nlmsghdr nh; + char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + + NLMSG_ALIGN(sizeof(struct nlmsgerr)) + + NLMSG_ALIGN(sizeof(struct nlmsghdr))]; + } buf; // Get interface ID. ifindex = if_nametoindex(name); @@ -273,19 +279,17 @@ int ifc_act_on_address(int action, const char *name, const char *address, } // Convert string representation to sockaddr_storage. - ret = string_to_ip(address, &ss); + ret = string_to_ip(address, &sa.ss); if (ret) { return ret; } // Determine address type and length. - if (ss.ss_family == AF_INET) { - struct sockaddr_in *sin = (struct sockaddr_in *) &ss; - addr = &sin->sin_addr; + if (sa.ss.ss_family == AF_INET) { + addr = &sa.sin.sin_addr; addrlen = INET_ADDRLEN; - } else if (ss.ss_family == AF_INET6) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss; - addr = &sin6->sin6_addr; + } else if (sa.ss.ss_family == AF_INET6) { + addr = &sa.sin6.sin6_addr; addrlen = INET6_ADDRLEN; } else { return -EAFNOSUPPORT; @@ -301,7 +305,7 @@ int ifc_act_on_address(int action, const char *name, const char *address, req.n.nlmsg_pid = getpid(); // Interface address message header. - req.r.ifa_family = ss.ss_family; + req.r.ifa_family = sa.ss.ss_family; req.r.ifa_prefixlen = prefixlen; req.r.ifa_index = ifindex; @@ -318,18 +322,17 @@ int ifc_act_on_address(int action, const char *name, const char *address, return -errno; } - len = recv(s, buf, sizeof(buf), 0); + len = recv(s, buf.buf, sizeof(buf.buf), 0); close(s); if (len < 0) { return -errno; } // Parse the acknowledgement to find the return code. - nh = (struct nlmsghdr *) buf; - if (!NLMSG_OK(nh, (unsigned) len) || nh->nlmsg_type != NLMSG_ERROR) { + if (!NLMSG_OK(&buf.nh, (unsigned) len) || buf.nh.nlmsg_type != NLMSG_ERROR) { return -EINVAL; } - err = NLMSG_DATA(nh); + err = NLMSG_DATA(&buf.nh); // Return code is negative errno. return err->error; @@ -463,7 +466,10 @@ int ifc_get_addr(const char *name, in_addr_t *addr) if (ret < 0) { *addr = 0; } else { - *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; + struct sockaddr_in in; + memcpy(&in, &ifr.ifr_addr, sizeof(ifr.ifr_addr)); + memcpy(&addr, &in.sin_addr.s_addr, sizeof(struct sockaddr_in)); + //*addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; } } return ret; @@ -478,7 +484,9 @@ int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) { *addr = 0; } else { - *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; + struct sockaddr_in in; + memcpy(&in, &ifr.ifr_addr, sizeof(in)); + *addr = in.sin_addr.s_addr; } } @@ -486,8 +494,10 @@ int ifc_get_info(const char *name, in_addr_t *addr, int *prefixLength, unsigned if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) { *prefixLength = 0; } else { + struct sockaddr_in in; + memcpy(&in, &ifr.ifr_addr, sizeof(in)); *prefixLength = ipv4NetmaskToPrefixLength((int) - ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr); + in.sin_addr.s_addr); } } diff --git a/libnetutils/packet.c b/libnetutils/packet.c index 3ec83fe0..c1c6381f 100644 --- a/libnetutils/packet.c +++ b/libnetutils/packet.c @@ -42,20 +42,23 @@ int fatal(); int open_raw_socket(const char *ifname, uint8_t *hwaddr, int if_index) { int s, flag; - struct sockaddr_ll bindaddr; + union { + struct sockaddr_ll ll; + struct sockaddr generic; + } bindaddr; if((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { return fatal("socket(PF_PACKET)"); } memset(&bindaddr, 0, sizeof(bindaddr)); - bindaddr.sll_family = AF_PACKET; - bindaddr.sll_protocol = htons(ETH_P_IP); - bindaddr.sll_halen = ETH_ALEN; - memcpy(bindaddr.sll_addr, hwaddr, ETH_ALEN); - bindaddr.sll_ifindex = if_index; + bindaddr.ll.sll_family = AF_PACKET; + bindaddr.ll.sll_protocol = htons(ETH_P_IP); + bindaddr.ll.sll_halen = ETH_ALEN; + memcpy(bindaddr.ll.sll_addr, hwaddr, ETH_ALEN); + bindaddr.ll.sll_ifindex = if_index; - if (bind(s, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) { + if (bind(s, &bindaddr.generic, sizeof(bindaddr.ll)) < 0) { return fatal("Cannot bind raw socket to interface"); } |
