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

Zodianet zodianet at wanadoo.fr
Fri Jan 16 11:02:21 CET 2004


Thanks Trevor,
Moreover, check all tests at the beginning of my function (some are not very
useful), the order is not good to exit correctly. 
I tested it on Windows XP through SMC 7004VBR broadband router.  
Best regards,
Jean Pierre

-----Message d'origine-----
De : en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] De la part de Trevor O'Grady
Envoyé : vendredi 16 janvier 2004 10:23
À : Ethernut User Chat (English)
Objet : RE: [En-Nut-Discussion] Small WINS utility for the community

Hi Jean,
This is exactly what I was looking for. Nice tight implementation too. I
found one issue which confused me for several hours last night.

The u_char car variable was getting corrupted in the label compression loop.
The encoded string was ending up with every second byte having a rogue bit
set in the top nibble. I cured the problem by masking off the bottom nibble
before shifting it by changing encoded[j] = (car >>4) + 'A' ; to encoded[j]
= ((car&0xf0) >>4) + 'A' ;

I wasn't happy with this fix so I dug deeper. Eventually I found the root of
the problem is the declaration to toupper i.e.
extern u_char toupper (u_char);
but toupper is int toupper( int c );

I implemented my own toupper and sure enough this solved the problem. Here's
the code:

static u_char toupper_m ( u_char ch )
{
	if( ch >= 'a' && ch <= 'z' )
		ch -= ( 'a' - 'A' );
	return ch;
}

I thought I'd let you know.

Thanks and best regards
Trevor


-----Original Message-----
From: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de]On Behalf Of Zodianet
Sent: 15 January 2004 17:18
To: en-nut-discussion at egnite.de
Subject: [En-Nut-Discussion] Small WINS utility for the community


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));
	}
}

/*@}*/

_______________________________________________
En-Nut-Discussion mailing list
En-Nut-Discussion at egnite.de
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion

_______________________________________________
En-Nut-Discussion mailing list
En-Nut-Discussion at egnite.de
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion




More information about the En-Nut-Discussion mailing list