netbios.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. enum {
  10. NbNameLen = 16,
  11. NbnsTimeoutBroadcast = 1000,
  12. NbnsRetryBroadcast = 3,
  13. NbnsPort = 137,
  14. NbDgramMaxLen = 576,
  15. };
  16. typedef struct NbnsHdr {
  17. uchar name_trn_id[2];
  18. uchar ctrl[2];
  19. uchar qdcount[2];
  20. uchar ancount[2];
  21. uchar nscount[2];
  22. uchar arcount[2];
  23. } NbnsHdr;
  24. enum {
  25. NbnsResponse = 1 << 15
  26. };
  27. enum {
  28. NbnsOpShift = 11,
  29. NbnsOpMask = 0xf,
  30. NbnsOpQuery = 0,
  31. NbnsOpRegistration = 5,
  32. NbnsOpRelease = 6,
  33. NbnsOpWack = 7,
  34. NbnsOpRefresh = 8
  35. };
  36. enum {
  37. NbnsFlagBroadcast = (1 << 4),
  38. NbnsFlagRecursionAvailable = (1 << 7),
  39. NbnsFlagRecursionDesired = (1 << 8),
  40. NbnsFlagTruncation = (1 << 9),
  41. NbnsFlagAuthoritativeAnswer = (1 << 10),
  42. };
  43. enum {
  44. NbnsRcodeShift = 0,
  45. NbnsRcodeMask = 0xf,
  46. };
  47. enum {
  48. NbnsQuestionTypeNb = 0x0020,
  49. NbnsQuestionTypeNbStat = 0x0021,
  50. NbnsQuestionClassIn = 0x0001,
  51. };
  52. enum {
  53. NbnsResourceTypeA = 0x0001,
  54. NbnsResourceTypeNs = 0x0002,
  55. NbnsResourceTypeNull = 0x000a,
  56. NbnsResourceTypeNb = 0x0020,
  57. NbnsResourceTypeNbStat = 0x0021,
  58. NbnsResourceClassIn = 0x0001,
  59. };
  60. typedef struct NbnsMessageQuestion NbnsMessageQuestion;
  61. typedef struct NbnsMessageResource NbnsMessageResource;
  62. typedef uchar NbName[NbNameLen];
  63. int nbnamedecode(uchar *base, uchar *p, uchar *ep, NbName name);
  64. int nbnameencode(uchar *p, uchar *ep, NbName name);
  65. int nbnameequal(NbName name1, NbName name2);
  66. void nbnamecpy(NbName n1, NbName n2);
  67. void nbmknamefromstring(NbName nbname, char *string);
  68. void nbmknamefromstringandtype(NbName nbname, char *string, uchar type);
  69. void nbmkstringfromname(char *buf, int buflen, NbName name);
  70. int nbnamefmt(Fmt *);
  71. struct NbnsMessageQuestion {
  72. NbName name;
  73. ushort type;
  74. ushort class;
  75. NbnsMessageQuestion *next;
  76. };
  77. NbnsMessageQuestion *nbnsmessagequestionnew(NbName name, ushort type, ushort class);
  78. struct NbnsMessageResource {
  79. NbName name;
  80. ushort type;
  81. ushort class;
  82. uint32_t ttl;
  83. ushort rdlength;
  84. uchar *rdata;
  85. NbnsMessageResource *next;
  86. };
  87. NbnsMessageResource *nbnsmessageresourcenew(NbName name, ushort type, ushort class,
  88. uint32_t ttl, int rdcount,
  89. uchar *rdata);
  90. typedef struct NbnsMessage {
  91. ushort id;
  92. int response;
  93. int opcode;
  94. int broadcast;
  95. int recursionavailable;
  96. int recursiondesired;
  97. int truncation;
  98. int authoritativeanswer;
  99. int rcode;
  100. NbnsMessageQuestion *q;
  101. NbnsMessageResource *an;
  102. NbnsMessageResource *ns;
  103. NbnsMessageResource *ar;
  104. } NbnsMessage;
  105. NbnsMessage *nbnsmessagenew(void);
  106. void nbnsmessageaddquestion(NbnsMessage *s, NbnsMessageQuestion *q);
  107. void nbnsmessageaddresource(NbnsMessageResource **rp, NbnsMessageResource *r);
  108. NbnsMessage *nbnsconvM2S(uchar *ap, int nap);
  109. void nbnsmessagefree(NbnsMessage **sp);
  110. void nbnsdumpmessage(NbnsMessage *s);
  111. int nbnsconvS2M(NbnsMessage *s, uchar *ap, int nap);
  112. NbnsMessage *nbnsmessagenamequeryrequestnew(ushort id, int broadcast, NbName name);
  113. NbnsMessage *nbnsmessagenameregistrationrequestnew(ushort id, int broadcast, NbName name,
  114. uint32_t ttl,
  115. uchar *ipaddr);
  116. typedef struct NbnsTransaction NbnsTransaction;
  117. struct NbnsTransaction {
  118. ushort id;
  119. Channel *c;
  120. NbnsTransaction *next;
  121. };
  122. ushort nbnsnextid(void);
  123. int nbnsfindname(uchar *serveripaddr, NbName name, uchar *ipaddr,
  124. uint32_t *ttlp);
  125. int nbnsaddname(uchar *serveripaddr, NbName name, uint32_t ttl,
  126. uchar *ipaddr);
  127. NbnsTransaction *nbnstransactionnew(NbnsMessage *request, uchar *ipaddr);
  128. void nbnstransactionfree(NbnsTransaction **tp);
  129. typedef struct NbnsAlarm NbnsAlarm;
  130. struct NbnsAlarm {
  131. Channel *c;
  132. vlong expirems;
  133. NbnsAlarm *next;
  134. };
  135. void nbnsalarmset(NbnsAlarm *a, uint32_t millisec);
  136. void nbnsalarmcancel(NbnsAlarm *a);
  137. void nbnsalarmfree(NbnsAlarm **ap);
  138. NbnsAlarm *nbnsalarmnew(void);
  139. void nbnsalarmend(void);
  140. typedef struct NbSession NbSession;
  141. typedef int NBSSWRITEFN(NbSession *s, void *buf, long n);
  142. struct NbSession {
  143. int fd;
  144. void *magic;
  145. NbName from;
  146. NbName to;
  147. };
  148. int nbsslisten(NbName to, NbName from, int (*accept)(void *magic, NbSession *s, NBSSWRITEFN **write), void *magic);
  149. NbSession *nbssconnect(NbName to, NbName from);
  150. void nbssfree(NbSession *s);
  151. typedef struct NbScatterGather NbScatterGather;
  152. struct NbScatterGather {
  153. void *p;
  154. long l;
  155. };
  156. int nbssgatherwrite(NbSession *s, NbScatterGather *a);
  157. long nbssscatterread(NbSession *, NbScatterGather *a);
  158. int nbsswrite(NbSession *s, void *buf, long n);
  159. long nbssread(NbSession *s, void *buf, long n);
  160. void *nbemalloc(uint32_t);
  161. int nbnameresolve(NbName name, uchar *ipaddr);
  162. void nbdumpdata(void *data, long datalen);
  163. typedef struct NbDgram {
  164. uchar type;
  165. uchar flags;
  166. ushort id;
  167. uchar srcip[IPaddrlen];
  168. ushort srcport;
  169. union {
  170. struct {
  171. ushort length;
  172. ushort offset;
  173. NbName srcname;
  174. NbName dstname;
  175. uchar *data;
  176. } datagram;
  177. struct {
  178. uchar code;
  179. } error;
  180. struct {
  181. NbName dstname;
  182. } query;
  183. };
  184. } NbDgram;
  185. enum {
  186. NbDgramDirectUnique = 0x10,
  187. NbDgramDirectGroup,
  188. NbDgramBroadcast,
  189. NbDgramError,
  190. NbDgramQueryRequest,
  191. NbDgramPositiveQueryResponse,
  192. NbDgramNegativeQueryResponse,
  193. NbDgramMore = 1,
  194. NbDgramFirst = 2,
  195. NbDgramPort = 138,
  196. NbDgramErrorDestinationNameNotPresent = 0x82,
  197. NbDgramMaxPacket = 576,
  198. };
  199. typedef struct NbDgramSendParameters {
  200. NbName to;
  201. uchar type;
  202. } NbDgramSendParameters;
  203. int nbdgramconvM2S(NbDgram *s, uchar *p, uchar *ep);
  204. int nbdgramconvS2M(uchar *p, uchar *ep, NbDgram *s);
  205. void nbdgramdump(NbDgram *s);
  206. int nbdgramsendto(uchar *ipaddr, ushort port, NbDgram *s);
  207. int nbdgramsend(NbDgramSendParameters *p, unsigned char *data, long datalen);
  208. char *nbdgramlisten(NbName to, int (*deliver)(void *magic, NbDgram *s), void *magic);
  209. int nbnametablefind(NbName name, int add);
  210. int nbnameisany(NbName name);
  211. int nbremotenametablefind(NbName name, uchar *ipaddr);
  212. int nbremotenametableadd(NbName name, uchar *ipaddr, uint32_t ttl);
  213. typedef struct NbGlobals {
  214. uchar myipaddr[IPaddrlen];
  215. uchar bcastaddr[IPaddrlen];
  216. NbName myname;
  217. } NbGlobals;
  218. extern NbGlobals nbglobals;
  219. extern NbName nbnameany;
  220. int nbinit(void);
  221. char *nbudpannounce(ushort port, int *fdp);
  222. extern int nbudphdrsize;