SlideShare a Scribd company logo
Value-Result Arguments
•Length of socket passed as an argument
•Method by which length is passed depends on which direction the structure is
being passed (from process to kernel, or vice versa)
Value-Result Arguments
•Value-only: bind, connect, sendto (from process to kernel)
•Value-Result: accept, recvfrom, getsockname, getpeername (from kernel to process,
pass a pointer to an integer containing size)
Tells process how much information kernel actually stored
struct sockaddr_in clientaddr ;
socklen_t len;
int listenfd, connectfd;
len = sizeof (clientaddr);
connectfd = accept (listenfd, (SA *) &clientaddr, &len);
Byte Ordering
 Port number and IP Address are represented by
multi byte data type , which are placed in packets for the
purpose of routing and multiplexing.
Port Number are two byte or 16 bit and IPv4 address
are 4 bytes or 32 bits.
Now the problem arrises when transfering multi byte
data types between different architecture.
Byte Ordering
 Let host A uses big endian architecture and sends a
packet across the network to host B which uses a little
endian architecture.
If host B looks at the address to see if the packet is for
him/her(choose a gender !) it will interprete the byte in
the opposite order and will wrongly conclude that it is
not his/ her packet.
The internet uses big endian and we call it the Network
Byte Order.
It is not important to know which method it uses, since
we have function to convert host-byte-order values in
network-byte-order values and reverse also.
Byte Ordering Functions
htonl : convert host to network, long integer
htons : convert host to network, short integer
ntohl : convert network to host, long integer
ntohs : convert network to host, short integer
Byte Ordering Functions 1/4
•Two ways to store 2 bytes (16-bit integer) in memory
Low-order byte at starting address  little-endian byte order
High-order byte at starting address  big-endian byte order
•in a big-endian computer  store 4F52
Stored as 4F52  4F is stored at storage address 1000, 52 will
be at address 1001, for example
•In a little-endian system  store 4F52
it would be stored as 524F (52 at address 1000, 4F at 1001)
•Byte order used by a given system known as host byte order
•Network programmers use network byte order
•Internet protocol uses big-endian byte ordering for integers (port number and IP
address)
Byte Ordering Functions 2/4
High-order byte low-order byte
MSB 16bit value LSB
High-order byte low-order byte
Increasing memory
address
Address A+1 Address A
Little-endian byte order:
big-endian byte order:
Address A+1
Address A
Increasing memory
address
Byte Ordering Functions 3/4
#include "unp.h"
int main(int argc, char **argv)
{
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2)
printf("big-endiann");
else if (un.c[0] == 2 && un.c[1] == 1)
printf("little-endiann");
else
printf("unknownn");
} else
printf("sizeof(short) = %dn", sizeof(short));
exit(0);
}
•Sample program to figure out little-
endian or big-endian machine
•Source code in byteorder.c
Byte Ordering Functions 4/4
•To convert between byte orders
Return value in network byte order
htons (s for short word 2 bytes)
htonl (l for long word 4 bytes)
Return value in host byte order
ntohs
ntohl
•Must call appropriate function to convert between host and network byte order
•On systems that have the same ordering as the Internet protocols, four functions
usually defined as null macros
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13);
Byte Manipulation Functions
#include <strings.h>
void bzero (void *dest, size_t nbytes);
// sets specified number of bytes to 0 in the destination
void bcopy (const void *src,void * dest, size_t nbytes);
// moves specified number of bytes from source to destination
void bcmp (const void *ptr1, const void *ptr2,size_t nbytes)
//compares two arbitrary byte strings, return value is zero if two byte strings are
identical, otherwise, nonzero
Address Conversion Functions 1/2
Convert an IPv4 address from a dotted-decimal string “206.168.112.96” to a 32-bit
network byte order binary value
#include <arpa/inet.h>
int inet_aton (const char* strptr, struct in_addr *addrptr);
// return 1 if string was valid, 0 on error. Address stored in *addrptr
in_addr_t inet_addr (const char * strptr);
// returns 32 bit binary network byte order IPv4 address, currently
deprecated
char * inet_nota (struct in_addr inaddr);
//returns pointer to dotted-decimal string
Address Conversion Functions 2/2
To handle both IPv4 and IPv6 addresses
#include <arpa/inet.h>
int inet_pton (int family, const char* strptr, void *addrptr);
// return 1 if OK, 0 on error. 0 if not a valid presentation, -1 on error,
Address stored in *addrptr
Const char * inet_ntop (int family, const void* addrptr, char *strptr, size_t len);
// return pointer to result if OK, NULL on error
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
ptr = inet_ntop (AF_INET,&addr.sin_addr,str,sizeof(str));
Reading and Writing Functions 1/2
int send (int socket, char *message, int msg_len, int flags) (TCP)
int sendto (int socket, void *msg, int len, int flags, struct sockaddr *
to, int tolen ); (UDP)
int write(int socket, void *msg, int len); /* TCP */
int recv (int socket, char *buffer, int buf_len, int flags) (TCP)
int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr
*from, int *fromlen); (UDP)
int read(int socket, void *msg, int len); (TCP)
Reading and Writing Functions 2/2
•Stream sockets (TCP sockets) exhibit a behavior with read and write that differs
from normal file I/O
•A read or write on a stream socket might input or output fewer bytes than
requested (not an error)
readn function
writen function
readline function

