示例#1
0
static int media_alloc(struct sdp_media **mp, struct list *list)
{
    struct sdp_media *m;
    int i;

    m = mem_zalloc(sizeof(*m), destructor);
    if (!m)
        return ENOMEM;

    list_append(list, &m->le, m);

    m->ldir  = SDP_SENDRECV;
    m->rdir  = SDP_SENDRECV;
    m->dynpt = RTP_DYNPT_START;

    sa_init(&m->laddr, AF_INET);
    sa_init(&m->raddr, AF_INET);
    sa_init(&m->laddr_rtcp, AF_INET);
    sa_init(&m->raddr_rtcp, AF_INET);

    for (i=0; i<SDP_BANDWIDTH_MAX; i++) {
        m->lbwv[i] = -1;
        m->rbwv[i] = -1;
    }

    *mp = m;

    return 0;
}
示例#2
0
/*
 * zfs_init_libshare(zhandle, service)
 *
 * Initialize the libshare API if it hasn't already been initialized.
 * In all cases it returns 0 if it succeeded and an error if not. The
 * service value is which part(s) of the API to initialize and is a
 * direct map to the libshare sa_init(service) interface.
 */
int
zfs_init_libshare(libzfs_handle_t *zhandle, int service)
{
	int ret = SA_OK;

	if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
		/*
		 * We had a cache miss. Most likely it is a new ZFS
		 * dataset that was just created. We want to make sure
		 * so check timestamps to see if a different process
		 * has updated any of the configuration. If there was
		 * some non-ZFS change, we need to re-initialize the
		 * internal cache.
		 */
		zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
		if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
			zfs_uninit_libshare(zhandle);
			zhandle->libzfs_sharehdl = sa_init(service);
		}
	}

	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
		zhandle->libzfs_sharehdl = sa_init(service);

	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
		ret = SA_NO_MEMORY;

	return (ret);
}
示例#3
0
文件: init.c 项目: sofuture/bitrig
void
init(void)
{
	app_init();
	doi_init();
	exchange_init();
	group_init();
	ipsec_init();
	isakmp_doi_init();
	libcrypto_init();

	timer_init();

	/* The following group are depending on timer_init having run.  */
	conf_init();
	connection_init();

	/* This depends on conf_init, thus check as soon as possible. */
	log_reinit();

	/* policy_init depends on conf_init having run.  */
	policy_init();

	/* Depends on conf_init and policy_init having run */
	cert_init();
	crl_init();

	sa_init();
	transport_init();
	virtual_init();
	udp_init();
	nat_t_init();
	udp_encap_init();
	vendor_init();
}
示例#4
0
static int stream_sock_alloc(struct stream *s, int af)
{
	struct sa laddr;
	int tos, err;

	if (!s)
		return EINVAL;

	/* we listen on all interfaces */
	sa_init(&laddr, sa_af(net_laddr_af(af)));

	err = rtp_listen(&s->rtp, IPPROTO_UDP, &laddr,
			 config.avt.rtp_ports.min, config.avt.rtp_ports.max,
			 s->rtcp, rtp_recv, rtcp_handler, s);
	if (err)
		return err;

	tos = config.avt.rtp_tos;
	(void)udp_setsockopt(rtp_sock(s->rtp), IPPROTO_IP, IP_TOS,
			     &tos, sizeof(tos));
	(void)udp_setsockopt(rtcp_sock(s->rtp), IPPROTO_IP, IP_TOS,
			     &tos, sizeof(tos));

	udp_rxsz_set(rtp_sock(s->rtp), RTP_RECV_SIZE);

	return 0;
}
    int lcs(char* s,char* t)
    {
        int l1 = strlen(s);
        int l2 = strlen(t);
        sa_init( );
        for(int i=0;i < l1;i++)
            sa_extend(s[i]);
                 
        int v = 0,l = 0,best = 0,bestpos = 0;
        for(int i=0;i < l2;i++) 
        {
            while(v && !st[v].next.count(t[i])) 
            {
                v = st[v].link;
                l = st[v].len;
            }

            if(st[v].next.count(t[i])) 
            {
                v = st[v].next[t[i]];
                ++l;
            }
            if(l > best)
                best = l,bestpos = i;
        }
        return best;
    }
    string lcs(string s,string t)
    {
        sa_init( );
        for(int i=0;i<(int)s.length( );i++)
            sa_extend(s[i]);
                 
        int v = 0,l = 0,best = 0,bestpos = 0;
        for(int i=0;i<(int)t.length();i++) 
        {
            while(v && !st[v].next.count(t[i])) 
            {
                v = st[v].link;
                l = st[v].len;
            }

            if(st[v].next.count(t[i])) 
            {
                v = st[v].next[t[i]];
                ++l;
            }
            if(l > best)
                best = l,bestpos = i;
        }
        return t.substr(bestpos - best + 1,best);
    }
