1048f6023SShuo Chen // Copyright 2010, Shuo Chen. All rights reserved. 2048f6023SShuo Chen // http://code.google.com/p/muduo/ 3048f6023SShuo Chen // 4048f6023SShuo Chen // Use of this source code is governed by a BSD-style license 5048f6023SShuo Chen // that can be found in the License file. 6048f6023SShuo Chen 7048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 8048f6023SShuo Chen // 9048f6023SShuo Chen // This is an internal header file, you should not include this. 10048f6023SShuo Chen 11048f6023SShuo Chen #ifndef MUDUO_NET_SOCKETSOPS_H 12048f6023SShuo Chen #define MUDUO_NET_SOCKETSOPS_H 13048f6023SShuo Chen 14048f6023SShuo Chen #include <arpa/inet.h> 15048f6023SShuo Chen #include <endian.h> 16048f6023SShuo Chen 17048f6023SShuo Chen namespace muduo 18048f6023SShuo Chen { 19048f6023SShuo Chen namespace sockets 20048f6023SShuo Chen { 21048f6023SShuo Chen 22048f6023SShuo Chen inline uint64_t hostToNetwork64(uint64_t host64) 23048f6023SShuo Chen { 24048f6023SShuo Chen return htobe64(host64); 25048f6023SShuo Chen } 26048f6023SShuo Chen 27048f6023SShuo Chen inline uint32_t hostToNetwork32(uint32_t host32) 28048f6023SShuo Chen { 29048f6023SShuo Chen return htonl(host32); 30048f6023SShuo Chen } 31048f6023SShuo Chen 32048f6023SShuo Chen inline uint16_t hostToNetwork16(uint16_t host16) 33048f6023SShuo Chen { 34048f6023SShuo Chen return htons(host16); 35048f6023SShuo Chen } 36048f6023SShuo Chen 37048f6023SShuo Chen inline uint64_t networkToHost64(uint64_t net64) 38048f6023SShuo Chen { 39048f6023SShuo Chen return be64toh(net64); 40048f6023SShuo Chen } 41048f6023SShuo Chen 42048f6023SShuo Chen inline uint32_t networkToHost32(uint32_t net32) 43048f6023SShuo Chen { 44048f6023SShuo Chen return ntohl(net32); 45048f6023SShuo Chen } 46048f6023SShuo Chen 47048f6023SShuo Chen inline uint16_t networkToHost16(uint16_t net16) 48048f6023SShuo Chen { 49048f6023SShuo Chen return ntohs(net16); 50048f6023SShuo Chen } 51048f6023SShuo Chen 52048f6023SShuo Chen /// 53048f6023SShuo Chen /// Creates a non-blocking socket file descriptor, 54048f6023SShuo Chen /// abort if any error. 55048f6023SShuo Chen int createNonblockingOrDie(); 56048f6023SShuo Chen 57048f6023SShuo Chen void bindOrDie(int sockfd, const struct sockaddr_in& addr); 58048f6023SShuo Chen void listenOrDie(int sockfd); 59048f6023SShuo Chen int accept(int sockfd, struct sockaddr_in* addr); 60048f6023SShuo Chen void close(int sockfd); 61048f6023SShuo Chen 62048f6023SShuo Chen void toHostPort(char* buf, size_t size, 63048f6023SShuo Chen const struct sockaddr_in& addr); 64048f6023SShuo Chen void fromHostPort(const char* ip, uint16_t port, 65048f6023SShuo Chen struct sockaddr_in* addr); 66048f6023SShuo Chen+ 67048f6023SShuo Chen+struct sockaddr_in getLocalAddr(int sockfd); 68048f6023SShuo Chen+ 69048f6023SShuo Chen } 70048f6023SShuo Chen } 71048f6023SShuo Chen 72048f6023SShuo Chen #endif // MUDUO_NET_SOCKETSOPS_H 73