11bc8a9dfSShuo Chen // Copyright 2010, Shuo Chen.  All rights reserved.
21bc8a9dfSShuo Chen // http://code.google.com/p/muduo/
31bc8a9dfSShuo Chen //
41bc8a9dfSShuo Chen // Use of this source code is governed by a BSD-style license
51bc8a9dfSShuo Chen // that can be found in the License file.
61bc8a9dfSShuo Chen 
71bc8a9dfSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
81bc8a9dfSShuo Chen //
91bc8a9dfSShuo Chen // This is an internal header file, you should not include this.
101bc8a9dfSShuo Chen 
111bc8a9dfSShuo Chen #ifndef MUDUO_NET_SOCKETSOPS_H
121bc8a9dfSShuo Chen #define MUDUO_NET_SOCKETSOPS_H
131bc8a9dfSShuo Chen 
141bc8a9dfSShuo Chen #include <arpa/inet.h>
151bc8a9dfSShuo Chen #include <endian.h>
161bc8a9dfSShuo Chen 
171bc8a9dfSShuo Chen namespace muduo
181bc8a9dfSShuo Chen {
191bc8a9dfSShuo Chen namespace sockets
201bc8a9dfSShuo Chen {
211bc8a9dfSShuo Chen 
221bc8a9dfSShuo Chen inline uint64_t hostToNetwork64(uint64_t host64)
231bc8a9dfSShuo Chen {
241bc8a9dfSShuo Chen   return htobe64(host64);
251bc8a9dfSShuo Chen }
261bc8a9dfSShuo Chen 
271bc8a9dfSShuo Chen inline uint32_t hostToNetwork32(uint32_t host32)
281bc8a9dfSShuo Chen {
291bc8a9dfSShuo Chen   return htonl(host32);
301bc8a9dfSShuo Chen }
311bc8a9dfSShuo Chen 
321bc8a9dfSShuo Chen inline uint16_t hostToNetwork16(uint16_t host16)
331bc8a9dfSShuo Chen {
341bc8a9dfSShuo Chen   return htons(host16);
351bc8a9dfSShuo Chen }
361bc8a9dfSShuo Chen 
371bc8a9dfSShuo Chen inline uint64_t networkToHost64(uint64_t net64)
381bc8a9dfSShuo Chen {
391bc8a9dfSShuo Chen   return be64toh(net64);
401bc8a9dfSShuo Chen }
411bc8a9dfSShuo Chen 
421bc8a9dfSShuo Chen inline uint32_t networkToHost32(uint32_t net32)
431bc8a9dfSShuo Chen {
441bc8a9dfSShuo Chen   return ntohl(net32);
451bc8a9dfSShuo Chen }
461bc8a9dfSShuo Chen 
471bc8a9dfSShuo Chen inline uint16_t networkToHost16(uint16_t net16)
481bc8a9dfSShuo Chen {
491bc8a9dfSShuo Chen   return ntohs(net16);
501bc8a9dfSShuo Chen }
511bc8a9dfSShuo Chen 
521bc8a9dfSShuo Chen ///
531bc8a9dfSShuo Chen /// Creates a non-blocking socket file descriptor,
541bc8a9dfSShuo Chen /// abort if any error.
551bc8a9dfSShuo Chen int createNonblockingOrDie();
561bc8a9dfSShuo Chen 
571bc8a9dfSShuo Chen void bindOrDie(int sockfd, const struct sockaddr_in& addr);
581bc8a9dfSShuo Chen void listenOrDie(int sockfd);
591bc8a9dfSShuo Chen int  accept(int sockfd, struct sockaddr_in* addr);
601bc8a9dfSShuo Chen void close(int sockfd);
611bc8a9dfSShuo Chen+void shutdownWrite(int sockfd);
621bc8a9dfSShuo Chen 
631bc8a9dfSShuo Chen void toHostPort(char* buf, size_t size,
641bc8a9dfSShuo Chen                 const struct sockaddr_in& addr);
651bc8a9dfSShuo Chen void fromHostPort(const char* ip, uint16_t port,
661bc8a9dfSShuo Chen                   struct sockaddr_in* addr);
671bc8a9dfSShuo Chen 
681bc8a9dfSShuo Chen struct sockaddr_in getLocalAddr(int sockfd);
691bc8a9dfSShuo Chen 
701bc8a9dfSShuo Chen int getSocketError(int sockfd);
711bc8a9dfSShuo Chen 
721bc8a9dfSShuo Chen }
731bc8a9dfSShuo Chen }
741bc8a9dfSShuo Chen 
751bc8a9dfSShuo Chen #endif  // MUDUO_NET_SOCKETSOPS_H
76