quic_socket_utils.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Some socket related helper methods for quic.
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <stddef.h>
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <sys/socket.h>
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <string>
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/base/ip_endpoint.h"
154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/quic/quic_protocol.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace net {
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace tools {
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class QuicSocketUtils {
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the msghdr contains IP_PKTINFO or IPV6_PKTINFO, this will return the
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // IPAddressNumber in that header.  Returns an uninitialized IPAddress on
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // failure.
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static IPAddressNumber GetAddressFromMsghdr(struct msghdr *hdr);
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the msghdr contains an SO_RXQ_OVFL entry, this will set dropped_packets
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // to the correct value and return true. Otherwise it will return false.
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool GetOverflowFromMsghdr(struct msghdr *hdr,
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    uint32 *dropped_packets);
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Sets either IP_PKTINFO or IPV6_PKTINFO on the socket, based on
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // address_family.  Returns the return code from setsockopt.
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static int SetGetAddressInfo(int fd, int address_family);
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Sets the send buffer size to |size| and returns false if it fails.
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static bool SetSendBufferSize(int fd, size_t size);
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Sets the receive buffer size to |size| and returns false if it fails.
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static bool SetReceiveBufferSize(int fd, size_t size);
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Reads buf_len from the socket.  If reading is successful, returns bytes
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // read and sets peer_address to the peer address.  Otherwise returns -1.
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If dropped_packets is non-null, it will be set to the number of packets
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // dropped on the socket since the socket was created, assuming the kernel
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // supports this feature.
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If self_address is non-null, it will be set to the address the peer sent
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // packets to, assuming a packet was read.
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static int ReadPacket(int fd,
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                        char* buffer,
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                        size_t buf_len,
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                        uint32* dropped_packets,
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        IPAddressNumber* self_address,
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        IPEndPoint* peer_address);
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Writes buf_len to the socket. If writing is successful, sets the result's
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // status to WRITE_STATUS_OK and sets bytes_written.  Otherwise sets the
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // result's status to WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR and sets
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // error_code to errno.
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  static WriteResult WritePacket(int fd, const char* buffer, size_t buf_len,
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 const IPAddressNumber& self_address,
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 const IPEndPoint& peer_address);
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // A helper for WritePacket which fills in the cmsg with the supplied self
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // address.
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static void SetIpInfoInCmsg(const IPAddressNumber& self_address,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              cmsghdr* cmsg);
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch private:
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DISALLOW_COPY_AND_ASSIGN(QuicSocketUtils);
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace tools
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace net
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif  // NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_
79