1 //metadoc IPAddress copyright Steve Dekorte, 2004
2 //metadoc IPAddress license BSD revised
3 //metadoc IPAddress description Object representation of an Internet Protocol Address.
4 //metadoc IPAddress category Networking")
6 /*doc IPAddress setHostName(hostName)
7 Translates hostName to an IP using asynchronous DNS and
8 sets the host attribute. Returns self.
11 #include "IoIPAddress.h"
16 #define IPADDRESS(self) ((IPAddress *)IoObject_dataPointer(self))
18 IoIPAddress
*IoMessage_locals_addressArgAt_(IoMessage
*self
, IoObject
*locals
, int n
)
20 IoObject
*v
= IoMessage_locals_valueArgAt_(self
, locals
, n
);
24 IoMessage_locals_numberArgAt_errorForType_(self
, locals
, n
, "IPAddress");
30 IPAddress
*IoMessage_locals_rawIPAddressArgAt_(IoMessage
*self
, IoObject
*locals
, int n
)
32 return IoIPAddress_rawIPAddress(IoMessage_locals_addressArgAt_(self
, locals
, n
));
35 IPAddress
*IoIPAddress_rawIPAddress(IoIPAddress
*self
)
37 return IPADDRESS(self
);
40 IoTag
*IoIPAddress_newTag(void *state
)
42 IoTag
*tag
= IoTag_newWithName_("IPAddress");
43 IoTag_state_(tag
, state
);
44 IoTag_cloneFunc_(tag
, (IoTagCloneFunc
*)IoIPAddress_rawClone
);
45 IoTag_freeFunc_(tag
, (IoTagFreeFunc
*)IoIPAddress_free
);
49 IoIPAddress
*IoIPAddress_proto(void *state
)
51 IoObject
*self
= IoObject_new(state
);
53 IoObject_tag_(self
, IoIPAddress_newTag(state
));
54 IoObject_setDataPointer_(self
, IPAddress_new());
56 IoState_registerProtoWithFunc_((IoState
*)state
, self
, IoIPAddress_proto
);
59 IoMethodTable methodTable
[] = {
60 {"setIp", IoIPAddress_setIp
},
61 {"ip", IoIPAddress_ip
},
63 {"setPort", IoIPAddress_setPort
},
64 {"port", IoIPAddress_port
},
68 IoObject_addMethodTable_(self
, methodTable
);
74 IoIPAddress
*IoIPAddress_rawClone(IoIPAddress
*proto
)
76 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
77 IoObject_setDataPointer_(self
, IPAddress_new());
81 IoIPAddress
*IoIPAddress_new(void *state
)
83 IoObject
*proto
= IoState_protoWithInitFunction_((IoState
*)state
, IoIPAddress_proto
);
84 return IOCLONE(proto
);
87 void IoIPAddress_free(IoIPAddress
*self
)
89 IPAddress_free(IPADDRESS(self
));
92 // -----------------------------------------------------------
95 IoIPAddress *IoIPAddress_newWithIPAddress_size_(void *state,
96 struct sockaddr *address,
99 IoIPAddress *self = IoIPAddress_new(state);
100 free(DATA(self)->address);
101 DATA(self)->address = malloc(size);
102 DATA(self)->size = size;
103 memcpy(DATA(self)->address, address, size);
110 IoObject
*IoIPAddress_setIp(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
112 IoSeq
*ip
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
113 char *ipString
= IoSeq_asCString(ip
);
114 IPAddress_setIp_(IPADDRESS(self
), ipString
);
118 IoObject
*IoIPAddress_ip(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
120 char *ip
= IPAddress_ip(IPADDRESS(self
));
126 IoObject
*IoIPAddress_setPort(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
128 uint16_t port
= IoMessage_locals_intArgAt_(m
, locals
, 0);
129 IPAddress_setPort_(IPADDRESS(self
), port
);
133 IoObject
*IoIPAddress_port(IoIPAddress
*self
, IoObject
*locals
, IoMessage
*m
)
135 int port
= IPAddress_port(IPADDRESS(self
));
136 return IONUMBER(port
);