summaryrefslogtreecommitdiff
path: root/libcody/netserver.cc
blob: 0499b5790b457e665478c12c09a0f0d63ebc6f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// CODYlib		-*- mode:c++ -*-
// Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
// License: Apache v2.0

// Cody
#include "internal.hh"
#if CODY_NETWORKING
// C
#include <cerrno>
#include <cstring>
// OS
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>

#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif

// Server-side networking helpers

namespace Cody {

int ListenSocket (char const **e, sockaddr const *addr, socklen_t len,
		  unsigned backlog)
{
  char const *errstr = nullptr;

  int fd = socket (addr->sa_family, SOCK_STREAM, 0);
  if (fd < 0)
    {
      errstr = "creating socket";

    fail:;
      int err = errno;
      if (e)
	*e = errstr;
      if (fd >= 0)
	close (fd);
      errno = err;
      return -1;
    }

  if (bind (fd, addr, len) < 0)
    {
      errstr = "binding socket";
      goto fail;
    }

  if (listen (fd, backlog ? backlog : 17) < 0)
    {
      errstr = "listening socket";
      goto fail;
    }

  return fd;
}

int ListenLocal (char const **e, char const *name, unsigned backlog)
{
  sockaddr_un addr;
  size_t len = strlen (name);

  if (len >= sizeof (addr.sun_path))
    {
      errno = ENAMETOOLONG;
      return -1;
    }

  memset (&addr, 0, offsetof (sockaddr_un, sun_path));
  addr.sun_family = AF_UNIX;
  memcpy (addr.sun_path, name, len + 1);

  return ListenSocket (e, (sockaddr *)&addr, sizeof (addr), backlog);
}

int ListenInet6 (char const **e, char const *name, int port, unsigned backlog)
{
  addrinfo *addrs = nullptr;
  int fd = -1;
  char const *errstr = nullptr;

  fd = socket (AF_INET6, SOCK_STREAM, 0);
  if (fd < 0)
    {
      errstr = "creating socket";

    fail:;
      int err = errno;
      if (e)
	*e = errstr;
      if (fd >= 0)
	close (fd);
      if (addrs)
	freeaddrinfo (addrs);
      errno = err;
      return -1;
    }

  addrinfo hints;
  hints.ai_flags = AI_NUMERICSERV;
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  hints.ai_addrlen = 0;
  hints.ai_addr = nullptr;
  hints.ai_canonname = nullptr;
  hints.ai_next = nullptr;

  /* getaddrinfo requires a port number, but is quite happy to accept
     invalid ones.  So don't rely on it.  */
  if (int err = getaddrinfo (name, "0", &hints, &addrs))
    {
      errstr = gai_strerror (err);
      // What's the best errno to set?
      errno = 0;
      goto fail;
    }

  sockaddr_in6 addr;
  memset (&addr, 0, sizeof (addr));
  addr.sin6_family = AF_INET6;

  for (struct addrinfo *next = addrs; next; next = next->ai_next)
    if (next->ai_family == AF_INET6
	&& next->ai_socktype == SOCK_STREAM)
      {
	sockaddr_in6 *in6 = (sockaddr_in6 *)next->ai_addr;
	in6->sin6_port = htons (port);
	if (ntohs (in6->sin6_port) != port)
	  errno = EINVAL;
	else if (!bind (fd, next->ai_addr, next->ai_addrlen))
	  goto listen;
      }

  errstr = "binding socket";
  goto fail;

 listen:;
  freeaddrinfo (addrs);
  addrs = nullptr;

  if (listen (fd, backlog ? backlog : 17) < 0)
    {
      errstr = "listening socket";
      goto fail;
    }

  return fd;
}

}
#endif