summaryrefslogtreecommitdiff
path: root/chromium/net/tools/quic/quic_socket_utils.h
blob: 8f0feffced24e5519889e527523fc5a5713f1a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Some socket related helper methods for quic.

#ifndef NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_
#define NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_

#include <stddef.h>
#include <sys/socket.h>
#include <string>

#include "net/base/ip_endpoint.h"
#include "net/quic/quic_protocol.h"

namespace net {
namespace tools {

class QuicSocketUtils {
 public:
  // If the msghdr contains IP_PKTINFO or IPV6_PKTINFO, this will return the
  // IPAddressNumber in that header.  Returns an uninitialized IPAddress on
  // failure.
  static IPAddressNumber GetAddressFromMsghdr(struct msghdr *hdr);

  // If the msghdr contains an SO_RXQ_OVFL entry, this will set dropped_packets
  // to the correct value and return true. Otherwise it will return false.
  static bool GetOverflowFromMsghdr(struct msghdr *hdr, int *dropped_packets);

  // Sets either IP_PKTINFO or IPV6_PKTINFO on the socket, based on
  // address_family.  Returns the return code from setsockopt.
  static int SetGetAddressInfo(int fd, int address_family);

  // Reads buf_len from the socket.  If reading is successful, returns bytes
  // read and sets peer_address to the peer address.  Otherwise returns -1.
  //
  // If dropped_packets is non-null, it will be set to the number of packets
  // dropped on the socket since the socket was created, assuming the kernel
  // supports this feature.
  //
  // If self_address is non-null, it will be set to the address the peer sent
  // packets to, assuming a packet was read.
  static int ReadPacket(int fd, char* buffer, size_t buf_len,
                        int* dropped_packets,
                        IPAddressNumber* self_address,
                        IPEndPoint* peer_address);

  // Writes buf_len to the socket. If writing is successful, sets the result's
  // status to WRITE_STATUS_OK and sets bytes_written.  Otherwise sets the
  // result's status to WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR and sets
  // error_code to errno.
  static WriteResult WritePacket(int fd, const char* buffer, size_t buf_len,
                                 const IPAddressNumber& self_address,
                                 const IPEndPoint& peer_address);
};

}  // namespace tools
}  // namespace net

#endif  // NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_