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