Example #1
0
/**
 * Encode an RTCP Generic NACK (GNACK) message
 *
 * @param mb  Buffer to encode into
 * @param pid Packet ID
 * @param blp Bitmask of following lost packets (BLP)
 *
 * @return 0 for success, otherwise errorcode
 */
int rtcp_rtpfb_gnack_encode(struct mbuf *mb, uint16_t pid, uint16_t blp)
{
	int err;
	err  = mbuf_write_u16(mb, htons(pid));
	err |= mbuf_write_u16(mb, htons(blp));
	return err;
}
Example #2
0
void udp_server::init()
{
	_sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (_sock < 0)
	{
		print_log("create socket error!", __FUNCTION__, __LINE__);
		exit(1);		
	}

	struct sockaddr_in local;
	socklen_t len = sizeof(local);
	local.sin_family = AF_INET;
	local.sin_port = htons(_port);
	if (_ip == "any")
	{
		local.sin_addr.s_addr = htons(INADDR_ANY);
	}
	else
	{
		local.sin_addr.s_addr = inet_addr(_ip.c_str());
	}

	if (bind(_sock, (struct sockaddr *)&local, len) < 0)
	{
		print_log(strerror(errno), __FUNCTION__, __LINE__);
		exit(2);
	}
}
/**
 * @brief 转换响应报文中的字节序
 * @param header 直接接收的,尚未转换字节序的响应报文头指针
 * @return       转换字节序后的响应报文头,与参数相同。
 */
