Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_socket_utils.h
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 "net/base/ip_endpoint.h"
15 #include "net/quic/quic_protocol.h"
16
17 namespace net {
18 namespace tools {
19
20 class QuicSocketUtils {
21  public:
22   // If the msghdr contains IP_PKTINFO or IPV6_PKTINFO, this will return the
23   // IPAddressNumber in that header.  Returns an uninitialized IPAddress on
24   // failure.
25   static IPAddressNumber GetAddressFromMsghdr(struct msghdr *hdr);
26
27   // If the msghdr contains an SO_RXQ_OVFL entry, this will set dropped_packets
28   // to the correct value and return true. Otherwise it will return false.
29   static bool GetOverflowFromMsghdr(struct msghdr *hdr,
30                                     uint32 *dropped_packets);
31
32   // Sets either IP_PKTINFO or IPV6_PKTINFO on the socket, based on
33   // address_family.  Returns the return code from setsockopt.
34   static int SetGetAddressInfo(int fd, int address_family);
35
36   // Reads buf_len from the socket.  If reading is successful, returns bytes
37   // read and sets peer_address to the peer address.  Otherwise returns -1.
38   //
39   // If dropped_packets is non-null, it will be set to the number of packets
40   // dropped on the socket since the socket was created, assuming the kernel
41   // supports this feature.
42   //
43   // If self_address is non-null, it will be set to the address the peer sent
44   // packets to, assuming a packet was read.
45   static int ReadPacket(int fd, char* buffer, size_t buf_len,
46                         uint32* dropped_packets,
47                         IPAddressNumber* self_address,
48                         IPEndPoint* peer_address);
49
50   // Writes buf_len to the socket. If writing is successful, sets the result's
51   // status to WRITE_STATUS_OK and sets bytes_written.  Otherwise sets the
52   // result's status to WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR and sets
53   // error_code to errno.
54   static WriteResult WritePacket(int fd, const char* buffer, size_t buf_len,
55                                  const IPAddressNumber& self_address,
56                                  const IPEndPoint& peer_address);
57 };
58
59 }  // namespace tools
60 }  // namespace net
61
62 #endif  // NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_