0% found this document useful (0 votes)
37 views

Network Programming

The document discusses network addressing and special addresses used in networking. It defines the network address as the first address in the network that identifies the network to the rest of the internet. It also describes direct broadcast addresses, limited broadcast addresses, loopback addresses, and private addresses. It discusses how network address translation (NAT) allows private addresses to interconnect networks.

Uploaded by

Kooky Messi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Network Programming

The document discusses network addressing and special addresses used in networking. It defines the network address as the first address in the network that identifies the network to the rest of the internet. It also describes direct broadcast addresses, limited broadcast addresses, loopback addresses, and private addresses. It discusses how network address translation (NAT) allows private addresses to interconnect networks.

Uploaded by

Kooky Messi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Network Addressing

Dr. Elhossiny Ibrahim 0 4th year: network programming


Special address
1-Network address
➢ The network address is the first address in the network.
• It defines the network to the rest of the internet.
• Given the network address, we can find the class of the address, the block, and
the range of the addresses in the block.
• The network address is the one that assigned to the organization.
• It is used for registering on the router. When a network wants to send a
message to another network, it must know the special network address of the
router and then resend to another router and so on.
• This address do not be a source nor destination.

Dr. Elhossiny Ibrahim 1 4th year: network programming


Special address
2- Direct Broadcast address
• Broadcast means on device sends a message to all devises in this network.
• Used as destination only.

Dr. Elhossiny Ibrahim 2 4th year: network programming


Special address
3- Limited Broadcast address
• Used as destination only.

Dr. Elhossiny Ibrahim 3 4th year: network programming


Special address
4- This host in this address
• Used as source only.

Dr. Elhossiny Ibrahim 4 4th year: network programming


Special address
5- Specific host in this network
• Used as destination only.

Dr. Elhossiny Ibrahim 5 4th year: network programming


Special address
6- Loopback address
• Used as a destination only.

Dr. Elhossiny Ibrahim 6 4th year: network programming


Private addresses
• A number of blocks in each class are assigned for private use.

• They are not recognized globally (assigned for local use only).

➢ Network Address Translation (NAT)


• Generally, NAT works on a router or gateway and interconnects two networks with
each other by translating the private addresses into the registered addresses (public
address) before the data being transmitted to another network via Internet.

Dr. Elhossiny Ibrahim 7 4th year: network programming


Classful IP Addressing

When an organization received an IP network address, that ❖


address was associated with a “Class”, A, B, or C.
This is known as Classful IP Addressing ❖
The first octet of the address determined what class the network ❖
belonged to and which bits were the network bits and which bits
were the host bits.
There were no subnet masks. ❖
It was not until 1992 when the IETF introduced CIDR (Classless ❖
Interdomain Routing), making the address class meaning less.
This is known as Classless IP Addressing. ❖
Public vs Private

Dr. Elhossiny Ibrahim 9 4th year: network programming


Encapsulation

Encapsulation of data as it goes down the protocol stack.


Dr. Elhossiny Ibrahim 10 4th year: network programming
Encapsulation
➢ When an application sends data using TCP, the data is sent down the protocol stack,
through each layer, until it is sent as a stream of bits across the network.

➢ Each layer adds information to the data by prepending headers (and sometimes
adding trailer information) to the data that it receives.

➢ The unit of data that TCP sends to IP is called a TCP segment.

➢ The unit of data that IP sends to the network interface is called a packet (IP
datagram).

➢ The stream of bits that flows across the Ethernet is called a frame.

➢ The numbers at the bottom of the headers and trailer of the Ethernet frame in the
Figure are the typical sizes of the headers in bytes.

➢ A physical property of an Ethernet frame is that the size of its data must be between
46 and 1500 bytes.

➢ We could draw a nearly identical picture for UDP data. The only changes are that the
unit of information that UDP passes to IP is called a UDP datagram, and the size of
the UDP header is 8 bytes.

Dr. Elhossiny Ibrahim 11 4th year: network programming


Encapsulation

