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