示例#7
0
static int stream_sock_alloc(struct stream *s, int af)
{
	struct sa laddr;
	int tos, err;

	if (!s)
		return EINVAL;

	/* we listen on all interfaces */
	sa_init(&laddr, af);

	err = rtp_listen(&s->rtp, IPPROTO_UDP, &laddr,
			 s->cfg.rtp_ports.min, s->cfg.rtp_ports.max,
			 s->rtcp, rtp_recv, rtcp_handler, s);
	if (err) {
		warning("stream: rtp_listen failed: af=%s ports=%u-%u"
			" (%m)\n", net_af2name(af),
			s->cfg.rtp_ports.min, s->cfg.rtp_ports.max, err);
		return err;
	}

	tos = s->cfg.rtp_tos;
	(void)udp_setsockopt(rtp_sock(s->rtp), IPPROTO_IP, IP_TOS,
			     &tos, sizeof(tos));
	(void)udp_setsockopt(rtcp_sock(s->rtp), IPPROTO_IP, IP_TOS,
			     &tos, sizeof(tos));

	udp_rxsz_set(rtp_sock(s->rtp), RTP_RECV_SIZE);

	return 0;
}
示例#8
0
文件: platform.c 项目: tlauda/sof
int platform_init(struct sof *sof)
{
	int ret;
	struct dai *esai;

	clock_init();
	scheduler_init();

	platform_timer_start(platform_timer);
	sa_init(sof);

	clock_set_freq(CLK_CPU(cpu_get_id()), CLK_MAX_CPU_HZ);

	/* init DMA */
	ret = edma_init();
	if (ret < 0)
		return -ENODEV;

	/* initialize the host IPC mechanims */
	ipc_init(sof);

	ret = dai_init();
	if (ret < 0)
		return -ENODEV;

	esai = dai_get(SOF_DAI_IMX_ESAI, 0, DAI_CREAT);
	if (!esai)
		return -ENODEV;

	dai_probe(esai);

	return 0;
}
示例#9
0
文件: satest.c 项目: bringhurst/tmpi
int main(int argc, char *argv[])
{
	pthread_t thrs[NWORKER];
# if (CHECK)
	char *ret;
# endif
	
	int i;

	sa_init(&sa, 37, NWORKER*CONCURRENT);

	for (i=0; i<NWORKER; i++)
		pthread_create(thrs+i, NULL, worker, NULL);

	for (i=0; i<NWORKER; i++)
		pthread_join(thrs[i], NULL);

# if (CHECK)
	if ((ret=(char *)sa_check(&sa))) {
		fprintf(stderr, "StackAllocator check failed!\n");
		switch ((int)ret) {
			case 1 :
				fprintf(stderr, "Count doesn't match.\n");
				break;
			default :
				fprintf(stderr, "Invalid block %p\n", ret);
		}
		sa_dump(&sa, stderr);
	}
	else {
		fprintf(stderr, "StackAllocator check passed!\n");
	}
# endif
	return 0;
}
示例#10
0
/**
 * Get the Link-local address for a specific network interface
 *
 * @param ifname Name of the interface
 * @param af     Address family
 * @param ip     Returned link-local address
 *
 * @return 0 if success, otherwise errorcode
 */
int net_if_getlinklocal(const char *ifname, int af, struct sa *ip)
{
	struct sa addr;
	void *argv[3];
	int err;

	if (!ip)
		return EINVAL;

	sa_init(&addr, sa_af(ip));

	argv[0] = (void *)ifname;
	argv[1] = &af;
	argv[2] = &addr;

	err = net_if_apply(linklocal_handler, argv);
	if (err)
		return err;

	if (!sa_isset(&addr, SA_ADDR))
		return ENOENT;

	*ip = addr;

	return 0;
}
示例#11
0
文件: pcp.c 项目: GGGO/baresip
static int media_alloc(struct mnat_media **mp, struct mnat_sess *sess,
		       int proto, void *sock1, void *sock2,
		       struct sdp_media *sdpm)
{
	struct mnat_media *m;
	struct sa laddr;
	struct pcp_map map;
	unsigned i;
	int err = 0;

