lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

iptools.sh (4005B)


      1 #!/bin/sh
      2 ##############################################################################
      3 #
      4 # This program is free software; you can redistribute it and/or modify
      5 # it under the terms of the GNU General Public License version 2 as
      6 # published by the Free Software Foundation.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11 # GNU General Public License for more details.
     12 #
     13 # Copyright (C) 2016 Eric Luehrsen
     14 #
     15 ##############################################################################
     16 #
     17 # These are iptools that might be useful in a larger package, if provided
     18 # elsewhere for common use. One example that many may find useful is turning
     19 # flexible IPV6 colon dividers into PTR. Otherwise these are incomplete and
     20 # would need robustness improvements for more generic applications.
     21 #
     22 ##############################################################################
     23 
     24 domain_ptr_ip6() {
     25   # Get the nibble rounded /CIDR ...ip6.arpa.
     26   echo "$1" | awk -F: \
     27   'BEGIN { OFS = "" ; }
     28   { CIDR = $0 ;
     29   sub(/.*\//,"",CIDR) ;
     30   CIDR = (CIDR / 4) ;
     31   sub(/\/[0-9]+/,"",$0) ;
     32   ct_stop = 9 - NF ;
     33   for(i=1; i<=NF; i++) {
     34     if(length($i) == 0) {
     35       for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
     36     else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
     37   y = $0 ;
     38   ct_start = length(y) - 32 + CIDR ;
     39   for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
     40   gsub(/./,"&\.",x) ;
     41   x = (x "ip6.arpa") ;
     42   print x }'
     43 }
     44 
     45 ##############################################################################
     46 
     47 host_ptr_ip6() {
     48   # Get complete host ...ip6.arpa.
     49   echo "$1" | awk -F: \
     50   'BEGIN { OFS = "" ; }
     51   { sub(/\/[0-9]+/,"",$0) ;
     52   ct_stop = 9 - NF ;
     53   for(i=1; i<=NF; i++) {
     54     if(length($i) == 0) {
     55       for(j=1; j<=ct_stop; j++) { $i = ($i "0000") ; } }
     56     else { $i = substr(("0000" $i), length($i)+5-4) ; } } ;
     57   y = $0 ;
     58   ct_start = length(y);
     59   for(i=ct_start; i>0; i--) { x = (x substr(y,i,1)) ; } ;
     60   sub(/[0-9]+\//,"",x) ;
     61   gsub(/./,"&\.",x) ;
     62   x = (x "ip6.arpa") ;
     63   print x }'
     64 }
     65 
     66 ##############################################################################
     67 
     68 domain_ptr_ip4() {
     69   # Get the byte rounded /CIDR ...in-addr.arpa.
     70   echo "$1" | awk \
     71   '{ CIDR = $0 ;
     72   sub(/.*\//,"",CIDR) ;
     73   CIDR = (CIDR / 8) ;
     74   dtxt = $0 ;
     75   sub(/\/.*/,"",dtxt) ;
     76   split(dtxt, dtxt, ".") ;
     77   for(i=1; i<=CIDR; i++) { x = (dtxt[i] "." x) ; }
     78   x = (x "in-addr.arpa") ;
     79   print x }'
     80 }
     81 
     82 ##############################################################################
     83 
     84 host_ptr_ip4() {
     85   # Get omplete host ...in-addr.arpa.
     86   echo "$1" | awk -F. \
     87   '{ x = ( $4"."$3"."$2"."$1".in-addr.arpa" ) ;
     88   sub(/\/[0-9]+/,"",x) ;
     89   print x }'
     90 }
     91 
     92 ##############################################################################
     93 
     94 valid_subnet6() {
     95   case "$1" in
     96     # GA
     97     [1-9][0-9a-f][0-9a-f][0-9a-f]":"*) echo "ok" ;;
     98     # ULA
     99     f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
    100     # fe80::, ::1, and such
    101     *) echo "not" ;;
    102   esac
    103 }
    104 
    105 ##############################################################################
    106 
    107 valid_subnet4() {
    108   case "$1" in
    109     # Link, Local, and Such
    110     169"."254"."*) echo "not" ;;
    111     127"."*) echo "not" ;;
    112     0"."*) echo "not" ;;
    113     255"."*) echo "not" ;;
    114     # Other Normal
    115     25[0-4]"."[0-9]*) echo "ok" ;;
    116     2[0-4][0-9]"."[0-9]*) echo "ok" ;;
    117     1[0-9][0-9]"."[0-9]*) echo "ok" ;;
    118     [0-9][0-9]"."[0-9]*) echo "ok" ;;
    119     [0-9]"."[0-9]*) echo "ok" ;;
    120     # Not Right
    121     *) echo "not";;
    122   esac
    123 }
    124 
    125 ##############################################################################
    126 
    127 private_subnet() {
    128   case "$1" in
    129     10"."*) echo "ok" ;;
    130     172"."1[6-9]"."*) echo "ok" ;;
    131     172"."2[0-9]"."*) echo "ok" ;;
    132     172"."3[0-1]"."*) echo "ok" ;;
    133     192"."168"."*) echo "ok" ;;
    134     f[cd][0-9a-f][0-9a-f]":"*) echo "ok" ;;
    135     *) echo "not" ;;
    136   esac
    137 }
    138 
    139 ##############################################################################
    140