CityResponseHeader *
response_ntoh(CityResponseHeader *header)
{
    header->type = htons(header->type);
    header->year = htons(header->year);
    return header;
}
Example #4
0
static void udp_send(char *addr_str, char *port_str, char *data)
{
    struct sockaddr_in6 src, dst;
    size_t data_len = strlen(data);
    uint16_t port;
    int s;
    src.sin6_family = AF_INET6;
    dst.sin6_family = AF_INET6;
    memset(&src.sin6_addr, 0, sizeof(src.sin6_addr));
    /* parse destination address */
    if (inet_pton(AF_INET6, addr_str, &dst.sin6_addr) != 1) {
        puts("Error: unable to parse destination address");
    }
    /* parse port */
    port = (uint16_t)atoi(port_str);
    dst.sin6_port = htons(port);
    src.sin6_port = htons(port);
    s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (s < 0) {
        puts("error initializing socket");
    }

    if (sendto(s, data, data_len, 0, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
	puts("could not send");
    }
    else {
	printf("Success: send %u byte to %s:%u\n", (unsigned)data_len, addr_str, port);
    }
    usleep(1000000);

    close(s);
}
// write given TL *before* position buf+offset, adjust offset and return len
int
ccnl_ccntlv_prependTL(unsigned short type, unsigned short len,
                      int *offset, unsigned char *buf)
{
// RFduino compiler has problems with casting pointers to unsigned short?
// Workaround:
    uint16_t tmp;
    buf += *offset;
    tmp = htons(len);
    memcpy(buf-2, &tmp, 2);
    tmp = htons(type);
    memcpy(buf-4, &tmp, 2);
    *offset -= 4;
    return 4;
/*
    unsigned short *ip;

    if (*offset < 4)
        return -1;
    ip = (unsigned short*) (buf + *offset - 2);
    *ip-- = htons(len);
    *ip = htons(type);
    *offset -= 4;
    return 4;
*/
}
Example #6
0
/**
 * Change the port of the socket address, if it maps with an entry.
 * Return 0 if no relevant entry is found, and 1 if the port has been changed.
 */
int change_inet6_socket_port(Tracee *tracee, Config *config, word_t sockfd, struct sockaddr_in6 *sockaddr, bool bind_mode) {
	uint16_t port_in, port_out;

	port_in = sockaddr->sin6_port;
	port_out = get_port(&config->portmap, port_in);

	if(port_out == PORTMAP_DEFAULT_VALUE) {
		if (bind_mode && config->netcoop_mode && !config->need_to_check_new_port) {
			VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 netcoop mode with: %d", htons(port_in));
			sockaddr->sin6_port = 0; // the system will assign an available port
			config->old_port = port_in; // we keep this one for adding a new entry
			config->need_to_check_new_port = true;
			config->sockfd = sockfd;
			return 1;
		}

		VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 port ignored: %d ", htons(port_in));
		return 0;
	}

	sockaddr->sin6_port = port_out;
	VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 port translation: %d -> %d (NOT GUARANTEED: bind might still fail on target port)", htons(port_in), htons(port_out));

	return 1;
}
Example #7
0
void
btv_send_msg_thru_eth(const unsigned char *buf, int size)
{
    struct socket *sock[NUM_OF_SOCKS];
    struct sockaddr ksockaddr[NUM_OF_SOCKS] = {0};
    struct msghdr msg;
    struct kvec   iov;

    printk("Send Message Thru Eth\n");

    for (;;) {

    /* To communicate TVSOC, we have to prepare two sockets for sync and async ports */

    sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock[SOCK_FOR_SYNC]);
    sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock[SOCK_FOR_ASYNC]);

    /* initialize socket for sync command */
    ksockaddr[SOCK_FOR_SYNC].sa_family      = PF_INET;
    /* sockaddr.sin_addr.s_addr = inet_addr((const char*)("198.18.186.199")); */
    //*((unsigned short *)&(ksockaddr.sa_data[0])) = 0x409c; /* 0x9c40:40000 */
    *((unsigned short *)&(ksockaddr[SOCK_FOR_SYNC].sa_data[0])) = htons(40000); /* 0x9c40:40000 */
    //*((unsigned int   *)&(ksockaddr.sa_data[2])) = 0x0b9c082b; /*  0x2b:43 0x08:08 0x9c:156 0x0b:11 */
    *((unsigned int   *)&(ksockaddr[SOCK_FOR_SYNC].sa_data[2])) = 0xc7ba12c6; /*  0xc6:198 0x12:18 0xba:186 0xc7:199 */

    /* initialize socket for async command */
    ksockaddr[SOCK_FOR_ASYNC].sa_family      = PF_INET;
    *((unsigned short *)&(ksockaddr[SOCK_FOR_ASYNC].sa_data[0])) = htons(40001); /* 0x9d40:40001 */
    //*((unsigned int   *)&(ksockaddr.sa_data[2])) = 0x0b9c082b; /*  0x2b:43 0x08:08 0x9c:156 0x0b:11 */
    *((unsigned int   *)&(ksockaddr[SOCK_FOR_ASYNC].sa_data[2])) = 0xc7ba12c6; /*  0xc6:198 0x12:18 0xba:186 0xc7:199 */



    while(kernel_connect(sock[SOCK_FOR_SYNC], (struct sockaddr *)&ksockaddr[SOCK_FOR_SYNC], sizeof(struct sockaddr), 0) < 0) {
        printk("Cannot connect server sync port, Retry \n");
        mdelay(REBOOT_CMD_INTERVAL_MSEC);
	}
    while(kernel_connect(sock[SOCK_FOR_ASYNC], (struct sockaddr *)&ksockaddr[SOCK_FOR_ASYNC], sizeof(struct sockaddr), 0) < 0) {
        printk("Cannot connect server async port, Retry \n");
        mdelay(REBOOT_CMD_INTERVAL_MSEC);
    }

    iov.iov_base = (void *)buf;
    iov.iov_len = ETH_CMD_SIZE;

    msg.msg_name = (struct sockaddr *)&ksockaddr[SOCK_FOR_SYNC];
    msg.msg_namelen = sizeof(struct sockaddr);
    msg.msg_iov = (struct iovec *) &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = NULL;

    kernel_sendmsg(sock[SOCK_FOR_SYNC], &msg, &iov, 1, ETH_CMD_SIZE);

	sock_release(sock[SOCK_FOR_SYNC]);
	sock_release(sock[SOCK_FOR_ASYNC]);

    mdelay(ETHER_CMD_INTERVAL);
	}
    /* NOREACHED */
}
Example #8
0
size_t
ofl_structs_meter_band_pack(struct ofl_meter_band_header *src, struct ofp_meter_band_header *dst){

    dst->type = htons(src->type);
    dst->rate = htonl(src->rate);
    dst->burst_size = htonl(src->burst_size);
    switch (src->type) {
        case OFPMBT_DROP:{
            struct ofp_meter_band_drop *di = (struct ofp_meter_band_drop *)dst;
            di->len = htons(sizeof(struct ofp_meter_band_drop));
            memset(di->pad, 0x0, 4);
            return sizeof(struct ofp_meter_band_drop);
        }
        case OFPMBT_DSCP_REMARK:{
            struct ofl_meter_band_dscp_remark *si = (struct ofl_meter_band_dscp_remark*)src;
            struct ofp_meter_band_dscp_remark *di = (struct ofp_meter_band_dscp_remark *)dst;
            di->len = htons(sizeof(struct ofp_meter_band_dscp_remark));
            di->prec_level = si->prec_level;
            memset(di->pad,0x0,3);
            return sizeof(struct ofp_meter_band_dscp_remark);
        }
        case OFPMBT_EXPERIMENTER:{
            struct ofl_meter_band_experimenter *si = (struct ofl_meter_band_experimenter*)src;
            struct ofp_meter_band_experimenter *di = (struct ofp_meter_band_experimenter *)dst;
            di->len = htons(sizeof(struct ofp_meter_band_experimenter));
            di->experimenter = htonl(si->experimenter);
            return sizeof(struct ofp_meter_band_experimenter);
        }
        default:
            OFL_LOG_WARN(LOG_MODULE, "Trying to pack unknown meter band.");
            return 0;
    }
}
Example #9
0
size_t
ofl_structs_bucket_pack(struct ofl_bucket *src, struct ofp_bucket *dst, struct ofl_exp *exp) {
    size_t total_len, rem, align, len;
    uint8_t *data;
    size_t i;

    total_len = sizeof(struct ofp_bucket) + ofl_actions_ofp_total_len(src->actions, src->actions_num, exp);
    /* Note: buckets are 64 bit aligned according to spec 1.1 draft 3 */
    rem = total_len % 8;
    align = rem == 0 ? 0 : (8-rem);
    total_len += align;

    dst->len = htons(total_len);
    dst->weight = htons(src->weight);
    dst->watch_port = htonl(src->watch_port);
    dst->watch_group = htonl(src->watch_group);
    memset(dst->pad, 0x00, 4);

    data = (uint8_t *)dst + sizeof(struct ofp_bucket);

    for (i=0; i<src->actions_num; i++) {
        len = ofl_actions_pack(src->actions[i], (struct ofp_action_header *)data, data, exp);
        data += len;
    }

    memset(data, 0x00, align);

    return total_len;
}
Example #10
0
int
isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
{

    int written = 1;
    struct sockaddr_ll sa;

    stream_set_getp (circuit->snd_stream, 0);
    memset (&sa, 0, sizeof (struct sockaddr_ll));
    sa.sll_family = AF_PACKET;
    sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
    sa.sll_ifindex = circuit->interface->ifindex;
    sa.sll_halen = ETH_ALEN;
    if (level == 1)
        memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
    else
        memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);


    /* lets try correcting the protocol */
    sa.sll_protocol = htons (0x00FE);
    written = sendto (circuit->fd, circuit->snd_stream->data,
                      stream_get_endp (circuit->snd_stream), 0,
                      (struct sockaddr *) &sa,
                      sizeof (struct sockaddr_ll));

    return ISIS_OK;
}
Example #11
0
static unsigned char *
agent_set_rewrite(struct vr_interface *vif, struct vr_packet *pkt,
        unsigned char *rewrite, unsigned short len)
{
    unsigned char *head;
    unsigned int hdr_len;
    struct agent_hdr *hdr;

    vr_preset(pkt);

    hdr_len = sizeof(struct agent_hdr) + len;
    if (pkt_head_space(pkt) < hdr_len) {
        pkt = vr_pexpand_head(pkt, hdr_len - pkt_head_space(pkt));
        if (!pkt)
            return NULL;
    }

    head = pkt_push(pkt, hdr_len);
    if (!head)
        return NULL;

    /* copy the rewrite first */
    memcpy(head, rewrite, len);

    hdr = (struct agent_hdr *)(head + len);
    hdr->hdr_ifindex = htons(pkt->vp_if->vif_idx);
    hdr->hdr_vrf = htons(pkt->vp_if->vif_vrf);
    /* this needs some thought */
    hdr->hdr_cmd = htons(AGENT_TRAP_NEXTHOP);
    hdr->hdr_cmd_param = 0;

    return head;
}
Example #12
0
/* Send a LE_ARP_RESPONSE for a LAN destination (MAC address) when
 * the LAN destination is present in kernel bridging table and we
 * are acting as a proxy lane client
 */
