Browse Source

refactor udp module

Signed-off-by: surenyi <surenyi@gmail.com>
master
surenyi 9 years ago
parent
commit
6deb6ac1e2
  1. 13
      char_conv.c
  2. 23
      class/udp.c
  3. 26
      codec.c
  4. 33
      lan.c
  5. 4
      makefile
  6. 61
      udp.c
  7. 27
      udp.h
  8. 5
      udp_readable.c
  9. 1
      udp_readable.h
  10. 15
      wan.c

13
char_conv.c

@ -289,7 +289,7 @@ static void __is_readable(udp_readable_t *ur)
return;
}
if ((len = ur->udp->recv(ur->udp, &tbabuf->bj, sizeof(tbabuf->bj))) <= 0) {
if ((len = udp_recv(ur->udp, &tbabuf->bj, sizeof(tbabuf->bj))) <= 0) {
log_printf("recv baojin error!\n");
free(tbabuf);
return;
@ -370,7 +370,7 @@ static void __on_alarm(struct wan_alarm_handler *self, struct WanAlarmType *wat)
buf[idx++] = (cs >> 16) & 0xff;
buf[idx++] = (cs >> 24) & 0xff;
_udp->send(_udp, buf, idx);
udp_send(_udp, buf, idx);
memcpy(&cv->wat, wat, sizeof *wat);
}
@ -385,14 +385,14 @@ char_conv_t *char_conv_new(int to_port, int from_port)
return NULL;
}
if (u->bind(u, LOIFADDR, from_port) < 0) {
if (udp_bind(u, LOIFADDR, from_port) < 0) {
log_printf("'%s[%d]: can't bind [%s:%d] socket (%d: %s)\n", __FILE__, __LINE__, LOIFADDR, from_port, errno, strerror(errno));
u->close(u);
udp_close(u);
return NULL;
}
if (u->connect(u, LOIFADDR, to_port) < 0) {
if (udp_connect(u, LOIFADDR, to_port) < 0) {
log_printf("'%s[%d]: can't connect [%s:%d] socket\n", __FILE__, __LINE__, LOIFADDR, to_port);
u->close(u);
udp_close(u);
return NULL;
}
@ -424,4 +424,3 @@ void char_conv_free(char_conv_t *cv)
iconv_close(cv->cd);
udp_readable_close(&cv->base);
}

23
class/udp.c

@ -23,10 +23,10 @@ static void __is_readable(udp_readable_t *ur)
log_printf("lua udp no callback: %d\n", udp->uread);
return;
}
if ((len = ur->udp->recv(ur->udp, buf, sizeof buf)) > 0) {
if ((len = udp_recv(ur->udp, buf, sizeof buf)) > 0) {
lua_rawgeti(udp->uls, LUA_REGISTRYINDEX, udp->uread);
lua_pushlstring(udp->uls, buf,len);
lua_pcall(udp->uls, 1, 0, 0);
lua_pcall(udp->uls, 1, 0, 0);
}
}
@ -118,7 +118,7 @@ int sock_udp_connect(lua_State *L)
p = luaL_checkinteger(L, 3);
u = udp->base.udp;
p = u->connect(u, remote, p);
p = udp_connect(u, remote, p);
lua_pushinteger(L,p);
return 1;
}
@ -129,11 +129,11 @@ int sock_udp_recv(lua_State *L)
char buf[1056] = {0};
int n;
udp_t *u;
udp = luaL_checkudata(L, 1, UDP_MT);
u = udp->base.udp;
n = u->recv(u, buf, sizeof buf);
n = udp_recv(u, buf, sizeof buf);
if (n < 0)
n = 0;
lua_pushlstring(L, buf, n);
@ -152,7 +152,7 @@ int sock_udp_send(lua_State *L)
buf = lua_tolstring(L, 2, &len);
u = udp->base.udp;
if (len > 0)
n = u->send(u, (void *)buf, len);
n = udp_send(u, (void *)buf, len);
lua_pushinteger(L, n);
return 1;
}
@ -167,8 +167,8 @@ static int sock_udp_bind(lua_State *L)
udp = luaL_checkudata(L, 1, UDP_MT);
addr = lua_tostring(L, 2);
p = luaL_checkinteger(L, 3);
u = udp->base.udp;
p = u->bind(u, addr, p);
u = udp->base.udp;
p = udp_bind(u, addr, p);
lua_pushinteger(L, p);
return 1;
@ -184,8 +184,8 @@ static int sock_udp_close(lua_State *L)
if (udp->uread != LUA_REFNIL) {
luaL_unref(L, LUA_REGISTRYINDEX, udp->uread);
udp_readable_end(&udp->base);
}
u->close(u);
}
udp_close(u);
return 0;
}
@ -225,5 +225,4 @@ int init_udp_class(lua_State *L)
};
return lua_add_class(L, "Udp", udp_class, UDP_MT, mt_udp_class);
}
}

