Calculate subnet bits and host bits - host

An organization is given an IP address block of 130.34.12.128/27. The
organization must have 5 different subnets.
What is the number of subnet bits and host bits?
How many hosts are available per subnet?
What are the subnet addresses and range of addresses for each
subnet?
What is the new subnet mask value?
I google and found out that the organization must only have the value of power 2. So the subnets is it must be 8? May I know how does this works?

As you may know, networks have adresses shorter than 32 bits ended by trailing zeroes. In your case, the org have a 27 bit adress ended by .128 which is .10000000 in binary, among which only .100 serves to identify which network. The last 5 bits are trailing zeroes and can be used to identify either subnets or machines inside the network.
This means a machine can have an adress 130.34.12.129/32 it will have a public adress inside the network (same first 27 bits but ending by 00001). With only machines, this would be 2^5 - 2 = 30 distinct hosts in the total network (we remove the adress 00000 as its the net adress and 11111 as its the broadcast adress). The adress range would be 130.34.12.129 to 130.34.12.159
Now if you need 5 subnets, this means you have to find five addresses ended by trailing zeroes inside the original net address. An example would be 00 000 01 000 10 000 111 00 110 00. If you have understood you should be able to answer the 4 questions for each of these subnets (number of hosts, mask etc.)

You have address 130.34.12.128 with mask 255.255.255.224.
In binary you have:
10000010 00100010 00001100 100 00000
11111111 11111111 11111111 111 00000
You can change only the last 5 binary so you have from 130.34.12.128 to 130.34.12.159 ip address.
All subnet need 1 addres for brodcast and 1 for that subnet address.
You can make example:
130.34.12.128 - 130.34.12.135 /29 6 ip address + 1 brodcast + 1 subnet address
130.34.12.136 - 130.34.12.143 /29 6 ip address + 1 brodcast + 1 subnet address
130.34.12.144 - 130.34.12.151 /29 6 ip address + 1 brodcast + 1 subnet address
130.34.12.152 - 130.34.12.155 /30 2 ip address + 1 brodcast + 1 subnet address
130.34.12.156 - 130.34.12.159 /30 2 ip address + 1 brodcast + 1 subnet address
In binary last octet:
10000000 - 10000111 /29
10001000 - 10001111 /29
10010000 - 10010111 /29
10011000 - 10011011 /30
10011100 - 10011111 /30
You can see on the last subnet we can't make more ip address, mask show you what you can change, so every your company ip addres will start from 10000010 00100010 00001100 100 and you can changed only the last 5 "numbers".
1 1 1 1 1 1 1 1
128 64 32 16 8 4 2 1
10000010 00100010 00001100 10000000
128 + 2 32 + 2 8 + 4 128
130 34 12 128

Related

Bitmasking and Air724 LTE Module