	if (!mp || !sess || !sdpm || proto != IPPROTO_UDP)
		return EINVAL;

	m = mem_zalloc(sizeof(*m), media_destructor);
	if (!m)
		return ENOMEM;

	m->compc = sock2 ? 2 : 1;

	list_append(&sess->medial, &m->le, m);
	m->sess = sess;
	m->sdpm = mem_ref(sdpm);

	for (i=0; i<m->compc; i++) {
		struct comp *comp = &m->compv[i];

		comp->id = i+1;
		comp->media = m;

		err = udp_local_get(i==0 ? sock1 : sock2, &laddr);
		if (err)
			goto out;

		rand_bytes(map.nonce, sizeof(map.nonce));
		map.proto = proto;
		map.int_port = sa_port(&laddr);
		/* note: using same address-family as the PCP server */
		sa_init(&map.ext_addr, sa_af(&pcp_srv));

		info("pcp: %s: internal port for %s is %u\n",
		     sdp_media_name(sdpm),
		     i==0 ? "RTP" : "RTCP",
		     map.int_port);

		err = pcp_request(&comp->pcp, NULL, &pcp_srv, PCP_MAP,
				  LIFETIME, &map, pcp_resp_handler, comp, 0);
		if (err)
			goto out;
	}

 out:
	if (err)
		mem_deref(m);
	else if (mp) {
		*mp = m;
	}

	return err;
}
示例#12
0
void sdp_media_rreset(struct sdp_media *m)
{
    int i;

    if (!m)
        return;

    sa_init(&m->raddr, AF_INET);
    sa_init(&m->raddr_rtcp, AF_INET);

    list_flush(&m->rfmtl);
    list_flush(&m->rattrl);

    m->rdir = SDP_SENDRECV;

    for (i=0; i<SDP_BANDWIDTH_MAX; i++)
        m->rbwv[i] = -1;
}
示例#13
0
int allocation_create(struct allocator *allocator, unsigned ix, int proto,
		      const struct sa *srv,
		      const char *username, const char *password,
		      struct tls *tls, bool turn_ind,
		      allocation_h *alloch, void *arg)
{
	struct allocation *alloc;
	struct sa laddr;
	int err;

	if (!allocator || !proto || !srv)
		return EINVAL;

	sa_init(&laddr, sa_af(srv));

	alloc = mem_zalloc(sizeof(*alloc), destructor);
	if (!alloc)
		return ENOMEM;

	list_append(&allocator->allocl, &alloc->le, alloc);

	(void)gettimeofday(&alloc->sent, NULL);

	alloc->atime     = -1;
	alloc->ix        = ix;
	alloc->allocator = allocator;
	alloc->proto     = proto;
	alloc->secure    = tls != NULL;
	alloc->srv       = *srv;
	alloc->user      = username;
	alloc->pass      = password;
	alloc->turn_ind  = turn_ind;
	alloc->alloch    = alloch;
	alloc->arg       = arg;
	alloc->tls       = mem_ref(tls);

	receiver_init(&alloc->recv, allocator->session_cookie, alloc->ix);

	err = udp_listen(&alloc->us_tx, &laddr, NULL, NULL);
	if (err) {
		re_fprintf(stderr, "allocation: failed to create UDP tx socket"
			   " (%m)\n", err);
		goto out;
	}

	udp_local_get(alloc->us_tx, &alloc->laddr_tx);

	err = start(alloc);
	if (err)
		goto out;

 out:
	if (err)
		mem_deref(alloc);

	return err;
}
示例#14
0
文件: http.c 项目: muromec/rehttp
void http_init(struct httpc *app, struct request **rpp, char *str_uri)
{
    int ok;
    struct request *request;
    struct pl pl_uri;
    struct url url;

    *rpp = NULL;

    pl_uri.p = NULL;
    str_dup((char**)&pl_uri.p, str_uri);
    pl_uri.l = strlen(str_uri);

    ok = url_decode(&url, &pl_uri);

    if(ok!=0)
        goto err_uri;

    request = mem_zalloc(sizeof(*request), destructor);
    ok = hash_alloc(&request->hdrht, HDR_HASH_SIZE);
    request->err_h = dummy_err;
    request->done_h = http_done;
    request->post = NULL;
    request->form = 0;
    request->www_auth.p = NULL;
    request->www_auth.l = 0;
    request->auth = NULL;

    request->retry = 0;

    pl_strdup(&request->host, &url.host);
    pl_strdup(&request->path, &url.path);
    request->secure = !pl_strcmp(&url.scheme, "https");
    memcpy(&request->meth, "GET", 4);
    request->meth[4] = 0;

    if(url.port)
	request->port = url.port;
    else
        request->port = request->secure ? 443 : 80;

    DEBUG_INFO("secure: %d port %d\n", request->secure, request->port);
    sa_init(&request->dest, AF_INET);
    ok = sa_set_str(&request->dest, request->host, request->port);

    request->state = ok ? START : RESOLVED;

    request->app = app;
    *rpp = request;

err_uri:
    if(pl_uri.p)
        mem_deref((void*)pl_uri.p);

    return;
}
示例#15
0
/**
 * Test parsing of various SDP messages from various vendors
 */
