Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

214 lines (139 sloc) 4.024 kB
/*
Copyright (C) 2008-2014, Parrot Foundation.
=head1 NAME
src/pmc/sockaddr.pmc - Sockaddr PMC
=head1 DESCRIPTION
The Sockaddr PMC holds a C<sockaddr> struct and saves its length (to
distinguish C<sockaddr_in> and C<sockaddr_in6>).
=head2 Vtable Functions
These are the vtable functions for the Sockaddr class.
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass Sockaddr auto_attrs {
ATTR INTVAL family;
ATTR INTVAL type;
ATTR INTVAL protocol;
ATTR INTVAL len; /* Length of the sockaddr struct */
ATTR void *pointer; /* Pointer to a sockaddr struct */
/*
=item C<void init()>
Initializes the PMC.
=cut
*/
VTABLE void init() {
Parrot_Sockaddr_attributes * const attrs =
(Parrot_Sockaddr_attributes *) PMC_data(SELF);
attrs->type = 0;
attrs->protocol = 0;
attrs->pointer = NULL;
attrs->len = 0;
PObj_custom_destroy_SET(SELF);
}
/*
=item C<void destroy()>
Destroys the PMC and frees all allocated memory.
=cut
*/
VTABLE void destroy() :no_wb {
Parrot_Sockaddr_attributes * const data = PARROT_SOCKADDR(SELF);
if (data) {
if (data->pointer)
mem_gc_free(INTERP, data->pointer);
data->pointer = NULL;
}
}
/*
=item C<PMC *clone()>
Creates a new Sockaddr PMC with the same contents and length as the current
one.
=cut
*/
VTABLE PMC *clone() :no_wb {
PMC * const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
const Parrot_Sockaddr_attributes * const old_attrs = PARROT_SOCKADDR(SELF);
Parrot_Sockaddr_attributes * const new_attrs = PARROT_SOCKADDR(dest);
new_attrs->type = old_attrs->type;
new_attrs->protocol = old_attrs->protocol;
new_attrs->len = old_attrs->len;
if (old_attrs->len) {
new_attrs->pointer = Parrot_gc_allocate_memory_chunk(INTERP,
old_attrs->len);
memcpy(new_attrs->pointer, old_attrs->pointer, old_attrs->len);
}
return dest;
}
/*
=item C<INTVAL get_bool()>
Returns true if the Sockaddr is defined.
=cut
*/
VTABLE INTVAL get_bool() :no_wb {
const Parrot_Sockaddr_attributes * const data = PARROT_SOCKADDR(SELF);
UNUSED(INTERP)
return data->pointer ? 1 : 0;
}
/*
=item C<INTVAL get_integer()>
Returns the length of the sockaddr struct.
=cut
*/
VTABLE INTVAL get_integer() :no_wb {
INTVAL len;
GET_ATTR_len(INTERP, SELF, len);
return len;
}
/*
=item C<void *get_pointer()>
Returns a pointer to the C<sockaddr_in> or C<sockaddr_in6>.
=cut
*/
VTABLE void *get_pointer() :no_wb {
const Parrot_Sockaddr_attributes * const data = PARROT_SOCKADDR(SELF);
UNUSED(INTERP)
return data->pointer;
}
/*
=item C<STRING *get_string()>
Returns the string representation of this sockaddr by calling C<getnameinfo(3)>.
=cut
*/
VTABLE STRING *get_string() :no_wb {
const Parrot_Sockaddr_attributes * const data = PARROT_SOCKADDR(SELF);
if (!data->pointer)
return Parrot_sprintf_c(INTERP, "(?)");
return Parrot_io_internal_getnameinfo(INTERP, data->pointer, data->len);
}
/*
=item C<INTVAL get_integer_keyed_int(INTVAL what)>
Returns family, type or protocol. Used for debugging.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL what) :no_wb {
const Parrot_Sockaddr_attributes * const data = PARROT_SOCKADDR(SELF);
UNUSED(INTERP)
switch (what) {
case 0:
return data->family;
case 1:
return data->type;
case 2:
return data->protocol;
default:
return -1;
}
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Jump to Line
Something went wrong with that request. Please try again.