Wed Jan 8 2020 09:50:15

Asterisk developer's documentation


netsock.h File Reference

Network socket handling. More...

#include "asterisk/network.h"
#include "asterisk/io.h"

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 sockaddr_in *bindaddr, int tos, int cos, ast_io_cb callback, void *data)
 
struct sockaddr_in * ast_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 sockaddr_in *sa)
 
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 netsocket, 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.

Definition in file netsock.h.

Function Documentation

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 178 of file netsock.c.

References ast_netsock_bindaddr(), ast_strdupa, inet_aton(), and strsep().

Referenced by peer_set_srcaddr(), and set_config().

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 }
char * strsep(char **str, const char *delims)
static unsigned int tos
Definition: chan_h323.c:146
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
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static unsigned int cos
Definition: chan_h323.c:147
int inet_aton(const char *cp, struct in_addr *pin)
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 at line 108 of file netsock.c.

References ast_calloc, ast_enable_packet_fragmentation(), ast_free, ast_inet_ntoa(), ast_io_add(), AST_IO_IN, ast_log(), ast_netsock_set_qos(), ASTOBJ_CONTAINER_LINK, ASTOBJ_INIT, ast_netsock::bindaddr, ast_netsock::data, errno, ast_netsock::ioc, ast_netsock::ioref, LOG_ERROR, LOG_WARNING, netsocket, and ast_netsock::sockfd.

Referenced by ast_netsock_bind().

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 }
void * data
Definition: netsock.c:56
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Definition: utils.c:2141
struct io_context * ioc
Definition: netsock.c:55
static unsigned int tos
Definition: chan_h323.c:146
int sockfd
Definition: netsock.c:53
#define LOG_WARNING
Definition: logger.h:144
#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
int * ioref
Definition: netsock.c:54
static int netsocket
Definition: pbx_dundi.c:178
#define ASTOBJ_INIT(object)
Initialize an object.
Definition: astobj.h:264
#define LOG_ERROR
Definition: logger.h:155
#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
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 ast_free(a)
Definition: astmm.h:97
#define ast_calloc(a, b)
Definition: astmm.h:82
static unsigned int cos
Definition: chan_h323.c:147
struct sockaddr_in* ast_netsock_boundaddr ( const struct ast_netsock ns)

Definition at line 207 of file netsock.c.

References ast_netsock::bindaddr.

208 {
209  return &(ns->bindaddr);
210 }
struct sockaddr_in bindaddr
Definition: netsock.c:52
void* ast_netsock_data ( const struct ast_netsock ns)

Definition at line 212 of file netsock.c.

References ast_netsock::data.

213 {
214  return ns->data;
215 }
void * data
Definition: netsock.c:56
struct ast_netsock* ast_netsock_find ( struct ast_netsock_list list,
struct sockaddr_in *  sa 
)

Definition at line 93 of file netsock.c.

References ASTOBJ_CONTAINER_TRAVERSE, ASTOBJ_RDLOCK, ASTOBJ_UNLOCK, and inaddrcmp().

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 (!inaddrcmp(&iterator->bindaddr, sa))
101  sock = iterator;
102  ASTOBJ_UNLOCK(iterator);
103  });
104 
105  return sock;
106 }
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 ASTOBJ_CONTAINER_TRAVERSE(container, continue, eval)
Iterate through the objects in a container.
Definition: astobj.h:376
#define ASTOBJ_UNLOCK(object)
Unlock a locked object.
Definition: astobj.h:109
#define ASTOBJ_RDLOCK(object)
Lock an ASTOBJ for reading.
Definition: astobj.h:100
int ast_netsock_init ( struct ast_netsock_list list)

Definition at line 76 of file netsock.c.

References ASTOBJ_CONTAINER_INIT.

Referenced by load_module(), and set_config().

77 {
78  memset(list, 0, sizeof(*list));
80 
81  return 0;
82 }
#define ASTOBJ_CONTAINER_INIT(container)
Initialize a container.
Definition: astobj.h:752
struct ast_netsock_list* ast_netsock_list_alloc ( void  )

Definition at line 71 of file netsock.c.

References ast_calloc.

Referenced by load_module(), and set_config().

72 {
73  return ast_calloc(1, sizeof(struct ast_netsock_list));
74 }
#define ast_calloc(a, b)
Definition: astmm.h:82
int ast_netsock_release ( struct ast_netsock_list list)

Definition at line 84 of file netsock.c.

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

Referenced by __unload_module(), and set_config().

85 {
88  ast_free(list);
89 
90  return 0;
91 }
#define ASTOBJ_CONTAINER_DESTROY(container)
Destroy a container.
Definition: astobj.h:765
#define ASTOBJ_CONTAINER_DESTROYALL(container, destructor)
Empty a container.
Definition: astobj.h:453
#define ast_free(a)
Definition: astmm.h:97
static void ast_netsock_destroy(struct ast_netsock *netsock)
Definition: netsock.c:64
int ast_netsock_set_qos ( int  netsocket,
int  tos,
int  cos,
const char *  desc 
)

Definition at line 158 of file netsock.c.

References ast_log(), ast_verb, and LOG_WARNING.

Referenced by ast_netsock_bindaddr(), ast_udptl_setqos(), config_load(), load_module(), and reload_config().

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 }
static unsigned int tos
Definition: chan_h323.c:146
#define LOG_WARNING
Definition: logger.h:144
#define ast_verb(level,...)
Definition: logger.h:243
static int netsocket
Definition: pbx_dundi.c:178
static const char desc[]
Definition: cdr_radius.c:85
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
static unsigned int cos
Definition: chan_h323.c:147
int ast_netsock_sockfd ( const struct ast_netsock ns)

Definition at line 202 of file netsock.c.

Referenced by peer_set_srcaddr(), and set_config().

203 {
204  return ns ? ns-> sockfd : -1;
205 }
int sockfd
Definition: netsock.c:53
void ast_netsock_unref ( struct ast_netsock ns)

Definition at line 217 of file netsock.c.

References ast_netsock_destroy(), and ASTOBJ_UNREF.

Referenced by peer_set_srcaddr(), and set_config().

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