26
codec.c

@ -156,7 +156,7 @@ static void up_tout_handler(struct timer *t)
sizeof cc->_encBuf - 2);
cc->_encBuf.id5 = 0x14;
u->send(u, &cc->_encBuf, sizeof cc->_encBuf);
udp_send(u, &cc->_encBuf, sizeof cc->_encBuf);
} else {
log_printf("BUG>> Upstream tout scheduled for NULL\n");
}
@ -235,7 +235,7 @@ static void __on_up_audio( struct lan_audio_handler *lh,
cc->_encBuf.checkNum = getcs((uint8_t *)&cc->_encBuf, \
sizeof cc->_encBuf - 2);
cc->_encBuf.id5 = 0x14;
u->send(u, &cc->_encBuf, sizeof cc->_encBuf);
udp_send(u, &cc->_encBuf, sizeof cc->_encBuf);
/* after */
upbuf->cbuf.len = 0;
@ -286,7 +286,7 @@ static void __on_down_audio(struct wan_audio_handler *wh, struct WanAudioType *w
dump_bytes((uint8_t *)&cc->_decBuf, n);
}
#else
u->send(u, (uint8_t *)&cc->_decBuf, sizeof cc->_decBuf);
udp_send(u, (uint8_t *)&cc->_decBuf, sizeof cc->_decBuf);
#endif
}
@ -297,14 +297,14 @@ static udp_t *__make_link(const char *bridge_ip, int bridge_port, const char *lo
log_printf("'%s[%d]: can't create socket\n", __FILE__, __LINE__);
return NULL;
}
if (_u->bind(_u, local_ip, local_port) < 0) {
if (udp_bind(_u, local_ip, local_port) < 0) {
log_printf("'%s[%d]: can't bind [%s:%d] socket (%d: %s)\n", __FILE__, __LINE__, local_ip, local_port, errno, strerror(errno));
_u->close(_u);
udp_close(_u);
return NULL;
}
if (_u->connect(_u, bridge_ip, bridge_port) < 0) {
if (udp_connect(_u, bridge_ip, bridge_port) < 0) {
log_printf("'%s[%d]: can't connect [%s:%d] socket\n", __FILE__, __LINE__, local_ip, local_port);
_u->close(_u);
udp_close(_u);
return NULL;
}
return _u;
@ -320,7 +320,7 @@ static void __up_readable(udp_readable_t *ur)
struct codec_complete *ccp = cc->_up_complete;
/* send to uplink */
if ((n = u->recv(u, buf, sizeof buf)) > 0) {
if ((n = udp_recv(u, buf, sizeof buf)) > 0) {
ccp->on_complete(ccp, NULL, buf, n);
}
}
@ -424,7 +424,7 @@ static void __down_readable(udp_readable_t *ur)
uint8_t lanid;
/* send to uplink */
if ((n = u->recv(u, buf, sizeof(buf))) <= 0)
if ((n = udp_recv(u, buf, sizeof(buf))) <= 0)
return;
if (n != sizeof(struct AudioEncType)) {
@ -485,7 +485,7 @@ void codec_self_test(codec_t *cc, udp_t *u)
};
(void)cc;
buf[8] = getcs(buf, 8);
u->send(u, buf, sizeof buf);
udp_send(u, buf, sizeof buf);
};
codec_t *codec_new(const char *bridge_ip, int bridge_port, const char *local_ip, int down_port, int up_port)
@ -499,7 +499,7 @@ codec_t *codec_new(const char *bridge_ip, int bridge_port, const char *local_ip,
}
if (( u = __make_link( bridge_ip, bridge_port, local_ip, up_port)) == NULL) {
d->close(d);
udp_close(d);
return NULL;
}
cc = calloc(1, sizeof *cc);
@ -562,6 +562,4 @@ void codec_free(codec_t *cc)
uw = cc->_uplink;
udp_readable_close(&uw->base);
free(cc);
}
}

33
lan.c

