Asterisk - The Open Source Telephony Project GIT-master-0bf3178
Data Structures | Functions
netsock.c File Reference

Network socket handling. More...

#include "asterisk.h"
#include <ifaddrs.h>
#include "include/netsock.h"
#include "asterisk/netsock2.h"
#include "asterisk/utils.h"
#include "include/astobj.h"
Include dependency graph for netsock.c:

Go to the source code of this file.

Data Structures

struct  ast_netsock
 
struct  ast_netsock_list
 

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)
 
static void ast_netsock_destroy (struct ast_netsock *netsock)
 
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.

Author
Kevin P. Fleming kpfle.nosp@m.ming.nosp@m.@digi.nosp@m.um.c.nosp@m.om
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file netsock.c.

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.

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 tos
Definition: chan_iax2.c:355
unsigned int cos
Definition: chan_iax2.c:356
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
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 ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:532
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
#define NULL
Definition: resample.c:96
Socket address structure.
Definition: netsock2.h:97

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

Referenced by peer_set_srcaddr().

◆ 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.

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 }
129 "Unable to bind to %s: %s\n",
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;
157 ASTOBJ_CONTAINER_LINK(list, ns);
158
159 return ns;
160}
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ASTOBJ_CONTAINER_LINK(container, newobj)
Add an object to a container.
Definition: astobj.h:778
#define ASTOBJ_INIT(object)
Initialize an object.
Definition: astobj.h:266
struct ast_sockaddr bindaddr
Definition: chan_ooh323.c:353
#define LOG_ERROR
#define LOG_WARNING
#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 errno
@ AST_AF_INET6
Definition: netsock2.h:56
@ AST_AF_INET
Definition: netsock2.h:55
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
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:167
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:621
static int netsocket
Definition: pbx_dundi.c:186
int * ioref
Definition: netsock.c:55
void * data
Definition: netsock.c:57
struct ast_sockaddr bindaddr
Definition: netsock.c:53
struct io_context * ioc
Definition: netsock.c:56
int sockfd
Definition: netsock.c:54
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Definition: utils.c:2469

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, bindaddr, ast_netsock::bindaddr, cos, ast_netsock::data, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, NULL, ast_netsock::sockfd, and tos.

Referenced by ast_netsock_bind(), and set_config().

◆ ast_netsock_boundaddr()

const struct ast_sockaddr * ast_netsock_boundaddr ( const struct ast_netsock ns)

Definition at line 188 of file netsock.c.

189{
190 return &ns->bindaddr;
191}

References ast_netsock::bindaddr.

◆ ast_netsock_data()

void * ast_netsock_data ( const struct ast_netsock ns)

Definition at line 193 of file netsock.c.

194{
195 return ns->data;
196}

References ast_netsock::data.

◆ ast_netsock_destroy()

static void ast_netsock_destroy ( struct ast_netsock netsock)
static

Definition at line 65 of file netsock.c.

66{
68 close(netsock->sockfd);
70}
static struct ast_netsock_list * netsock
Definition: chan_iax2.c:366
int ast_io_remove(struct io_context *ioc, int *id)
Removes an IO context.
Definition: io.c:245
struct io_context * ioc
Definition: netsock.c:62

References ast_free, ast_io_remove(), ast_netsock_list::ioc, and netsock.

Referenced by ast_netsock_release(), and ast_netsock_unref().

◆ 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.

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 ASTOBJ_CONTAINER_TRAVERSE(container, continue, eval)
Iterate through the objects in a container.
Definition: astobj.h:378
#define ASTOBJ_RDLOCK(object)
Lock an ASTOBJ for reading.
Definition: astobj.h:102
#define ASTOBJ_UNLOCK(object)
Unlock a locked object.
Definition: astobj.h:111
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388

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

Referenced by peer_set_srcaddr().

◆ ast_netsock_init()

int ast_netsock_init ( struct ast_netsock_list list)

Definition at line 77 of file netsock.c.

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

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

◆ ast_netsock_list_alloc()

struct ast_netsock_list * ast_netsock_list_alloc ( void  )

Definition at line 72 of file netsock.c.

73{
74 return ast_calloc(1, sizeof(struct ast_netsock_list));
75}

References ast_calloc.

Referenced by load_module(), and set_config().

◆ ast_netsock_release()

int ast_netsock_release ( struct ast_netsock_list list)

Definition at line 85 of file netsock.c.

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
static void ast_netsock_destroy(struct ast_netsock *netsock)
Definition: netsock.c:65

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

Referenced by __unload_module(), and set_config().

◆ 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.

163{
164 return ast_set_qos(sockfd, tos, cos, desc);
165}
static const char desc[]
Definition: cdr_radius.c:84

References ast_set_qos(), cos, desc, ast_netsock::sockfd, and tos.

◆ ast_netsock_sockfd()

int ast_netsock_sockfd ( const struct ast_netsock ns)

Definition at line 183 of file netsock.c.

184{
185 return ns ? ns-> sockfd : -1;
186}

Referenced by peer_set_srcaddr(), and set_config().

◆ ast_netsock_unref()

void ast_netsock_unref ( struct ast_netsock ns)

Definition at line 198 of file netsock.c.

199{
201}
#define ASTOBJ_UNREF(object, destructor)
Decrement the reference count on an object.
Definition: astobj.h:220

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().