Asterisk - The Open Source Telephony Project  18.5.0
Functions
netsock.h File Reference

Network socket handling. More...

#include "asterisk/network.h"
#include "asterisk/io.h"
#include "asterisk/netsock2.h"
Include dependency graph for netsock.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct ast_netsockast_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)
 
struct ast_netsockast_netsock_bindaddr (struct ast_netsock_list *list, struct io_context *ioc, struct ast_sockaddr *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
 
const struct ast_sockaddrast_netsock_boundaddr (const struct ast_netsock *ns)
 
void * ast_netsock_data (const struct ast_netsock *ns)
 
struct ast_netsockast_netsock_find (struct ast_netsock_list *list, struct ast_sockaddr *addr)
 
int ast_netsock_init (struct ast_netsock_list *list)
 
struct ast_netsock_listast_netsock_list_alloc (void)
 
int ast_netsock_release (struct ast_netsock_list *list)
 
int ast_netsock_set_qos (int sockfd, int tos, int cos, const char *desc)
 
int ast_netsock_sockfd (const struct ast_netsock *ns)
 
void ast_netsock_unref (struct ast_netsock *ns)
 

Detailed Description

Network socket handling.

Deprecated:
Use netsock2.h instead

Definition in file netsock.h.

Function Documentation

◆ ast_netsock_bind()

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 at line 167 of file netsock.c.

References ast_netsock_bindaddr(), ast_sockaddr_parse(), ast_sockaddr_port, ast_sockaddr_set_port, and NULL.

Referenced by peer_set_srcaddr().

168 {
169  struct ast_sockaddr addr;
170 
171  if (ast_sockaddr_parse(&addr, bindinfo, 0)) {
172 
173  if (!ast_sockaddr_port(&addr)) {
174  ast_sockaddr_set_port(&addr, defaultport);
175  }
176 
177  return ast_netsock_bindaddr(list, ioc, &addr, tos, cos, callback, data);
178  }
179 
180  return NULL;
181 }
unsigned int cos
Definition: chan_iax2.c:352
struct ast_netsock * ast_netsock_bindaddr(struct ast_netsock_list *list, struct io_context *ioc, struct ast_sockaddr *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
Definition: netsock.c:109
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
#define NULL
Definition: resample.c:96
Socket address structure.
Definition: netsock2.h:97
unsigned int tos
Definition: chan_iax2.c:351
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:521
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:537

◆ ast_netsock_bindaddr()

struct ast_netsock* ast_netsock_bindaddr ( struct ast_netsock_list list,
struct io_context ioc,
struct ast_sockaddr bindaddr,
int  tos,
int  cos,
ast_io_cb  callback,
void *  data 
)

Definition at line 109 of file netsock.c.

References AST_AF_INET, AST_AF_INET6, ast_bind(), ast_calloc, ast_enable_packet_fragmentation(), ast_free, ast_io_add(), AST_IO_IN, ast_log, ast_set_qos(), ast_sockaddr_copy(), ast_sockaddr_is_ipv6(), ast_sockaddr_stringify(), ASTOBJ_CONTAINER_LINK, ASTOBJ_INIT, ast_netsock::bindaddr, ast_netsock::data, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, NULL, and ast_netsock::sockfd.

Referenced by ast_netsock_bind(), and set_config().

110 {
111  int netsocket = -1;
112  int *ioref;
113 
114  struct ast_netsock *ns;
115  const int reuseFlag = 1;
116 
117  /* Make a UDP socket */
118  netsocket = socket(ast_sockaddr_is_ipv6(bindaddr) ? AST_AF_INET6 : AST_AF_INET, SOCK_DGRAM, IPPROTO_IP);
119 
120  if (netsocket < 0) {
121  ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
122  return NULL;
123  }
124  if (setsockopt(netsocket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseFlag, sizeof reuseFlag) < 0) {
125  ast_log(LOG_WARNING, "Error setting SO_REUSEADDR on sockfd '%d'\n", netsocket);
126  }
127  if (ast_bind(netsocket, bindaddr)) {
129  "Unable to bind to %s: %s\n",
130  ast_sockaddr_stringify(bindaddr),
131  strerror(errno));
132  close(netsocket);
133  return NULL;
134  }
135 
136  ast_set_qos(netsocket, tos, cos, "IAX2");
137 
139 
140  if (!(ns = ast_calloc(1, sizeof(*ns)))) {
141  close(netsocket);
142  return NULL;
143  }
144 
145  /* Establish I/O callback for socket read */
146  if (!(ioref = ast_io_add(ioc, netsocket, callback, AST_IO_IN, ns))) {
147  close(netsocket);
148  ast_free(ns);
149  return NULL;
150  }
151  ASTOBJ_INIT(ns);
152  ns->ioref = ioref;
153  ns->ioc = ioc;
154  ns->sockfd = netsocket;
155  ns->data = data;
156  ast_sockaddr_copy(&ns->bindaddr, bindaddr);
157  ASTOBJ_CONTAINER_LINK(list, ns);
158 
159  return ns;
160 }
void * data
Definition: netsock.c:57
unsigned int cos
Definition: chan_iax2.c:352
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Definition: main/utils.c:2221
struct io_context * ioc
Definition: netsock.c:56
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:171
int sockfd
Definition: netsock.c:54
#define LOG_WARNING
Definition: logger.h:274
#define AST_IO_IN
Definition: io.h:34
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:162
int * ioref
Definition: netsock.c:55
#define NULL
Definition: resample.c:96
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
struct ast_sockaddr bindaddr
Definition: netsock.c:53
unsigned int tos
Definition: chan_iax2.c:351
static int netsocket
Definition: pbx_dundi.c:186
#define ast_log
Definition: astobj2.c:42
#define ASTOBJ_INIT(object)
Initialize an object.
Definition: astobj.h:266
#define LOG_ERROR
Definition: logger.h:285
#define ASTOBJ_CONTAINER_LINK(container, newobj)
Add an object to a container.
Definition: astobj.h:778
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:621
int errno
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:260
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524

◆ ast_netsock_boundaddr()

const struct ast_sockaddr* ast_netsock_boundaddr ( const struct ast_netsock ns)

Definition at line 188 of file netsock.c.

References ast_netsock::bindaddr.

189 {
190  return &ns->bindaddr;
191 }
struct ast_sockaddr bindaddr
Definition: netsock.c:53

◆ ast_netsock_data()

void* ast_netsock_data ( const struct ast_netsock ns)

Definition at line 193 of file netsock.c.

References ast_netsock::data.

194 {
195  return ns->data;
196 }
void * data
Definition: netsock.c:57

◆ ast_netsock_find()

struct ast_netsock* ast_netsock_find ( struct ast_netsock_list list,
struct ast_sockaddr addr 
)

Definition at line 94 of file netsock.c.

References ast_sockaddr_cmp(), ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and NULL.

Referenced by peer_set_srcaddr().

95 {
96  struct ast_netsock *sock = NULL;
97 
98  ASTOBJ_CONTAINER_TRAVERSE(list, !sock, {
99  ASTOBJ_RDLOCK(iterator);
100  if (!ast_sockaddr_cmp(&iterator->bindaddr, addr)) {
101  sock = iterator;
102  }
103  ASTOBJ_UNLOCK(iterator);
104  });
105 
106  return sock;
107 }
#define NULL
Definition: resample.c:96
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
#define ASTOBJ_CONTAINER_TRAVERSE(container, continue, eval)
Iterate through the objects in a container.
Definition: astobj.h:378
#define ASTOBJ_UNLOCK(object)
Unlock a locked object.
Definition: astobj.h:111
#define ASTOBJ_RDLOCK(object)
Lock an ASTOBJ for reading.
Definition: astobj.h:102

◆ ast_netsock_init()

int ast_netsock_init ( struct ast_netsock_list list)

Definition at line 77 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

78 {
79  memset(list, 0, sizeof(*list));
81 
82  return 0;
83 }
#define ASTOBJ_CONTAINER_INIT(container)
Initialize a container.
Definition: astobj.h:754

◆ ast_netsock_list_alloc()

struct ast_netsock_list* ast_netsock_list_alloc ( void  )

Definition at line 72 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

73 {
74  return ast_calloc(1, sizeof(struct ast_netsock_list));
75 }
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204

◆ ast_netsock_release()

int ast_netsock_release ( struct ast_netsock_list list)

Definition at line 85 of file netsock.c.

References ast_free, ast_netsock_destroy(), ASTOBJ_CONTAINER_DESTROY, and ASTOBJ_CONTAINER_DESTROYALL.

Referenced by __unload_module(), and set_config().

86 {
89  ast_free(list);
90 
91  return 0;
92 }
#define ASTOBJ_CONTAINER_DESTROY(container)
Destroy a container.
Definition: astobj.h:767
#define ASTOBJ_CONTAINER_DESTROYALL(container, destructor)
Empty a container.
Definition: astobj.h:455
#define ast_free(a)
Definition: astmm.h:182
static void ast_netsock_destroy(struct ast_netsock *netsock)
Definition: netsock.c:65

◆ ast_netsock_set_qos()

int ast_netsock_set_qos ( int  sockfd,
int  tos,
int  cos,
const char *  desc 
)
Deprecated:
Use ast_seq_qos in netsock2.h which properly handles IPv4 and IPv6 sockets, instead.

Definition at line 162 of file netsock.c.

References ast_set_qos().

163 {
164  return ast_set_qos(sockfd, tos, cos, desc);
165 }
unsigned int cos
Definition: chan_iax2.c:352
int sockfd
Definition: netsock.c:54
static const char desc[]
Definition: cdr_mysql.c:73
unsigned int tos
Definition: chan_iax2.c:351
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:621

◆ ast_netsock_sockfd()

int ast_netsock_sockfd ( const struct ast_netsock ns)

Definition at line 183 of file netsock.c.

References ast_netsock::sockfd.

Referenced by peer_set_srcaddr(), and set_config().

184 {
185  return ns ? ns-> sockfd : -1;
186 }
int sockfd
Definition: netsock.c:54

◆ ast_netsock_unref()

void ast_netsock_unref ( struct ast_netsock ns)

Definition at line 198 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

199 {
201 }
#define ASTOBJ_UNREF(object, destructor)
Decrement the reference count on an object.
Definition: astobj.h:220
static void ast_netsock_destroy(struct ast_netsock *netsock)
Definition: netsock.c:65