int test_sdp_parse(void)
{
	struct sdp_session *sess = NULL;
	struct sdp_media *audio;
	struct mbuf *mb;
	struct sa laddr;
	uint32_t i;
	int err;

	mb = mbuf_alloc(2048);
	if (!mb)
		return ENOMEM;

	sa_init(&laddr, AF_INET);

	for (i=0; i<ARRAY_SIZE(msgs); i++) {

		sess = mem_deref(sess);

		err = sdp_session_alloc(&sess, &laddr);
		if (err)
			goto out;

		err = sdp_media_add(&audio, sess, sdp_media_audio, 5004,
				    sdp_proto_rtpavp);
		if (err)
			goto out;

		err = sdp_format_add(NULL, audio, false, ref_pt, ref_cname,
				     ref_srate, 1, NULL, NULL, NULL, false,
				     NULL);
		if (err)
			goto out;

		err = sdp_format_add(NULL, audio, false, "8", "PCMA", 8000, 1,
				     NULL, NULL, NULL, false, NULL);
		if (err)
			goto out;

		mbuf_rewind(mb);
		(void)mbuf_write_str(mb, msgs[i]);
		mb->pos = 0;

		err = sdp_decode(sess, mb, true);
		if (err)
			goto out;
	}

 out:
	mem_deref(sess);
	mem_deref(mb);

	return err;
}
示例#16
0
/**
 * Get local network address of TCP Socket
 *
 * @param ts    TCP Socket
 * @param local Returned local network address
 *
 * @return 0 if success, otherwise errorcode
 */
int tcp_sock_local_get(const struct tcp_sock *ts, struct sa *local)
{
	if (!ts || !local)
		return EINVAL;

	sa_init(local, AF_UNSPEC);

	if (getsockname(ts->fd, &local->u.sa, &local->len) < 0) {
		DEBUG_WARNING("local get: getsockname(): %m\n", errno);
		return errno;
	}

	return 0;
}
示例#17
0
文件: libshare.c 项目: AB17/zfs
__attribute__((constructor)) static void
libshare_init(void)
{
	libshare_nfs_init();
	libshare_smb_init();

	/*
	 * This bit causes /etc/dfs/sharetab to be updated before libzfs gets a
	 * chance to read that file; this is necessary because the sharetab file
	 * might be out of sync with the NFS kernel exports (e.g. due to reboots
	 * or users manually removing shares)
	 */
	sa_fini(sa_init(0));
}
示例#18
0
/**
 * Get remote peer network address of TCP Connection
 *
 * @param tc    TCP Connection
 * @param peer Returned remote peer network address
 *
 * @return 0 if success, otherwise errorcode
 */
int tcp_conn_peer_get(const struct tcp_conn *tc, struct sa *peer)
{
	if (!tc || !peer)
		return EINVAL;

	sa_init(peer, AF_UNSPEC);

	if (getpeername(tc->fdc, &peer->u.sa, &peer->len) < 0) {
		DEBUG_WARNING("conn peer get: getpeername(): %m\n", errno);
		return errno;
	}

	return 0;
}
示例#19
0
/**
 * Get local network address of TCP Connection
 *
 * @param tc    TCP Connection
 * @param local Returned local network address
 *
 * @return 0 if success, otherwise errorcode
 */
