1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 #include "allobjects.h"
26 #include "modsupport.h" /* For getargs() etc. */
29 #include <AddressXlation.h>
32 #ifndef HAVE_UNIVERSAL_HEADERS
33 #define ResultUPP ResultProcPtr
34 #define NewResultProc(x) (x)
35 #define ResultProc2UPP ResultProc2Ptr
36 #define NewResultProc2Proc(x) (x)
39 static object
*ErrorObject
;
41 /* ----------------------------------------------------- */
42 /* Declarations for objects of type MacTCP DNR Result */
44 /* Types of records we have */
51 int type
; /* DNR_XXX */
52 int waiting
; /* True while completion proc not called */
53 struct returnRec hinfo
;
56 staticforward typeobject Dnrrtype
;
58 #define is_dnrrobject(v) ((v)->ob_type == &Dnrrtype)
60 /* ---------------------------------------------------------------- */
64 struct hostInfo
*rrp
; /* Unused */
67 if ( !udp
->waiting
) {
68 printf("macdnr: dnrr_done: spurious completion call!\n");
75 static int dnrwait(self
)
78 while ( self
->waiting
) {
90 if (!newgetargs(args
, ""))
92 if ( !dnrwait(self
) ) {
93 /* XXX An interrupt is pending -- is this correct? */
97 if ( self
->hinfo
.rtnCode
) {
98 PyErr_Mac(ErrorObject
, self
->hinfo
.rtnCode
);
106 dnrr_isdone(self
, args
)
110 if (!newgetargs(args
, ""))
112 return newintobject(!self
->waiting
);
115 static struct methodlist dnrr_methods
[] = {
116 {"wait", (method
)dnrr_wait
, 1},
117 {"isdone", (method
)dnrr_isdone
, 1},
119 {NULL
, NULL
} /* sentinel */
130 self
= NEWOBJ(dnrrobject
, &Dnrrtype
);
135 memset(&self
->hinfo
, 0, sizeof(self
->hinfo
));
143 self
->waiting
= 0; /* Not really needed, since we incref for completion */
147 /* Code to access structure members by accessing attributes */
149 #include "structmember.h"
151 #define OFF(x) offsetof(struct returnRec, x)
153 static struct memberlist dnrr_memberlist_addr
[] = {
154 { "rtnCode", T_INT
, OFF(rtnCode
), RO
},
155 { "cname", T_STRING_INPLACE
, OFF(cname
), RO
},
156 { "ip0", T_UINT
, OFF(rdata
.addr
[0]), RO
},
157 { "ip1", T_UINT
, OFF(rdata
.addr
[1]), RO
},
158 { "ip2", T_UINT
, OFF(rdata
.addr
[2]), RO
},
159 { "ip3", T_UINT
, OFF(rdata
.addr
[3]), RO
},
160 {NULL
} /* Sentinel */
163 static struct memberlist dnrr_memberlist_hinfo
[] = {
164 { "rtnCode", T_INT
, OFF(rtnCode
), RO
},
165 { "cname", T_STRING_INPLACE
, OFF(cname
), RO
},
166 { "cpuType", T_STRING_INPLACE
, OFF(rdata
.hinfo
.cpuType
), RO
},
167 { "osType", T_STRING_INPLACE
, OFF(rdata
.hinfo
.osType
), RO
},
168 {NULL
} /* Sentinel */
171 static struct memberlist dnrr_memberlist_mx
[] = {
172 { "rtnCode", T_INT
, OFF(rtnCode
), RO
},
173 { "cname", T_STRING_INPLACE
, OFF(cname
), RO
},
174 { "preference", T_USHORT
, OFF(rdata
.mx
.preference
), RO
},
175 { "exchange", T_STRING_INPLACE
, OFF(rdata
.mx
.exchange
), RO
},
176 {NULL
} /* Sentinel */
179 static struct memberlist
*dnrr_mlists
[3] = {
180 dnrr_memberlist_addr
,
181 dnrr_memberlist_hinfo
,
186 dnrr_getattr(self
, name
)
193 rv
= findmethod(dnrr_methods
, (object
*)self
, name
);
197 if ( !dnrwait(self
) ) {
198 /* XXX An interrupt is pending -- is this correct? */
199 err_setstr(ErrorObject
, "Resolver busy");
203 return getmember((char *)&self
->hinfo
, dnrr_mlists
[tp
], name
);
207 static typeobject Dnrrtype
= {
208 OB_HEAD_INIT(&Typetype
)
210 "MacTCP DNR Result", /*tp_name*/
211 sizeof(dnrrobject
), /*tp_basicsize*/
214 (destructor
)dnrr_dealloc
, /*tp_dealloc*/
215 (printfunc
)0, /*tp_print*/
216 (getattrfunc
)dnrr_getattr
, /*tp_getattr*/
217 (setattrfunc
)0, /*tp_setattr*/
218 (cmpfunc
)0, /*tp_compare*/
219 (reprfunc
)0, /*tp_repr*/
221 0, /*tp_as_sequence*/
223 (hashfunc
)0, /*tp_hash*/
226 /* End of code for MacTCP DNR Result objects */
227 /* -------------------------------------------------------- */
237 if ( dnr_is_open
) return 1;
238 if ( (err
=OpenResolver(fn
)) ) {
239 PyErr_Mac(ErrorObject
, err
);
248 object
*self
; /* Not used */
253 if (!newgetargs(args
, "|s", &fn
))
256 err_setstr(ErrorObject
, "DNR already open");
266 dnr_Close(self
, args
)
267 object
*self
; /* Not used */
272 if (!newgetargs(args
, ""))
275 if ( (err
=CloseResolver()) ) {
276 PyErr_Mac(ErrorObject
, err
);
284 dnr_StrToAddr(self
, args
)
285 object
*self
; /* Not used */
291 ResultUPP cb_upp
= NewResultProc(dnrr_done
);
293 if (!newgetargs(args
, "s", &hostname
))
295 if ( !opendnr(NULL
) )
297 if ( (rv
=newdnrrobject(DNR_ADDR
)) == NULL
)
299 err
= StrToAddr(hostname
, (struct hostInfo
*)&rv
->hinfo
, cb_upp
, (char *)rv
);
300 if ( err
== cacheFault
) {
305 PyErr_Mac(ErrorObject
, err
);
312 dnr_AddrToName(self
, args
)
313 object
*self
; /* Not used */
317 unsigned long ipaddr
;
319 ResultUPP cb_upp
= NewResultProc(dnrr_done
);
321 if (!newgetargs(args
, "l", &ipaddr
))
323 if ( !opendnr(NULL
) )
325 if ( (rv
=newdnrrobject(DNR_ADDR
)) == NULL
)
327 err
= AddrToName(ipaddr
, (struct hostInfo
*)&rv
->hinfo
, cb_upp
, (char *)rv
);
328 if ( err
== cacheFault
) {
333 PyErr_Mac(ErrorObject
, err
);
340 dnr_AddrToStr(self
, args
)
341 object
*self
; /* Not used */
345 unsigned long ipaddr
;
348 if (!newgetargs(args
, "l", &ipaddr
))
350 if ( !opendnr(NULL
) )
352 if ( (err
=AddrToStr(ipaddr
, ipname
)) ) {
353 PyErr_Mac(ErrorObject
, err
);
356 return newstringobject(ipname
);
360 dnr_HInfo(self
, args
)
361 object
*self
; /* Not used */
367 ResultProc2UPP cb_upp
= NewResultProc2Proc(dnrr_done
);
369 if (!newgetargs(args
, "s", &hostname
))
371 if ( !opendnr(NULL
) )
373 if ( (rv
=newdnrrobject(DNR_HINFO
)) == NULL
)
375 err
= HInfo(hostname
, &rv
->hinfo
, cb_upp
, (char *)rv
);
376 if ( err
== cacheFault
) {
381 PyErr_Mac(ErrorObject
, err
);
386 if (!newgetargs(args
, ""))
393 dnr_MXInfo(self
, args
)
394 object
*self
; /* Not used */
400 ResultProc2UPP cb_upp
= NewResultProc2Proc(dnrr_done
);
402 if (!newgetargs(args
, "s", &hostname
))
404 if ( !opendnr(NULL
) )
406 if ( (rv
=newdnrrobject(DNR_MX
)) == NULL
)
408 err
= MXInfo(hostname
, &rv
->hinfo
, cb_upp
, (char *)rv
);
409 if ( err
== cacheFault
) {
414 PyErr_Mac(ErrorObject
, err
);
420 /* List of methods defined in the module */
422 static struct methodlist dnr_methods
[] = {
423 {"Open", dnr_Open
, 1},
424 {"Close", dnr_Close
, 1},
425 {"StrToAddr", dnr_StrToAddr
, 1},
426 {"AddrToStr", dnr_AddrToStr
, 1},
427 {"AddrToName", dnr_AddrToName
, 1},
428 {"HInfo", dnr_HInfo
, 1},
429 {"MXInfo", dnr_MXInfo
, 1},
431 {NULL
, NULL
} /* sentinel */
435 /* Initialization function for the module (*must* be called initmacdnr) */
442 /* Create the module and add the functions */
443 m
= initmodule("macdnr", dnr_methods
);
445 /* Add some symbolic constants to the module */
446 d
= getmoduledict(m
);
447 ErrorObject
= newstringobject("macdnr.error");
448 dictinsert(d
, "error", ErrorObject
);
450 /* Not needed, after all */
451 #define CONST(name, value) o = newintobject(value); dictinsert(d, name, o);
452 CONST("ADDR", DNR_ADDR
);
453 CONST("HINFO", DNR_HINFO
);
456 /* Check for errors */
458 fatal("can't initialize module macdnr");