Looking for a bit (pardon the pun) of help on this one.
I need to set the various band parameters on an Air724 modem as in the descriptor below for the AT*BAND= syntax.
I am in Australia and looking to use the Telstra FDD-LTE Band 3 only. I am seeking some help as to how to get the syntax correct please? That is the bit operations for bands.
Any help is greatly appreciated.
The solution is for an Arduino C compiler, so any C construct would be useful.
Thank you.
SYNTAX
AT*BAND=[<mode>[<GSMband>,<UMTSband>,<LTEbandH>,<LTEbandL>[,<roamingConfig>,<srvDomain>,<bandPriorityFlag>]]]
Note: <mode>: integer type
0 –GSM network
1 –UMTS network
2 –Dual mode(GSM and UMTS) (auto)
3 –Dual mode(GSM and UMTS) (GSM preferred)
4 –Dual mode(GSM and UMTS) (UMTS preferred)
5 –LTE network
6–Dual mode(GSM and LTE)(auto)
7–Dual mode(GSM and LTE)( GSM preferred)
8–Dual mode(GSM and LTE)(LTE preferred)
9–Dual mode(UMTS and LTE)(auto)
10–Dual mode(UMTS and LTE)(UMTS preferred)
11–Dual mode(UMTS and LTE)(LTE preferred)
12–Trip mode(auto)
13–Trip mode(GSM preferred)
14–Trip mode(TD preferred)
15–Trip mode(LTE preferred)
<GSMband>: integer type <GSMband> is a sum of integers each representing a GSM band
(in other words bit mask)
1 –PGSM 900 (standard or primary)
2 –DCS GSM 1800
4 –PCS GSM 1900
8 –EGSM 900 (extended)
16 –GSM 450
32 –GSM 480
64 –GSM 850
<UMTSband>: integer type <UMTSband> is a sum of integers each representing a UMTS
band (in other words bit mask)
1 –UMTS_BAND_1
2 –UMTS_BAND_2
4 –UMTS_BAND_3
8 –UMTS_BAND_4
16 –UMTS_BAND_5
32 –UMTS_BAND_6
64 –UMTS_BAND_7
128 –UMTS_BAND_8
256 –UMTS_BAND_9
<LTEbandH>: integer type <LTEbandH> is a sum of integers each representing a TDD
LTEband (in other words bit mask)
32 –TDLTE_BAND_38
64 –TDLTE _BAND_39
128 –TDLTE _BAND_40
256 –TDLTE _BAND_41
<LTEbandL> integer type <LTEbandL>is a sum of integers each representing a FDDers each representing a TDDLTEband (in other words bit mask)
32 –TDLTE_BAND_38
64 –TDLTE _BAND_39
128 –TDLTE _BAND_40
256 –TDLTE _BAND_41
<LTEbandL> integer type
<LTEbandL>is a sum of integers each representing a FDD
1 –FDDLTE_BAND_1
4 –FDDLTE _BAND_3
8 –FDDLTE _BAND_4
64– FDDLTE _BAND_7
65536 –FDDLTE _BAND_17
524288 –FDDLTE _BAND_20
<roamingConfig>: integer value
0 –not support
1 –support
2 –no change
<srvDomain>: integer value
0 –CS only
1 –PS only
2 –CS and PS
3 –ANY
4 –no change
<bandPriorityFlag>
0 –default
1 –TD-LTE
2 –FDD-LTE
AT*BAND=6,0,0,0,4
how to get the syntax correct please? That is the bit operations for bands
Ignore bit operations. The text states:
integer type is a sum of integers each representing a GSM band
So for example if you want to support UMTS bands 1. 4 and 9: then you see that 1 –UMTS_BAND_1 with 8 –UMTS_BAND_4 with 256 –UMTS_BAND_9 so you add the numbers 1 + 8 + 256 = 265 and you send the value 265.
Anyway, if you would convert the numbers to binary:
0b000001 –UMTS_BAND_1
0b000010 –UMTS_BAND_2
0b000100 –UMTS_BAND_3
0b001000 –UMTS_BAND_4
... etc. ...
Then you can see that each number has s single bit set. Addition of such numbers is equal to AND bit masking such numbers, as each bit is unqiue..

Reading bits in 5-bits chunk in "Memory Aligned" way

For example, I am trying to read 15 bits from Starting address lets say "11". By definition of memory aligned (15 % 5 == 0). How to read or write these bits in terms (Memory Aligned Access) but the location of bits should also be in such a way that only 1 byte should be read to read a chunk of 5 bits. My thought would be that memory aligned reading should be in the following way.
MSB x x x x x x LSB : Byte read
0 0 0 15 14 13 12 11 : 0
0 0 0 20 19 18 17 16 : 1
0 0 0 25 24 23 22 21 : 2
Most-significant three bits are 0-padded in all bytes.
Please share your thoughts and also help me writing a C code to read bits in chunk of 5 bits? Thanks

DOES htonl() change byte order on BIG ENDIAN machine?

