summaryrefslogtreecommitdiff
path: root/milkway/mw-socket-address.c
blob: d260d80a0b2d6f3a02933449656238868afd6afa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Milkway
 *
 * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "milkwayint.h"
#include "milkway/mw-byteorder.h"
#include "milkway/mw-network-private.h"
#include "milkway/mw-socket-address.h"
#include "milkway/mw-inet-socket-address.h"

#include <stdlib.h>
#include <string.h>

mw_socket_address_t*
mw_socket_address_init(mw_socket_address_t *self)
{
    if (!mw_object_init(&self->base))
	return NULL;
    return self;
}

mw_socket_address_t*
mw_socket_address_new(void               *native,
		      size_t              size)
{
    struct sockaddr *addr = native;
    mw_socket_address_t *self = NULL;

    if (!addr)
	return NULL;

    if (addr->sa_family == AF_INET) {
	struct sockaddr_in *inaddr = native;
	mw_inet_address_t *inetaddr =
	    mw_inet_address_new_from_native(&inaddr->sin_addr,
					    MW_SOCKET_FAMILY_INET);
	if (!inetaddr)
	    return NULL;

	self = (mw_socket_address_t*)
	    mw_inet_socket_address_new(inetaddr, mw_ntohs(inaddr->sin_port));
	mw_object_unref(inetaddr);
    } else if (addr->sa_family == AF_INET6) {
	struct sockaddr_in6 *inaddr = native;
	mw_inet_address_t *inetaddr =
	    mw_inet_address_new_from_native(&inaddr->sin6_addr,
					    MW_SOCKET_FAMILY_INET6);
	if (!inetaddr)
	    return NULL;

	self = (mw_socket_address_t*)
	    mw_inet_socket_address_new(inetaddr, mw_ntohs(inaddr->sin6_port));
	mw_object_unref(inetaddr);
    }

    return self;
}

static void
mw_socket_address_finalize(mw_object_t *super)
{
    MW_SUPER_FINALIZE(super, MW_SOCKET_ADDRESS_TYPE)(super);
}

static void
mw_socket_address_type_init(mw_socket_address_type_t *self)
{
    self->base.finalize = mw_socket_address_finalize;
}

MW_DEFINE_GET_TYPE(mw_socket_address, mw_socket_address_type_t,
		   MW_OBJECT_TYPE, "MWSocketAddress", 0);