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