Preliminary Questions Report

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 10

Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

RESPONSES
Q1 What is the function htons used for?
This function is used for converting 16-bit quantities from host network to network byte order.
Htons is exactly for uint16_t hostshort.
This function is most often used in conjunction with IPv4 addresses.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12

Q2 What is uint32_t?
Unit32_t is a data type for variables that need to store values from 0 to 2^32-1 without any
negative values. This is important for IP addresses because for sure the size of the number is 32
bits.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12

Q3 What is the difference between ssize_t and size_t?


The main difference between this two types of variables is that type ssize_t is an unsigned
integer the size of the different objects and memory allocations and ssize_t is a signed integer
that is usually used for represent the number of bytes (when it’s negative means that there is an
error).
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12

Q4 What is the value of b in the following code snippet? const ssize_t a = -1; const
size_t b = (size_t) a;
As we can see, in this case ‘a’ is -1 and is ssize_t variable. This means that, as we explain in the
last question, when we want to transfer that data to a size_t, we have to be concient that size_t
data can’t be negative so we need to transform the number.
Number -1 is ‘1111 1111’ in 8 bits, so if we see this number in size_t it will be 2^64-1).
2^64-1 = 18.446.744.073.709.551.616
I tried the code in the linux terminal and this is the number I got, so I deduce that there is a 64 bit
system.
I try it in my terminal & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12

Q5 What is the value of b in the code snippet below? const uint32_t a = 256; const
uint8_t b = (uint8_t) a;
When we want to transform one number of 32 bits to a number of 8 bits what we do is truncate
the number. So 256 in 32 bit is 0000 0001 0000 0000, and if we truncate in bit 8 we see that the
number resultant is 0000 0000, so b value in this case is 0.
I already know that & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12

Q6 What is a file descriptor?


Is an integer value that identifies an open file or stream.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10

Q7 What values for the protocol parameter of the socket function are available for

Spring 10/03/23 1/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

AF_INET in POSIX? What does it mean that the default value for protocol in socket
is implementation defined?
The different values for the protocol parameter of the socket() function are:
● TCP protocol
● UDP protocol
● SCTP protocol
When the protocol parameter is not defined in the socket() , the behavior is implementation-
defined. This means that the protocol used will be the default protocol selected by the operating
system.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10

Q8 Write some code that creates a TCP socket.


tcp_socket = socket(AF_INET, SOCK_STREAM, 0);

SOCK_STREAM argument specifies its an TCP socket:


SOCK_STREAM
Provides sequenced, reliable, two-way, connection-based
byte streams. An out-of-band data transmission mechanism
may be supported.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
ip(7) - Linux manual page (man7.org)

Q9 Write some code that creates a UDP socket.


udp_socket = socket(AF_INET, SOCK_DGRAM, 0);

SOCK_DGRAM argument specifies its an UDP socket:


SOCK_DGRAM
Supports datagrams (connectionless, unreliable messages of
a fixed maximum length).

https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
ip(7) - Linux manual page (man7.org)

Spring 10/03/23 2/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

Q10 How do we close a socket?


To close a socket we have the function close(sock). This function takes the file descriptor of the
socket and closes it. After we close the socket, the in/out operations will fail.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_07

Q11 What happens if we leave it open?


Leaving a socket open can cause problems in the network application. The sockets require
system resources (memory and a file descriptor) and this can cause the computer to
malfunction. Also can cause malfunction of the network and also security risks, especially if the
application opened is in administrator mode.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_07

Q12 If we never close any sockets, we may eventually receive an EMFILE error code in
socket. Why?
When we have a lot of sockets open, we have this error because the system can open a limited
number of file descriptors at the same time. This number depends on the operating system. This
can be avoided by closing the sockets we are not using.
https://www.baeldung.com/linux/error-too-many-open-files &
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_07

Q13 What is errno? How is it used?


Errno is a variable of C used to indicate errors that happen during the program execution. When
an error happens, this variable is set to a non-zero value, and it indicates that there is an error
and, depending on the number it indicates the type of error too.
For example, if the function close() fail, this variable will get a value that indicate that the function
is failing and a number that indicates the type of error.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html#tag_16_110

Q14 Why is the perror function used? Show a small example.


Perror is a function used to print an error message with the errno (explained in the previous
question) and the corresponding explanation of the failure.
For example (in our pong code):

if (bind(sock, (struct sockaddr *) &source, sizeof(source)) == -1) {


perror("Socket couldn't be binded");
return 1;
}
https://pubs.opengroup.org/onlinepubs/9699919799/functions/perror.html#tag_16_361

