当前位置: 首页>>代码示例>>C++>>正文


C++ SA_LEN函数代码示例

本文整理汇总了C++中SA_LEN函数的典型用法代码示例。如果您正苦于以下问题:C++ SA_LEN函数的具体用法?C++ SA_LEN怎么用?C++ SA_LEN使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SA_LEN函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: rc_ipaddr_local

/** Checks if provided address is local address
 *
 * @param addr an %AF_INET or %AF_INET6 address
 * @return 0 if local, 1 if not local, -1 on failure.
 */
static int rc_ipaddr_local(const struct sockaddr *addr)
{
	int temp_sock, res, serrno;
	struct sockaddr tmpaddr;

	memcpy(&tmpaddr, addr, SA_LEN(addr));

	temp_sock = socket(addr->sa_family, SOCK_DGRAM, 0);
	if (temp_sock == -1)
		return -1;

	if (addr->sa_family == AF_INET) {
		((struct sockaddr_in*)&tmpaddr)->sin_port = 0;
	} else {
		((struct sockaddr_in6*)&tmpaddr)->sin6_port = 0;
	}
	res = bind(temp_sock, &tmpaddr, SA_LEN(&tmpaddr));
	serrno = errno;
	close(temp_sock);
	if (res == 0)
		return 0;
	if (serrno == EADDRNOTAVAIL)
		return 1;
	return -1;
}
开发者ID:ionutrazvanionita,项目名称:free-radius-client,代码行数:30,代码来源:config.c

示例2: rc_get_srcaddr

/** Find outbound interface address for a given destination
 *
 * Given remote address find local address which the system will use as a source address for sending
 * datagrams to that remote address.
 *
 * @param lia local address.
 * @param ria the remove address.
 * @return 0 in success, -1 on failure, address is filled into the first argument.
 */
int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
{
	int temp_sock;
	socklen_t namelen;

	temp_sock = socket(ria->sa_family, SOCK_DGRAM, 0);
	if (temp_sock == -1) {
		rc_log(LOG_ERR, "rc_get_srcaddr: socket: %s", strerror(errno));
		return -1;
	}

	if (connect(temp_sock, ria, SA_LEN(ria)) != 0) {
		rc_log(LOG_ERR, "rc_get_srcaddr: connect: %s",
		    strerror(errno));
		close(temp_sock);
		return -1;
	}

	namelen = SA_LEN(ria);
	if (getsockname(temp_sock, lia, &namelen) != 0) {
		rc_log(LOG_ERR, "rc_get_srcaddr: getsockname: %s",
		    strerror(errno));
		close(temp_sock);
		return -1;
	}

	close(temp_sock);
	return 0;
}
开发者ID:credil,项目名称:radcli,代码行数:38,代码来源:ip_util.c

示例3: get_tp4wp

static struct rtpp_tnotify_target *
get_tp4wp(struct rtpp_tnotify_set *pvt, struct rtpp_tnotify_wildcard *wp,
  struct sockaddr *ccaddr, struct sockaddr *laddr)
{
    int i;
    struct rtpp_tnotify_target *tp;
    struct sockaddr_in localhost;

    if (ccaddr == NULL || ccaddr->sa_family != AF_INET) {
        /* Request on the unix/IPv6 domain socket, assume it's 127.0.0.1 */
        memset(&localhost, '\0', sizeof(struct sockaddr_in));
        inet_aton("127.0.0.1", &localhost.sin_addr);
        ccaddr = sstosa(&localhost);
        ccaddr->sa_family = AF_INET;
    }
    for (i = 0; i < pvt->tp_len; i++) {
        /* First check against existing targets */
        tp = pvt->tp[i];
        if (tp->socket_name != NULL) {
            /* Only match "automatic" entries */
            continue;
        }
        if (tp->socket_type != wp->socket_type)
            continue;
        if (!ishostseq(ccaddr, sstosa(&tp->remote)))
            continue;
        if (getport(sstosa(&tp->remote)) != wp->port)
            continue;
        return (tp);
    }
    /* Nothing found, crank up a new entry */
    if (pvt->tp_len == RTPP_TNOTIFY_TARGETS_MAX) {
        return (NULL);
    }
    tp = malloc(sizeof(struct rtpp_tnotify_target));
    if (tp == NULL) {
        return (NULL);
    }
    memset(tp, '\0', sizeof(struct rtpp_tnotify_target));
    if (laddr != NULL && laddr->sa_family == ccaddr->sa_family) {
        tp->local = malloc(SA_LEN(laddr));
        if (tp->local == NULL) {
            free(tp);
            return (NULL);
        }
        memcpy(tp->local, laddr, SA_LEN(laddr));
        setanyport(tp->local);
    }
    tp->remote_len = SA_LEN(ccaddr);
    memcpy(&tp->remote, ccaddr, tp->remote_len);
    setport(sstosa(&tp->remote), wp->port);
    tp->socket_type = wp->socket_type;
    tp->connected = 0;
    tp->fd = -1;
    pvt->tp[pvt->tp_len] = tp;
    pvt->tp_len += 1;
    return (tp);
}
开发者ID:Marhbere,项目名称:rtpproxy,代码行数:58,代码来源:rtpp_tnotify_set.c