static void send_proxy_arp_rsp(unsigned char *target_mac, uint32_t tran_id,
                               uint16_t lec_id, unsigned char *src_atm_addr)
{
    struct ctrl_frame *frame;
    int frame_size;

    frame_size = sizeof(struct ctrl_frame) + lec_params.sizeoftlvs;
    frame = malloc(frame_size);
    if (frame == NULL) return;
    memset(frame, 0, frame_size);
    prefill_frame(frame, LE_ARP_RSP);
    frame->header.tran_id = tran_id;
    frame->header.lec_id = lec_id;
    frame->header.flags = htons(REMOTE_ADDRESS);
    memcpy(frame->target_atm_addr, lec_params.c1n_my_atm_addr, ATM_ESA_LEN);
    frame->target_lan_dst.tag = htons(LAN_DST_MAC_ADDR);
    memcpy(frame->target_lan_dst.mac, target_mac, ETH_ALEN);
    memcpy(frame->src_atm_addr, src_atm_addr, ATM_ESA_LEN);
    frame->num_tlvs = lec_params.num_tlvs;
    if (lec_params.num_tlvs > 0)
        memcpy(frame + 1, lec_params.tlvs, lec_params.sizeoftlvs);

    if (send_frame(lec_params.ctrl_direct, frame, frame_size) < 0)
        diag(COMPONENT, DIAG_ERROR, "send_proxy_arp_rsp: send_frame() failed");
    free(frame);

    return;
}
// This is the standard tcp connection in just one function
int tcp_connect( unsigned long int addr , int port )
{
struct sockaddr_in host;
int fd;

        if(( fd = socket( PF_INET , SOCK_STREAM , IPPROTO_TCP )) == -1 )
		do_exit( "tcp_connect(): socket()" , __FILE__ , __LINE__ );

        host.sin_family = AF_INET;
        host.sin_addr.s_addr = INADDR_ANY;
        host.sin_port = htons( LOCAL_PORT );

        if(( bind( fd , ( struct sockaddr *)&host , sizeof( struct sockaddr ))) == -1 )
		do_exit( "tcp_connect(): bind()" , __FILE__ , __LINE__ );

        bzero( &host , sizeof( struct sockaddr_in ));
        host.sin_family = AF_INET;
        host.sin_addr.s_addr = addr;
        host.sin_port = htons( port );

	if(( connect( fd , ( struct sockaddr *)&host , sizeof( struct sockaddr ))) == -1 )
		do_exit( "tcp_connect(): connect()" , __FILE__ , __LINE__ );

	return fd;
}
Example #14
0
void init_rawsockets(router_state* rs) {
	struct sr_instance* sr = (struct sr_instance*)rs->sr;
	int base = atoi(&(sr->interface[4]));

	char iface_name[32] = "nf2c";
	int i;
	for (i = 0; i < 4; ++i) {
		sprintf(&(iface_name[4]), "%i", base+i);
		int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

		struct ifreq ifr;
		bzero(&ifr, sizeof(struct ifreq));
		strncpy(ifr.ifr_ifrn.ifrn_name, iface_name, IFNAMSIZ);
		if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
			perror("ioctl SIOCGIFINDEX");
			exit(1);
		}

		struct sockaddr_ll saddr;
		bzero(&saddr, sizeof(struct sockaddr_ll));
		saddr.sll_family = AF_PACKET;
		saddr.sll_protocol = htons(ETH_P_ALL);
		saddr.sll_ifindex = ifr.ifr_ifru.ifru_ivalue;

		if (bind(s, (struct sockaddr*)(&saddr), sizeof(saddr)) < 0) {
			perror("bind error");
			exit(1);
		}

		rs->raw_sockets[i] = s;
	}
}
Example #15
0
File: ipip.c Project: 7799/linux
static int
ipip_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	int err = 0;
	struct ip_tunnel_parm p;

	if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
		return -EFAULT;

	if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
		if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
		    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
			return -EINVAL;
	}

	p.i_key = p.o_key = p.i_flags = p.o_flags = 0;
	if (p.iph.ttl)
		p.iph.frag_off |= htons(IP_DF);

	err = ip_tunnel_ioctl(dev, &p, cmd);
	if (err)
		return err;

	if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
		return -EFAULT;

	return 0;
}
Example #16
0
unsigned char* make_arp_packet(device_info dev_info, u_char dest_last_addr)
{
	static unsigned char pack_data[42];
	etherhdr_t et_hdr;
	arphdr_t arp_hdr;

	/*ethernet*/
	memset(&et_hdr, 0xff, 6);			//et_hdr.h_dest[]
	memcpy((unsigned char*)&et_hdr+6, &dev_info, 6);	//et_hdr.h_source[]

	et_hdr.h_proto = htons(0x0806);
	
	/*arp*/
	memset(&arp_hdr, 0x00, sizeof(arphdr_t));		//init
	arp_hdr.htype = htons(0x0001);
	arp_hdr.ptype = htons(0x0800);
	arp_hdr.oper = htons(ARP_REQUEST);
	arp_hdr.hlen = 0x06;
	arp_hdr.plen = 0x04;
	memcpy((unsigned char*)&arp_hdr+8, &dev_info, 6);		//arp_hdr.sha[]
	memcpy((unsigned char*)&arp_hdr+14, (unsigned char*)&dev_info+6, 4);	//arp_hdr.sha[]
	memcpy((unsigned char*)&arp_hdr+24, (unsigned char*)&dev_info+6, 3);	//arp_hdr.tpa[3] 까지

	arp_hdr.tpa[3] = dest_last_addr;

	memset(pack_data, 0, sizeof(pack_data));
	memcpy(pack_data, &et_hdr, 14);
	memcpy(pack_data+14, &arp_hdr, 28);

	return pack_data;
}
Example #17
0
File: ipip.c Project: 7799/linux
static void ipip_netlink_parms(struct nlattr *data[],
			       struct ip_tunnel_parm *parms)
{
	memset(parms, 0, sizeof(*parms));