@ -1,4 +1,5 @@
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
@ -62,7 +63,7 @@ static void __mcast_readable(udp_readable_t *ur)
udp_t *u = ur->udp;
/* 16 bytes header + 160 bytes playload */
if ((len = u->recv(u, buf, sizeof buf)) != 176)
if ((len = udp_recv(u, buf, sizeof buf)) != 176)
return;
//log_printf("lan recv audio packet %d bytes\n", len);
if (buf[0] == MSG_TYPE_AUDIO)
@ -130,7 +131,7 @@ static void __on_downlink(struct codec_complete *cc, void *msgType, uint8_t *buf
len = sizeof msg - sizeof *ma;
ma->payloadLen = htonl(len);
memcpy(ma->payload, buf, len);
u->send(u, msg, sizeof msg);
udp_send(u, msg, sizeof msg);
}
static void __do_cmd(lan_t *ln, udp_t *u, uint8_t *buf, int len)
@ -154,7 +155,7 @@ static void __do_cmd(lan_t *ln, udp_t *u, uint8_t *buf, int len)
#ifdef DEBUG_LAN
dump_bytes((uint8_t *)&hdr, 12);
#endif
u->send(u, &hdr, sizeof hdr);
udp_send(u, &hdr, sizeof hdr);
len -= sizeof hdr;
buf += sizeof hdr;
/* 6 x 4 bytes:
@ -195,7 +196,7 @@ static void __is_readable(udp_readable_t *ur)
#ifdef DEBUG_LAN
log_printf("lan reading\n");
#endif
if ((len = _u->recv(_u, buf, sizeof buf)) < LAN_PKT_MIN_LEN) {
if ((len = udp_recv(_u, buf, sizeof buf)) < LAN_PKT_MIN_LEN) {
return;
}
#ifdef DEBUG_LAN
@ -253,7 +254,7 @@ static int __on_alarm_data(struct alarm_data_handler *self, struct WanAlarmType
buf[n++] = 160;
memcpy(buf+16, bufin, len);
return u->send(u, buf, sizeof(buf));
return udp_send(u, buf, sizeof(buf));
}
static void __on_alarming(struct alarm_data_handler *self, struct WanAlarmType *wat, int on)
@ -280,7 +281,7 @@ static void __on_alarming(struct alarm_data_handler *self, struct WanAlarmType *
buf[n++] = (sn >> 16) & 0xff;
buf[n++] = (sn >> 8) & 0xff;
buf[n++] = sn & 0xff;
u->send(u, buf, n);
udp_send(u, buf, n);
}
int lan_ptt_response(lan_t *ln, int resp_type, struct MsgCmd *hdr, int status)
@ -303,7 +304,7 @@ int lan_ptt_response(lan_t *ln, int resp_type, struct MsgCmd *hdr, int status)
pkt.msgCmd = htons(MSG_RES_PTTOFF);
break;
}
u->send(u, &pkt, sizeof pkt);
udp_send(u, &pkt, sizeof pkt);
return 0;
}
@ -318,20 +319,21 @@ lan_t *lan_new(const char *dest_addr, int port, const char *local_if)
return NULL;
}
log_printf("local: %s, %s, %d\n", local_if, dest_addr, port);
if (u->bind(u, local_if, port) < 0) {
log_printf("'%s[%d]: can't bind Lan socket\n", __func__, __LINE__);
u->close(u);
if (udp_bind(u, local_if, port) < 0) {
log_printf("'%s[%d]: bind lan socket(%s:%d), errno %d: %s.\n", __func__, __LINE__, local_if, port,
errno, strerror(errno));
udp_close(u);
return NULL;
}
if (u->connect(u, dest_addr, port) < 0) {
if (udp_connect(u, dest_addr, port) < 0) {
log_printf("'%s[%d]: can't connect Lan socket\n", __func__, __LINE__);
u->close(u);
udp_close(u);
return NULL;
}
ln = (lan_t *)udp_readable_open(sizeof *ln, u);
if (ln == NULL) {
u->close(u);
udp_close(u);
return NULL;
}
ln->base.is_readable = __is_readable;
@ -352,7 +354,6 @@ void lan_free(lan_t *ln)
#ifdef LAN_RECORD
udp_readable_end(&ln->audioSock.base);
#endif
u->close(u);
udp_close(u);
udp_readable_close(&ln->base);
}
}

4
makefile

@ -5,8 +5,8 @@ CC := cc
INCLUDE := $(TARGET_ROOT)/include
LIBDIR := $(TARGET_ROOT)/lib
DEBUG := -g -ggdb -Werror
#DEBUG := -O2 -s
#DEBUG := -g -ggdb -Werror
DEBUG := -O2 -s
CFLAGS += -std=gnu99 -D_GNU_SOURCE -fno-strict-aliasing \
-I$(PWD) -I$(PWD)/class -I$(INCLUDE) -I$(INCLUDE)/luajit-2.0 -Wall -Wextra \
$(DEBUG)

61
udp.c

@ -81,33 +81,37 @@ udp_t *udp_open()
{
int s;
udp_t *u;
static struct udpops uops = {
.bind = __bind,
.connect = __connect,
.send = __send,
.recv = __recv,
.close =__close
};
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return NULL;
set_sock_attributes(s);
u = calloc(1, sizeof *u);
u->uops = &uops;
u->sock = s;
u->bind = __bind;
u->connect = __connect;
u->send = __send;
u->recv = __recv;
u->close = __close;
return u;
}
static struct udp_multicast * __mcast_new(const char *grp_addr, int port, int s)
{
struct udp_multicast *u;
static struct udpops uops = {
.bind = __bind,
.connect = __connect,
.send = __send2,
.recv = __recv,
.close = __close
};
u = calloc(1, sizeof *u);
u->base.sock = s;
u->base.bind = __bind;
u->base.connect = __connect;
u->base.send = __send2;
u->base.recv = __recv;
u->base.close = __close;
u->base.uops = &uops;
u->base.sock = s;
u->dest_addr.sin_family = AF_INET;
u->dest_addr.sin_port = htons(port);
u->dest_addr.sin_addr.s_addr = inet_addr(grp_addr);
@ -120,6 +124,7 @@ udp_t * udp_open_mcast(const char *grp_addr, int port, const char *local)
struct ip_mreq group;
struct in_addr ethx;
int sock;
udp_t *u;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
@ -137,7 +142,7 @@ udp_t * udp_open_mcast(const char *grp_addr, int port, const char *local)
log_printf("%s[%d]: bind %d (%s)\n", __func__, __LINE__, errno, strerror(errno));
close(sock);
return NULL;
}
}
ethx.s_addr = inet_addr(local);
@ -153,15 +158,12 @@ udp_t * udp_open_mcast(const char *grp_addr, int port, const char *local)
log_printf("%s[%d]: setsockopt %d (%s)\n", __func__, __LINE__, errno, strerror(errno));
close(sock);
return NULL;
}
/* don't lookback */
do {
uint8_t loop = 0;
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof loop);
} while (0);
}
return (udp_t *) __mcast_new(grp_addr, port, sock);
u = (udp_t *) __mcast_new(grp_addr, port, sock);
/* don't loopback */
udp_mcast_set_loop(u, 0);
return u;
}
int udp_mcast_set_loop(udp_t *u, int on)
@ -175,12 +177,13 @@ udp_t *udp_open_mcast_write_only(const char *group, int port, const char *local)
struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
udp_t *u;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0) {
log_printf("opening datagram socket error");
return NULL;
}
}
set_sock_attributes(sd);
@ -196,8 +199,9 @@ udp_t *udp_open_mcast_write_only(const char *group, int port, const char *local)
close(sd);
return NULL;
}
return (udp_t *)__mcast_new(group, port, sd);
u = (udp_t *)__mcast_new(group, port, sd);
udp_mcast_set_loop(u, 0);
return u;
}
udp_t *udp_open_mcast_read_only(const char *grp_addr, int port, const char *local)
@ -210,7 +214,7 @@ udp_t *udp_open_mcast_read_only(const char *grp_addr, int port, const char *loca
if(sd < 0){
log_printf("%s:%d opening datagram socket error", __FILE__, __LINE__);
return NULL;
}
}
set_sock_attributes(sd);
@ -222,7 +226,7 @@ udp_t *udp_open_mcast_read_only(const char *grp_addr, int port, const char *loca
log_printf("binding datagram socket error");
close(sd);
return NULL;
}
}
group.imr_multiaddr.s_addr = inet_addr(grp_addr);
group.imr_interface.s_addr = inet_addr(local);
if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group,
@ -265,5 +269,4 @@ int getip(const char *ethx, char *addr, int len)
}
close(fd);
return ret;
}
}

