#ifndef __NETBIOS_H__
#define __NETBIOS_H__
#include "wizchip_conf.h"
#include "W7500x.h"

#define NETBIOS_W7500_NAME "WIZNETW7500P"
#define SOCK_NETBIOS    7						/**socket number for NetBIOS Name service**/
#define NETBIOS_PORT     137				/** default port number for "NetBIOS Name service */
#define NETBIOS_NAME_LEN 16					/** size of a NetBIOS name */
#define NETBIOS_MSG_MAX_LEN 512			/**NetBIOS message maximum length*/
/** The Time-To-Live for NetBIOS name responds (in seconds)
 * Default is 300000 seconds (3 days, 11 hours, 20 minutes) */
#define NETBIOS_NAME_TTL 10//300000

/** NetBIOS header flags */
#define NETB_HFLAG_RESPONSE           0x8000U
#define NETB_HFLAG_OPCODE             0x7800U
#define NETB_HFLAG_OPCODE_NAME_QUERY  0x0000U
#define NETB_HFLAG_AUTHORATIVE        0x0400U
#define NETB_HFLAG_TRUNCATED          0x0200U
#define NETB_HFLAG_RECURS_DESIRED     0x0100U
#define NETB_HFLAG_RECURS_AVAILABLE   0x0080U
#define NETB_HFLAG_BROADCAST          0x0010U
#define NETB_HFLAG_REPLYCODE          0x0008U
#define NETB_HFLAG_REPLYCODE_NOERROR  0x0000U

/** NetBIOS name flags */
#define NETB_NFLAG_UNIQUE             0x8000U
#define NETB_NFLAG_NODETYPE           0x6000U
#define NETB_NFLAG_NODETYPE_HNODE     0x6000U
#define NETB_NFLAG_NODETYPE_MNODE     0x4000U
#define NETB_NFLAG_NODETYPE_PNODE     0x2000U
#define NETB_NFLAG_NODETYPE_BNODE     0x0000U

/** NetBIOS message header */
#pragma pack(1)
typedef struct _NETBIOS_HDR {
  uint16_t trans_id;
  uint16_t flags;
  uint16_t questions;
  uint16_t answerRRs;
  uint16_t authorityRRs;
  uint16_t additionalRRs;
}NETBIOS_HDR;

/** NetBIOS message name part */
typedef struct _NETBIOS_NAME_HDR {
  uint8_t  nametype;
  uint8_t  encname[(NETBIOS_NAME_LEN*2)+1];
  uint16_t type;
  uint16_t cls;
  uint32_t ttl;
  uint16_t datalen;
  uint16_t flags;
  //ip_addr_p_t addr;
  uint8_t addr[4];
}NETBIOS_NAME_HDR;

/** NetBIOS message */
typedef struct _NETBIOS_RESP
{
  NETBIOS_HDR      resp_hdr;
  NETBIOS_NAME_HDR resp_name;
}NETBIOS_RESP;
#pragma pack()

void do_netbios(void);

#endif /* __NETBIOS_H__ */