More Related Content

Similar to File 10 - CSX 334 _VRA NBO.ppsx

03 sockets
03 sockets03 sockets
03 sockets
Pavan Illa
 
Sockets
SocketsSockets
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
Ghaffar Khan
 
Np unit2
Np unit2Np unit2
Np unit2
vamsitricks
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
hugo lu
 
mobile_network_layer.pptx
mobile_network_layer.pptxmobile_network_layer.pptx
mobile_network_layer.pptx
singhram281982
 
2 logical addressing
2 logical addressing2 logical addressing
2 logical addressing
gafurov_x
 
Custom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_RouterCustom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_Router
Vishal Vasudev
 
Ex 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chenEx 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chen
Đô GiẢn
 
Ppt fnr arbitrary length small domain block cipher proposal
Ppt fnr  arbitrary length small domain block cipher proposalPpt fnr  arbitrary length small domain block cipher proposal
Ppt fnr arbitrary length small domain block cipher proposal
Karunakar Saroj
 
I pv4 and ipv6
I pv4 and ipv6I pv4 and ipv6
I pv4 and ipv6
manirajan12
 
C08-Network_Protocols (1).ppt
C08-Network_Protocols (1).pptC08-Network_Protocols (1).ppt
C08-Network_Protocols (1).ppt
Arul Jothi Yuvaraja
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
Rishav Bhurtel
 
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptxRHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
AbdellahELMAMOUN
 
11 coms 525 tcpip - internet protocol - forward
11   coms 525 tcpip - internet protocol - forward11   coms 525 tcpip - internet protocol - forward
11 coms 525 tcpip - internet protocol - forward
Palanivel Kuppusamy
 
10 coms 525 tcpip - internet protocol - ip
10   coms 525 tcpip -  internet protocol - ip10   coms 525 tcpip -  internet protocol - ip
10 coms 525 tcpip - internet protocol - ip
Palanivel Kuppusamy
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
HelpWithAssignment.com
 
Internetworking - IP
Internetworking - IPInternetworking - IP
Internetworking - IP
selvakumar_b1985
 
Networking presentation 9 march 2009
Networking presentation   9 march 2009Networking presentation   9 march 2009
Networking presentation 9 march 2009
Kinshook Chaturvedi
 

Similar to File 10 - CSX 334 _VRA NBO.ppsx (20)

03 sockets
03 sockets03 sockets
03 sockets
 
Sockets
SocketsSockets
Sockets
 
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
 
Np unit2
Np unit2Np unit2
Np unit2
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
mobile_network_layer.pptx
mobile_network_layer.pptxmobile_network_layer.pptx
mobile_network_layer.pptx
 
2 logical addressing
2 logical addressing2 logical addressing
2 logical addressing
 
Custom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_RouterCustom_IP_Network_Protocol_and_Router
Custom_IP_Network_Protocol_and_Router
 
Ex 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chenEx 1 chapter06-i-pv4-tony_chen
Ex 1 chapter06-i-pv4-tony_chen
 
Ppt fnr arbitrary length small domain block cipher proposal
Ppt fnr  arbitrary length small domain block cipher proposalPpt fnr  arbitrary length small domain block cipher proposal
Ppt fnr arbitrary length small domain block cipher proposal
 
I pv4 and ipv6
I pv4 and ipv6I pv4 and ipv6
I pv4 and ipv6
 
C08-Network_Protocols (1).ppt
C08-Network_Protocols (1).pptC08-Network_Protocols (1).ppt
C08-Network_Protocols (1).ppt
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
 
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptxRHSA_1_Chapter(11)_Resume_chaptre_11.pptx
RHSA_1_Chapter(11)_Resume_chaptre_11.pptx
 
11 coms 525 tcpip - internet protocol - forward
11   coms 525 tcpip - internet protocol - forward11   coms 525 tcpip - internet protocol - forward
11 coms 525 tcpip - internet protocol - forward
 
10 coms 525 tcpip - internet protocol - ip
10   coms 525 tcpip -  internet protocol - ip10   coms 525 tcpip -  internet protocol - ip
10 coms 525 tcpip - internet protocol - ip
 
Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
 
Internetworking - IP
Internetworking - IPInternetworking - IP
Internetworking - IP
 
Networking presentation 9 march 2009
Networking presentation   9 march 2009Networking presentation   9 march 2009
Networking presentation 9 march 2009
 

Recently uploaded

Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 

Recently uploaded (20)

Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 

