SocketsOps.h revision a1bde736
1a1bde736SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
2a1bde736SShuo Chen// http://code.google.com/p/muduo/
3a1bde736SShuo Chen//
4a1bde736SShuo Chen// Use of this source code is governed by a BSD-style license
5a1bde736SShuo Chen// that can be found in the License file.
6a1bde736SShuo Chen
7a1bde736SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
8a1bde736SShuo Chen//
9a1bde736SShuo Chen// This is an internal header file, you should not include this.
10a1bde736SShuo Chen
11a1bde736SShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H
12a1bde736SShuo Chen#define MUDUO_NET_SOCKETSOPS_H
13a1bde736SShuo Chen
14a1bde736SShuo Chen#include <arpa/inet.h>
15a1bde736SShuo Chen#include <endian.h>
16a1bde736SShuo Chen
17a1bde736SShuo Chennamespace muduo
18a1bde736SShuo Chen{
19a1bde736SShuo Chennamespace sockets
20a1bde736SShuo Chen{
21a1bde736SShuo Chen
22a1bde736SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64)
23a1bde736SShuo Chen{
24a1bde736SShuo Chen  return htobe64(host64);
25a1bde736SShuo Chen}
26a1bde736SShuo Chen
27a1bde736SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32)
28a1bde736SShuo Chen{
29a1bde736SShuo Chen  return htonl(host32);
30a1bde736SShuo Chen}
31a1bde736SShuo Chen
32a1bde736SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16)
33a1bde736SShuo Chen{
34a1bde736SShuo Chen  return htons(host16);
35a1bde736SShuo Chen}
36a1bde736SShuo Chen
37a1bde736SShuo Cheninline uint64_t networkToHost64(uint64_t net64)
38a1bde736SShuo Chen{
39a1bde736SShuo Chen  return be64toh(net64);
40a1bde736SShuo Chen}
41a1bde736SShuo Chen
42a1bde736SShuo Cheninline uint32_t networkToHost32(uint32_t net32)
43a1bde736SShuo Chen{
44a1bde736SShuo Chen  return ntohl(net32);
45a1bde736SShuo Chen}
46a1bde736SShuo Chen
47a1bde736SShuo Cheninline uint16_t networkToHost16(uint16_t net16)
48a1bde736SShuo Chen{
49a1bde736SShuo Chen  return ntohs(net16);
50a1bde736SShuo Chen}
51a1bde736SShuo Chen
52a1bde736SShuo Chen///
53a1bde736SShuo Chen/// Creates a non-blocking socket file descriptor,
54a1bde736SShuo Chen/// abort if any error.
55a1bde736SShuo Chenint createNonblockingOrDie();
56a1bde736SShuo Chen
57a1bde736SShuo Chenint  connect(int sockfd, const struct sockaddr_in& addr);
58a1bde736SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr);
59a1bde736SShuo Chenvoid listenOrDie(int sockfd);
60a1bde736SShuo Chenint  accept(int sockfd, struct sockaddr_in* addr);
61a1bde736SShuo Chenvoid close(int sockfd);
62a1bde736SShuo Chenvoid shutdownWrite(int sockfd);
63a1bde736SShuo Chen
64a1bde736SShuo Chenvoid toHostPort(char* buf, size_t size,
65a1bde736SShuo Chen                const struct sockaddr_in& addr);
66a1bde736SShuo Chenvoid fromHostPort(const char* ip, uint16_t port,
67a1bde736SShuo Chen                  struct sockaddr_in* addr);
68a1bde736SShuo Chen
69a1bde736SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd);
70a1bde736SShuo Chenstruct sockaddr_in getPeerAddr(int sockfd);
71a1bde736SShuo Chen
72a1bde736SShuo Chenint getSocketError(int sockfd);
73a1bde736SShuo Chenbool isSelfConnect(int sockfd);
74a1bde736SShuo Chen
75a1bde736SShuo Chen}
76a1bde736SShuo Chen}
77a1bde736SShuo Chen
78a1bde736SShuo Chen#endif  // MUDUO_NET_SOCKETSOPS_H
79