SocketsOps.h revision 04e5c324
140161064SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
240161064SShuo Chen// http://code.google.com/p/muduo/
340161064SShuo Chen//
440161064SShuo Chen// Use of this source code is governed by a BSD-style license
540161064SShuo Chen// that can be found in the License file.
640161064SShuo Chen
740161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
840161064SShuo Chen//
940161064SShuo Chen// This is an internal header file, you should not include this.
1040161064SShuo Chen
1140161064SShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H
1240161064SShuo Chen#define MUDUO_NET_SOCKETSOPS_H
1340161064SShuo Chen
1440161064SShuo Chen#include <arpa/inet.h>
1540161064SShuo Chen#include <endian.h>
1640161064SShuo Chen
1740161064SShuo Chennamespace muduo
1840161064SShuo Chen{
1940161064SShuo Chennamespace sockets
2040161064SShuo Chen{
2140161064SShuo Chen
2240161064SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64)
2340161064SShuo Chen{
2440161064SShuo Chen  return htobe64(host64);
2540161064SShuo Chen}
2640161064SShuo Chen
2740161064SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32)
2840161064SShuo Chen{
2940161064SShuo Chen  return htonl(host32);
3040161064SShuo Chen}
3140161064SShuo Chen
3240161064SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16)
3340161064SShuo Chen{
3440161064SShuo Chen  return htons(host16);
3540161064SShuo Chen}
3640161064SShuo Chen
3740161064SShuo Cheninline uint64_t networkToHost64(uint64_t net64)
3840161064SShuo Chen{
3940161064SShuo Chen  return be64toh(net64);
4040161064SShuo Chen}
4140161064SShuo Chen
4240161064SShuo Cheninline uint32_t networkToHost32(uint32_t net32)
4340161064SShuo Chen{
4440161064SShuo Chen  return ntohl(net32);
4540161064SShuo Chen}
4640161064SShuo Chen
4740161064SShuo Cheninline uint16_t networkToHost16(uint16_t net16)
4840161064SShuo Chen{
4940161064SShuo Chen  return ntohs(net16);
5040161064SShuo Chen}
5140161064SShuo Chen
5240161064SShuo Chen///
5340161064SShuo Chen/// Creates a non-blocking socket file descriptor,
5440161064SShuo Chen/// abort if any error.
5540161064SShuo Chenint createNonblockingOrDie();
5640161064SShuo Chen
5704e5c324SShuo Chenint  connect(int sockfd, const struct sockaddr_in& addr);
5840161064SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr);
5940161064SShuo Chenvoid listenOrDie(int sockfd);
6040161064SShuo Chenint  accept(int sockfd, struct sockaddr_in* addr);
6140161064SShuo Chenvoid close(int sockfd);
6240161064SShuo Chenvoid shutdownWrite(int sockfd);
6340161064SShuo Chen
6440161064SShuo Chenvoid toHostPort(char* buf, size_t size,
6540161064SShuo Chen                const struct sockaddr_in& addr);
6640161064SShuo Chenvoid fromHostPort(const char* ip, uint16_t port,
6740161064SShuo Chen                  struct sockaddr_in* addr);
6840161064SShuo Chen
6940161064SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd);
7004e5c324SShuo Chenstruct sockaddr_in getPeerAddr(int sockfd);
7140161064SShuo Chen
7240161064SShuo Chenint getSocketError(int sockfd);
7304e5c324SShuo Chenbool isSelfConnect(int sockfd);
7440161064SShuo Chen
7540161064SShuo Chen}
7640161064SShuo Chen}
7740161064SShuo Chen
7840161064SShuo Chen#endif  // MUDUO_NET_SOCKETSOPS_H
79