Wed Jan 8 2020 09:49:48

Asterisk developer's documentation


netsock.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Kevin P. Fleming <kpfleming@digium.com>
7  * Mark Spencer <markster@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19 
20 /*! \file
21  *
22  * \brief Network socket handling
23  *
24  * \author Kevin P. Fleming <kpfleming@digium.com>
25  * \author Mark Spencer <markster@digium.com>
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
35 
36 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
37 #include <net/if_dl.h>
38 #endif
39 
40 #if defined (SOLARIS)
41 #include <sys/sockio.h>
42 #elif defined(HAVE_GETIFADDRS)
43 #include <ifaddrs.h>
44 #endif
45 
46 #include "asterisk/netsock.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/astobj.h"
49 
50 struct ast_netsock {
52  struct sockaddr_in bindaddr;
53  int sockfd;
54  int *ioref;
55  struct io_context *ioc;
56  void *data;
57 };
58 
61  struct io_context *ioc;
62 };
63 
65 {
66  ast_io_remove(netsock->ioc, netsock->ioref);
67  close(netsock->sockfd);
68  ast_free(netsock);
69 }
70 
72 {
73  return ast_calloc(1, sizeof(struct ast_netsock_list));
74 }
75 
77 {
78  memset(list, 0, sizeof(*list));
80 
81  return 0;
82 }
83 
85 {
88  ast_free(list);
89 
90  return 0;
91 }
92 
94  struct sockaddr_in *sa)
95 {
96  struct ast_netsock *sock = NULL;
97 
98  ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
99  ASTOBJ_RDLOCK(iterator);
100  if (!inaddrcmp(&iterator->bindaddr, sa))
101  sock = iterator;
102  ASTOBJ_UNLOCK(iterator);
103  });
104 
105  return sock;
106 }
107 
108 struct ast_netsock *ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct sockaddr_in *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
109 {
110  int netsocket = -1;
111  int *ioref;
112 
113  struct ast_netsock *ns;
114  const int reuseFlag = 1;
115 
116  /* Make a UDP socket */
117  netsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
118 
119  if (netsocket < 0) {
120  ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
121  return NULL;
122  }
123  if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
124  ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
125  }
126  if (bind(netsocket,(struct sockaddr *)bindaddr, sizeof(struct sockaddr_in))) {
127  ast_log(LOG_ERROR, "Unable to bind to %s port %d: %s\n", ast_inet_ntoa(bindaddr->sin_addr), ntohs(bindaddr->sin_port), strerror(errno));
128  close(netsocket);
129  return NULL;
130  }
131 
132  ast_netsock_set_qos(netsocket, tos, cos, "IAX2");
133 
135 
136  if (!(ns = ast_calloc(1, sizeof(*ns)))) {
137  close(netsocket);
138  return NULL;
139  }
140 
141  /* Establish I/O callback for socket read */
142  if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
143  close(netsocket);
144  ast_free(ns);
145  return NULL;
146  }
147  ASTOBJ_INIT(ns);
148  ns->ioref = ioref;
149  ns->ioc = ioc;
150  ns->sockfd = netsocket;
151  ns->data = data;
152  memcpy(&ns->bindaddr, bindaddr, sizeof(ns->bindaddr));
153  ASTOBJ_CONTAINER_LINK(list, ns);
154 
155  return ns;
156 }
157 
158 int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
159 {
160  int res;
161 
162  if ((res = setsockopt(netsocket, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
163  ast_log(LOG_WARNING, "Unable to set %s TOS to %d, may be you have no root privileges\n", desc, tos);
164  else if (tos)
165  ast_verb(2, "Using %s TOS bits %d\n", desc, tos);
166 
167 #if defined(linux)
168  if (setsockopt(netsocket, SOL_SOCKET, SO_PRIORITY, &cos, sizeof(cos)))
169  ast_log(LOG_WARNING, "Unable to set %s CoS to %d\n", desc, cos);
170  else if (cos)
171  ast_verb(2, "Using %s CoS mark %d\n", desc, cos);
172 #endif
173 
174  return res;
175 }
176 
177 
178 struct ast_netsock *ast_netsock_bind(struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, int cos, ast_io_cb callback, void *data)
179 {
180  struct sockaddr_in sin;
181  char *tmp;
182  char *host;
183  char *port;
184  int portno;
185 
186  memset(&sin, 0, sizeof(sin));
187  sin.sin_family = AF_INET;
188  sin.sin_port = htons(defaultport);
189  tmp = ast_strdupa(bindinfo);
190 
191  host = strsep(&tmp, ":");
192  port = tmp;
193 
194  if (port && ((portno = atoi(port)) > 0))
195  sin.sin_port = htons(portno);
196 
197  inet_aton(host, &sin.sin_addr);
198 
199  return ast_netsock_bindaddr(list, ioc, &sin, tos, cos, callback, data);
200 }
201 
202 int ast_netsock_sockfd(const struct ast_netsock *ns)
203 {
204  return ns ? ns-> sockfd : -1;
205 }
206 
207 const struct sockaddr_in *ast_netsock_boundaddr(const struct ast_netsock *ns)
208 {
209  return &(ns->bindaddr);
210 }
211 
212 void *ast_netsock_data(const struct ast_netsock *ns)
213 {
214  return ns->data;
215 }
216 
218 {
220 }
221 
222 char *ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
223 {
224  int x;
225  char *os = s;
226  if (maxlen < 18) {
227  if (s && (maxlen > 0))
228  *s = '\0';
229  } else {
230  for (x = 0; x < 5; x++) {
231  sprintf(s, "%02x:", (unsigned)eid->eid[x]);
232  s += 3;
233  }
234  sprintf(s, "%02x", (unsigned)eid->eid[5]);
235  }
236  return os;
237 }
238 
239 void ast_set_default_eid(struct ast_eid *eid)
240 {
241 #if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_IFRU_IFRU_HWADDR)
242  int s, x = 0;
243  char eid_str[20];
244  struct ifreq ifr;
245  static const unsigned int MAXIF = 10;
246 
247  s = socket(AF_INET, SOCK_STREAM, 0);
248  if (s < 0)
249  return;
250  for (x = 0; x < MAXIF; x++) {
251  static const char *prefixes[] = { "eth", "em" };
252  unsigned int i;
253 
254  for (i = 0; i < ARRAY_LEN(prefixes); i++) {
255  memset(&ifr, 0, sizeof(ifr));
256  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", prefixes[i], x);
257  if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
258  break;
259  }
260  }
261 
262  if (i == ARRAY_LEN(prefixes)) {
263  /* Try pciX#[1..N] */
264  for (i = 0; i < MAXIF; i++) {
265  memset(&ifr, 0, sizeof(ifr));
266  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "pci%d#%u", x, i);
267  if (!ioctl(s, SIOCGIFHWADDR, &ifr)) {
268  break;
269  }
270  }
271  if (i == MAXIF) {
272  continue;
273  }
274  }
275 
276  memcpy(eid, ((unsigned char *)&ifr.ifr_hwaddr) + 2, sizeof(*eid));
277  ast_debug(1, "Seeding global EID '%s' from '%s' using 'siocgifhwaddr'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), ifr.ifr_name);
278  close(s);
279  return;
280  }
281  close(s);
282 #else
283 #if defined(ifa_broadaddr) && !defined(SOLARIS)
284  char eid_str[20];
285  struct ifaddrs *ifap;
286 
287  if (getifaddrs(&ifap) == 0) {
288  struct ifaddrs *p;
289  for (p = ifap; p; p = p->ifa_next) {
290  if ((p->ifa_addr->sa_family == AF_LINK) && !(p->ifa_flags & IFF_LOOPBACK) && (p->ifa_flags & IFF_RUNNING)) {
291  struct sockaddr_dl* sdp = (struct sockaddr_dl*) p->ifa_addr;
292  memcpy(&(eid->eid), sdp->sdl_data + sdp->sdl_nlen, 6);
293  ast_debug(1, "Seeding global EID '%s' from '%s' using 'getifaddrs'\n", ast_eid_to_str(eid_str, sizeof(eid_str), eid), p->ifa_name);
294  freeifaddrs(ifap);
295  return;
296  }
297  }
298  freeifaddrs(ifap);
299  }
300 #endif
301 #endif
302  ast_debug(1, "No ethernet interface found for seeding global EID. You will have to set it manually.\n");
303 }
304 
305 int ast_str_to_eid(struct ast_eid *eid, const char *s)
306 {
307  unsigned int eid_int[6];
308  int x;
309 
310  if (sscanf(s, "%2x:%2x:%2x:%2x:%2x:%2x", &eid_int[0], &eid_int[1], &eid_int[2],
311  &eid_int[3], &eid_int[4], &eid_int[5]) != 6)
312  return -1;
313 
314  for (x = 0; x < 6; x++)
315  eid->eid[x] = eid_int[x];
316 
317  return 0;
318 }
319 
320 int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
321 {
322  return memcmp(eid1, eid2, sizeof(*eid1));
323 }
void * data
Definition: netsock.c:56
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Definition: utils.c:2141
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * strsep(char **str, const char *delims)
struct io_context * ioc
Definition: netsock.c:55
static unsigned int tos
Definition: chan_h323.c:146
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Definition: netsock.c:222
int ast_netsock_sockfd(const struct ast_netsock *ns)
Definition: netsock.c:202
int sockfd
Definition: netsock.c:53
int ast_netsock_release(struct ast_netsock_list *list)
Definition: netsock.c:84
static struct ast_netsock_list * netsock
Definition: chan_iax2.c:315
#define LOG_WARNING
Definition: logger.h:144
static force_inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2)
Compares the source address and port of two sockaddr_in.
Definition: network.h:90
#define AST_IO_IN
Definition: io.h:33
int ast_netsock_set_qos(int netsocket, int tos, int cos, const char *desc)
Definition: netsock.c:158
int * ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
Adds an IO context.
Definition: io.c:157
struct ast_netsock_list * ast_netsock_list_alloc(void)
Definition: netsock.c:71
struct ast_netsock * ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct sockaddr_in *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
Definition: netsock.c:108
void ast_set_default_eid(struct ast_eid *eid)
Fill in an ast_eid with the default eid of this machine.
Definition: netsock.c:239
int * ioref
Definition: netsock.c:54
struct ast_netsock * ast_netsock_find(struct ast_netsock_list *list, struct sockaddr_in *sa)
Definition: netsock.c:93
An Entity ID is essentially a MAC address, brief and unique.
Definition: utils.h:808
#define ast_verb(level,...)
Definition: logger.h:243
struct ast_netsock * ast_netsock_bind(struct ast_netsock_list *list, struct io_context *ioc, const char *bindinfo, int defaultport, int tos, int cos, ast_io_cb callback, void *data)
Definition: netsock.c:178
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
Definition: netsock.c:320
Utility functions.
#define ASTOBJ_COMPONENTS(type)
Add ASTOBJ components to a struct (with locking support).
Definition: astobj.h:173
#define ASTOBJ_CONTAINER_INIT(container)
Initialize a container.
Definition: astobj.h:752
static int netsocket
Definition: pbx_dundi.c:178
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
void ast_netsock_unref(struct ast_netsock *ns)
Definition: netsock.c:217
Network socket handling.
Global IO variables are now in a struct in order to be made threadsafe.
Definition: io.c:66
struct io_context * ioc
Definition: netsock.c:61
int ast_str_to_eid(struct ast_eid *eid, const char *s)
Convert a string into an EID.
Definition: netsock.c:305
#define ASTOBJ_INIT(object)
Initialize an object.
Definition: astobj.h:264
struct sockaddr_in * ast_netsock_boundaddr(const struct ast_netsock *ns)
Definition: netsock.c:207
#define ASTOBJ_UNREF(object, destructor)
Decrement the reference count on an object.
Definition: astobj.h:218
static const char desc[]
Definition: cdr_radius.c:85
unsigned char eid[6]
Definition: utils.h:809
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define ASTOBJ_CONTAINER_DESTROY(container)
Destroy a container.
Definition: astobj.h:765
#define LOG_ERROR
Definition: logger.h:155
#define ASTOBJ_CONTAINER_DESTROYALL(container, destructor)
Empty a container.
Definition: astobj.h:453
A set of macros implementing objects and containers. Macros are used for maximum performance, to support multiple inheritance, and to be easily integrated into existing structures without additional malloc calls, etc.
#define ASTOBJ_CONTAINER_LINK(container, newobj)
Add an object to a container.
Definition: astobj.h:776
struct ast_sockaddr bindaddr
Definition: chan_sip.c:1146
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ASTOBJ_CONTAINER_TRAVERSE(container, continue, eval)
Iterate through the objects in a container.
Definition: astobj.h:376
struct sockaddr_in bindaddr
Definition: netsock.c:52
int errno
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Definition: utils.c:564
#define ASTOBJ_UNLOCK(object)
Unlock a locked object.
Definition: astobj.h:109
#define ast_free(a)
Definition: astmm.h:97
int ast_io_remove(struct io_context *ioc, int *id)
Removes an IO context.
Definition: io.c:240
int ast_netsock_init(struct ast_netsock_list *list)
Definition: netsock.c:76
#define ASTOBJ_RDLOCK(object)
Lock an ASTOBJ for reading.
Definition: astobj.h:100
int(* ast_io_cb)(int *id, int fd, short events, void *cbdata)
Definition: io.h:71
#define ast_calloc(a, b)
Definition: astmm.h:82
static unsigned int cos
Definition: chan_h323.c:147
ASTOBJ_CONTAINER_COMPONENTS(struct ast_netsock)
void * ast_netsock_data(const struct ast_netsock *ns)
Definition: netsock.c:212
int inet_aton(const char *cp, struct in_addr *pin)
static void ast_netsock_destroy(struct ast_netsock *netsock)
Definition: netsock.c:64
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180