	parms->iph.version = 4;
	parms->iph.protocol = IPPROTO_IPIP;
	parms->iph.ihl = 5;

	if (!data)
		return;

	if (data[IFLA_IPTUN_LINK])
		parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);

	if (data[IFLA_IPTUN_LOCAL])
		parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);

	if (data[IFLA_IPTUN_REMOTE])
		parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);

	if (data[IFLA_IPTUN_TTL]) {
		parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
		if (parms->iph.ttl)
			parms->iph.frag_off = htons(IP_DF);
	}

	if (data[IFLA_IPTUN_TOS])
		parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);

	if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
		parms->iph.frag_off = htons(IP_DF);
}
Example #18
0
int getStorageStatus(STORAGE_INFO_T *st)
{
	memset(st, sizeof(st), 0);

	if(nvram_get_int("sw_mode")!=SW_MODE_ROUTER) {
		return 0;
	}

	st->MagicWord = EXTEND_MAGIC;
	st->ExtendCap = EXTEND_CAP_WEBDAV;
	if(nvram_get_int("enable_webdav")) 	
		st->u.wt.EnableWebDav = 1;
	else
		st->u.wt.EnableWebDav = 0;

	st->u.wt.HttpType = nvram_get_int("st_webdav_mode");
	st->u.wt.HttpPort = htons(nvram_get_int("webdav_http_port"));
	st->u.wt.HttpsPort = htons(nvram_get_int("webdav_https_port"));

	if(nvram_get_int("ddns_enable_x")) {
		st->u.wt.EnableDDNS = 1;
		snprintf(st->u.wt.HostName, sizeof(st->u.wt.HostName), nvram_safe_get("ddns_hostname_x"));
	}
	else {
		st->u.wt.EnableDDNS = 0;
	}

	// setup st->u.WANIPAddr
	st->u.wt.WANIPAddr = inet_network(get_wanip());

	st->u.wt.WANState = get_wanstate(); 
	st->u.wt.isNotDefault = nvram_get_int("x_Setting");
	return 0;
}
Example #19
0
bool STcpSender::connect( const char * serverIP, const int serverPort )
{
	if (serverIP==NULL || serverPort<=0)
		return false;

	SOCKET sock;
	// Create a new socket to make a client connection.
	if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))== INVALID_SOCKET)
	{
		printf("create socket failed with error %d\n", WSAGetLastError());	
		return false;
	}

	SOCKADDR_IN          serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(serverPort);    
	serverAddr.sin_addr.s_addr = inet_addr(serverIP);

	if (::connect(sock, (SOCKADDR *) &serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
	{
		printf("connect to %s:%d failed with error %d\n", inet_ntoa(serverAddr.sin_addr), htons(serverAddr.sin_port),
			WSAGetLastError());
		closesocket(sock);
		return false;
	} 
	//关闭掉原来的连接
	this->close();
	//更新变量
	_serverIP = serverIP;
	_serverPort = serverPort;
	_sock = sock;
	_isConnected =true;

	return true;
}
Example #20
0
int swHttp2_send_setting_frame(swProtocol *protocol, swConnection *conn)
{
    char setting_frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_SETTING_OPTION_SIZE * 3];
    char *p = setting_frame;
    uint16_t id;
    uint32_t value;

    swHttp2_set_frame_header(p, SW_HTTP2_TYPE_SETTINGS, SW_HTTP2_SETTING_OPTION_SIZE * 3, 0, 0);
    p += SW_HTTP2_FRAME_HEADER_SIZE;

    id = htons(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
    memcpy(p, &id, sizeof(id));
    value = htonl(SW_HTTP2_MAX_CONCURRENT_STREAMS);
    memcpy(p + 2, &value, sizeof(value));
    p += SW_HTTP2_SETTING_OPTION_SIZE;

    id = htons(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE);
    memcpy(p, &id, sizeof(id));
    value = htonl(SW_HTTP2_DEFAULT_WINDOW_SIZE);
    memcpy(p + 2, &value, sizeof(value));
    p += SW_HTTP2_SETTING_OPTION_SIZE;

    id = htons(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE);
    memcpy(p, &id, sizeof(id));
    value = htonl(SW_HTTP2_MAX_FRAME_SIZE);
    memcpy(p + 2, &value, sizeof(value));

    return swConnection_send(conn, setting_frame, sizeof(setting_frame), 0);
}
Example #21
0
static void
test_util_format_unaligned_accessors(void *ignored)
{
  (void)ignored;
  char buf[9] = "onionsoup"; // 6f6e696f6e736f7570

  tt_u64_op(get_uint64(buf+1), OP_EQ, htonll(U64_LITERAL(0x6e696f6e736f7570)));
  tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
  tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
  tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);

  set_uint8(buf+7, 0x61);
  tt_mem_op(buf, OP_EQ, "onionsoap", 9);

  set_uint16(buf+6, htons(0x746f));
  tt_mem_op(buf, OP_EQ, "onionstop", 9);

  set_uint32(buf+1, htonl(0x78696465));
  tt_mem_op(buf, OP_EQ, "oxidestop", 9);

  set_uint64(buf+1, htonll(U64_LITERAL(0x6266757363617465)));
  tt_mem_op(buf, OP_EQ, "obfuscate", 9);
 done:
  ;
}
Example #22
0
struct ip *
traceroute__init_outip(struct traceroute *t)
{
	u_short off = 0;

	if (t->outip == NULL)
		free(t->outip);

	t->outip = (struct ip *)malloc((unsigned)t->packlen);
	if (t->outip == NULL)
		return t->outip;

	memset((char *)t->outip, 0, t->packlen);

	t->outip->ip_v = IPVERSION;
#ifdef BYTESWAP_IP_HDR
	t->outip->ip_len = htons(packlen);
	t->outip->ip_off = htons(off);
#else
	t->outip->ip_len = t->packlen;
	t->outip->ip_off = off;
#endif
	t->outip->ip_p = t->proto->num;
	t->outp = (u_char *)(t->outip + 1);
	t->outip->ip_dst = t->to->sin_addr;

	t->outip->ip_hl = (t->outp - (u_char *)t->outip) >> 2;

	t->outip->ip_src = t->from->sin_addr;
	return t->outip;
}
Example #23
0
/* RFC4666 Ch. 3.8.2. Notify */
struct xua_msg *m3ua_encode_notify(const struct osmo_xlm_prim_notify *npar)
{
	struct xua_msg *xua = xua_msg_alloc();
	uint32_t status;