Literally confused about htonl(). In so many links I found that code to do htonl is :
#define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
If the same code is ran on both the machines, it is going to swap the byte orders.
Example : uint32_t a = 0x1;
On Little Endian:
Addr value
100 1
101 0
102 0
103 0
After htonl(a)
Addr value
100 0
101 0
102 0
103 1
============================================
On Big Endian machine:
Addr value
100 0
101 0
102 0
103 1
After htonl(a)
Addr value
100 1
101 0
102 0
103 0
Does that mean that htonl() will change the order of the bytes irrespective of machine architecture ?
If you use it correctly then it should not swap bytes on big endian machines.
htonl is defined in a header which is architecture specific. Normally machine/endian.h will include your architecture specific header. If you redefine it then it will do what you set it to. If you want the real behaviour then you should always use the right architecture header. On big endian machines it's a no op. On little endian machines it's often linked to a specific processor instruction.
I think key to understanding the function is understanding its name.
The function htonl is:
H ost to
N etwork L ong
That is: it converts from the Host order to the Network defined (Big Endian) order.
Different hosts could have different representations:
Big-Ending
Little-Endian
even some other representation (imagine some new machine that works on base-3, or has Middle-Outwards representation?
Whatever the machine format, this function converts to a common Network format so the data can be easily, reliably sent to other machines on the network that may have different representations.
Once you understand the concept of Host / Network, then it shouldn't be hard to understand that Network order is Big-Endian, and any Host that is Big-Endian doesn't need any conversion at all.

inet_pton() counterpart for link layer address

I have two problems related to my implementation -
I need a function which can convert a given link-layer address from text to standard format like we have a similar function at n/w layer for IP addresses inet_pton() which converts a given IP address from text to standard IPv4/IPv6 format.
Is there any difference b/w Link-layer address and 48-bit mac address
(in case of IPv6 specifically)?
If no, then link layer address should always also be of 48 bit in length, if I am not wrong.
Thanks in advance. Please excuse if I am missing something trivial.
EDIT :
Ok.. I am clear on the difference b/w link layer address and ethernet mac address. There are several types of data link layer addresses, ethernet mac address is just one.
Now, this arises one more problem ... As i said in my first question I need to convert a link layer address given from command line to its standard form. The solution provided here will just work for ethernet mac address only.
Isn't there any standard function for the purpose ? What I would like to do is to create an application where user will enter values for different options present in ICMP router advertisement message as stated in RFC 4861.
Option Formats
Neighbor Discovery messages include zero or more options, some of
which may appear multiple times in the same message. Options should
be padded when necessary to ensure that they end on their natural
64-bit boundaries. All options are of the form:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length | ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
~ ... ~
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fields:
Type 8-bit identifier of the type of option. The
options defined in this document are:
Option Name Type
Source Link-Layer Address 1
Target Link-Layer Address 2
Prefix Information 3
Redirected Header 4
MTU 5
Length 8-bit unsigned integer. The length of the option
(including the type and length fields) in units of
8 octets. The value 0 is invalid. Nodes MUST
silently discard an ND packet that contains an
option with length zero.
4.6.1. Source/Target Link-layer Address
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Length | Link-Layer Address ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fields:
Type
1 for Source Link-layer Address
2 for Target Link-layer Address
Length The length of the option (including the type and
length fields) in units of 8 octets. For example,
the length for IEEE 802 addresses is 1
[IPv6-ETHER].
Link-Layer Address
The variable length link-layer address.
The content and format of this field (including
byte and bit ordering) is expected to be specified
in specific documents that describe how IPv6
operates over different link layers. For instance,
[IPv6-ETHER].
One more thing I am not quite handy with C++, can you please provide a C alternative ?
Thanks.
Your first question, it's not that hard to write, and since MAC addresses are represented by a 6 byte array you don't need to take machine-dependency into account ( like endian-ness and stuff )
void str2MAC(string str,char* mac) {
for(int i=0;i<5;i++) {
string b = str.substr(0,str.find(':'));
str = str.substr(str.find(':')+1);
mac[i] = 0;
for(int j=0;j<b.size();b++) {
mac[i] *= 0x10;
mac[i] += (b[j]>'9'?b[j]-'a'+10:b[j]-'0');
}
}
mac[5] = 0;
for(int i=0;i<str.size();i++) {
mac[5] *= 0x10;
mac[5] += (str[i]>'9'?str[i]-'a'+10:str[i]-'0');
}
}
About your second question, IP ( and IPv6 specifically) is a Network Layer protocol and is above the Link Layer, thus doesn't have to do anything with the Link Layer.
If by Link Layer you mean Ethernet, yes Ethernet Address is always 48bits, but there are other Link Layer protocols presents which may use other formats.

C GPIO hex numbering

I have been given the following bit of code as an example:
Make port 0 bits 0-2 outputs, other to be inputs.
FIO0DIR = 0x00000007;
Set P0.0, P0.1, P0.2 all low (0)
FIO0CLR = 0x00000007;
I have been told that the port has 31 LED's attached to it. I cant understand why, to enable the first 3 outputs, it is 0x00000007 not 0x00000003?
These GPIO config registers are bitmaps.
Use your Windows calculator to convert the hex to binary:
0x00000007 = 111, or with 32 bits - 00000000000000000000000000000111 // three outputs
0x00000003 = 11, or with 32 bits - 00000000000000000000000000000011 // only two outputs
Because the value you write to the register is a binary bit-mask, with a bit being one meaning "this is an output". You don't write the "number of outputs I'd like to have", you are setting 8 individual flags at the same time.
The number 7 in binary is 00000111, so it has the lower-most three bits set to 1, which here seems to mean "this is an output". The decimal value 3, on the other hand, is just 00000011 in binary, thus only having two bits set to 1, which clearly is one too few.
Bits are indexed from the right, starting at 0. The decimal value of bit number n is 2n. The decimal value of a binary number with more than one bit set is simply the sum of all the values of all the set bits.
So, for instance, the decimal value of the number with bits 0, 1 and 2 set is 20 + 21 + 22 = 1 + 2 + 4 = 7.
Here is an awesome ASCII table showing the 8 bits of a byte and their individual values:
+---+---+---+---+---+---+---+---+
index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+
value |128| 64| 32| 16| 8 | 4 | 2 | 1 |
+---+---+---+---+---+---+---+---+

Resources