19a1e991dSShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 29a1e991dSShuo Chen// http://code.google.com/p/muduo/ 39a1e991dSShuo Chen// 49a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license 59a1e991dSShuo Chen// that can be found in the License file. 69a1e991dSShuo Chen 79a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 89a1e991dSShuo Chen// 99a1e991dSShuo Chen// This is an internal header file, you should not include this. 109a1e991dSShuo Chen 119a1e991dSShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H 129a1e991dSShuo Chen#define MUDUO_NET_SOCKETSOPS_H 139a1e991dSShuo Chen 149a1e991dSShuo Chen#include <arpa/inet.h> 159a1e991dSShuo Chen#include <endian.h> 169a1e991dSShuo Chen 179a1e991dSShuo Chennamespace muduo 189a1e991dSShuo Chen{ 199a1e991dSShuo Chennamespace sockets 209a1e991dSShuo Chen{ 219a1e991dSShuo Chen 229a1e991dSShuo Cheninline uint64_t hostToNetwork64(uint64_t host64) 239a1e991dSShuo Chen{ 249a1e991dSShuo Chen return htobe64(host64); 259a1e991dSShuo Chen} 269a1e991dSShuo Chen 279a1e991dSShuo Cheninline uint32_t hostToNetwork32(uint32_t host32) 289a1e991dSShuo Chen{ 299a1e991dSShuo Chen return htonl(host32); 309a1e991dSShuo Chen} 319a1e991dSShuo Chen 329a1e991dSShuo Cheninline uint16_t hostToNetwork16(uint16_t host16) 339a1e991dSShuo Chen{ 349a1e991dSShuo Chen return htons(host16); 359a1e991dSShuo Chen} 369a1e991dSShuo Chen 379a1e991dSShuo Cheninline uint64_t networkToHost64(uint64_t net64) 389a1e991dSShuo Chen{ 399a1e991dSShuo Chen return be64toh(net64); 409a1e991dSShuo Chen} 419a1e991dSShuo Chen 429a1e991dSShuo Cheninline uint32_t networkToHost32(uint32_t net32) 439a1e991dSShuo Chen{ 449a1e991dSShuo Chen return ntohl(net32); 459a1e991dSShuo Chen} 469a1e991dSShuo Chen 479a1e991dSShuo Cheninline uint16_t networkToHost16(uint16_t net16) 489a1e991dSShuo Chen{ 499a1e991dSShuo Chen return ntohs(net16); 509a1e991dSShuo Chen} 519a1e991dSShuo Chen 529a1e991dSShuo Chen/// 539a1e991dSShuo Chen/// Creates a non-blocking socket file descriptor, 549a1e991dSShuo Chen/// abort if any error. 559a1e991dSShuo Chenint createNonblockingOrDie(); 569a1e991dSShuo Chen 579a1e991dSShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr); 589a1e991dSShuo Chenvoid listenOrDie(int sockfd); 599a1e991dSShuo Chenint accept(int sockfd, struct sockaddr_in* addr); 609a1e991dSShuo Chenvoid close(int sockfd); 619a1e991dSShuo Chen 629a1e991dSShuo Chenvoid toHostPort(char* buf, size_t size, 639a1e991dSShuo Chen const struct sockaddr_in& addr); 649a1e991dSShuo Chenvoid fromHostPort(const char* ip, uint16_t port, 659a1e991dSShuo Chen struct sockaddr_in* addr); 66e54e5389SShuo Chen 67e54e5389SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd); 68e54e5389SShuo Chen 699a1e991dSShuo Chen} 709a1e991dSShuo Chen} 719a1e991dSShuo Chen 729a1e991dSShuo Chen#endif // MUDUO_NET_SOCKETSOPS_H 73