Spring 10/03/23 3/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

Q15 Can we modify errno ourselves in our code? And should we?
Modifying errno is possible, but is not recommended. The errno variable is used for attribute a
value to an error. If we modify this it can cause a non-predictable behavior because we can
overwrite a system error that we don’t want to change.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html#tag_16_110

Q16 Which errors can result from a socket function call? And from sendto? (do not
describe them the error codes, just mention them: EAGAIN)
Socket:

● EACCES
● EAFNOSUPPORT
● EINVAL
● EMFILE
● ENFILE
● ENOBUFS or ENOMEM
● EPROTONOSUPPORT

Sendto:

● EACCES
● EAGAIN or EWOULDBLOCK
● EBADF
● ECONNRESET
● EDESTADDRREQ
● EFAULT
● EINTR
● EINVAL
● EISCONN
● EMSGSIZE
● ENOBUFS

Spring 10/03/23 4/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

● ENOMEM
● ENOTCONN
● ENOTSOCK
● EOPNOTSUPP
● EPIPE
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html

Q17 What happens if a program terminates (e.g., exit(EXIT_FAILURE)) while still having
some open sockets (not having called close)?
If a program terminates without closing its open sockets, the operating system will liberate the
resources of that socket, but it depends on the operating system. Leaving open sockets can
behave to a malfunction (explained in Q11). Other systems can wait a period of time before
closing the sockets.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10

Q18 What does bind do?


The bind function connects the socket to the network address and port number. We call it after creating the socket.
If bind is successful, it returns 0, and if it isn’t successful it return -1 with the errno value.

Bind function in our program:


bind(sock, (struct sockaddr *) &source, sizeof(source)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html#tag_16_33

Q19 What is the difference between sockaddr, sockaddr_in, sockaddr_in6, and


{sockaddr_storage}?
The main differences between them are: the size and the type of address they can represent.
● Sockaddr: base structure for network addresses, used when the address family is not known.
● Sockaddr_in: Structure to represent IPv4 addresses.
● Sockaddr_in6: Structure to represent IPv6 addresses.
● Sockaddr_storage: It can hold any of the previous structures. It’s used for when we don’t know the IP
family but we need to save sufficient space.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html#tag_16_562

Q20 Why is there an addrlen parameter in bind?


This parameter is necessary because the function needs to know the length of the address to interpret it. Without
this parameter the bind function can cause memory access errors because it wouldn’t be able to determine how
long the address is.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html#tag_16_33

Q21 What is in_port_t?


Is a C data type that is used to represent a network port number. We can find it in the <netinet/in.h> and is an
unsigned integral type of exactly 16 bits.
https://pubs.opengroup.org/onlinepubs/7908799/xns/netinetin.h.html

Spring 10/03/23 5/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

Q22 Which of the following statements regarding port 8080 is correct: in_port_t port =
htons(8080), in_port_t port = ntohs(8080), or in_port_t port = 8080? Why?
The correct form is: in_port_t port = htons(8080);
The htons() function is used to convert 16-bit value from host byte order to network byte order.
The ntohs() function is used to convert 16-bit value from network order to host order, but in this case this have no
sense because 8080 is in host order so it would fail.
The in_port_t assign directly the value to the port, and this would fail too.
https://pubs.opengroup.org/onlinepubs/7908799/xns/netinetin.h.html

Q23 Give a proper initialization code for a sockaddr_in structure for port 8080 and
address 127.0.0.0.1.
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");

ip(7) - Linux manual page (man7.org)

Q24 Give a proper initialization code for a sockaddr_in structure for port 8080 and any
address.
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
ip(7) - Linux manual page (man7.org)

Q25 What is a null pointer?


Null pointer is a value saved for indicating that the pointer is not reference to a valid object. Is like a pointer
pointing to anything.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03

Q26 What does it mean when a program receives a SIGSEGV?


This happens when the program tries to access memory location that it doesn’t have permission to access, or
memory that doesn’t exist. For example, when you try to reference a pointer that is null this problem can happen.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03

Q27 What is valgrind and how can we use it to debug memory problems?
Valgrind is an instrumentation used for building dynamic analysis tools. These tools can automatically detect
memory management and threading bugs, and profile our programs in detail. Valgrind can be used to detect other
errors from the program, but it’s mainly used to detect memory errors.
https://valgrind.org/ & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03

Q28 Why is it a good practice to free all allocated memory before calling
exit(EXIT_SUCCESS)? (hint: valgrind)
It allows valgrind to check for memory leaks without false positives.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03

Spring 10/03/23 6/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

Q29 What does connect do?


Initiate a connection on a socket
https://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html

Q30 Why does connect take one addr parameter?


Specifies the address of the socket that must connect to. The addr parameter is a pointer to a sockaddr structure
that contains information about the destination address and port.
https://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html

Q31 Why do we need to call accept after listen?


To accept an incoming connection on the socket that is listening.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

Q32 Why does accept take one addr parameter?


Addr is either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket shall
be returned.

If address is not a null pointer, the address of the peer for the accepted connection shall be stored in the sockaddr
structure pointed to by address,
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

Q33 In the following code snippet, are a and b equal? Why?


No.

a -> accept() first argument socket


(Specifies a socket that was created with socket(), has been bound to an address with bind(), and has issued a
successful call to listen().)

b -> New socket. (Create a new socket with the same socket type protocol and address family as the specified
socket, and allocate a new file descriptor for that socket)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

Q34 What happens if we listen or connect to an unbound socket? (bind has not been
called).
If we listen without a bind, we will get an error:

[EDESTADDRREQ]

Spring 10/03/23 7/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

The socket is not bound to a local address, and the protocol does not support listening on an unbound socket.

If we connect with an unbound socket, the following will happen:

If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the
socket's address family is AF_UNIX, is an unused local address.

We can’t connect to an unbound socket because the listen will get an error.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html

Q35 On a forking server, after accept and fork, why should the parent close the socket
returned by accept? (hint: resource exhaustion)
In a forking server, after accept and fork, the parent should close the socket because it is no longer needed. The
child process will now use this socket to communicate with the client while the parent process continues to listen
for new connections on the listening socket.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html

Q36 What is non-blocking I/O?


An operation that doesn’t block the execution of the program if the incoming data is not immediately available,
instead, it returns an error.
I already know that

Q37 How can we set a socket to perform non-blocking I/O? (give some code)
We need to set O_NONBLOCK flag to the file descriptor of the socket using fcntl(). An example:

int flags;
if (-1 == (flags = fcntl(socket_fd, F_GETFL, 0)))
flags = 0;
fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK);
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html

