SocketsOps.h revision 65c497a3
165c497a3SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 265c497a3SShuo Chen// http://code.google.com/p/muduo/ 365c497a3SShuo Chen// 465c497a3SShuo Chen// Use of this source code is governed by a BSD-style license 565c497a3SShuo Chen// that can be found in the License file. 665c497a3SShuo Chen 765c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 865c497a3SShuo Chen// 965c497a3SShuo Chen// This is an internal header file, you should not include this. 1065c497a3SShuo Chen 1165c497a3SShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H 1265c497a3SShuo Chen#define MUDUO_NET_SOCKETSOPS_H 1365c497a3SShuo Chen 1465c497a3SShuo Chen#include <arpa/inet.h> 1565c497a3SShuo Chen#include <endian.h> 1665c497a3SShuo Chen 1765c497a3SShuo Chennamespace muduo 1865c497a3SShuo Chen{ 1965c497a3SShuo Chennamespace sockets 2065c497a3SShuo Chen{ 2165c497a3SShuo Chen 2265c497a3SShuo Chen// the inline assembler code makes type blur, 2365c497a3SShuo Chen// so we disable warnings for a while. 2465c497a3SShuo Chen#pragma GCC diagnostic ignored "-Wconversion" 2565c497a3SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64) 2665c497a3SShuo Chen{ 2765c497a3SShuo Chen return htobe64(host64); 2865c497a3SShuo Chen} 2965c497a3SShuo Chen 3065c497a3SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32) 3165c497a3SShuo Chen{ 3265c497a3SShuo Chen return htonl(host32); 3365c497a3SShuo Chen} 3465c497a3SShuo Chen 3565c497a3SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16) 3665c497a3SShuo Chen{ 3765c497a3SShuo Chen return htons(host16); 3865c497a3SShuo Chen} 3965c497a3SShuo Chen 4065c497a3SShuo Cheninline uint64_t networkToHost64(uint64_t net64) 4165c497a3SShuo Chen{ 4265c497a3SShuo Chen return be64toh(net64); 4365c497a3SShuo Chen} 4465c497a3SShuo Chen 4565c497a3SShuo Cheninline uint32_t networkToHost32(uint32_t net32) 4665c497a3SShuo Chen{ 4765c497a3SShuo Chen return ntohl(net32); 4865c497a3SShuo Chen} 4965c497a3SShuo Chen 5065c497a3SShuo Cheninline uint16_t networkToHost16(uint16_t net16) 5165c497a3SShuo Chen{ 5265c497a3SShuo Chen return ntohs(net16); 5365c497a3SShuo Chen} 5465c497a3SShuo Chen#pragma GCC diagnostic error "-Wconversion" 5565c497a3SShuo Chen 5665c497a3SShuo Chen/// 5765c497a3SShuo Chen/// Creates a non-blocking socket file descriptor, 5865c497a3SShuo Chen/// abort if any error. 5965c497a3SShuo Chenint createNonblockingOrDie(); 6065c497a3SShuo Chen 6165c497a3SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr); 6265c497a3SShuo Chenvoid listenOrDie(int sockfd); 6365c497a3SShuo Chenint accept(int sockfd, struct sockaddr_in* addr); 6465c497a3SShuo Chenvoid close(int sockfd); 6565c497a3SShuo Chen 6665c497a3SShuo Chenvoid toHostPort(char* buf, size_t size, 6765c497a3SShuo Chen const struct sockaddr_in& addr); 6865c497a3SShuo Chenvoid fromHostPort(const char* ip, uint16_t port, 6965c497a3SShuo Chen struct sockaddr_in* addr); 7065c497a3SShuo Chen 7165c497a3SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd); 7265c497a3SShuo Chen 7365c497a3SShuo Chenint getSocketError(int sockfd); 7465c497a3SShuo Chen 7565c497a3SShuo Chen} 7665c497a3SShuo Chen} 7765c497a3SShuo Chen 7865c497a3SShuo Chen#endif // MUDUO_NET_SOCKETSOPS_H 79