示例4: create_twinlistener

static int
create_twinlistener(struct cfg_stable *cf, struct sockaddr *ia, int port, int *fds)
{
    struct sockaddr_storage iac;
    int rval, i, flags;

    fds[0] = fds[1] = -1;

    rval = -1;

    for (i = 0; i < 2; i++) {
        fds[i] = socket(ia->sa_family, SOCK_DGRAM, 0);

        if (fds[i] == -1) {
            rtpp_log_ewrite(RTPP_LOG_ERR, cf->glog, "can't create %s socket",
                            (ia->sa_family == AF_INET) ? "IPv4" : "IPv6");
            goto failure;
        }

        memcpy(&iac, ia, SA_LEN(ia));
        satosin(&iac)->sin_port = htons(port);

        if (bind(fds[i], sstosa(&iac), SA_LEN(ia)) != 0) {
            if (errno != EADDRINUSE && errno != EACCES) {
                rtpp_log_ewrite(RTPP_LOG_ERR, cf->glog, "can't bind to the %s port %d",
                                (ia->sa_family == AF_INET) ? "IPv4" : "IPv6", port);
            } else {
                rval = -2;
            }

            goto failure;
        }

        port++;

        if ((ia->sa_family == AF_INET) && (cf->tos >= 0) &&
                (setsockopt(fds[i], IPPROTO_IP, IP_TOS, &cf->tos, sizeof(cf->tos)) == -1))
            rtpp_log_ewrite(RTPP_LOG_ERR, cf->glog, "unable to set TOS to %d", cf->tos);

        flags = fcntl(fds[i], F_GETFL);
        fcntl(fds[i], F_SETFL, flags | O_NONBLOCK);
    }

    return 0;

failure:

    for (i = 0; i < 2; i++)
        if (fds[i] != -1) {
            close(fds[i]);
            fds[i] = -1;
        }

    return rval;
}
开发者ID:sambatyon,项目名称:rtpproxy,代码行数:55,代码来源:rtpp_command.c

示例5: ui_teardown

/* Tear down a connection, can be phase 1 or 2.  */
static void
ui_teardown(char *cmd)
{
	struct sockaddr_in	 addr;
	struct sockaddr_in6	 addr6;
	struct sa		*sa;
	int			 phase;
	char			 name[201];

	/* If no phase is given, we default to phase 2. */
	phase = 2;
	if (sscanf(cmd, "t main %200s", name) == 1)
		phase = 1;
	else if (sscanf(cmd, "t quick %200s", name) == 1)
		phase = 2;
	else if (sscanf(cmd, "t %200s", name) != 1) {
		log_print("ui_teardown: command \"%s\" malformed", cmd);
		return;
	}
	LOG_DBG((LOG_UI, 10, "ui_teardown: teardown connection \"%s\", "
	    "phase %d", name, phase));

	bzero(&addr, sizeof(addr));
	bzero(&addr6, sizeof(addr6));

	if (inet_pton(AF_INET, name, &addr.sin_addr) == 1) {
		addr.sin_len = sizeof(addr);
		addr.sin_family = AF_INET;

		while ((sa = sa_lookup_by_peer((struct sockaddr *)&addr,
		    SA_LEN((struct sockaddr *)&addr), phase)) != 0) {
			if (sa->name)
				connection_teardown(sa->name);
			sa_delete(sa, 1);
		}
	} else if (inet_pton(AF_INET6, name, &addr6.sin6_addr) == 1) {
		addr6.sin6_len = sizeof(addr6);
		addr6.sin6_family = AF_INET6;

		while ((sa = sa_lookup_by_peer((struct sockaddr *)&addr6,
		    SA_LEN((struct sockaddr *)&addr6), phase)) != 0) {
			if (sa->name)
				connection_teardown(sa->name);
			sa_delete(sa, 1);
		}
	} else {
		if (phase == 2)
			connection_teardown(name);
		while ((sa = sa_lookup_by_name(name, phase)) != 0)
			sa_delete(sa, 1);
	}
}
开发者ID:SylvestreG,项目名称:bitrig,代码行数:53,代码来源:ui.c

示例6: export_address

/*
 * Export a struct sockaddr as an SADB_ADDRESS payload.
 */
