Пример #1
0
/* syslog: a tx-only socket: no queue is there */
static struct wrpc_socket __static_syslog_socket = {
	.queue.buff = NULL,
	.queue.size = 0,
};
static struct wrpc_socket *syslog_socket;

static struct wr_udp_addr syslog_addr;
unsigned char syslog_mac[6];

static uint32_t tics, tics_zero;

void syslog_init(void)
{
	syslog_socket = ptpd_netif_create_socket(&__static_syslog_socket, NULL,
					       PTPD_SOCK_UDP, 514 /* time */);
	syslog_addr.sport = syslog_addr.dport = htons(514);
	tics_zero = timer_get_tics();
}

static int cmd_syslog(const char *args[])
{
	char b1[32], b2[32];

	if (args[0] && !strcmp(args[0], "off")) {
		syslog_addr.daddr = 0;
		return 0;
	}
	if (!args[1]) {
		pp_printf("use: syslog <ipaddr> <macaddr> (or just \"off\"\n");
		return -1;
	}
	decode_ip(args[0], (void *)&syslog_addr.daddr);
	decode_mac(args[1], syslog_mac);
	pp_printf("Syslog parameters: %s, %s\n",
		  format_ip(b1, (void *)&syslog_addr.daddr),
		  format_mac(b2, syslog_mac));
	tics = 0; /* send the first frame immediately to the new host */
	return 0;
}

DEFINE_WRC_COMMAND(syslog) = {
	.name = "syslog",
	.exec = cmd_syslog,
};

