3 docCopyright("Steve Dekorte", 2004)
4 docLicense("BSD revised")
5 docDescription("Object representation of an Internet Protocol Address.")
6 docCategory("Networking")
12 docSlot("setHostName(hostName)",
13 "Translates hostName to an IP using asynchronous DNS and sets the host attribute. Returns self.")
16 #include "IoIPAddress.h"
21 #define IPADDRESS(self) ((IPAddress *)IoObject_dataPointer(self))
23 IoIPAddress
*IoMessage_locals_addressArgAt_(IoMessage
*self
, IoObject
*locals
, int n
)
25 IoObject
*v
= IoMessage_locals_valueArgAt_(self
, locals
, n
);
29 IoMessage_locals_numberArgAt_errorForType_(self
, locals
, n
, "IPAddress");
35 IPAddress
*IoMessage_locals_rawIPAddressArgAt_(IoMessage
*self
, IoObject
*locals
, int n
)
37 return IoIPAddress_rawIPAddress(IoMessage_locals_addressArgAt_(self
, locals
, n
));
40 IPAddress
*IoIPAddress_rawIPAddress(IoIPAddress
*self
)
42 return IPADDRESS(self
);
45 IoTag
*IoIPAddress_newTag(void *state
)
47 IoTag
*tag
= IoTag_newWithName_("IPAddress");
48 IoTag_state_(tag
, state
);
49 IoTag_cloneFunc_(tag
, (IoTagCloneFunc
*)IoIPAddress_rawClone
);
50 IoTag_freeFunc_(tag
, (IoTagFreeFunc
*)IoIPAddress_free
);
54 IoIPAddress
*IoIPAddress_proto(void *state
)
56 IoObject
*self
= IoObject_new(state
);
58 IoObject_tag_(self
, IoIPAddress_newTag(state
));
59 IoObject_setDataPointer_(self
, IPAddress_new());
61 IoState_registerProtoWithFunc_((IoState
*)state
, self
, IoIPAddress_proto
);
64 IoMethodTable methodTable
[] = {
65 {"setIp", IoIPAddress_setIp
},
66 {"ip", IoIPAddress_ip
},
68 {"setPort", IoIPAddress_setPort
},
69 {"port", IoIPAddress_port
},
73 IoObject_addMethodTable_(self
, methodTable
);
79 IoIPAddress
*IoIPAddress_rawClone(IoIPAddress
*proto
)
81 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
82 IoObject_setDataPointer_(self
, IPAddress_new());
86 IoIPAddress
*IoIPAddress_new(void *state
)
88 IoObject
*proto
= IoState_protoWithInitFunction_((IoState
*)state
, IoIPAddress_proto
);
89 return IOCLONE(proto
);
92 void IoIPAddress_free(IoIPAddress
*self
)
94 IPAddress_free(IPADDRESS(self
));
97 // -----------------------------------------------------------
100 IoIPAddress *IoIPAddress_newWithIPAddress_size_(void *state,
101 struct sockaddr *address,
104 IoIPAddress *self = IoIPAddress_new(state);
105 free(DATA(self)->address);
106 DATA(self)->address = malloc(size);
107 DATA(self)->size = size;
108 memcpy(DATA(self)->address, address, size);
115 IoObject
*IoIPAddress_setIp(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
117 IoSeq
*ip
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
118 char *ipString
= IoSeq_asCString(ip
);
119 IPAddress_setIp_(IPADDRESS(self
), ipString
);
123 IoObject
*IoIPAddress_ip(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
125 char *ip
= IPAddress_ip(IPADDRESS(self
));
131 IoObject
*IoIPAddress_setPort(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
133 uint16_t port
= IoMessage_locals_intArgAt_(m
, locals
, 0);
134 IPAddress_setPort_(IPADDRESS(self
), port
);
138 IoObject
*IoIPAddress_port(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
140 int port
= IPAddress_port(IPADDRESS(self
));
141 return IONUMBER(port
);