Skip to content

Commit

Permalink
[GH #606] Refactor Socket.getprotobyname, add test
Browse files Browse the repository at this point in the history
Put it into platform/generic/socket.c
  • Loading branch information
Reini Urban committed Dec 13, 2012
1 parent bc6d9e6 commit b21673b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 39 deletions.
10 changes: 5 additions & 5 deletions include/parrot/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ Parrot_io_fprintf(PARROT_INTERP,

PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL Parrot_io_get_proto_by_name(PARROT_INTERP, ARGIN(STRING * name))
PIOHANDLE Parrot_io_getfd(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);

PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PIOHANDLE Parrot_io_getfd(PARROT_INTERP, ARGIN(PMC *pmc))
INTVAL Parrot_io_getprotobyname(PARROT_INTERP, ARGIN(STRING * name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);

Expand Down Expand Up @@ -724,12 +724,12 @@ INTVAL Parrot_io_write_byte_buffer_pmc(PARROT_INTERP,
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc) \
, PARROT_ASSERT_ARG(s))
#define ASSERT_ARGS_Parrot_io_get_proto_by_name __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(name))
#define ASSERT_ARGS_Parrot_io_getfd __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_Parrot_io_getprotobyname __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(name))
#define ASSERT_ARGS_Parrot_io_init __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_io_is_async __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
Expand Down
3 changes: 2 additions & 1 deletion include/parrot/platform_interface.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2003-2011, Parrot Foundation.
* Copyright (C) 2003-2012, Parrot Foundation.
*/

#ifndef PARROT_PLATFORM_INTERFACE_H_GUARD
Expand Down Expand Up @@ -101,6 +101,7 @@ PMC *Parrot_io_internal_getaddrinfo(PARROT_INTERP, ARGIN(STRING *addr), INTVAL p
INTVAL Parrot_io_internal_addr_match(PARROT_INTERP, ARGIN(PMC *sa), INTVAL family, INTVAL type,
INTVAL protocol);
STRING *Parrot_io_internal_getnameinfo(PARROT_INTERP, ARGIN(const void *addr), INTVAL addr_len);
INTVAL Parrot_io_internal_getprotobyname(PARROT_INTERP, ARGIN(STRING *name));
PIOHANDLE Parrot_io_internal_socket(PARROT_INTERP, int fam, int type, int proto);
void Parrot_io_internal_connect(PARROT_INTERP, PIOHANDLE handle, ARGIN(void *addr), INTVAL addr_len);
void Parrot_io_internal_bind(PARROT_INTERP, PIOHANDLE handle, ARGIN(void *addr), INTVAL addr_len);
Expand Down
8 changes: 4 additions & 4 deletions src/io/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ Parrot_io_reencode_string_for_handle(PARROT_INTERP, ARGIN(PMC *handle), ARGIN_NU

/*
=item C<INTVAL Parrot_io_get_proto_by_name(PARROT_INTERP, STRING * name)>
=item C<INTVAL Parrot_io_getprotobyname(PARROT_INTERP, STRING * name)>
Return a protocol number given a protocol name.
Expand All @@ -2128,11 +2128,11 @@ Return a protocol number given a protocol name.
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_io_get_proto_by_name(PARROT_INTERP, ARGIN(STRING * name))
Parrot_io_getprotobyname(PARROT_INTERP, ARGIN(STRING * name))
{
ASSERT_ARGS(Parrot_io_get_proto_by_name)
ASSERT_ARGS(Parrot_io_getprotobyname)

return PIO_GET_PROTO_BY_NAME(interp, name);
return Parrot_io_internal_getprotobyname(interp, name);
}


Expand Down
26 changes: 0 additions & 26 deletions src/platform/generic/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,32 +514,6 @@ Parrot_io_internal_pipe(SHIM_INTERP, ARGMOD(PIOHANDLE *reader), ARGMOD(PIOHANDLE
return rv;
}

/*
=item C<INTVAL Parrot_io_get_proto_by_name(PARROT_INTERP, STRING *name)>
Return a protocol number based on the name of a protocol
=cut
*/

PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_io_get_proto_by_name(PARROT_INTERP, ARGIN(STRING *name))
{
ASSERT_ARGS(Parrot_io_get_proto_by_name)

char * const s = Parrot_str_to_cstring(interp, name);
struct protoent * const protoent = getprotobyname(s);
int proto_number;

Parrot_str_free_cstring(s);
proto_number = protoent->p_proto;
return proto_number;
}


/*
=back
Expand Down
21 changes: 21 additions & 0 deletions src/platform/generic/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,27 @@ Parrot_io_internal_getnameinfo(PARROT_INTERP, ARGIN(const void *addr), INTVAL le

/*
=item C<INTVAL Parrot_io_internal_getprotobyname(PARROT_INTERP, STRING *name)>
Return a protocol number based on the name of a protocol.
=cut
*/

PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_io_internal_getprotobyname(PARROT_INTERP, ARGIN(STRING *name))
{
char * const s = Parrot_str_to_cstring(interp, name);
struct protoent * const protoent = getprotobyname(s);

Parrot_str_free_cstring(s);
return protoent->p_proto;
}

/*
=item C<PIOHANDLE Parrot_io_internal_socket(PARROT_INTERP, int fam, int type,
int proto)>
Expand Down
4 changes: 2 additions & 2 deletions src/pmc/socket.pmc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2008-2011, Parrot Foundation.
Copyright (C) 2008-2012, Parrot Foundation.

=head1 NAME

Expand Down Expand Up @@ -278,7 +278,7 @@ necessary when opening sockets in raw mode.
*/

METHOD getprotobyname(STRING * name) {
INTVAL proto = Parrot_io_get_proto_by_name(INTERP, name);
INTVAL proto = Parrot_io_getprotobyname(INTERP, name);
RETURN(INTVAL proto);
}

Expand Down
9 changes: 8 additions & 1 deletion t/pmc/socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ stack, so we don't need to check if this parrot is IPv6-aware.
.sub main :main
.include 'test_more.pir'
plan(19)
plan(20)
test_init()
test_get_fd()
Expand All @@ -37,6 +37,7 @@ stack, so we don't need to check if this parrot is IPv6-aware.
test_tcp_socket6()
test_udp_socket()
test_udp_socket6()
test_getprotobyname()
test_server()
.end
Expand Down Expand Up @@ -80,6 +81,12 @@ stack, so we don't need to check if this parrot is IPv6-aware.
is($N0, 1, 'Socket is_closed returned 1 to new socket')
.end
.sub test_getprotobyname
new $P0, ['Socket']
$N0 = $P0.'getprotobyname'("tcp")
is($N0, 6, 'Socket getprotobyname(tcp) returned 6')
.end
.sub test_clone
new $P0, ['Socket']
$P1 = $P0."sockaddr"("localhost", 1234)
Expand Down

0 comments on commit b21673b

Please sign in to comment.