Does linux socket "send" function do a hton conversion? - sockets

When trying to use TCP/IP socket - "socket(AF_INET, SOCK_STREAM, 0)", tcp in this case, does the call to send and recv do a byte-order conversion automatically ?

At the TCP level, byte ordering only applies to the IPs and ports in the TCP/IP headers, which are established when connect()/accept() are called. When working with instances of sockaddr_in... structs, the user is responsible for handling byte conversions to/from network byte order as needed.
send()/recv() simply deal with a socket handle and a raw byte array, so there are no byte order issues when calling them. However, if the byte array has data that contains multi-byte integers in it, those have to be handled separately by the user as needed.

Related

Checksum checking in UDP using UDP socket

Is it possible for a UDP socket (SOCK_DGRAM) to access checksum field from an incoming UDP packet and check for errors? I know that we can do that using raw sockets (SOCK_RAW), but I want to know whether we can do it using datagram sockets. If so, how can we do it in C?
If you create a normal UDP socket you don't have access to the UDP header and thus also not to the checksum. But the kernel will already discard packets where the checksum is incorrect so you would not see these packets anyway.
You can't do it using datagram sockets (SOCK_DGRAM), because the TCP/IP stack removes those UDP header bytes from the received buffer before passing it up to higher layer APIs. You need to use raw sockets (SOCK_RAW) so that these bytes are preserved.

Actual bytes read and written by IO::Socket::SSL

I'd like to count bytes in/out from a socket. For a regular socket, I can just total the size change of buffer effected by recv() and the return value of send(). How do you do this with IO::Socket::SSL?
IO::Socket::SSL does not provide you with that view to the underlying TCP socket since it let just OpenSSL handle the TCP socket (via Net::SSLeay). In order to get such details you would need to handle read/write on the TCP socket yourself and then interact with the SSL layer using the BIO interface. Of course, this is way more complex than just using the abstraction offered by IO::Socket::SSL.

Why use htons() to specify protocol when creating packet socket?

To create a packet socket, following socket() function call is used (socket type and protocol may be different):
socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
And to create a stream socket, following call is used:
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
My question is why use htons() to specify protocol when creating a packet socket and not when creating socket of AF_INET or AF_INET6 family? Why not use
socket(AF_INET, SOCK_XXX, htons(IPPROTO_XXX))
to create a STREAM or DATAGRAM socket as used when creating a packet socket or vice-versa. What is different with the use of the protocols in the two calls to socket() function as both the calls are used to create sockets, one for packet socket and the other for socket at TCP level?
First, like most other network parameters that are passed to the kernel (IP addresses, ports, etc), the parameters are passed in their "on-the-wire" format so that underlying software doesn't need to manipulate them before comparing/copying/transmitting/etc. (For comparison, consider that AF_PACKET and SOCK_RAW are parameters to the kernel itself -- hence "native format" is appropriate -- while the ETH_P_xxx value is generally for "comparison with incoming packets"; it just so happens that ETH_P_ALL is a special signal value saying 'capture everything'.)
Second, interpretation of the protocol is potentially different by address family. A different address family could choose to interpret the protocol in whatever form made sense for it. It just so happens that Ethernet and IP have always used big-endian (and were important/ubiquitous enough that big-endian came to be called network order).
Third, the protocol number in the AF_INET world (i.e. Internet Protocol) only occupies a single byte so it doesn't make sense to specify a byte-ordering.

What happens when sending an empty string with sctp_recvmsg() to a SOCK_STREAM SCTP socket?

As mentioned in SCTP receive message function returning zero, sctp_recvmsg() returns a length of 0 for a one-to-one SCTP connection (socket(PF_INET, SOCK_STREAM, IPPROTO_SCT)) when the other side closes the connection.
What happens / is supposed to happen for a standards compliant implementation when I send an empty string ("") with sctp_sendmsg() through such a connection?
(How) Does the receiving side tell apart whether I closed the connection or just sent an empty string/
Or is it illegal to send an empty string?
I haven't yet come across anything explaining this.
EDIT: I should mention that for standard recv() of SOCK_STREAM (e.g. for TCP), receiving a 0-length buffer happens when the peer has performed an orderly shutdown, thus apparently you're not allowed to send() an empty string in TCP. For a stream-only protocol like TCP this makes total sense, since 0-length data cannot have a meaning in a stream. But it could well have a meaning for a datagram as in the case of sctp_recvmsg(), so we can't really say "it's just like for TCP".

Can UDP (unicast client) recvfrom() other servers other than the one sendto()?

I am creating a UDP socket client in C (unicast) and is wondering why recvfrom() has a struct sockaddr * argument in which in the man page says,
A null pointer, or points to a sockaddr structure in which the sending address is to be stored.
Is it possible that I could receive a message from a different server other than the one I sendto? If yes, how to create this scenario?
If no, is it correct to say that this argument is only useful when broadcast mode is used?
Yes, this is perfectly possible. The reason for this is that UDP is not stream-based, but packet-based. Every packet is treated without any history (other packets sent or received).
For this reason you may also open a UDP port and then send packets to different hosts from it. However, I do not remember how well this is supported by the API.
The UDP socket will recvfrom() any host sending to this one with correct port unless you explicitly connect(), in which case you can just write() and read(), and get errors upon received ICMP messages.
Considering you always have two parties in UDP, it seems rather obvious that someone has to recvfrom() first.