void
export_address(void **p, struct sockaddr *sa)
{
	struct sadb_address *sadb_address = (struct sadb_address *) *p;

	sadb_address->sadb_address_len = (sizeof(struct sadb_address) +
	    PADUP(SA_LEN(sa))) / sizeof(uint64_t);

	*p += sizeof(struct sadb_address);
	bcopy(sa, *p, SA_LEN(sa));
	((struct sockaddr *) *p)->sa_family = sa->sa_family;
	*p += PADUP(SA_LEN(sa));
}
开发者ID:MarginC,项目名称:kame,代码行数:16,代码来源:pfkeyv2_convert.c

示例7: NET_SendPacket

void
NET_SendPacket (int length, const void *data, netadr_t to)
{
	int         ret;
	AF_address_t addr;

	NetadrToSockadr (&to, &addr);

	ret = sendto (net_socket, data, length, 0, &addr.sa, SA_LEN (&addr.sa));
	if (ret == -1) {
#ifdef _WIN32
		int         err = WSAGetLastError ();

		if (err == WSAEADDRNOTAVAIL)
			Sys_Printf ("NET_SendPacket Warning: %i\n", err);
#else /* _WIN32 */
		int         err = errno;

		if (err == ECONNREFUSED)
			return;
#endif /* _WIN32 */
		if (err == EWOULDBLOCK)
			return;

		Sys_Printf ("NET_SendPacket: %s\n", strerror (err));
	}
}
开发者ID:EIREXE,项目名称:Quakeforge-gcw0,代码行数:27,代码来源:net_udp.c

示例8: send_packet

static void
send_packet(struct cfg *cf, struct rtpp_session *sp, int ridx,
  struct rtp_packet *packet)
{
    int i, sidx;

    GET_RTP(sp)->ttl[ridx] = cf->stable.max_ttl;

    /* Select socket for sending packet out. */
    sidx = (ridx == 0) ? 1 : 0;

    /*
     * Check that we have some address to which packet is to be
     * sent out, drop otherwise.
     */
    if (sp->addr[sidx] == NULL || GET_RTP(sp)->rtps[sidx] != NULL) {
	sp->pcount[3]++;
    } else {
	sp->pcount[2]++;
	for (i = (cf->stable.dmode && packet->size < LBR_THRS) ? 2 : 1; i > 0; i--) {
	    sendto(sp->fds[sidx], packet->data.buf, packet->size, 0, sp->addr[sidx],
	      SA_LEN(sp->addr[sidx]));
	}
    }

    if (sp->rrcs[ridx] != NULL && GET_RTP(sp)->rtps[ridx] == NULL)
	rwrite(sp, sp->rrcs[ridx], packet);
}
开发者ID:DrumTechnologiesLtd,项目名称:rtpproxy,代码行数:28,代码来源:main.c

示例9: ntp_sendmsg

int
ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg)
{
	socklen_t	sa_len;
	ssize_t		n;

	if (sa != NULL)
		sa_len = SA_LEN(sa);
	else
		sa_len = 0;

	n = sendto(fd, msg, sizeof(*msg), 0, sa, sa_len);
	if (n == -1) {
		if (errno == ENOBUFS || errno == EHOSTUNREACH ||
		    errno == ENETDOWN || errno == EHOSTDOWN) {
			/* logging is futile */
			return (-1);
		}
		log_warn("sendto");
		return (-1);
	}

	if (n != sizeof(*msg)) {
		log_warnx("ntp_sendmsg: only %zd of %zu bytes sent", n,
		    sizeof(*msg));
		return (-1);
	}

	return (0);
}
开发者ID:github188,项目名称:SimpleCode,代码行数:30,代码来源:ntp_msg.c

示例10: controlfd_init_tcp

static int
controlfd_init_tcp(struct cfg *cf, struct rtpp_ctrl_sock *csp)
{
    struct sockaddr *ifsin;
    char *cp;
    int controlfd, so_rcvbuf, i;

    cp = strrchr(csp->cmd_sock, ':');
    if (cp != NULL) {
        *cp = '\0';
        cp++;
    }
    if (cp == NULL || *cp == '\0')
        cp = CPORT;
    csp->port_ctl = atoi(cp);
    i = (csp->type == RTPC_TCP6) ? AF_INET6 : AF_INET;
    ifsin = sstosa(&csp->bindaddr);
    if (setbindhost(ifsin, i, csp->cmd_sock, cp) != 0)
        exit(1);
    controlfd = socket(i, SOCK_STREAM, 0);
    if (controlfd == -1)
        err(1, "can't create socket");
    so_rcvbuf = 16 * 1024;
    if (setsockopt(controlfd, SOL_SOCKET, SO_RCVBUF, &so_rcvbuf, sizeof(so_rcvbuf)) == -1)
        rtpp_log_ewrite(RTPP_LOG_ERR, cf->stable->glog, "unable to set 16K receive buffer size on controlfd");
    if (bind(controlfd, ifsin, SA_LEN(ifsin)) < 0)
        err(1, "can't bind to a socket");
    if (listen(controlfd, 32) != 0)
        err(1, "can't listen on a socket: %s", csp->cmd_sock);

    return (controlfd);
}
开发者ID:Marhbere,项目名称:rtpproxy,代码行数:32,代码来源:rtpp_controlfd.c

