Пример #1
0
void
usrv_task(intptr_t exinf)
{
	static uint8_t buffer[BUF_SIZE];

	T_IPV4EP	dst;
	T_IN4_ADDR	addr;
	ER_UINT		len;
	ID		tskid;

	get_tid(&tskid);
	addr = IPV4_ADDR_LOCAL;
	syslog(LOG_NOTICE, "[UDP ECHO SRV:%d,%d] started, IP Address: %s.", 
	                   tskid, (ID)exinf, ip2str(NULL, &addr));
	while (true) {
		if ((len = udp_rcv_dat((ID)exinf, &dst, buffer, sizeof(buffer), 20 * 1000)) >= 0) {
			buffer[len] = '\0';
			syslog(LOG_NOTICE, "[UDP ECHO SRV] recv, addr: %s:%d, len: %d, msg: '%s'",
			                   ip2str(NULL, &dst.ipaddr), dst.portno, len, buffer);
			if ((int)len > 0) {
				if ((len = udp_snd_dat((ID)exinf, &dst, buffer, len, TMO_FEVR)) < 0)
					syslog(LOG_NOTICE, "[UDP ECHO SRV] send, error: %s", itron_strerror(len));
				}
			}
		else
			syslog(LOG_NOTICE, "[UDP ECHO SRV] recv, error: %s", itron_strerror(len));
		}
	}
Пример #2
0
void dhcp_request(DHCP *dhcp)
{
    const UB request_ip[] = {0x32,0x04};
    ER       ercd;
    T_IPV4EP p_dstaddr;

    memset((VP)dhcp_packet, 0, sizeof(DHCP_PACKET));

    dhcp_packet->opecode        = 0x01;            /* OPコード */
    dhcp_packet->hard_addr      = 0x01;            /* ハードウェア・アドレス */
    dhcp_packet->hard_addr_len  = 0x06;            /* ハードウェア・アドレス長 */
    dhcp_packet->hop_count      = 0x00;            /* ホップ・カウント */
    dhcp_packet->transaction_id = htonl(TRANSACTION_ID);
    dhcp_packet->second         = htons(0x0000);   /* 経過時間 */
    dhcp_packet->dummy          = htons(0x0000);   /* フラグ */
    /* dhcp_packet->client_ip[4]; */               /* クライアント・アドレス */
    memcpy((VP)dhcp_packet->user_ip, (const VP)dhcp->ipaddr, 4);  /* ユーザIPアドレス */
    /* dhcp_packet->server_ip[4]; */               /* サーバIPアドレス */
    /* dhcp_packet->gateway_ip[4]; */              /* ゲートウェイIPアドレス */
    memcpy((VP)dhcp_packet->client_hard_addr, (const VP)dhcp->macaddr, 6);
                                                   /* クライアントMACアドレス */
    /* dhcp_packet->server_host_name[64]; */
    /* dhcp_packet->file_name[128]; */

    memcpy((VP)dhcp_packet->options.magic_cookie, (const VP)magic_cookie, 4);
                                                          /* Magic Cookie */
    memcpy((VP)dhcp_packet->options.message_type1, (const VP)message_and_length, 2);
                                                          /* DHCP Message */
    dhcp_packet->options.message_type2 = DHCP_REQUEST;    /* DHCP Request */
    memcpy((VP)dhcp_packet->options.client_id1, (const VP)client_identifier, 3);
                                                          /* Client Identifier, Ethernet */
    memcpy((VP)dhcp_packet->options.client_mac, (const VP)dhcp->macaddr, 6);
    memset((VP)dhcp_packet->options.dummy, 0, sizeof(dhcp_packet->options.dummy));
    memcpy((VP)dhcp_packet->options.dummy, (const VP)request_ip, 2);
    memcpy((VP)&dhcp_packet->options.dummy[2], (const VP)dhcp->ipaddr, 4);
    dhcp_packet->options.dummy[6] = 0xff;

    p_dstaddr.ipaddr = BROADCAST_ADDR;
    p_dstaddr.portno = BROADCAST_PORT;
    ercd = udp_snd_dat(UDP_CLIENT_CEPID, &p_dstaddr, (VP)&dhcp_packet->opecode,
               sizeof(DHCP_PACKET), TIMEOUT);
    syslog(LOG_NOTICE, "[dhcp_request] ercd = %d", ercd);
}
Пример #3
0
/* これはちゃんと動作しない */
ER dhcp_release(DHCP *dhcp)
{

    T_IPV4EP p_dstaddr;
    ER       ercd;

    memset((VP)dhcp_packet, 0, sizeof(DHCP_PACKET));

    dhcp_packet->opecode        = 0x01;
    dhcp_packet->hard_addr      = 0x01;
    dhcp_packet->hard_addr_len  = 0x06;
    dhcp_packet->hop_count      = 0x00;
    dhcp_packet->transaction_id = htonl(TRANSACTION_ID);
    dhcp_packet->second         = htons(0x0000);
    dhcp_packet->dummy          = htons(0x0000);
    /* dhcp_packet->client_ip[4]; */
    /* dhcp_packet->user_ip[4]; */
    /* dhcp_packet->server_ip[4]; */
    /* dhcp_packet->gateway_ip[4]; */
    memcpy((VP)dhcp_packet->client_hard_addr, (const VP)dhcp->macaddr, 6);
    /* dhcp_packet->server_host_name[64]; */
    /* dhcp_packet->file_name[128]; */

    memcpy((VP)dhcp_packet->options.magic_cookie, (const VP)magic_cookie, 4);
                                                    /* Magic Cookie */
    memcpy((VP)dhcp_packet->options.message_type1, (const VP)message_and_length, 2);
                                                    /* DHCP Message */
    dhcp_packet->options.message_type2 = DHCP_RELEASE;
    memcpy((VP)dhcp_packet->options.client_id1, (const VP)client_identifier, 3);
                                                    /* Client Identifier, Ethernet */
    memcpy((VP)dhcp_packet->options.client_mac, (const VP)dhcp->macaddr, 6);
    dhcp_packet->options.dummy[0]      = 0xff;

    p_dstaddr.ipaddr = str2ip(dhcp->dhcpsrvaddr);
    p_dstaddr.portno = BROADCAST_PORT;
    ercd = udp_snd_dat(UDP_CLIENT_CEPID, &p_dstaddr, (VP)&dhcp_packet->opecode,
             sizeof(DHCP_PACKET), TMO_NBLK);

    return ercd;
}
Пример #4
0
int cg_socket_sendto(CgSocket *sock, char *addr, int port, char *data, int dataLen)
{
#if defined(BTRON) || defined(TENGINE)
	struct sockaddr_in sockaddr;
#elif defined(ITRON)
	T_IPV4EP dstaddr;
	T_UDP_CCEP udpccep = { 0, { IPV4_ADDRANY, UDP_PORTANY }, (FP)cg_socket_udp_callback };
#else
	struct addrinfo *addrInfo;
#endif
	int sentLen;
	BOOL isBoundFlag;

	cg_log_debug_l4("Entering...\n");

	if (data == NULL)
		return 0;
	if (dataLen < 0)
		dataLen = cg_strlen(data);
	if (dataLen <= 0)
		return 0;

	isBoundFlag = cg_socket_isbound(sock);
	sentLen = -1;
#if defined(BTRON) || (defined(TENGINE) && !defined(CG_TENGINE_NET_KASAGO))
	if (cg_socket_tosockaddrin(addr, port, &sockaddr, TRUE) == FALSE)
		return -1;
	if (isBoundFlag == FALSE)
	   	cg_socket_setid(sock, so_socket(PF_INET, cg_socket_getrawtype(sock), 0));
	if (0 <= sock->id)
		sentLen = so_sendto(sock->id, (B*)data, dataLen, 0, (SOCKADDR*)&sockaddr, sizeof(struct sockaddr_in));
#elif defined(TENGINE) && defined(CG_TENGINE_NET_KASAGO)
	if (cg_socket_tosockaddrin(addr, port, &sockaddr, TRUE) == FALSE)
		return -1;
	if (isBoundFlag == FALSE) {
	   	cg_socket_setid(sock, ka_socket(PF_INET, cg_socket_getrawtype(sock), cg_socket_getprototype(sock)));
		cg_socket_setmulticastinterface(sock, NULL);
	}
	if (0 <= sock->id)
		sentLen = ka_sendto(sock->id, data, dataLen, 0, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr_in));