Dr. Elhossiny Ibrahim 12 4th year: network programming


Encapsulation
➢ Recall from Figure 2.6 that TCP, UDP, ICMP, and IGMP all send data to IP.

➢ IP must add some type of identifier to the IP header that it generates, to indicate the
layer to which the data belongs.

➢ IP handles this by storing an 8-bit value in its header called the protocol field. A value
of 1 is for ICMP, 2 is for IGMP, 6 indicates TCP, and 17 is for UDP.

➢ Similarly, many different applications can be using TCP or UDP at anyone time.

➢ The transport layer protocols store an identifier in the headers they generate to
identify the application.

➢ Both TCP and UDP use 16-bit port numbers to identify applications.

➢ TCP and UDP store the source port number and the destination port number in their
respective headers.

➢ The network interface sends and receives frames on behalf of IP, ARP, and RARP.

➢ There must be some form of identification in the Ethernet header indicating which
protocol generated the data.

➢ To handle this there is a 16-bit frame type field in the Ethernet header.

Dr. Elhossiny Ibrahim 13 4th year: network programming


Subnet Addressing
➢ All hosts are required to support subnet addressing.
➢ Instead of considering an IP address as just a network ID and host ID, the
host ID portion is divided into a subnet ID and a host ID.
➢ This makes better use of the network because class A and class B
addresses have too many bits allocated for the host ID: 224 - 2 and 216−2,
respectively.
➢ These many hosts don’t attached to a single network, they attached to one
subnet from many subnets.
➢ We subtract 2 in these expressions because host IDs of all zero bits or all
one bits are invalid.
➢ After obtaining an IP network ID of a certain class from the ICANN, it is up
to the local system administrator whether to subnet or not, and if so, how
many bits to allocate to the subnet ID and host ID.
➢ For example, an Internet in a class B network address is (140.252) and
the remaining 16 bits, 8 are for the subnet ID and 8 for the host ID. This is
shown in the Figure:

Fig: Subnetting a class B address.


Dr. Elhossiny Ibrahim 14 4th year: network programming
Subnet Addressing
➢ This division allows 254 subnets, with 254 hosts per subnet.

➢ Many administrators use the natural 8-bit boundary in the 16 bits of a class B host ID
as the subnet boundary.

➢ This makes it easier to determine the subnet ID from a dotted-decimal number, but
there is no requirement that the subnet boundary for a class A or class B address be
on a byte boundary.

➢ Subnetting is also allowed for a class C address, but there are fewer bits to work
with.

➢ Subnetting is rarely shown with a class D address because there are so few available
bits allocated to host ID.

➢ Most class A addresses are, however, subnetted.

➢ Subnetting hides the details of internal network organization (within a company or


campus) to external routers.

Dr. Elhossiny Ibrahim 15 4th year: network programming


Subnet Addressing

Dr. Elhossiny Ibrahim 16 4th year: network programming


Subnet Mask
➢ Part of the configuration of any host that takes place at bootstrap time is
the specification of the host’s IP address.
➢ Most systems have this stored in a disk file that’s read at bootstrap time.
➢ In addition to the IP address, a host also needs to know how many bits are
to be used for the subnet ID and how many bits are for the host ID.
➢ This is also specified at bootstrap time using a subnet mask.
➢ This mask is a 32-bit value containing one bits for the network ID and
subnet ID, and zero bits for the host ID.

Example subnet masks for two different class B subnet arrangements.


