[En-Nut-Discussion] Small WINS utility for the community

Zodianet zodianet at wanadoo.fr
Thu Jan 15 18:17:31 CET 2004


Hello,
if you prefer to type http://myboard  or even directy 'myboard' instead
http://192.168.0.xxx to address your Ethernut board, NETBIOS WINS is for
you. The well known UDP Port 137 is used for this. Only works on switched
LAN (Hubs, Switchs, Personal broadband gateway like SMC, Netgear etc...) not
WAN/internet (layer 3 routed).
Jean Pierre


You have only to launch :
THREAD(wins_deamon, arg)
{
NutWinsPutIp  (  "myboard", inet_addr(MYIP) ) ;
}

/* ********************************************************* */
/* 
Small Netbios WINS (RFC 1002 / www.ietf.org) Name Query. 
int NutWinsPutIp  (  u_char * name, u_long ipaddr ) Routine. 
- Only "Query Request Client Routine sending" / "Positive Name Query
Response receiving"
are taken into account. 
- When the Netbios Name Query request UDP datagram is on the ethernet
network asking 
"Who is 'name'?", NutWinsPutIp process answers with the specified 'ipaddr'
Ethernut IP address. 
- Answer to Microsoft Windows/Internet Explorer calls by "http://name"
command line
(and even directly "name" as command line when "name" is not a Windows
shared folder).  
Author: Jean Pierre Gauthier
*/
/* ********************************************************* */

#include <string.h>

#include <sys/device.h>
#include <sys/timer.h>
#include <sys/heap.h>

#include <arpa/inet.h>
#include <net/if_var.h>
#include <netinet/sostream.h>

#ifdef NUTDEBUG
#include <stdio.h>
#endif

extern u_char toupper (u_char);
/* ********************************************************* */
typedef struct { 
	u_short id;
	u_short flags;
	u_short quests;
	u_short answers;
	u_short authrr;
	u_short addrr;
	u_char  namelen;
	u_char  name[33];
	u_short type;
	u_short class; /* end of request */
	u_long  ttl;
	u_short len_rep;
	u_char  node_flags;
	u_char  node_type;
	u_long  ip_addr ; /* end of answer*/
} 
WINSHEADER;


/* ********************************************************* */
int NutWinsPutIp  (  u_char * name, u_long ipaddr ) 
{ /* name : netbios label (15 chars max), ipaddr : network ordered IP
address bytes */
	WINSHEADER *pkt=NULL;
	UDPSOCKET *sock;
	u_long raddr;
	u_short rport;
	u_char *encoded = NULL;
	u_char car ;
	int i,j;
	/* printf("Name is %s \r\n",name); */
	/*
	* Create client socket and allocate
	* a buffer for the UDP packet.
	*/
	if (( (pkt = NutHeapAllocClear(sizeof(WINSHEADER))) == NULL) ||
		    ((encoded = NutHeapAllocClear(33)) == NULL) ||
		    ((sock = NutUdpCreateSocket(137)) == 0)||  /* NETBIOS
UDP port */
		((ipaddr == 0) || (*name==0))
		    )
	{
		if (pkt != NULL)
			NutHeapFree(pkt);
		if (encoded != NULL)
			NutHeapFree(encoded);
		return -1;
	}
	if ( strlen(name) > 15)
		return -1;
	j= 0;
	for (i=0; i<16; i++) /* label  'compression' */
	{
		car= toupper (i < strlen(name) ? name[i] : ' ') ;
		if (i==15)
			car = 0;
		encoded[j] = (car >>4) + 'A' ;
		encoded[j+1] = (car & 0xf) + 'A' ;
		j += 2;
	}
	encoded[j] = 0;
	/* printf("local compressed name=\n%s \n",encoded); */
	for(;;)   /* infinite loop / Netbios deamon */
	{
		NutUdpReceiveFrom(sock, &raddr, &rport, pkt,
sizeof(WINSHEADER), 0);
		/* RFC1002 Name Query Request verification */
		if   ( ((ntohs(pkt->flags) & 0xf800) != 0) || 
			    (ntohs(pkt->quests) != 1) || 
			    (pkt->namelen != 0x20)  ||
			    (ntohs(pkt->type) != 32) ||
			    (ntohs(pkt->class) != 1) ||
			    (strcmp (pkt->name, encoded)))
			continue; /* bad request, try again */
		/* printf("Name=%s recognized\r\n",name); */
		/* build RFC1002 Positive Name Query Response */
		pkt->flags = htons (0x8580) ; /* Response flags */
		pkt->answers = htons (1) ;
		pkt->ttl = htonl ((u_long) 60); /* 60 seconds validity */
		pkt->len_rep = htons (6);
		pkt->node_flags = pkt->node_type = pkt->quests = 0; /*
B-type node, etc... */
		pkt->ip_addr = ipaddr ; /* Returned IP Address, end of
answer*/
		NutUdpSendTo(sock, raddr, 137, pkt, sizeof(WINSHEADER)); /*
send to netbios port */
		memset(pkt, 0, sizeof(WINSHEADER));
	}
}

/*@}*/




More information about the En-Nut-Discussion mailing list