int tcp_conn_local_get(const struct tcp_conn *tc, struct sa *local)
{
	if (!tc || !local)
		return EINVAL;

	sa_init(local, AF_UNSPEC);

	if (getsockname(tc->fdc, &local->u.sa, &local->len) < 0) {
		DEBUG_WARNING("conn local get: getsockname(): %m\n", errno);
		return errno;
	}

	return 0;
}
示例#20
0
void
libshare_init(void)
#endif
{
	libshare_nfs_init();
	libshare_smb_init();

	/*
	 * This bit causes /etc/dfs/sharetab to be updated before libzfs gets a
	 * chance to read that file; this is necessary because the sharetab file
	 * might be out of sync with the NFS kernel exports (e.g. due to reboots
	 * or users manually removing shares)
	 */
#ifndef __APPLE__
	sa_fini(sa_init(0));
#endif
}
string lcs ( string s, string t ) {
    sa_init ( ) ;
    int slen = s.length() , tlen = t.length() ;
    rep(i,slen) sa_extend ( s [ i ] ) ;
    int v = 0 ,  l = 0 , best = 0 ,  bestpos = 0 ;
    rep(i,tlen)
    {
        while ( v && ! st [ v ] . next.count ( t[i] ) ) {
            v = st [v].link ;
            l = st [v].len ;
        }
        if ( st [v] . next.count ( t[i] ) ) {
            v = st [v].next [ t [i] ] ;
            ++l ;
        }
        if ( l > best ) best = l,  bestpos = i ;
    }
    return t. substr ( bestpos - best + 1 , best ) ;
}
示例#22
0
文件: rtp.c 项目: hbowden/re
/**
 * Allocate a new RTP socket
 *
 * @param rsp Pointer to returned RTP socket
 *
 * @return 0 for success, otherwise errorcode
 */
int rtp_alloc(struct rtp_sock **rsp)
{
	struct rtp_sock *rs;

	if (!rsp)
		return EINVAL;

	rs = mem_zalloc(sizeof(*rs), destructor);
	if (!rs)
		return ENOMEM;

	sa_init(&rs->rtcp_peer, AF_UNSPEC);

	rs->enc.seq  = rand_u16() & 0x7fff;
	rs->enc.ssrc = rand_u32();

	*rsp = rs;

	return 0;
}
示例#23
0
文件: natpmp.c 项目: GGGO/baresip
static int module_init(void)
{
	int err;

	sa_init(&natpmp_srv, AF_INET);
	sa_set_port(&natpmp_srv, NATPMP_PORT);

	net_rt_list(net_rt_handler, NULL);

	conf_get_sa(conf_cur(), "natpmp_server", &natpmp_srv);

	info("natpmp: using NAT-PMP server at %J\n", &natpmp_srv);

	err = natpmp_external_request(&natpmp_ext, &natpmp_srv,
				      extaddr_handler, NULL);
	if (err)
		return err;

	return mnat_register(&mnat, "natpmp", NULL,
			     session_alloc, media_alloc, NULL);
}
示例#24
0
文件: via.c 项目: allanlei/lib-re
/**
 * Decode a pointer-length string into a SIP Via header
 *
 * @param via SIP Via header
 * @param pl  Pointer-length string
 *
 * @return 0 for success, otherwise errorcode
 */
