Skip to content

Commit

Permalink
fix/native: fix warnings in the native code
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Dec 6, 2017
1 parent b763f95 commit 0ff95f5
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 43 deletions.
42 changes: 42 additions & 0 deletions ifaddrs-sys/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
// Copyright 2017 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement. This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

//! ifaddrs-sys
#![doc(html_logo_url = "https://raw.githubusercontent.com/maidsafe/QA/master/Images/
maidsafe_logo.png",
html_favicon_url = "http://maidsafe.net/img/favicon.ico",
html_root_url = "http://maidsafe.github.io/ifaddrs")]
// For explanation of lint checks, run `rustc -W help` or see
// https://github.com/maidsafe/QA/blob/master/Documentation/Rust%20Lint%20Checks.md
#![forbid(exceeding_bitshifts, mutable_transmutes, no_mangle_const_items, unknown_crate_types,
warnings)]
#![deny(bad_style, deprecated, improper_ctypes, missing_docs, non_shorthand_field_patterns,
overflowing_literals, plugin_as_library, private_no_mangle_fns, private_no_mangle_statics,
stable_features, unconditional_recursion, unknown_lints, unsafe_code, unused,
unused_allocation, unused_attributes, unused_comparisons, unused_features, unused_parens,
while_true)]
#![warn(trivial_casts, trivial_numeric_casts, unused_extern_crates, unused_import_braces,
unused_qualifications, unused_results)]
#![allow(box_pointers, missing_copy_implementations, missing_debug_implementations,
variant_size_differences)]
#![cfg_attr(feature = "cargo-clippy",
deny(clippy, unicode_not_nfc, wrong_pub_self_convention, option_unwrap_used))]
#![cfg_attr(feature = "cargo-clippy", allow(use_debug, too_many_arguments))]

#![cfg(target_os = "android")]
extern crate libc;

use libc::*;

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug)]
pub struct ifaddrs {
Expand Down
86 changes: 43 additions & 43 deletions ifaddrs-sys/native/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int netlink_socket(void)
{
return -1;
}

struct sockaddr_nl l_addr;
memset(&l_addr, 0, sizeof(l_addr));
l_addr.nl_family = AF_NETLINK;
Expand All @@ -59,7 +59,7 @@ static int netlink_socket(void)
close(l_socket);
return -1;
}

return l_socket;
}

Expand All @@ -72,14 +72,14 @@ static int netlink_send(int p_socket, int p_request)
} l_data;

memset(&l_data, 0, sizeof(l_data));

l_data.m_hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
l_data.m_hdr.nlmsg_type = p_request;
l_data.m_hdr.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
l_data.m_hdr.nlmsg_pid = 0;
l_data.m_hdr.nlmsg_seq = p_socket;
l_data.m_msg.rtgen_family = AF_UNSPEC;

struct sockaddr_nl l_addr;
memset(&l_addr, 0, sizeof(l_addr));
l_addr.nl_family = AF_NETLINK;
Expand All @@ -102,7 +102,7 @@ static int netlink_recv(int p_socket, void *p_buffer, size_t p_len)
l_msg.msg_controllen = 0;
l_msg.msg_flags = 0;
int l_result = recvmsg(p_socket, &l_msg, 0);

if(l_result < 0)
{
if(errno == EINTR)
Expand All @@ -111,7 +111,7 @@ static int netlink_recv(int p_socket, void *p_buffer, size_t p_len)
}
return -2;
}

if(l_msg.msg_flags & MSG_TRUNC)
{ // buffer was too small
return -1;
Expand All @@ -124,7 +124,7 @@ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_don
{
size_t l_size = 4096;
void *l_buffer = NULL;

for(;;)
{
free(l_buffer);
Expand All @@ -133,7 +133,7 @@ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_don
{
return NULL;
}

int l_read = netlink_recv(p_socket, l_buffer, l_size);
*p_size = l_read;
if(l_read == -2)
Expand All @@ -150,13 +150,13 @@ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_don
{
continue;
}

if(l_hdr->nlmsg_type == NLMSG_DONE)
{
*p_done = 1;
break;
}

if(l_hdr->nlmsg_type == NLMSG_ERROR)
{
free(l_buffer);
Expand All @@ -165,7 +165,7 @@ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_don
}
return l_buffer;
}

l_size *= 2;
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ static NetlinkList *getResultList(int p_socket, int p_request)
freeResultList(l_list);
return NULL;
}

NetlinkList *l_item = newListItem(l_hdr, l_size);
if (!l_item)
{
Expand Down Expand Up @@ -300,12 +300,12 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
size_t l_nameSize = 0;
size_t l_addrSize = 0;
size_t l_dataSize = 0;

size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
struct rtattr *l_rta;
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void *l_rtaData = RTA_DATA(l_rta);
// void *l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
switch(l_rta->rta_type)
{
Expand All @@ -323,25 +323,25 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
break;
}
}

struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
if (l_entry == NULL)
{
return -1;
}
memset(l_entry, 0, sizeof(struct ifaddrs));
l_entry->ifa_name = "";

char *l_index = ((char *)l_entry) + sizeof(struct ifaddrs);
char *l_name = l_index + sizeof(int);
char *l_addr = l_name + l_nameSize;
char *l_data = l_addr + l_addrSize;

// save the interface index so we can look it up when handling the addresses.
memcpy(l_index, &l_info->ifi_index, sizeof(int));

l_entry->ifa_flags = l_info->ifi_flags;

l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
Expand Down Expand Up @@ -380,7 +380,7 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
break;
}
}

