make doc update
[io.git] / addons / Socket / source / IoIPAddress.c
bloba7740a785088de9bc16ece114ac95e85ebc77ad9
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.
9 */
11 #include "IoIPAddress.h"
12 #include "IoState.h"
13 #include "IoNumber.h"
14 #include "IoSeq.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);
22 if (!ISIPADDRESS(v))
24 IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "IPAddress");
27 return v;
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);
46 return tag;
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},
66 {NULL, NULL},
68 IoObject_addMethodTable_(self, methodTable);
71 return self;
74 IoIPAddress *IoIPAddress_rawClone(IoIPAddress *proto)
76 IoObject *self = IoObject_rawClonePrimitive(proto);
77 IoObject_setDataPointer_(self, IPAddress_new());
78 return self;
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,
97 size_t size)
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);
104 return self;
108 // ip
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);
115 return self;
118 IoObject *IoIPAddress_ip(IoIPAddress *self, IoObject *locals, IoMessage *m)
120 char *ip = IPAddress_ip(IPADDRESS(self));
121 return IOSYMBOL(ip);
124 // port
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);
130 return self;
133 IoObject *IoIPAddress_port(IoIPAddress *self, IoObject *locals, IoMessage *m)
135 int port = IPAddress_port(IPADDRESS(self));
136 return IONUMBER(port);