network-info 0.1.0.1 → 0.2
raw patch · 11 files changed
+166/−138 lines, 11 filesdep −network-infodep ~basePVP ok
version bump matches the API change (PVP)
Dependencies removed: network-info
Dependency ranges changed: base
API changes (from Hackage documentation)
- Network.Info: IPv4 :: !!Word32 -> IPv4
+ Network.Info: IPv4 :: {-# UNPACK #-} !Word32 -> IPv4
- Network.Info: IPv6 :: !!Word32 -> !!Word32 -> !!Word32 -> !!Word32 -> IPv6
+ Network.Info: IPv6 :: {-# UNPACK #-} !Word32 -> {-# UNPACK #-} !Word32 -> {-# UNPACK #-} !Word32 -> {-# UNPACK #-} !Word32 -> IPv6
- Network.Info: MAC :: !!Word8 -> !!Word8 -> !!Word8 -> !!Word8 -> !!Word8 -> !!Word8 -> MAC
+ Network.Info: MAC :: {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> {-# UNPACK #-} !Word8 -> MAC
Files
- README.mkd +5/−4
- cbits/network-linux.c +0/−79
- cbits/network-unix.c +91/−0
- network-info.cabal +25/−38
- run-tests.bat +0/−2
- run-tests.sh +0/−2
- test/Main.hs +0/−13
- test/network-info-test.cabal +24/−0
- test/run-tests.bat +2/−0
- test/run-tests.sh +6/−0
- test/src/Main.hs +13/−0
README.mkd view
@@ -9,7 +9,8 @@ Compatibility -------------- -network-info has been tested and is known to work on Ubuntu 10.04 and-Windows XP. It probably works on other flavours of Linux and Windows as-well. I haven't tried to get it working on BSD or Mac OS X yet but it-should be quite similar if not the same as the code for Linux.+network-info has been tested and is known to work on Ubuntu 10.10, Mac+OS X 10.6.4 and Windows XP. It probably works on other flavours of+Linux, OS X and Windows as well. I haven't tried to get it working on+BSD yet, but it should be quite similar if not the same as the code for+OS X.
− cbits/network-linux.c
@@ -1,79 +0,0 @@-#include <stdio.h>-#include <stdlib.h>-#include <string.h>-#include <wchar.h>--#include <ifaddrs.h>-#include <netdb.h>-#include <netpacket/packet.h>--#include "network.h"-#include "common.h"---void maccopy(unsigned char *dst, struct sockaddr *addr)-{- /* TODO check that sll_halen is equal to 6 (MAC_SIZE) */- memcpy(dst, ((struct sockaddr_ll *)addr)->sll_addr, MAC_SIZE);-}--struct network_interface *add_interface(struct network_interface *ns, const wchar_t *name, int max_ns)-{- int i;- for (i = 0; i < max_ns; i++) {- if (wcsempty(ns[i].name)) {- wszcopy(ns[i].name, name, NAME_SIZE);- return &ns[i];- } else if (wcscmp(ns[i].name, name) == 0) {- return &ns[i];- }- }- return NULL;-}--int count_interfaces(struct network_interface *ns, int max_ns)-{- int i;- for (i = 0; i < max_ns; i++) {- if (wcsempty(ns[i].name)) {- break;- }- }- return i;-}--int c_get_network_interfaces(struct network_interface *ns, int max_ns)-{- struct network_interface *n;- struct ifaddrs *ifaddr, *ifa;- struct sockaddr *addr;- wchar_t name[NAME_SIZE];- int family, error;- - error = getifaddrs(&ifaddr);- if (error != 0) {- perror("getifaddrs");- return 0;- }-- memset(ns, 0, sizeof(struct network_interface) * max_ns);-- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {- mbswszcopy(name, ifa->ifa_name, NAME_SIZE);- addr = ifa->ifa_addr;- family = addr->sa_family;-- n = add_interface(ns, name, max_ns);-- if (family == AF_INET) {- ipv4copy(&n->ip_address, addr);- } else if (family == AF_INET6) {- ipv6copy(&n->ip6_address, addr);- } else if (family == AF_PACKET) {- maccopy(n->mac_address, addr);- }- }-- freeifaddrs(ifaddr);- return count_interfaces(ns, max_ns);-}
+ cbits/network-unix.c view
@@ -0,0 +1,91 @@+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <wchar.h>++#include <ifaddrs.h>+#include <netdb.h>++#ifdef __linux__+# include <netpacket/packet.h>+#else+# include <net/if_dl.h>+# define AF_PACKET AF_LINK+#endif++#include "network.h"+#include "common.h"+++void maccopy(unsigned char *dst, struct sockaddr *addr)+{+#ifdef __linux__+ /* TODO check that sll_halen is equal to 6 (MAC_SIZE) */+ memcpy(dst, ((struct sockaddr_ll *)addr)->sll_addr, MAC_SIZE);+#else+ /* TODO check that sdl_alen is equal to 6 (MAC_SIZE) */+ struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;+ memcpy(dst, sdl->sdl_data + sdl->sdl_nlen, MAC_SIZE);+#endif+}++struct network_interface *add_interface(struct network_interface *ns, const wchar_t *name, int max_ns)+{+ int i;+ for (i = 0; i < max_ns; i++) {+ if (wcsempty(ns[i].name)) {+ wszcopy(ns[i].name, name, NAME_SIZE);+ return &ns[i];+ } else if (wcscmp(ns[i].name, name) == 0) {+ return &ns[i];+ }+ }+ return NULL;+}++int count_interfaces(struct network_interface *ns, int max_ns)+{+ int i;+ for (i = 0; i < max_ns; i++) {+ if (wcsempty(ns[i].name)) {+ break;+ }+ }+ return i;+}++int c_get_network_interfaces(struct network_interface *ns, int max_ns)+{+ struct network_interface *n;+ struct ifaddrs *ifaddr, *ifa;+ struct sockaddr *addr;+ wchar_t name[NAME_SIZE];+ int family, error;+ + error = getifaddrs(&ifaddr);+ if (error != 0) {+ perror("getifaddrs");+ return 0;+ }++ memset(ns, 0, sizeof(struct network_interface) * max_ns);++ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {+ mbswszcopy(name, ifa->ifa_name, NAME_SIZE);+ addr = ifa->ifa_addr;+ family = addr->sa_family;++ n = add_interface(ns, name, max_ns);++ if (family == AF_INET) {+ ipv4copy(&n->ip_address, addr);+ } else if (family == AF_INET6) {+ ipv6copy(&n->ip6_address, addr);+ } else if (family == AF_PACKET) {+ maccopy(n->mac_address, addr);+ }+ }++ freeifaddrs(ifaddr);+ return count_interfaces(ns, max_ns);+}
network-info.cabal view
@@ -1,37 +1,35 @@-Name: network-info-version: 0.1.0.1-synopsis: Access the local computer's basic network configuration-description: This library provides simple read-only access to the local computer's- networking configuration. It is currently capable of getting a list of- all the network interfaces and their respective IPv4, IPv6 and MAC- addresses.- .- The executable 'test-network-info' will only be built if the flag 'test'- has been set.-homepage: http://github.com/jystic/network-info-license: BSD3-license-file: LICENSE-author: Jacob Stanley-maintainer: jacob@stanley.io-category: Network-build-type: Simple-cabal-version: >=1.8.0.4+name: network-info+version: 0.2+synopsis: Access the local computer's basic network configuration+description: This library provides simple read-only access to the local computer's+ networking configuration. It is currently capable of getting a list of+ all the network interfaces and their respective IPv4, IPv6 and MAC+ addresses.+ .+ network-info has been tested and is known to work on Ubuntu 10.10, Mac+ OS X 10.6.4 and Windows XP.+homepage: http://github.com/jystic/network-info+license: BSD3+license-file: LICENSE+author: Jacob Stanley+maintainer: jacob@stanley.io+category: Network+build-type: Simple+cabal-version: >= 1.6 extra-source-files: cbits/common.h, cbits/network.h,- README.mkd,- run-tests.sh,- run-tests.bat+ test/src/Main.hs,+ test/network-info-test.cabal,+ test/run-tests.bat,+ test/run-tests.sh,+ README.mkd source-repository head type: git location: git://github.com/jystic/network-info.git -Flag test- description: Build test program- default: False- Library hs-source-dirs: src include-dirs: cbits@@ -42,22 +40,11 @@ build-depends: base == 4.* - if os(linux)- c-sources: cbits/network-linux.c+ if os(linux) || os(darwin)+ c-sources: cbits/network-unix.c else if os(windows) c-sources: cbits/network-windows.c extra-libraries: iphlpapi else buildable: False--Executable test-network-info- hs-source-dirs: test- main-is: Main.hs-- if flag(test)- build-depends:- base == 4.*,- network-info- else- buildable: False
− run-tests.bat
@@ -1,2 +0,0 @@-@echo off -cabal configure -ftest && cabal build && dist\build\test-network-info\test-network-info.exe
− run-tests.sh
@@ -1,2 +0,0 @@-#!/bin/bash-cabal configure -ftest && cabal build && dist/build/test-network-info/test-network-info
− test/Main.hs
@@ -1,13 +0,0 @@-module Main (main) where - -import Network.Info - -main = do - ns <- getNetworkInterfaces - mapM (putStrLn . showInterface) ns - -showInterface :: NetworkInterface -> String -showInterface n = name n ++ "\n" - ++ "IPv4 Address: " ++ show (ipv4 n) ++ "\n" - ++ "IPv6 Address: " ++ show (ipv6 n) ++ "\n" - ++ "MAC Address: " ++ show (mac n) ++ "\n"
+ test/network-info-test.cabal view
@@ -0,0 +1,24 @@+name: network-info-test+version: 0.1+build-type: Simple+cabal-version: >= 1.6++Executable network-info-test+ hs-source-dirs: ../src src+ include-dirs: ../cbits+ cc-options: -Wall -Werror++ main-is: Main.hs+ other-modules: Network.Info++ build-depends:+ base == 4.*++ if os(linux) || os(darwin)+ c-sources: ../cbits/network-unix.c+ else+ if os(windows)+ c-sources: ../cbits/network-windows.c+ extra-libraries: iphlpapi+ else+ buildable: False
+ test/run-tests.bat view
@@ -0,0 +1,2 @@+@echo off +cabal configure && cabal build && .\dist\build\network-info-test\network-info-test.exe
+ test/run-tests.sh view
@@ -0,0 +1,6 @@+#!/bin/bash++TESTDIR="$(dirname -- "${0}")"+cd $TESTDIR++cabal configure && cabal build && ./dist/build/network-info-test/network-info-test
+ test/src/Main.hs view
@@ -0,0 +1,13 @@+module Main (main) where + +import Network.Info + +main = do + ns <- getNetworkInterfaces + mapM (putStrLn . showInterface) ns + +showInterface :: NetworkInterface -> String +showInterface n = name n ++ "\n" + ++ "IPv4 Address: " ++ show (ipv4 n) ++ "\n" + ++ "IPv6 Address: " ++ show (ipv6 n) ++ "\n" + ++ "MAC Address: " ++ show (mac n) ++ "\n"