1 #pragma src "/sys/src/libndb" 2 #pragma lib "libndb.a" 3 4 /* 5 * this include file requires includes of <u.h> and <bio.h> 6 */ 7 typedef struct Ndb Ndb; 8 typedef struct Ndbtuple Ndbtuple; 9 typedef struct Ndbhf Ndbhf; 10 typedef struct Ndbs Ndbs; 11 12 enum 13 { 14 Ndbalen= 32, /* max attribute length */ 15 Ndbvlen= 64, /* max value length */ 16 }; 17 18 /* 19 * the database 20 */ 21 struct Ndb 22 { 23 Ndb *next; 24 25 Biobufhdr b; /* buffered input file */ 26 uchar buf[256]; /* and it's buffer */ 27 28 ulong mtime; /* mtime of db file */ 29 Qid qid; /* qid of db file */ 30 char file[2*NAMELEN];/* path name of db file */ 31 ulong length; /* length of db file */ 32 33 Ndbhf *hf; /* open hash files */ 34 }; 35 36 /* 37 * a parsed entry, doubly linked 38 */ 39 struct Ndbtuple 40 { 41 char attr[Ndbalen]; /* attribute name */ 42 char val[Ndbvlen]; /* value(s) */ 43 Ndbtuple *entry; /* next tuple in this entry */ 44 Ndbtuple *line; /* next tuple on this line */ 45 ulong ptr; /* (for the application - starts 0) */ 46 }; 47 48 /* 49 * each hash file is of the form 50 * 51 * +---------------------------------------+ 52 * | mtime of db file (4 bytes) | 53 * +---------------------------------------+ 54 * | size of table (in entries - 4 bytes) | 55 * +---------------------------------------+ 56 * | hash table | 57 * +---------------------------------------+ 58 * | hash chains | 59 * +---------------------------------------+ 60 * 61 * hash collisions are resolved using chained entries added to the 62 * the end of the hash table. 63 * 64 * Hash entries are of the form 65 * 66 * +-------------------------------+ 67 * | offset (3 bytes) | 68 * +-------------------------------+ 69 * 70 * Chain entries are of the form 71 * 72 * +-------------------------------+ 73 * | offset1 (3 bytes) | 74 * +-------------------------------+ 75 * | offset2 (3 bytes) | 76 * +-------------------------------+ 77 * 78 * The top bit of an offset set to 1 indicates a pointer to a hash chain entry. 79 */ 80 #define NDBULLEN 4 /* unsigned long length in bytes */ 81 #define NDBPLEN 3 /* pointer length in bytes */ 82 #define NDBHLEN (2*NDBULLEN) /* hash file header length in bytes */ 83 84 /* 85 * finger pointing to current point in a search 86 */ 87 struct Ndbs 88 { 89 Ndb *db; /* data base file being searched */ 90 Ndbhf *hf; /* hash file being searched */ 91 int type; 92 ulong ptr; /* current pointer */ 93 ulong ptr1; /* next pointer */ 94 Ndbtuple *t; /* last attribute value pair found */ 95 }; 96 97 /* 98 * bit defs for pointers in hash files 99 */ 100 #define NDBSPEC (1<<23) 101 #define NDBCHAIN NDBSPEC /* points to a collision chain */ 102 #define NDBNAP (NDBSPEC|1) /* not a pointer */ 103 104 /* 105 * macros for packing and unpacking pointers 106 */ 107 #define NDBPUTP(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; } 108 #define NDBGETP(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16)) 109 110 /* 111 * macros for packing and unpacking unsigned longs 112 */ 113 #define NDBPUTUL(v,a) { (a)[0] = v; (a)[1] = (v)>>8; (a)[2] = (v)>>16; (a)[3] = (v)>>24; } 114 #define NDBGETUL(a) ((a)[0] | ((a)[1]<<8) | ((a)[2]<<16) | ((a)[3]<<24)) 115 116 enum 117 { 118 DNScache= 128, 119 }; 120 121 /* 122 * Domain name service cache 123 */ 124 typedef struct Dns Dns; 125 struct Dns 126 { 127 Ndbtuple *cache[DNScache]; 128 }; 129 130 /* 131 * ip information about a system 132 */ 133 typedef struct Ipinfo Ipinfo; 134 struct Ipinfo 135 { 136 char domain[128]; /* system domain name */ 137 char bootf[128]; /* boot file */ 138 uchar ipaddr[4]; /* ip address of system */ 139 uchar ipmask[4]; /* ip network mask */ 140 uchar ipnet[4]; /* ip network address (ipaddr & ipmask) */ 141 uchar etheraddr[6]; /* ethernet address */ 142 uchar gwip[4]; /* gateway ip address */ 143 uchar fsip[4]; /* file system ip address */ 144 uchar auip[4]; /* authentication server ip address */ 145 }; 146 147 ulong ndbhash(char*, int); 148 Ndbtuple* ndbparse(Ndb*); 149 void ndbfree(Ndbtuple*); 150 Ndb* ndbopen(char*); 151 int ndbreopen(Ndb*); 152 void ndbclose(Ndb*); 153 long ndbseek(Ndb*, long); 154 Ndbtuple* ndbsearch(Ndb*, Ndbs*, char*, char*); 155 Ndbtuple* ndbsnext(Ndbs*, char*, char*); 156 Ndbtuple* ndbgetval(Ndb*, Ndbs*, char*, char*, char*, char*); 157 Ndbtuple* csgetval(char*, char*, char*, char*); 158 char* ipattr(char*); 159 int ipinfo(Ndb*, char*, char*, char*, Ipinfo*); 160 161