	xua->hdr = XUA_HDR(M3UA_MSGC_MGMT, M3UA_MGMT_NTFY);

	status = M3UA_NOTIFY(htons(npar->status_type), htons(npar->status_info));
	/* cannot use xua_msg_add_u32() as it does endian conversion */
	xua_msg_add_data(xua, M3UA_IEI_STATUS, sizeof(status), (uint8_t *) &status);

	/* Conditional: ASP Identifier */
	if (npar->presence & NOTIFY_PAR_P_ASP_ID)
		xua_msg_add_u32(xua, M3UA_IEI_ASP_ID, npar->asp_id);

	/* Optional Routing Context */
	if (npar->presence & NOTIFY_PAR_P_ROUTE_CTX)
		xua_msg_add_u32(xua, M3UA_IEI_ROUTE_CTX, npar->route_ctx);

	/* Optional: Info String */
	if (npar->info_string)
		xua_msg_add_data(xua, M3UA_IEI_INFO_STRING,
				 strlen(npar->info_string)+1,
				 (uint8_t *) npar->info_string);

	return xua;
}
Example #24
0
static size_t
make_duid(unsigned char *duid, const struct interface *ifp, uint16_t type)
{
    unsigned char *p;
    uint16_t u16;
    time_t t;
    uint32_t u32;

    p = duid;
    u16 = htons(type);
    memcpy(p, &u16, 2);
    p += 2;
    u16 = htons(ifp->family);
    memcpy(p, &u16, 2);
    p += 2;
    if (type == DUID_LLT) {
        /* time returns seconds from jan 1 1970, but DUID-LLT is
         * seconds from jan 1 2000 modulo 2^32 */
        t = time(NULL) - DUID_TIME_EPOCH;
        u32 = htonl(t & 0xffffffff);
        memcpy(p, &u32, 4);
        p += 4;
    }
    /* Finally, add the MAC address of the interface */
    memcpy(p, ifp->hwaddr, ifp->hwlen);
    p += ifp->hwlen;
    return p - duid;
}
void open_UDP_socket() {
/** https://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html */
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
//inet_aton("192.168.0.164", &sa.sin_addr.s_addr);
sa.sin_addr.s_addr = htonl(odbiornik_IP);
sa.sin_port = htons(odbiornik_port);
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (-1 == sock) /* if socket failed to initialize, exit */
{
g_print("Error Creating Socket\n");
exit(EXIT_FAILURE);
}

struct sockaddr_in myaddr;
memset(&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
//inet_aton("192.168.1.100", &myaddr.sin_addr);
myaddr.sin_addr.s_addr = htonl(nadajnik_IP);
myaddr.sin_port = htons(6000);

if (-1 == bind(sock, (struct sockaddr *) &myaddr, sizeof(struct sockaddr)))
{
g_print("error bind failed %d\n", errno);
close(sock);
exit(EXIT_FAILURE);
}
iochan = g_io_channel_unix_new(sock);
int iowatch = -1;
iowatch = g_io_add_watch(iochan, G_IO_IN, feedback_event, NULL);
g_print("UDP socket open\n");
}
Example #26
0
/**
 * eth_header - create the Ethernet header
 * @skb:	buffer to alter
 * @dev:	source device
 * @type:	Ethernet type field
 * @daddr: destination address (NULL leave destination address)
 * @saddr: source address (NULL use device source address)
 * @len:   packet length (<= skb->len)
 *
 *
 * Set the protocol type. For a packet of type ETH_P_802_3/2 we put the length
 * in here instead.
 */
int eth_header(struct sk_buff *skb, struct net_device *dev,
	       unsigned short type,
	       const void *daddr, const void *saddr, unsigned int len)
{
	struct ethhdr *eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);

	if (type != ETH_P_802_3 && type != ETH_P_802_2)
		eth->h_proto = htons(type);
	else
		eth->h_proto = htons(len);

	/*
	 *      Set the source hardware address.
	 */

	if (!saddr)
		saddr = dev->dev_addr;
	memcpy(eth->h_source, saddr, ETH_ALEN);

	if (daddr) {
		memcpy(eth->h_dest, daddr, ETH_ALEN);
		return ETH_HLEN;
	}

	/*
	 *      Anyway, the loopback-device should never use this function...
	 */

	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
		eth_zero_addr(eth->h_dest);
		return ETH_HLEN;
	}

	return -ETH_HLEN;
}
/**
 * Compute a type map message for this peer.
 *
 * @return this peers current type map message.
 */