示例11: test_sa_len

static void test_sa_len(void)
{
    struct sockaddr sa;

    printf("Test sockaddr_in\n");
    sa.sa_family=AF_INET;
    SET_SA_LEN(&sa, sizeof(struct sockaddr_in));
    TEST(SA_LEN(&sa) == sizeof(struct sockaddr_in));

    printf("Test sockaddr_in6\n");
    sa.sa_family=AF_INET6;
    SET_SA_LEN(&sa, sizeof(struct sockaddr_in6));
    TEST(SA_LEN(&sa) == sizeof(struct sockaddr_in6));

    printf("Test sockadd\n");
    sa.sa_family=AF_UNSPEC;
    SET_SA_LEN(&sa, sizeof(struct sockaddr));
    TEST(SA_LEN(&sa) == sizeof(struct sockaddr));
}
开发者ID:SysMan-One,项目名称:pcp,代码行数:19,代码来源:test_gateway.c

示例12: log_sockaddr

const char *
log_sockaddr(struct sockaddr *sa)
{
	static char	buf[NI_MAXHOST];

	if (getnameinfo(sa, SA_LEN(sa), buf, sizeof(buf), NULL, 0,
	    NI_NUMERICHOST))
		return ("(unknown)");
	else
		return (buf);
}
开发者ID:acassis,项目名称:emlinux-ssd1935,代码行数:11,代码来源:log.c

示例13: dup_sockaddr

static struct sockaddr *
dup_sockaddr(struct sockaddr *sa)
{
	struct sockaddr *newsa;
	unsigned int size;
	
	size = SA_LEN(sa);
	if ((newsa = malloc(size)) == NULL)
		return (NULL);
	return (memcpy(newsa, sa, size));
}
开发者ID:aYosukeAkatsuka,项目名称:edimax-br-6528n,代码行数:11,代码来源:inet.c

示例14: cookie_gen

/*
 * Generate an anti-clogging token (a protection against an attacker forcing
 * us to keep state for a flood of connection requests) a.k.a. a cookie
 * at BUF, LEN bytes long.  The cookie will be generated by hashing of
 * information found, among otherplaces, in transport T and exchange
 * EXCHANGE.
 */
void
cookie_gen(struct transport *t, struct exchange *exchange, u_int8_t *buf,
    size_t len)
{
	struct hash    *hash = hash_get(HASH_SHA1);
	u_int8_t        tmpsecret[COOKIE_SECRET_SIZE];
	struct sockaddr *name;

	hash->Init(hash->ctx);
	(*t->vtbl->get_dst)(t, &name);
	hash->Update(hash->ctx, (u_int8_t *)name, SA_LEN(name));
	(*t->vtbl->get_src)(t, &name);
	hash->Update(hash->ctx, (u_int8_t *)name, SA_LEN(name));
	if (exchange->initiator == 0)
		hash->Update(hash->ctx, exchange->cookies +
		    ISAKMP_HDR_ICOOKIE_OFF, ISAKMP_HDR_ICOOKIE_LEN);
	arc4random_buf(tmpsecret, COOKIE_SECRET_SIZE);
	hash->Update(hash->ctx, tmpsecret, COOKIE_SECRET_SIZE);
	hash->Final(hash->digest, hash->ctx);
	memcpy(buf, hash->digest, len);
}
开发者ID:SylvestreG,项目名称:bitrig,代码行数:28,代码来源:cookie.c

示例15: isakmp_sa_check

/*
 * Check if SA matches what we are asking for through V_ARG.  It has to
 * be a finished phase 1 (ISAKMP) SA.
 */
static int
isakmp_sa_check(struct sa *sa, void *v_arg)
{
	struct dst_isakmpspi_arg *arg = v_arg;
	struct sockaddr		*dst, *src;

	if (sa->phase != 1 || !(sa->flags & SA_FLAG_READY))
		return 0;

	/* verify address is either src or dst for this sa */
	sa->transport->vtbl->get_dst(sa->transport, &dst);
	sa->transport->vtbl->get_src(sa->transport, &src);
	if (memcmp(src, arg->dst, SA_LEN(src)) &&
	    memcmp(dst, arg->dst, SA_LEN(dst)))
		return 0;

	/* match icookie+rcookie against spi */
	if (memcmp(sa->cookies, arg->spi, ISAKMP_HDR_COOKIES_LEN) == 0)
		return 1;

	return 0;
}
开发者ID:repos-holder,项目名称:openbsd-patches,代码行数:26,代码来源:sa.c


注:本文中的SA_LEN函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。