int sip_via_decode(struct sip_via *via, const struct pl *pl)
{
    struct pl transp, host, port;
    int err;

    if (!via || !pl)
        return EINVAL;

    err = re_regex(pl->p, pl->l,
                   "SIP[  \t\r\n]*/[ \t\r\n]*2.0[ \t\r\n]*/[ \t\r\n]*"
                   "[A-Z]+[ \t\r\n]*[^; \t\r\n]+[ \t\r\n]*[^]*",
                   NULL, NULL, NULL, NULL, &transp,
                   NULL, &via->sentby, NULL, &via->params);
    if (err)
        return err;

    if (!pl_strcmp(&transp, "TCP"))
        via->tp = SIP_TRANSP_TCP;
    else if (!pl_strcmp(&transp, "TLS"))
        via->tp = SIP_TRANSP_TLS;
    else if (!pl_strcmp(&transp, "UDP"))
        via->tp = SIP_TRANSP_UDP;
    else
        via->tp = SIP_TRANSP_NONE;

    err = decode_hostport(&via->sentby, &host, &port);
    if (err)
        return err;

    sa_init(&via->addr, AF_INET);

    (void)sa_set(&via->addr, &host, 0);

    if (pl_isset(&port))
        sa_set_port(&via->addr, pl_u32(&port));

    via->val = *pl;

    return msg_param_decode(&via->params, "branch", &via->branch);
}
示例#25
0
int  esp_server_alloc(struct esp_server **pp)
{
    struct esp_server *es;
    
    if(!pp)
        return EINVAL;
    
    *pp = NULL;
    
    es = mem_zalloc(sizeof(*es), es_de);
    if(!es)
        return ENOMEM;

    sa_init(&es->local, AF_INET);
    sa_set_port(&es->local, DM_PORT);
    DEBUG_NOTICE("listen %J\n", &es->local);
    tcp_listen(&es->ts, &es->local, tcp_conn_handler, es);

    *pp = es;
    
    return 0;
}
int main() {
    long start_time = clock();

    size_t size, last;
    std::vector<state> states = sa_init(size, last);
    char c;
    while (std::cin.get(c)) {
        if(c == '\n') break;
        sa_extend(states, size, last, c);
    }

    std::vector<long long> visited_st(2*last, -1); // cache for calculated states
    long long m = count_for_st(visited_st, states, 0);

    std::cout << m << std::endl;

    long total_time = clock() - start_time;
    char buffer[50];
    sprintf(buffer, "Total time: %ld", total_time);
    LOG(1, buffer);

    return 0;
}
static void print_udp_bufsize(int af)
{
	struct udp_sock *us = NULL;
	struct sa laddr;
	int fd;
	int sndbuf;
	int rcvbuf;
	socklen_t sndbuf_len = sizeof(sndbuf);
	socklen_t rcvbuf_len = sizeof(rcvbuf);
	int err;

	sa_init(&laddr, af);

	err = udp_listen(&us, &laddr, NULL, NULL);
	if (err) {
		warning("test: udp_listen failed (%m)\n", err);
		goto out;
	}

	fd = udp_sock_fd(us, af);
	if (-1 == fd) {
		warning("test: no udp socket fd\n");
		goto out;
	}

	getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, &sndbuf_len);
	getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &rcvbuf_len);

	re_printf("test: udp socket buffersize for %-8s:"
		  "    SO_SNDBUF=%d SO_RCVBUF=%d\n",
		  net_af2name(af),
		  sndbuf, rcvbuf);

 out:
	mem_deref(us);
}
示例#28
0
文件: turn.c 项目: AmesianX/restund
static int module_init(void)
{
	uint32_t x, bsize = ALLOC_DEFAULT_BSIZE;
	struct pl opt;
	int err = 0;

	restund_stun_register_handler(&stun);
	restund_cmd_subscribe(&cmd_turn);
	restund_cmd_subscribe(&cmd_turnstats);

	/* turn_external_addr */
	if (!conf_get(restund_conf(), "turn_relay_addr", &opt))
		err = sa_set(&turnd.rel_addr, &opt, 0);
	else
		sa_init(&turnd.rel_addr, AF_UNSPEC);

	if (err) {
		restund_error("turn: bad turn_relay_addr: '%r'\n", &opt);
		goto out;
	}

	/* turn_external_addr6 */
	if (!conf_get(restund_conf(), "turn_relay_addr6", &opt))
		err = sa_set(&turnd.rel_addr6, &opt, 0);
	else
		sa_init(&turnd.rel_addr6, AF_UNSPEC);

	if (err) {
		restund_error("turn: bad turn_relay_addr6: '%r'\n", &opt);
		goto out;
	}

	if (!sa_isset(&turnd.rel_addr, SA_ADDR) &&
	    !sa_isset(&turnd.rel_addr6, SA_ADDR)) {
		restund_error("turn: no relay address configured\n");
		err = EINVAL;
		goto out;
	}

	/* turn_max_lifetime, turn_max_allocations, udp_sockbuf_size */
	turnd.lifetime_max = TURN_DEFAULT_LIFETIME;
	conf_get_u32(restund_conf(), "turn_max_lifetime", &turnd.lifetime_max);
	conf_get_u32(restund_conf(), "turn_max_allocations", &bsize);
	conf_get_u32(restund_conf(), "udp_sockbuf_size",
		     &turnd.udp_sockbuf_size);

	for (x=2; (uint32_t)1<<x<bsize; x++);
	bsize = 1<<x;

	err = hash_alloc(&turnd.ht_alloc, bsize);
	if (err) {
		restund_error("turnd hash alloc error: %m\n", err);
		goto out;
	}

	restund_debug("turn: lifetime=%u ext=%j ext6=%j bsz=%u\n",
		      turnd.lifetime_max, &turnd.rel_addr, &turnd.rel_addr6,
		      bsize);

 out:
	return err;
}
示例#29
0
/**
 * Handler for incoming TCP connections.
 *
 * @param flags  Event flags.
 * @param arg    Handler argument.
 */
