SocketsOps.h revision 354280cf
1354280cfSShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
2354280cfSShuo Chen// http://code.google.com/p/muduo/
3354280cfSShuo Chen//
4354280cfSShuo Chen// Use of this source code is governed by a BSD-style license
5354280cfSShuo Chen// that can be found in the License file.
6354280cfSShuo Chen
7354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
8354280cfSShuo Chen//
9354280cfSShuo Chen// This is an internal header file, you should not include this.
10354280cfSShuo Chen
11354280cfSShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H
12354280cfSShuo Chen#define MUDUO_NET_SOCKETSOPS_H
13354280cfSShuo Chen
14354280cfSShuo Chen#include <arpa/inet.h>
15354280cfSShuo Chen#include <endian.h>
16354280cfSShuo Chen
17354280cfSShuo Chennamespace muduo
18354280cfSShuo Chen{
19354280cfSShuo Chennamespace sockets
20354280cfSShuo Chen{
21354280cfSShuo Chen
22354280cfSShuo Cheninline uint64_t hostToNetwork64(uint64_t host64)
23354280cfSShuo Chen{
24354280cfSShuo Chen  return htobe64(host64);
25354280cfSShuo Chen}
26354280cfSShuo Chen
27354280cfSShuo Cheninline uint32_t hostToNetwork32(uint32_t host32)
28354280cfSShuo Chen{
29354280cfSShuo Chen  return htonl(host32);
30354280cfSShuo Chen}
31354280cfSShuo Chen
32354280cfSShuo Cheninline uint16_t hostToNetwork16(uint16_t host16)
33354280cfSShuo Chen{
34354280cfSShuo Chen  return htons(host16);
35354280cfSShuo Chen}
36354280cfSShuo Chen
37354280cfSShuo Cheninline uint64_t networkToHost64(uint64_t net64)
38354280cfSShuo Chen{
39354280cfSShuo Chen  return be64toh(net64);
40354280cfSShuo Chen}
41354280cfSShuo Chen
42354280cfSShuo Cheninline uint32_t networkToHost32(uint32_t net32)
43354280cfSShuo Chen{
44354280cfSShuo Chen  return ntohl(net32);
45354280cfSShuo Chen}
46354280cfSShuo Chen
47354280cfSShuo Cheninline uint16_t networkToHost16(uint16_t net16)
48354280cfSShuo Chen{
49354280cfSShuo Chen  return ntohs(net16);
50354280cfSShuo Chen}
51354280cfSShuo Chen
52354280cfSShuo Chen///
53354280cfSShuo Chen/// Creates a non-blocking socket file descriptor,
54354280cfSShuo Chen/// abort if any error.
55354280cfSShuo Chenint createNonblockingOrDie();
56354280cfSShuo Chen
57354280cfSShuo Chenint  connect(int sockfd, const struct sockaddr_in& addr);
58354280cfSShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr);
59354280cfSShuo Chenvoid listenOrDie(int sockfd);
60354280cfSShuo Chenint  accept(int sockfd, struct sockaddr_in* addr);
61354280cfSShuo Chenvoid close(int sockfd);
62354280cfSShuo Chenvoid shutdownWrite(int sockfd);
63354280cfSShuo Chen
64354280cfSShuo Chenvoid toHostPort(char* buf, size_t size,
65354280cfSShuo Chen                const struct sockaddr_in& addr);
66354280cfSShuo Chenvoid fromHostPort(const char* ip, uint16_t port,
67354280cfSShuo Chen                  struct sockaddr_in* addr);
68354280cfSShuo Chen
69354280cfSShuo Chenstruct sockaddr_in getLocalAddr(int sockfd);
70354280cfSShuo Chenstruct sockaddr_in getPeerAddr(int sockfd);
71354280cfSShuo Chen
72354280cfSShuo Chenint getSocketError(int sockfd);
73354280cfSShuo Chenbool isSelfConnect(int sockfd);
74354280cfSShuo Chen
75354280cfSShuo Chen}
76354280cfSShuo Chen}
77354280cfSShuo Chen
78354280cfSShuo Chen#endif  // MUDUO_NET_SOCKETSOPS_H
79