struct GNUNET_MessageHeader *
GSC_TYPEMAP_compute_type_map_message ()
{
  char *tmp;
  uLongf dlen;
  struct GNUNET_MessageHeader *hdr;

#ifdef compressBound
  dlen = compressBound (sizeof (my_type_map));
#else
  dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
  tmp = (char *) &hdr[1];
  if ((Z_OK !=
       compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
                  sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
  {
    dlen = sizeof (my_type_map);
    memcpy (tmp, &my_type_map, sizeof (my_type_map));
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
  }
  else
  {
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
  }
  hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
  return hdr;
}
Example #28
0
int test_sock_connect(void)
{
	struct sockaddr_in addr;
	int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	socklen_t addrlen = sizeof(struct sockaddr_in);
	if (s < 0) {
		perror("socket");
		return 1;
	}
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = 0;
	addr.sin_port = 0;
	if (bind(s, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
		perror("bind");
		return 1;
	}
	getsockname(s, (struct sockaddr *)&addr, &addrlen);
	if (listen(s, 5) < 0) {
		perror("listen");
		return 1;
	}
	if (sock_connect("127.0.0.1", htons(addr.sin_port)) < 0) {
		return 1;
	}
	if (sock_connect("localhost", htons(addr.sin_port)) < 0) {
		return 1;
	}
	return 0;
}
Example #29
0
bool IcmpLayer::setEchoData(IcmpMessageType echoType, uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data, size_t dataLen)
{
	if (!cleanIcmpLayer())
		return false;

	if (!this->extendLayer(m_DataLen, sizeof(icmp_echo_hdr) - sizeof(icmphdr) + dataLen))
		return false;

	getIcmpHeader()->type = (uint8_t)echoType;

	icmp_echo_request* header = NULL;
	if (echoType == ICMP_ECHO_REQUEST)
		header = getEchoRequestData();
	else if (echoType == ICMP_ECHO_REPLY)
		header = (icmp_echo_request*)getEchoReplyData();
	else
		return false;

	header->header->code = 0;
	header->header->checksum = 0;
	header->header->id = htons(id);
	header->header->sequence = htons(sequence);
	header->header->timestamp = timestamp;
	if (data != NULL && dataLen > 0)
		memcpy(header->data, data, dataLen);

	return true;
}
Example #30
0
/*
 * Write an L2T entry.  Must be called with the entry locked.
 * The write may be synchronous or asynchronous.
 */
int
t4_write_l2e(struct l2t_entry *e, int sync)
{
	struct sge_wrq *wrq;
	struct adapter *sc;
	struct wrq_cookie cookie;
	struct cpl_l2t_write_req *req;
	int idx;

	mtx_assert(&e->lock, MA_OWNED);
	MPASS(e->wrq != NULL);

	wrq = e->wrq;
	sc = wrq->adapter;

	req = start_wrq_wr(wrq, howmany(sizeof(*req), 16), &cookie);
	if (req == NULL)
		return (ENOMEM);

	idx = e->idx + sc->vres.l2t.start;
	INIT_TP_WR(req, 0);
	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, idx |
	    V_SYNC_WR(sync) | V_TID_QID(e->iqid)));
	req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync));
	req->l2t_idx = htons(idx);
	req->vlan = htons(e->vlan);
	memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));

	commit_wrq_wr(wrq, req, &cookie);

	if (sync && e->state != L2T_STATE_SWITCHING)
		e->state = L2T_STATE_SYNC_WRITE;

	return (0);
}