Q38 What happens with a read on a blocking socket if all the requested data is not
available?
The function will block the execution of the program until all the data arrives:

If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recv() shall
block until a message arrives.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html

Q39 What happens with a write on a blocking socket if the socket buffer is full?
The function will block the execution of the program until all the data can be written:

Spring 10/03/23 8/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() shall fail
and set errno to [EAGAIN] or [EWOULDBLOCK].
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html

Q40 What happens with a read on a non-blocking socket if all the requested data is not
available? (hint: there are two possibilities)

If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() shall fail
and set errno to [EAGAIN] or [EWOULDBLOCK]. (Returning -1)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html

Q41 What happens with a write on a non-blocking socket if the data to write does not fit
in the socket buffer? (hint: there are two possibilities)

If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor
does have O_NONBLOCK set, sendto() shall fail. (Returning -1)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html

Q42 How is the function poll used?


The poll() function provides applications with a mechanism for multiplexing input/output over a set of file
descriptors. For each member of the array pointed to by fds, poll() shall examine the given file descriptor for the
event(s) specified in events. The number of pollfd structures in the fds array is specified by nfds. The poll() function
shall identify those file descriptors on which an application can read or write data, or on which certain events have
occurred.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

Q43 Can we mix file descriptors for sockets and disk files in a single poll call?
Yes.

The poll() function shall support regular files, terminal and pseudo-terminal devices, FIFOs, pipes, sockets and [XSR]
[Option Start] STREAMS-based files

https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

Q44 Which events can report a poll call for fds[i].events = POLLIN;?
POLLIN, POLLHUP, POLLERR, POLLNVAL
In addition, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flag in revents if the condition is true, even if the
application did not set the corresponding bit in events.
poll (opengroup.org)
poll(2) - Linux manual page (man7.org)

Q45 Are socket options inherited from a listening socket with accept?
No, accept only creates the same socket type protocol and address family as the specified socket, and allocate a
new file descriptor for that socket.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html

Spring 10/03/23 9/10


Xarxes: Preliminary Questions Àlex Chaves Valera (1637812),David Fuentes Insa (1637892)

Q46 Which size is the option for SO_RCVLOWAT?


An int
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_16

Q47 After poll signals revents = POLLOUT on a non-blocking socket, will a very large
send block execution?
No, The poll() function shall not be affected by the O_NONBLOCK flag.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

Q48 Can getaddrinfo return an empty linked list?


No. The list shall include at least one addrinfo structure.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html

Comments:

Spring 10/03/23 10/10

You might also like