Dr. Elhossiny Ibrahim 17 4th year: network programming
Subnet Mask
➢ The Figure shows the formation of the subnet mask for two different
partitions of a class B address.
➢ The top example shows that the subnet ID and host ID are both 8 bits.
➢ The lower example shows a class B address partitioned for a 10-bit subnet
ID and a 6-bit host ID.
➢ Although IP addresses are normally written in dotted decimal notation,
subnet masks are often written in hexadecimal, especially if the boundary
is not a byte boundary, since the subnet mask is a bit mask.
➢ Given the host IP address and subnet mask, a host can determine if an IP
datagram is destined for
➢ (1) a host on its own subnet,
➢ (2) a host on a different subnet on its own network, or
➢ (3) a host on a different network.
➢ Knowing your own IP address tells you whether you have a class A, B, or C
address (from the high-order bits), which tells you where the boundary is
between the network ID and the subnet ID.
➢ The subnet mask then tells you where the boundary is between the subnet
ID and the host ID.
➢ Number of subnets must be a power of 2 and equal 2^no. of subnet bits.
➢ Subnet mask is contiguous.
Dr. Elhossiny Ibrahim 18 4th year: network programming
Default Mask and
Subnet Mask
➢ Default Mask
• A default mask is a 32 bit binary number that given the first address in the
block (the network address) when ANDed with an IP address in the block.
• It retains the network ID of the block and sets the host ID to zero.
• Default mask of class A : 255.0.0.0 and class B : 255.255.0.0 and class C :
255.255.255.0

Dr. Elhossiny Ibrahim 19 4th year: network programming


Socket Library
➢ Machine name and IPv4 address
>>> from socket import *
>>> pc_name=gethostname()
>>> ipv4=gethostbyname(pc_name)
>>> print(pc_name) Elhossiny
>>> print(ipv4) 192.168.56.1

➢ Get service name, given the port and protocol type.


>>> from socket import *
>>> port=21
>>> print(getservbyport(port)) ftp
>>> print(getservbyport(port,"tcp")) ftp

➢ Get port number, given the service name and protocol type.
>>> service_name="ftp"
>>> print(getservbyname(service_name)) 21
>>> print(getservbyname(service_name,"tcp")) 21
Note by default the protocol type is tcp.

Dr. Elhossiny Ibrahim 20 4th year: network programming


Unicode and Byte Object
➢ Byte Objects versus String in Python:
• Byte objects are sequence of Bytes, whereas Strings are sequence of
characters.
• Byte objects are in machine readable form internally, Strings are only in human
readable form.
• Why do we convert from String to Byte?
• Since Byte objects are machine readable, they can be directly stored on the
disk. Whereas, Strings need encoding before which they can be stored on disk.
a = b'Hello'
print(a) b'Hello'
➢ Encoding
• Encoding is converting Strings to byte objects and this is necessary so that the
text can be stored on disk using mapping with ASCII or UTF-8 encoding
techniques.
• An encoding is a format to represent audio, images, text, etc. in bytes.
• This task is achieved using encode(). It take encoding technique as argument.
Default technique used in python is “UTF-8” technique.
a = 'Hello'
send = a.encode('utf-8')
print(send) b'Hello'
rec=send.decode("utf-8")
print(rec) Hello
Dr. Elhossiny Ibrahim 21 4th year: network programming
Unicode and Byte Object
➢ ASCII can only be used to encode a limited number of characters. It cannot be
used for most non-English characters.
➢ Unicode can be used to represent all the characters in all languages, plus the
emojis we use for messaging and online chatting.
➢ Decoding
• Similarly, Decoding is process to convert a Byte object to String.
• It is implemented using decode() . A byte object can be decoded back into a
string object, if you know which encoding was used to encode it.
a = '' ‫''اهال وسهال‬
send = a.encode('utf-8')
print(send) b'\xd8\xa7\xd9\x87\xd9\x84\xd8\xa7 \xd9\x88\xd8\xb3\xd9\x87\xd9\x84\xd8\xa'
rec=send.decode("utf-8")
print(rec) ‫اهال وسهال‬
a = b''‫‘'اهال وسهال‬ ERROR
original = '27岁少妇生孩子后'
encoded = original.encode('utf-8')
print(encoded)
b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e'
decoded = encoded.decode('utf-8')
print(decoded) 27岁少妇生孩子后
encoded = original.encode(‘ASCII') ERROR
Dr. Elhossiny Ibrahim 22 4th year: network programming
Thanks
Dr. Elhossiny Ibrahim 23 4th year: network programming

You might also like