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