File 10 - CSX 334 _VRA NBO.ppsx

  • 1. Value-Result Arguments •Length of socket passed as an argument •Method by which length is passed depends on which direction the structure is being passed (from process to kernel, or vice versa)
  • 2. Value-Result Arguments •Value-only: bind, connect, sendto (from process to kernel) •Value-Result: accept, recvfrom, getsockname, getpeername (from kernel to process, pass a pointer to an integer containing size) Tells process how much information kernel actually stored struct sockaddr_in clientaddr ; socklen_t len; int listenfd, connectfd; len = sizeof (clientaddr); connectfd = accept (listenfd, (SA *) &clientaddr, &len);
  • 3. Byte Ordering  Port number and IP Address are represented by multi byte data type , which are placed in packets for the purpose of routing and multiplexing. Port Number are two byte or 16 bit and IPv4 address are 4 bytes or 32 bits. Now the problem arrises when transfering multi byte data types between different architecture.
  • 4. Byte Ordering  Let host A uses big endian architecture and sends a packet across the network to host B which uses a little endian architecture. If host B looks at the address to see if the packet is for him/her(choose a gender !) it will interprete the byte in the opposite order and will wrongly conclude that it is not his/ her packet. The internet uses big endian and we call it the Network Byte Order. It is not important to know which method it uses, since we have function to convert host-byte-order values in network-byte-order values and reverse also.
  • 5. Byte Ordering Functions htonl : convert host to network, long integer htons : convert host to network, short integer ntohl : convert network to host, long integer ntohs : convert network to host, short integer
  • 6. Byte Ordering Functions 1/4 •Two ways to store 2 bytes (16-bit integer) in memory Low-order byte at starting address  little-endian byte order High-order byte at starting address  big-endian byte order •in a big-endian computer  store 4F52 Stored as 4F52  4F is stored at storage address 1000, 52 will be at address 1001, for example •In a little-endian system  store 4F52 it would be stored as 524F (52 at address 1000, 4F at 1001) •Byte order used by a given system known as host byte order •Network programmers use network byte order •Internet protocol uses big-endian byte ordering for integers (port number and IP address)
  • 7. Byte Ordering Functions 2/4 High-order byte low-order byte MSB 16bit value LSB High-order byte low-order byte Increasing memory address Address A+1 Address A Little-endian byte order: big-endian byte order: Address A+1 Address A Increasing memory address
  • 8. Byte Ordering Functions 3/4 #include "unp.h" int main(int argc, char **argv) { union { short s; char c[sizeof(short)]; } un; un.s = 0x0102; printf("%s: ", CPU_VENDOR_OS); if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf("big-endiann"); else if (un.c[0] == 2 && un.c[1] == 1) printf("little-endiann"); else printf("unknownn"); } else printf("sizeof(short) = %dn", sizeof(short)); exit(0); } •Sample program to figure out little- endian or big-endian machine •Source code in byteorder.c
  • 9. Byte Ordering Functions 4/4 •To convert between byte orders Return value in network byte order htons (s for short word 2 bytes) htonl (l for long word 4 bytes) Return value in host byte order ntohs ntohl •Must call appropriate function to convert between host and network byte order •On systems that have the same ordering as the Internet protocols, four functions usually defined as null macros servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13);
  • 10. Byte Manipulation Functions #include <strings.h> void bzero (void *dest, size_t nbytes); // sets specified number of bytes to 0 in the destination void bcopy (const void *src,void * dest, size_t nbytes); // moves specified number of bytes from source to destination void bcmp (const void *ptr1, const void *ptr2,size_t nbytes) //compares two arbitrary byte strings, return value is zero if two byte strings are identical, otherwise, nonzero
  • 11. Address Conversion Functions 1/2 Convert an IPv4 address from a dotted-decimal string “206.168.112.96” to a 32-bit network byte order binary value #include <arpa/inet.h> int inet_aton (const char* strptr, struct in_addr *addrptr); // return 1 if string was valid, 0 on error. Address stored in *addrptr in_addr_t inet_addr (const char * strptr); // returns 32 bit binary network byte order IPv4 address, currently deprecated char * inet_nota (struct in_addr inaddr); //returns pointer to dotted-decimal string
  • 12. Address Conversion Functions 2/2 To handle both IPv4 and IPv6 addresses #include <arpa/inet.h> int inet_pton (int family, const char* strptr, void *addrptr); // return 1 if OK, 0 on error. 0 if not a valid presentation, -1 on error, Address stored in *addrptr Const char * inet_ntop (int family, const void* addrptr, char *strptr, size_t len); // return pointer to result if OK, NULL on error if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]); ptr = inet_ntop (AF_INET,&addr.sin_addr,str,sizeof(str));
  • 13. Reading and Writing Functions 1/2 int send (int socket, char *message, int msg_len, int flags) (TCP) int sendto (int socket, void *msg, int len, int flags, struct sockaddr * to, int tolen ); (UDP) int write(int socket, void *msg, int len); /* TCP */ int recv (int socket, char *buffer, int buf_len, int flags) (TCP) int recvfrom(int socket, void *msg, int len, int flags, struct sockaddr *from, int *fromlen); (UDP) int read(int socket, void *msg, int len); (TCP)
  • 14. Reading and Writing Functions 2/2 •Stream sockets (TCP sockets) exhibit a behavior with read and write that differs from normal file I/O •A read or write on a stream socket might input or output fewer bytes than requested (not an error) readn function writen function readline function