addToEnd(p_resultList, l_entry);
return 0;
}
Expand All @@ -398,7 +398,7 @@ static struct ifaddrs *findInterface(int p_index, struct ifaddrs **p_links, int
{
return l_cur;
}

l_cur = l_cur->ifa_next;
++l_num;
}
Expand All @@ -409,24 +409,24 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
{
struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
struct ifaddrs *l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);

if(l_info->ifa_family == AF_PACKET)
{
return 0;
}

size_t l_nameSize = 0;
size_t l_addrSize = 0;

int l_addedNetmask = 0;

size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
struct rtattr *l_rta;
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void *l_rtaData = RTA_DATA(l_rta);
// void *l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);

switch(l_rta->rta_type)
{
case IFA_ADDRESS:
Expand All @@ -446,24 +446,24 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
break;
}
}

struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
if (l_entry == NULL)
{
return -1;
}
memset(l_entry, 0, sizeof(struct ifaddrs));
l_entry->ifa_name = (l_interface ? l_interface->ifa_name : "");

char *l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
char *l_addr = l_name + l_nameSize;

l_entry->ifa_flags = l_info->ifa_flags;
if(l_interface)
{
l_entry->ifa_flags |= l_interface->ifa_flags;
}

l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
Expand All @@ -484,7 +484,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
}
}

if(l_rta->rta_type == IFA_ADDRESS)
{ // apparently in a point-to-point network IFA_ADDRESS contains the dest address and IFA_LOCAL contains the local address
if(l_entry->ifa_addr)
Expand Down Expand Up @@ -520,7 +520,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
break;
}
}

if(l_entry->ifa_addr && (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
{
unsigned l_maxPrefix = (l_entry->ifa_addr->sa_family == AF_INET ? 32 : 128);
Expand All @@ -535,11 +535,11 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
{
l_mask[i] = 0xff << (8 - (l_prefix % 8));
}

makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr *)l_addr, l_mask, l_maxPrefix / 8);
l_entry->ifa_netmask = (struct sockaddr *)l_addr;
}

addToEnd(p_resultList, l_entry);
return 0;
}
Expand All @@ -557,12 +557,12 @@ static int interpretLinks(int p_socket, NetlinkList *p_netlinkList, struct ifadd
{
continue;
}

if(l_hdr->nlmsg_type == NLMSG_DONE)
{
break;
}

if(l_hdr->nlmsg_type == RTM_NEWLINK)
{
if(interpretLink(l_hdr, p_resultList) == -1)
Expand All @@ -588,12 +588,12 @@ static int interpretAddrs(int p_socket, NetlinkList *p_netlinkList, struct ifadd
{
continue;
}

if(l_hdr->nlmsg_type == NLMSG_DONE)
{
break;
}

if(l_hdr->nlmsg_type == RTM_NEWADDR)
{
if (interpretAddr(l_hdr, p_resultList, p_numLinks) == -1)
Expand All @@ -613,35 +613,35 @@ int getifaddrs(struct ifaddrs **ifap)
return -1;
}
*ifap = NULL;

int l_socket = netlink_socket();
if(l_socket < 0)
{
return -1;
}

NetlinkList *l_linkResults = getResultList(l_socket, RTM_GETLINK);
if(!l_linkResults)
{
close(l_socket);
return -1;
}

NetlinkList *l_addrResults = getResultList(l_socket, RTM_GETADDR);
if(!l_addrResults)
{
close(l_socket);
freeResultList(l_linkResults);
return -1;
}

int l_result = 0;
int l_numLinks = interpretLinks(l_socket, l_linkResults, ifap);
if(l_numLinks == -1 || interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
{
l_result = -1;
}

freeResultList(l_linkResults);
freeResultList(l_addrResults);
close(l_socket);
Expand Down
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
92a805d8ccbab7e4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":11807999472408288889,"features":"[]","target":16068348056699470742,"profile":731176819336294830,"deps":[],"local":[{"Precalculated":"0.3.53"}],"rustflags":[]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
26118f05d69ffbdb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":0,"features":"","target":0,"profile":0,"deps":[],"local":[{"Precalculated":"1512576692.311731012s (/home/adam/projects/maidsafe/ifaddrs/ifaddrs-sys/lib.rs)"}],"rustflags":[]}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
88ef17e787d4b006
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":11807999472408288889,"features":"[]","target":4288001497584489937,"profile":14119667493710126711,"deps":[["libc v0.2.30",16278783378035228853]],"local":[{"MtimeBased":[[1512576692,551727133],"/home/adam/projects/maidsafe/ifaddrs/ifaddrs-sys/target/debug/.fingerprint/ifaddrs-sys-726cd8665aa2f5ad/dep-lib-ifaddrs_sys-726cd8665aa2f5ad"]}],"rustflags":[]}
Loading

0 comments on commit 0ff95f5

Please sign in to comment.