#elif defined(ITRON)
	if (isBoundFlag == FALSE) {
		cg_socket_setid(sock, cg_socket_getavailableid(cg_socket_issocketstream(sock)));
		if (sock->id < 0)
			return FALSE;
		if (udp_cre_cep(sock->id, &udpccep) != E_OK)
			return FALSE;
	}
	dstaddr.ipaddr = ascii_to_ipaddr(addr);
	dstaddr.portno = htons(port);
	sentLen = udp_snd_dat(sock->id, &dstaddr, data, dataLen, TMO_FEVR);
#else
	if (cg_socket_tosockaddrinfo(cg_socket_getrawtype(sock), addr, port, &addrInfo, TRUE) == FALSE)
		return -1;
	if (isBoundFlag == FALSE)
		cg_socket_setid(sock, socket(addrInfo->ai_family, addrInfo->ai_socktype, 0));
	
	/* Setting multicast time to live in any case to default */
	cg_socket_setmulticastttl(sock, CG_UPNP_SSDP_MULTICAST_DEFAULT_TTL);
	
	if (0 <= sock->id)
		sentLen = sendto(sock->id, data, dataLen, 0, addrInfo->ai_addr, addrInfo->ai_addrlen);
	freeaddrinfo(addrInfo);
#endif

	if (isBoundFlag == FALSE)
		cg_socket_close(sock);

#ifdef SOCKET_DEBUG
cg_log_debug_s("sentLen : %d\n", sentLen);
#endif

	cg_log_debug_l4("Leaving...\n");

	return sentLen;
}