Wed Jan 8 2020 09:50:15

Asterisk developer's documentation


netsock.c File Reference

Network socket handling. More...

#include "asterisk.h"
#include <ifaddrs.h>
#include "asterisk/netsock.h"
#include "asterisk/utils.h"
#include "asterisk/astobj.h"

Go to the source code of this file.

Data Structures

struct  ast_netsock
 
struct  ast_netsock_list
 

Functions

int ast_eid_cmp (const struct ast_eid *eid1, const struct ast_eid *eid2)
 Compare two EIDs. More...
 
char * ast_eid_to_str (char *s, int maxlen, struct ast_eid *eid)
 
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)
 
static void ast_netsock_destroy (struct ast_netsock *netsock)
 
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)
 
void ast_set_default_eid (struct ast_eid *eid)
 Fill in an ast_eid with the default eid of this machine. More...
 
int ast_str_to_eid (struct ast_eid *eid, const char *s)
 Convert a string into an EID. More...
 

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

int ast_eid_cmp ( const struct ast_eid eid1,
const struct ast_eid eid2 
)
char* ast_eid_to_str ( char *  s,
int  maxlen,
struct ast_eid eid 
)

/brief Convert an EID to a string

Since
1.6.1

Definition at line 222 of file netsock.c.

References ast_eid::eid.

Referenced by aji_publish_device_state(), aji_publish_mwi(), append_transaction(), ast_set_default_eid(), ast_str_retrieve_variable(), build_peer(), build_transactions(), cache_lookup(), cache_lookup_internal(), check_key(), complete_peer_helper(), destroy_trans(), do_autokill(), do_register(), do_register_expire(), dump_answer(), dump_eid(), dump_raw_ie(), dundi_answer_entity(), dundi_lookup_internal(), dundi_lookup_local(), dundi_lookup_thread(), dundi_precache_thread(), dundi_prop_precache(), dundi_query_thread(), dundi_send(), dundi_show_entityid(), dundi_show_peer(), dundi_show_peers(), dundi_show_requests(), handle_command_response(), handle_show_settings(), populate_addr(), register_request(), and update_key().

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 }
unsigned char eid[6]
Definition: utils.h:809
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
static void ast_netsock_destroy ( struct ast_netsock netsock)
static

Definition at line 64 of file netsock.c.

References ast_free, ast_io_remove(), ast_netsock::ioc, ast_netsock::ioref, and ast_netsock::sockfd.

Referenced by ast_netsock_release(), and ast_netsock_unref().

65 {
66  ast_io_remove(netsock->ioc, netsock->ioref);
67  close(netsock->sockfd);
68  ast_free(netsock);
69 }
struct io_context * ioc
Definition: netsock.c:55
int sockfd
Definition: netsock.c:53
int * ioref
Definition: netsock.c:54
#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
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
void ast_set_default_eid ( struct ast_eid eid)

Fill in an ast_eid with the default eid of this machine.

Since
1.6.1

Definition at line 239 of file netsock.c.

References ARRAY_LEN, ast_debug, ast_eid_to_str(), and ast_eid::eid.

Referenced by ast_readconfig().

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 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Definition: netsock.c:222
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
unsigned char eid[6]
Definition: utils.h:809
int ast_str_to_eid ( struct ast_eid eid,
const char *  s 
)

Convert a string into an EID.

This function expects an EID in the format: 00:11:22:33:44:55

Returns
0 success, non-zero failure
Since
1.6.1

Definition at line 305 of file netsock.c.

References ast_eid::eid.

Referenced by aji_handle_pubsub_event(), ast_readconfig(), build_peer(), dundi_do_query(), and set_config().

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 }
unsigned char eid[6]
Definition: utils.h:809