static void tcp_conn_handler(int flags, void *arg)
{
	struct sa peer;
	struct tcp_sock *ts = arg;
	int err;

	(void)flags;

	sa_init(&peer, AF_UNSPEC);

	if (ts->fdc >= 0)
		(void)close(ts->fdc);

	ts->fdc = SOK_CAST accept(ts->fd, &peer.u.sa, &peer.len);
	if (-1 == ts->fdc) {

#if TARGET_OS_IPHONE
		if (EAGAIN == errno) {

			struct tcp_sock *ts_new;
			struct sa laddr;

			err = tcp_sock_local_get(ts, &laddr);
			if (err)
				return;

			if (ts->fd >= 0) {
				fd_close(ts->fd);
				(void)close(ts->fd);
				ts->fd = -1;
			}

			err = tcp_listen(&ts_new, &laddr, NULL, NULL);
			if (err)
				return;

			ts->fd = ts_new->fd;
			ts_new->fd = -1;

			mem_deref(ts_new);

			fd_listen(ts->fd, FD_READ, tcp_conn_handler, ts);
		}
#endif

		return;
	}

	err = net_sockopt_blocking_set(ts->fdc, false);
	if (err) {
		DEBUG_WARNING("conn handler: nonblock set: %m\n", err);
		(void)close(ts->fdc);
		ts->fdc = -1;
		return;
	}

	tcp_sockopt_set(ts->fdc);

	if (ts->connh)
		ts->connh(&peer, ts->arg);
}
示例#30
0
static int start(struct allocation *alloc)
{
	struct sa laddr;
	int err = 0;

	if (!alloc)
		return EINVAL;

	sa_init(&laddr, sa_af(&alloc->srv));

	switch (alloc->proto) {

	case IPPROTO_UDP:
		err = udp_listen(&alloc->us, &laddr, udp_recv, alloc);
		if (err) {
			re_fprintf(stderr, "allocation: failed to"
				   " create UDP socket"
				   " (%m)\n", err);
			goto out;
		}

		udp_sockbuf_set(alloc->us, 524288);

		if (alloc->secure) {

			/* note: re-using UDP socket for DTLS-traffic */
			err = dtls_listen(&alloc->dtls_sock, NULL, alloc->us,
					  2, DTLS_LAYER, NULL, NULL);
			if (err) {
				re_fprintf(stderr, "dtls_listen error: %m\n",
					   err);
				goto out;
			}

			err = dtls_connect(&alloc->tlsc, alloc->tls,
					   alloc->dtls_sock, &alloc->srv,
					   dtls_estab_handler,
					   dtls_recv_handler,
					   dtls_close_handler, alloc);
			if (err) {
				re_fprintf(stderr, "dtls_connect error: %m\n",
					   err);
				goto out;
			}
		}
		else {
			err = turnc_alloc(&alloc->turnc, NULL, IPPROTO_UDP,
					  alloc->us, TURN_LAYER, &alloc->srv,
					  alloc->user, alloc->pass,
					  TURN_DEFAULT_LIFETIME,
					  turnc_handler, alloc);
			if (err) {
				re_fprintf(stderr, "allocation: failed to"
					   " create TURN client"
					   " (%m)\n", err);
				goto out;
			}
		}
		break;

	case IPPROTO_TCP:
		err = tcp_connect(&alloc->tc, &alloc->srv, tcp_estab_handler,
				  tcp_recv_handler, tcp_close_handler, alloc);
		if (err)
			break;

		if (alloc->secure) {
			err = tls_start_tcp(&alloc->tlsc, alloc->tls,
					    alloc->tc, 0);
			if (err)
				break;
		}
		break;

	default:
		err = EPROTONOSUPPORT;
		goto out;
	}

 out:
	return err;
}