[asterisk-commits] mmichelson: branch mmichelson/udptl-v6 r277967 - in /team/mmichelson/udptl-v6...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Jul 19 16:21:32 CDT 2010
Author: mmichelson
Date: Mon Jul 19 16:21:25 2010
New Revision: 277967
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=277967
Log:
Initial conversion of udptl files.
This code obviously does not compile since no other files have been updated
to use the new API.
I found a couple of oddities here:
1. ast_udptl_new() is not used anywhere. Since no bindaddr is provided, it would
not be possible to determine the address family to use for UDPTL anyway, so I'm
considering removing it.
2. The ast_udptl struct has a member called "far" which is not used, so I'm
considering removing it.
Modified:
team/mmichelson/udptl-v6/include/asterisk/udptl.h
team/mmichelson/udptl-v6/main/udptl.c
Modified: team/mmichelson/udptl-v6/include/asterisk/udptl.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/udptl-v6/include/asterisk/udptl.h?view=diff&rev=277967&r1=277966&r2=277967
==============================================================================
--- team/mmichelson/udptl-v6/include/asterisk/udptl.h (original)
+++ team/mmichelson/udptl-v6/include/asterisk/udptl.h Mon Jul 19 16:21:25 2010
@@ -58,7 +58,7 @@
struct ast_udptl *ast_udptl_new(struct sched_context *sched, struct io_context *io, int callbackmode);
-struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr in);
+struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct ast_sockaddr *in);
/*!
* \brief Associates a character string 'tag' with a UDPTL session.
@@ -75,11 +75,11 @@
*/
void __attribute__((format(printf, 2, 3))) ast_udptl_set_tag(struct ast_udptl *udptl, const char *format, ...);
-void ast_udptl_set_peer(struct ast_udptl *udptl, const struct sockaddr_in *them);
+void ast_udptl_set_peer(struct ast_udptl *udptl, const struct ast_sockaddr *them);
-void ast_udptl_get_peer(const struct ast_udptl *udptl, struct sockaddr_in *them);
+void ast_udptl_get_peer(const struct ast_udptl *udptl, struct ast_sockaddr *them);
-void ast_udptl_get_us(const struct ast_udptl *udptl, struct sockaddr_in *us);
+void ast_udptl_get_us(const struct ast_udptl *udptl, struct ast_sockaddr *us);
void ast_udptl_destroy(struct ast_udptl *udptl);
Modified: team/mmichelson/udptl-v6/main/udptl.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/udptl-v6/main/udptl.c?view=diff&rev=277967&r1=277966&r2=277967
==============================================================================
--- team/mmichelson/udptl-v6/main/udptl.c (original)
+++ team/mmichelson/udptl-v6/main/udptl.c Mon Jul 19 16:21:25 2010
@@ -121,8 +121,8 @@
unsigned int lasteventseqn;
int nat;
int flags;
- struct sockaddr_in us;
- struct sockaddr_in them;
+ struct ast_sockaddr us;
+ struct ast_sockaddr them;
int *ioid;
struct sched_context *sched;
struct io_context *io;
@@ -172,7 +172,7 @@
int verbose;
- struct sockaddr_in far;
+ struct ast_sockaddr far;
unsigned int tx_seq_no;
unsigned int rx_seq_no;
@@ -672,20 +672,16 @@
struct ast_frame *ast_udptl_read(struct ast_udptl *udptl)
{
int res;
- struct sockaddr_in sin;
- socklen_t len;
+ struct ast_sockaddr addr;
uint16_t seqno = 0;
uint16_t *udptlheader;
-
- len = sizeof(sin);
/* Cache where the header will go */
- res = recvfrom(udptl->fd,
+ res = ast_recvfrom(udptl->fd,
udptl->rawdata + AST_FRIENDLY_OFFSET,
sizeof(udptl->rawdata) - AST_FRIENDLY_OFFSET,
0,
- (struct sockaddr *) &sin,
- &len);
+ &addr);
udptlheader = (uint16_t *)(udptl->rawdata + AST_FRIENDLY_OFFSET);
if (res < 0) {
if (errno != EAGAIN)
@@ -696,22 +692,22 @@
}
/* Ignore if the other side hasn't been given an address yet. */
- if (!udptl->them.sin_addr.s_addr || !udptl->them.sin_port)
+ if (ast_sockaddr_is_null(&udptl->them)) {
return &ast_null_frame;
+ }
if (udptl->nat) {
/* Send to whoever sent to us */
- if ((udptl->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
- (udptl->them.sin_port != sin.sin_port)) {
- memcpy(&udptl->them, &sin, sizeof(udptl->them));
+ if (ast_sockaddr_cmp(&udptl->them, &addr)) {
+ ast_sockaddr_copy(&udptl->them, &addr);
ast_debug(1, "UDPTL NAT (%s): Using address %s:%d\n",
LOG_TAG(udptl), ast_inet_ntoa(udptl->them.sin_addr), ntohs(udptl->them.sin_port));
}
}
- if (udptl_debug_test_addr(&sin)) {
- ast_verb(1, "UDPTL (%s): packet from %s:%d (type %d, seq %d, len %d)\n",
- LOG_TAG(udptl), ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), 0, seqno, res);
+ if (udptl_debug_test_addr(&addr)) {
+ ast_verb(1, "UDPTL (%s): packet from %s (type %d, seq %d, len %d)\n",
+ LOG_TAG(udptl), ast_sockaddr_stringify(&addr), 0, seqno, res);
}
if (udptl_rx_packet(udptl, udptl->rawdata + AST_FRIENDLY_OFFSET, res) < 1)
return &ast_null_frame;
@@ -916,7 +912,7 @@
return udptl->far_max_ifp;
}
-struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct in_addr addr)
+struct ast_udptl *ast_udptl_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int callbackmode, struct ast_sockaddr *addr)
{
struct ast_udptl *udptl;
int x;
@@ -940,10 +936,8 @@
udptl->tx[i].buf_len = -1;
}
- udptl->them.sin_family = AF_INET;
- udptl->us.sin_family = AF_INET;
-
- if ((udptl->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ if ((udptl->fd = socket(ast_sockaddr_is_ipv6(addr) ?
+ AF_INET6 : AF_INET, SOCK_DGRAM, 0)) < 0) {
ast_free(udptl);
ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
return NULL;
@@ -961,10 +955,11 @@
}
startplace = x;
for (;;) {
- udptl->us.sin_port = htons(x);
- udptl->us.sin_addr = addr;
- if (bind(udptl->fd, (struct sockaddr *) &udptl->us, sizeof(udptl->us)) == 0)
+ ast_sockaddr_copy(&udptl->us, addr);
+ ast_sockaddr_set_port(&udptl, x);
+ if (ast_bind(udptl->fd, &udptl->us)) {
break;
+ }
if (errno != EADDRINUSE) {
ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
close(udptl->fd);
@@ -1021,29 +1016,24 @@
return ast_netsock_set_qos(udptl->fd, tos, cos, "UDPTL");
}
-void ast_udptl_set_peer(struct ast_udptl *udptl, const struct sockaddr_in *them)
-{
- udptl->them.sin_port = them->sin_port;
- udptl->them.sin_addr = them->sin_addr;
-}
-
-void ast_udptl_get_peer(const struct ast_udptl *udptl, struct sockaddr_in *them)
-{
- memset(them, 0, sizeof(*them));
- them->sin_family = AF_INET;
- them->sin_port = udptl->them.sin_port;
- them->sin_addr = udptl->them.sin_addr;
-}
-
-void ast_udptl_get_us(const struct ast_udptl *udptl, struct sockaddr_in *us)
-{
- memcpy(us, &udptl->us, sizeof(udptl->us));
+void ast_udptl_set_peer(struct ast_udptl *udptl, const struct ast_sockaddr *them)
+{
+ ast_sockaddr_copy(&udptl->them, them);
+}
+
+void ast_udptl_get_peer(const struct ast_udptl *udptl, struct ast_sockaddr *them)
+{
+ ast_sockaddr_copy(them, &udptl->them);
+}
+
+void ast_udptl_get_us(const struct ast_udptl *udptl, struct ast_sockaddr *us)
+{
+ ast_sockaddr_copy(us, &udptl->us);
}
void ast_udptl_stop(struct ast_udptl *udptl)
{
- memset(&udptl->them.sin_addr, 0, sizeof(udptl->them.sin_addr));
- memset(&udptl->them.sin_port, 0, sizeof(udptl->them.sin_port));
+ ast_sockaddr_setnull(&udptl->them);
}
void ast_udptl_destroy(struct ast_udptl *udptl)
@@ -1068,9 +1058,11 @@
memset(buf, 0, sizeof(buf));
- /* If we have no peer, return immediately */
- if (s->them.sin_addr.s_addr == INADDR_ANY)
+ /* If we have no peer, return immediately */
+ /* XXX Should this be ast_sockaddr_isnull()? */
+ if (ast_sockaddr_is_any(&s->them)) {
return 0;
+ }
/* If there is no data length, return immediately */
if (f->datalen == 0)
@@ -1097,13 +1089,13 @@
/* Cook up the UDPTL packet, with the relevant EC info. */
len = udptl_build_packet(s, buf, sizeof(buf), f->data.ptr, len);
- if ((signed int) len > 0 && s->them.sin_port && s->them.sin_addr.s_addr) {
- if ((res = sendto(s->fd, buf, len, 0, (struct sockaddr *) &s->them, sizeof(s->them))) < 0)
- ast_log(LOG_NOTICE, "(%s): UDPTL Transmission error to %s:%d: %s\n",
- LOG_TAG(s), ast_inet_ntoa(s->them.sin_addr), ntohs(s->them.sin_port), strerror(errno));
+ if ((signed int) len > 0 && !ast_sockaddr_isnull(&s->them)) {
+ if ((res = ast_sendto(s->fd, buf, len, 0, &s->them)) < 0)
+ ast_log(LOG_NOTICE, "(%s): UDPTL Transmission error to %s: %s\n",
+ LOG_TAG(s), ast_sockaddr_stringify(&s->them), strerror(errno));
if (udptl_debug_test_addr(&s->them))
- ast_verb(1, "UDPTL (%s): packet to %s:%d (type %d, seq %d, len %d)\n",
- LOG_TAG(s), ast_inet_ntoa(s->them.sin_addr), ntohs(s->them.sin_port), 0, seq, len);
+ ast_verb(1, "UDPTL (%s): packet to %s (type %d, seq %d, len %d)\n",
+ LOG_TAG(s), ast_sockaddr_stringify(&s->them), 0, seq, len);
}
return 0;
More information about the asterisk-commits
mailing list