代写代考 CSci4061: Introduction to Operating Systems

CSci4061: Introduction to Operating Systems

December 2, 2021
Computer Science & Engineering, University of Minnesota

Copyright By PowCoder代写 加微信 powcoder

Last lecture
• Networking
• Layered Structure and Protocol Hierarchies • ISO/OSI Model and TCP/IP
• IP Addressing, Hostnames and Ports
• Encapsulation and Multiplexing
• Client-Server Model and P2P model

Overview of sockets

What are (network) sockets?
• An internal endpoint for sending or receiving data on a network
• Networking API provided by the OS
• Enable applications to “hook” onto the network • Support one end of a network connection
• Special files
• Program is given socket file descriptors • Read, write data to them
• Special operations needed

Socket Basics

Sockets Types
• Vary based on protocol families
• E.g.: TCP/IP, UUCP, Appletalk
• In general:
• TCP Sockets (stream socket): Connection-oriented
• UDP Sockets (datagram socket): Connectionless
• Raw sockets: typically available in routers and other network equipment, NO
protocol-specific transport layer formatting

Socket Addresses
• What addresses do you need for communicating between two processes on two hosts?
• Each socket is associated with a 5-tuple
• {protocol, local-addr, local-port, remote-addr, remote-port}

Socket Address: struct sockaddr
• Generic type: struct sockaddr
• Cast to/from the actual structure type
• TCP/IP: struct sockaddr_in
• Family: AF_INET/PF_INET
• Port: 16-bit TCP/UDP port number • Address: 32-bit IP address structure
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
in_port_t sin_port; struct in_addr sin_addr;
struct in_addr {
uint32_t s_addr;
/* port in network byte order */
/* internet address */
/* address in network byte order */

Address Conversion
• Three address formats: Dotted, binary, string (hostname) • inet_addr(): Dotted to binary format
• E.g.: 192.168.10.3 to its 32-bit equivalent • inet_ntoa(): Reverse function
• gethostbyname(): Hostname to IP address
• Uses DNS

Machine Byte Ordering
• Different machines use different byte orders
• Big-endian: MSB at low addresses • E.g.: Sparc, PowerPC
• Little-endian: MSB at high addresses • E.g.: x86
• What if sender is big-endian while receiver is little-endian?

Network Byte Order
• Common byte order used for all network data transmission
• Always big-endian
• Opposite of x86 machine architecture
• Integer data being sent out on the network must be converted to network byte order and vice versa
• Port numbers and IP addresses should also be converted

Network Byte Order Conversion
• Host-to-network byte order conversion
• htons(): 16-bit conversion
• htonl(): 32-bit conversion
• Reverse conversion
• ntohs(), ntohl()
• Why don’t we have htonc(),ntohc()?
• Should we use conversion functions if we are programming on a big-endian

Network Byte Order Conversion
• Host-to-network byte order conversion
• htons(): 16-bit conversion
• htonl(): 32-bit conversion
• Reverse conversion
• ntohs(), ntohl()
• Why don’t we have htonc(),ntohc()?
• Should we use conversion functions if we are programming on a big-endian
For the sake of portability, you should always use these functions

Socket Operations

Socket Operations
• Some generic operations
• Socket creation: socket()
• Connecting, data sending and receiving: specific to protocols, client/server,
• Connectionless vs. connection-oriented (TCP vs. UDP) • Client vs. server
• Socket closing: close()

TCP sockets on the server side
• Bind the socket to an address using bind()
• Parameters for the address: IP address and port number.
• Listen/wait for incoming connections using listen() • Accept an incoming connection using accept()
• Send and receive data using write() and read()

TCP sockets on the client side
• Connect the socket to server using connect()
• Need to provide the IP address and port number on the server
• Send and receive data using write() and read()

UDP sockets on the server side
• Bind the socket to an address using bind() • Wait for incoming requests using recvfrom()
• Instead of read() as in TCP
• Once the request arrives, read, serve it and return results
• Use sendto() to return results
• No listen() or accept() because of the connectionless nature of UDP

UDP sockets on the client side
• Send requests to server using sendto(). • Receive results using recvfrom()

Socket Programming

Socket headers
// Data types
#include
// Defines sockets-related types, structure, etc. #include
// Internet Protocol family
#include
// Definitions for internet operations
#include
// Definitions for network database operations #include

Create a socket
int socket(int domain, int type, int protocol);
• domain specifies a communication domain(the protocol family that will be used for communication)
• Possible value for domain
• AF_UNIX, AF_LOCAL are for local communication
• Sockets can be used for local IPC!
• AF_INET is for IPv4 internet protocols • AF_INET6 is for IPv6 internet protocols

Create a socket
• type specifies the communication semantics. • Defined types:
• SOCK_STREAM, provides sequenced, reliable, two-way, connection-based byte streams(TCP).
• SOCK_DGRAM, supports connectionless, unreliable datagrams(UDP).
• protocol specifies a particular protocol to be used with the socket. • socket() returns a file descriptor of the socket.
• On error, -1 is returned

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
• bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd

struct sockaddr
struct sockaddr_in{
unit8_t sin_len; /* length of the structure */ sa_family_t sin_family; /* protocol family, usually AF_INET */ in_port_t sin_port; /* 16-bit TCP or UDP port number */ struct in_addr sin_addr; /* 32-bit IPv4 address */
char sin_zero[8]; /* unused */

bind() example
int port = 6666;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port);
bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
/* INADDR_ANY means the local address pikced by the OS */

Network byte order
• uint32_t htonl(uint32_t hostlong)
• Converts the unsigned integer hostlong from host byte order to network byte
• uint16_t htons(uint16_t hostshort)
• Converts the unsigned short integer hostshort from host byte order to network
byte order.
• uint32_t ntohl(uint32_t netlong)
• Converts the unsigned integer netlong from network byte order to host byte
• uint16_t ntohs(uint16_t netshort)
• Converts the unsigned short integer netshort from network byte order to host
byte order.
Always use them!

Choosing port
• Server picks the port number.
• E.g.: Web: 80, ftp: 21, ssh: 22
• Want OS to pick the port number?
• You can bind to port zero
• You can pick >1024(up to 65536, 16-bit) as your server’s port number • netstat lists active ports

Listen and accept
int listen(int fd, int backlog);
• listen() marks the socket referred to by sockfd as a passive socket
• A socket that will be used to accept incoming connection requests using
• backlog defines the maximum length of the pending queue
• If a connection request arrives when the queue is full, the client may receive

Listen and accept
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
• accept() extracts the first connection request on the pending queue
• On success, accept() return a integer as the file descriptor for the accepted
• accept() blocks until a request arrives

Listen and accept example
listen(sockfd, 5); /* length of pending queue is 5 */
new_fd = accept(sockfd, (struct sockaddr*)&client_addr, &addr_len);

Server styles
• Single thread, can only serve one request at a time
• Multi threads, each thread in the server will serve a request

TCP Client
struct sockaddr_in server_addr;
int sockfd = socket(AF_INET, SOCK_STREAM, 0); char *hello = “Hi, I am a client”;
char buffer[128] = {0};
int status;
if(sockfd < 0){ printf("\n Socket creation error \n"); return -1; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); TCP Client(cont’d) if(inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) < 0){ printf("\n Invalid address / Address not supported\n"); return -1; if(connect(sockfd, (struct sockaddr *)&server_addr, \ sizeof(server_addr))){ printf("\n Connection Failed \n"); return -1; send(sockfd, hello, strlen(hello), 0); status = read(sockfd, buffer, 128); close(sockfd); Notes on TCP Client • No need to do bind(), OS will automatically bind the socket to a port number • After using the socket, close it with close() • The server’s address is specified by IP address and port number • Remember to convert integer from host to network formats. Socket Programming on UDP • When create socket, use SOCK_DGRAM instead of SOCK_STREAM. • No listen() or accept() on the server. • No connect() on the client • Just use sendto() and recvfrom() sendto and recvfrom #include
#include
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, \ const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, \ struct sockaddr *src_addr, socklen_t *addrlen);

Example server and client programs
See code/22-sockets/client.c and code/22-sockets/server.c

• What are Sockets?
• Endpoints, files, etc.
• Socket core functions
• socket(), connect(), send(), recv(), etc. • Address-related functions
• Conversion
• Byte ordering functions

• TCP is Connection less communication protocol
• True • False

Quiz 1 Ans
• TCP is Connection-less communication protocol
• Ans: False, TCP is connection oriented

• Each socket is associated with an n-tuple. Which one of the following represent the correct tuple?
• {protocol, local-addr, remote-addr}
• {protocol, local-port, remote-addr}
• {protocol, local-addr, local-port, remote-addr}
• {protocol, local-addr, local-port, remote-addr, remote-port}

Quiz 2 Ans
• Each socket is associated with an n-tuple. Which one of the following represent the correct tuple?
• {protocol, local-addr, remote-addr}
• {protocol, local-port, remote-addr}
• {protocol, local-addr, local-port, remote-addr}
• {protocol, local-addr, local-port, remote-addr, remote-port}
• Ans: {protocol, local-addr, local-port, remote-addr, remote-port}

• Little-endian sender can communicate with big-endian receiver without any byte order conversion
• True • False

Quiz 3 Ans
• Little-endian sender can communicate with big-endian receiver without any byte order conversion
• Ans: False, byte order conversion functions should be used on the data to be transferred to ensure compatible endian-ness.

• After binding a socket to an address, UDP connection requires the receiver to listen to the socket, to accept any incoming connection
• True • False

Quiz 4 Ans
• After binding a socket to an address, UDP connection requires the receiver to listen to the port, to accept any incoming connection
• Ans: False, UDP is connection-less, therefore there is no need to listen to the port

• AF_INET can be used to specify both IPv4 and IPv6 in socket()
• True • False

Quiz 5 Ans
• AF_INET can be used to specify both IPv4 and IPv6 in socket()
• Ans: False, AF_INET is used for IPv4 and AF_INET6 is used for IPv6

Next lecture
• Security: Access control

References
1. http://www.informit.com/articles/article.aspx?p=169505&seqNum=2
2. https://www.geeksforgeeks.org/socket-programming-cc/
3. https://www.kernel.org/doc/man-pages/
4. Lecture Notes from
http://www-users.cselabs.umn.edu/classes/Spring-2018/csci4061/
5. Lecture Notes from
http://www-users.cselabs.umn.edu/classes/Fall-2018/csci4061/

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com