1 // Copyright 2010, Shuo Chen. All rights reserved. 2 // http://code.google.com/p/muduo/ 3 // 4 // Use of this source code is governed by a BSD-style license 5 // that can be found in the License file. 6 7 // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 // 9 // This is an internal header file, you should not include this. 10 11 #ifndef MUDUO_NET_SOCKETSOPS_H 12 #define MUDUO_NET_SOCKETSOPS_H 13 14 #include <arpa/inet.h> 15 #include <endian.h> 16 17 namespace muduo 18 { 19 namespace sockets 20 { 21 22 inline uint64_t hostToNetwork64(uint64_t host64) 23 { 24 return htobe64(host64); 25 } 26 27 inline uint32_t hostToNetwork32(uint32_t host32) 28 { 29 return htonl(host32); 30 } 31 32 inline uint16_t hostToNetwork16(uint16_t host16) 33 { 34 return htons(host16); 35 } 36 37 inline uint64_t networkToHost64(uint64_t net64) 38 { 39 return be64toh(net64); 40 } 41 42 inline uint32_t networkToHost32(uint32_t net32) 43 { 44 return ntohl(net32); 45 } 46 47 inline uint16_t networkToHost16(uint16_t net16) 48 { 49 return ntohs(net16); 50 } 51 52 /// 53 /// Creates a non-blocking socket file descriptor, 54 /// abort if any error. 55 int createNonblockingOrDie(); 56 57+int connect(int sockfd, const struct sockaddr_in& addr); 58 void bindOrDie(int sockfd, const struct sockaddr_in& addr); 59 void listenOrDie(int sockfd); 60 int accept(int sockfd, struct sockaddr_in* addr); 61 void close(int sockfd); 62 void shutdownWrite(int sockfd); 63 64 void toHostPort(char* buf, size_t size, 65 const struct sockaddr_in& addr); 66 void fromHostPort(const char* ip, uint16_t port, 67 struct sockaddr_in* addr); 68 69 struct sockaddr_in getLocalAddr(int sockfd); 70+struct sockaddr_in getPeerAddr(int sockfd); 71 72 int getSocketError(int sockfd); 73+bool isSelfConnect(int sockfd); 74 75 } 76 } 77 78 #endif // MUDUO_NET_SOCKETSOPS_H 79