27
udp.h

@ -1,8 +1,7 @@
#ifndef __UDP_NEWC_H__
#define __UDP_NEWC_H__
typedef struct udp udp_t;
struct udp {
int sock;
struct udpops {
int (*bind)(udp_t *, const char *local_ip, int local_port);
int (*connect)(udp_t *, const char *remote_ip, int remote_port);
int (*send)(udp_t *, void *buf, int len);
@ -10,6 +9,11 @@ struct udp {
int (*close)(udp_t *);
};
struct udp {
struct udpops *uops;
int sock;
};
udp_t *udp_open(void);
udp_t *udp_open_mcast(const char *group, int port, const char *local_if);
udp_t *udp_open_mcast_write_only(const char *group, int port, const char *local);
@ -17,5 +21,22 @@ udp_t *udp_open_mcast_read_only(const char *grp_addr, int port, const char *loca
int udp_mcast_set_loop(udp_t *u, int on);
int getip(const char *ethx, char *addr, int len);
#endif
static inline int udp_bind(udp_t *u, const char *local_ip, int local_port) {
return u->uops->bind(u, local_ip, local_port);
}
static inline int udp_connect(udp_t *u, const char *remote_ip, int remote_port) {
return u->uops->connect(u, remote_ip, remote_port);
}
static inline int udp_send(udp_t *u, void *buf, int len) {
return u->uops->send(u, buf, len);
}
static inline int udp_recv(udp_t *u, void *buf, int len) {
return u->uops->recv(u, buf, len);
}
static inline int udp_close(udp_t *u) {
return u->uops->close(u);
}
#endif

5
udp_readable.c

@ -37,7 +37,6 @@ udp_readable_t *udp_readable_open(size_t alloc_size, udp_t *u)
void udp_readable_close(udp_readable_t *ur)
{
udp_readable_end(ur);
ur->udp->close(ur->udp);
udp_close(ur->udp);
free(ur);
}
}

1
udp_readable.h

@ -16,4 +16,3 @@ void udp_readable_end(udp_readable_t *ur);
udp_readable_t *udp_readable_open(size_t alloc_size, udp_t *u);
void udp_readable_close(udp_readable_t *);
#endif

15
wan.c

@ -50,7 +50,7 @@ static void __is_readable(udp_readable_t *ur)
wan_t *wn = (wan_t *)ur;
udp_t *u = ur->udp;
if ((len = u->recv(u, data, sizeof data)) <= 0)
if ((len = udp_recv(u, data, sizeof data)) <= 0)
return;
#ifdef WAN_DEBUG
@ -80,7 +80,7 @@ static void __on_uplink(struct codec_complete *cc, void *msgType, uint8_t *buf,
(void) msgType;
u->send(u, buf, len);
udp_send(u, buf, len);
}
static void __on_ptt(struct lanmsg_handler *lh, lan_t *ln, struct MsgCmd *hdr, uint8_t posId, int on)
@ -118,7 +118,7 @@ static void __on_ptt(struct lanmsg_handler *lh, lan_t *ln, struct MsgCmd *hdr, u
x = getcs(buf, pos);
buf[pos++] = x;
buf[pos++] = 0x14;
u->send(u, buf, pos);
udp_send(u, buf, pos);
lan_ptt_response(ln, on ? RESP_PTT_ON : RESP_PTT_OFF, hdr, RESP_PTT_OK);
}
@ -142,7 +142,7 @@ static void __on_status(struct lanmsg_handler *lh, uint8_t id, uint8_t status)
sbuf = (uint8_t *)&wn->_stBuf;
wn->_stBuf.checkNum = getcs(sbuf,len);
len = wn->stsock->send(wn->stsock, &wn->_stBuf, sizeof wn->_stBuf);
len = udp_send(wn->stsock, &wn->_stBuf, sizeof wn->_stBuf);
if (len < 0) {
log_printf("%s:%d errno (%d) %s\n", __FILE__, __LINE__, errno, strerror(errno));
}
@ -161,7 +161,7 @@ wan_t* wan_new(const char *group_addr, int port, const char *wlan_if, int stport
if ((sts = udp_open_mcast(group_addr, stport, wlan_if)) == NULL) {
log_printf("'%s[%d]: can't create status multicast socket\n", __FILE__, __LINE__);
udp->close(udp);
udp_close(udp);
return NULL;
}
wn = (wan_t *)udp_readable_open(sizeof *wn, udp);
@ -189,7 +189,6 @@ wan_t* wan_new(const char *group_addr, int port, const char *wlan_if, int stport
void wan_free(wan_t *wn)
{
wn->stsock->close(wn->stsock);
udp_close(wn->stsock);
udp_readable_close(&wn->base);
}
}
Loading…
Cancel
Save