#define SYSLOG_DEFAULT_LEVEL 14 /* 8 == user + 6 ==info */
static int syslog_header(char *buf, int level, unsigned char ip[4])
{
	uint64_t secs;
	char b[32];
	int len;

	shw_pps_gen_get_time(&secs, NULL);
	getIP(ip);
	len = pp_sprintf(buf + UDP_END, "<%i> %s %s ", level,
			 format_time(secs, TIME_FORMAT_SYSLOG),
			 format_ip(b, ip));
	return len + UDP_END;
}
Пример #2
0
static int cmd_syslog(const char *args[])
{
	char b1[32], b2[32];

	if (args[0] && !strcmp(args[0], "off")) {
		syslog_addr.daddr = 0;
		return 0;
	}
	if (!args[1]) {
		pp_printf("use: syslog <ipaddr> <macaddr> (or just \"off\"\n");
		return -1;
	}
	decode_ip(args[0], (void *)&syslog_addr.daddr);
	decode_mac(args[1], syslog_mac);
	pp_printf("Syslog parameters: %s, %s\n",
		  format_ip(b1, (void *)&syslog_addr.daddr),
		  format_mac(b2, syslog_mac));
	tics = 0; /* send the first frame immediately to the new host */
	return 0;
}
Пример #3
0
void process_dhcpack( DHCPSessionInfo_s *info, DHCPPacket_s *in_packet, DHCPOption_s *options )
{
	DHCPOption_s *option;
	option = options;

	// Copy out relevent information from packet to info struct
	// The IP address which is being offered to us
	if( in_packet->yiaddr != 0 )
	{
		char* ip = format_ip(in_packet->yiaddr);

		memcpy(&info->yiaddr,&in_packet->yiaddr,4);
		debug(INFO,__FUNCTION__,"our IP is %s\n",ip);
		free(ip);
	}

	// The server name, if there is one
	if( strcmp(in_packet->sname,"") != 0)
	{
		memcpy(&info->sname,&in_packet->sname,64);
		debug(INFO,__FUNCTION__,"server name is %s\n",info->sname);
	}

	// Now get the lease time etc. options from the supplied options
	do
	{
		switch( option->type )
		{
			case OPTION_MESSAGE:
				break;

			case OPTION_SUBNET:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"subnet mask field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					char* mask;

					memcpy(&info->subnetmask,option->data,4);

					mask = format_ip(info->subnetmask);
					debug(INFO,__FUNCTION__,"subnet mask is %s\n",mask);
					free(mask);
				}
				else
					debug(WARNING,__FUNCTION__,"subnet mask option data is NULL\n");

				break;

			}

			case OPTION_SERVERID:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"server id field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					char* ip;

					memcpy(&info->siaddr,option->data,4);

					ip = format_ip(info->siaddr);
					debug(INFO,__FUNCTION__,"server id is %s\n",ip);
					free(ip);
				}
				else
					debug(WARNING,__FUNCTION__,"server id option data is NULL\n");

				break;
			}

			case OPTION_LEASETIME:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"lease time field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					memcpy(&info->lease_time,option->data,4);
					debug(INFO,__FUNCTION__,"lease time is %u\n",(unsigned int)info->lease_time);
				}
				else
					debug(WARNING,__FUNCTION__,"lease time option data is NULL\n");

				break;
			}

			case OPTION_T1_TIME:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"t1 time field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					memcpy(&info->t1_time,option->data,4);

					debug(INFO,__FUNCTION__,"t1 time is %i\n",(unsigned int)info->t1_time);
				}
				else
					debug(WARNING,__FUNCTION__,"t1 time option data is NULL\n");

				break;
			}

			case OPTION_T2_TIME:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"t2 time field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					memcpy(&info->t2_time,option->data,4);

					debug(INFO,__FUNCTION__,"t2 time is %i\n",(unsigned int)info->t2_time);
				}
				else
					debug(WARNING,__FUNCTION__,"t2 time option data is NULL\n");

				break;
			}

			default:	// This is an option that we do not support (Or do not care about)
			{
				debug(INFO,__FUNCTION__,"reply has an extra option id %.2i\n",option->type);
				break;
			}
		}

		// Move onto the next option entry and process it
		option = option->next;
	}
	while( option != NULL );
}
Пример #4
0
static void print_route( uint8* anIp, uint8* anMask, uint8* anGw, uint16 nFlags, bool bLong )
{
	char *pzIpAddr, *pzGateway = "";

	if( ( nFlags & RTF_GATEWAY ) != 0 )
	{
		if( anGw != NULL )
			pzGateway = format_ip( anGw );
		else
			pzGateway = "not specified";
	}

	if( bLong )
	{
		printf( "%-15s  %-15s  %-15s  0x%4.4hX\n", format_ip( anIp ), format_ip( anMask ), pzGateway, nFlags );
	}
	else
	{
		int nMaskBits = 0;
		unsigned long nIpAddr;
		char acDestBuf[20];
		char acFlags[16], *pcFlag = acFlags;
		struct flag {
			uint16 nFlag;
			char cFlag, cNotFlag;
		} *psFlag, FLAG_NAMES[] = {
			{ RTF_UP, 'U', 0 },
			{ RTF_HOST, 'H', 0 },
			{ RTF_GATEWAY, 'G', 0 },
			{ RTF_STATIC, 'S', 0 },
			{ RTF_DYNAMIC, 'D', 0 },
			{ 0, 0, 0 }
		};
		
		/* Work out flags */
		for( psFlag = FLAG_NAMES; psFlag->nFlag != 0; ++psFlag )
		{
			if( (nFlags & psFlag->nFlag) != 0 )
			{
				if( psFlag->cFlag != 0 )
					*pcFlag++ = psFlag->cFlag;
			}
			else
			{
				if( psFlag->cNotFlag != 0 )
					*pcFlag++ = psFlag->cNotFlag;
			}
		}
		
		*pcFlag = 0;
		
		/* Count bits in netmask */
		for( nIpAddr = *((unsigned long *)anMask); nIpAddr != 0; nIpAddr >>= 1 )
			nMaskBits += nIpAddr & 1;

		if( nMaskBits == 0 )
			strcpy( acDestBuf, "default" );
		else if( nMaskBits == 32 )
			strcpy( acDestBuf, format_ip( anIp ) );
		else
			sprintf( acDestBuf, "%s/%d", format_ip( anIp ), nMaskBits );

		printf( "%-18s  %-15s  %s\n", acDestBuf, pzGateway, acFlags );
	}
}
Пример #5
0
void process_dhcpoffer( DHCPSessionInfo_s *info, DHCPPacket_s *in_packet, DHCPOption_s *options )
{
	DHCPOption_s *option;
	option = options;

	// Copy out relevent information from packet to info struct

	// The DHCP server IP address, if it is supplied in the siaddr
	// field (Otherwise it should be supplied as a server id option)
	if( in_packet->siaddr != 0 )
	{
		char* ip = format_ip(in_packet->siaddr);

		memcpy(&info->siaddr,&in_packet->siaddr,4);
		debug(INFO,__FUNCTION__,"server IP is %s\n",ip);
		free(ip);

	}

	// The IP address which is being offered to us
	if( in_packet->yiaddr != 0 )
	{
		char* ip = format_ip(in_packet->yiaddr);

		memcpy(&info->yiaddr,&in_packet->yiaddr,4);
		debug(INFO,__FUNCTION__,"offered IP is %s\n",ip);
		free(ip);
	}

	// The relay agent address, if one is used on this network
	if( in_packet->giaddr != 0 )
	{
		char* ip = format_ip(in_packet->giaddr);

		memcpy(&info->giaddr,&in_packet->giaddr,4);
		debug(INFO,__FUNCTION__,"DHCP relay agent IP is %s\n",ip);
		free(ip);
	}

	// The server name, if there is one
	if( strcmp(in_packet->sname,"") != 0)
	{
		memcpy(&info->sname,&in_packet->sname,64);
		debug(INFO,__FUNCTION__,"server name is %s\n",info->sname);
	}

	// Now get the subnet mask, server identifier & the lease time
	// from the supplied options
	do
	{
		switch( option->type )
		{
			case OPTION_MESSAGE:
				break;

			case OPTION_SUBNET:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"subnet mask field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					char* mask;

					memcpy(&info->subnetmask,option->data,4);

					mask = format_ip(info->subnetmask);
					debug(INFO,__FUNCTION__,"subnet mask is %s\n",mask);
					free(mask);
				}
				else
					debug(WARNING,__FUNCTION__,"subnet mask option data is NULL\n");

				break;

			}

			case OPTION_SERVERID:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"server id field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					char* ip;

					memcpy(&info->siaddr,option->data,4);

					ip = format_ip(info->siaddr);
					debug(INFO,__FUNCTION__,"server id is %s\n",ip);
					free(ip);
				}
				else
					debug(WARNING,__FUNCTION__,"server id option data is NULL\n");

				break;
			}

			case OPTION_LEASETIME:
			{
				if( option->length != 4 )
					debug(WARNING,__FUNCTION__,"lease time field is NOT 4 bytes long\n");

				if( option->data != NULL )
				{
					memcpy(&info->lease_time,option->data,4);
					debug(INFO,__FUNCTION__,"lease time is %u\n",(unsigned int)info->lease_time);
				}
				else
					debug(WARNING,__FUNCTION__,"lease time option data is NULL\n");

				break;
			}

			case OPTION_DNSSERVERS:
			{
				int servers;

				servers = (option->length / 4);		// Must always be a multiple of 4
				info->dns_server_count = servers;

				if( option->data != NULL )
				{
					int current_server;

					info->dns_servers = (uint32*)malloc(servers * sizeof(uint32*));
					if( info->dns_servers == NULL )
					{
						debug(PANIC,__FUNCTION__,"could not allocate space for DNS server entries\n");
						break;
					}

					for( current_server=0; current_server < servers; current_server++)
						memcpy(&info->dns_servers[current_server],&option->data[current_server*4],4);

					for( current_server=0; current_server < servers; current_server++)
					{
						char* ip;
						ip = format_ip(info->dns_servers[current_server]);
						debug(INFO,__FUNCTION__,"got DNS server entry %s\n",ip);
						free(ip);
					}
				}
				else
					debug(WARNING,__FUNCTION__,"DNS servers option data is NULL\n");

				break;
			}

			case OPTION_ROUTERS:
			{
				int routers;

				routers = (option->length / 4);		// Must always be a multiple of 4
				info->router_count = routers;

				if( option->data != NULL )
				{
					int current_router;

					info->routers = (uint32*)malloc(routers * sizeof(uint32*));
					if( info->routers == NULL )
					{
						debug(PANIC,__FUNCTION__,"could not allocate space for router entries\n");
						break;
					}

					for( current_router=0; current_router < routers; current_router++)
						memcpy(&info->routers[current_router],&option->data[current_router*4],4);

					for( current_router=0; current_router < routers; current_router++)
					{
						char* ip;
						ip = format_ip(info->routers[current_router]);
						debug(INFO,__FUNCTION__,"got router entry %s\n",ip);
						free(ip);
					}
				}
				else
					debug(WARNING,__FUNCTION__,"router option data is NULL\n");

				break;
			}

			case OPTION_NTPSERVERS:
			{
				int servers;

				servers = (option->length / 4);		// Must always be a multiple of 4
				info->ntp_server_count = servers;

				if( option->data != NULL )
				{
					int current_server;

					info->ntp_servers = (uint32_t*)malloc(servers * sizeof(uint32_t*));
					if( info->ntp_servers == NULL )
					{
						debug(PANIC,__FUNCTION__,"could not allocate space for NTP server entries\n");
						break;
					}

					for( current_server=0; current_server < servers; current_server++)
						memcpy(&info->ntp_servers[current_server],&option->data[current_server*4],4);

					for( current_server=0; current_server < servers; current_server++)
					{
						char* ip;
						ip = format_ip(info->ntp_servers[current_server]);
						debug(INFO,__FUNCTION__,"got NTP server entry %s\n",ip);
						free(ip);
					}
				}
				else
					debug(WARNING,__FUNCTION__,"NTP servers option data is NULL\n");

				break;
			}


			default:	// This is an option that we do not support (Or do not care about)
			{
				debug(INFO,__FUNCTION__,"reply has an extra option id %.2i\n",option->type);
				break;
			}
		}

		// Move onto the next option entry and process it
		option = option->next;
	}
	while( option != NULL );
}