monotone

monotone Mtn Source Tree

Root/netxx/sockaddr.cxx

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the implementation of the Netxx::SockAddr class.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include "sockaddr.h"
42
43// standard includes
44#include <cstring>
45
46//####################################################################
47Netxx::SockAddr::SockAddr (Socket::Type type, port_type port)
48 : sa_(0), sa_size_(0)
49{
50 switch (type) {
51case Socket::TCP:
52case Socket::UDP:
53 setup(AF_INET, port);
54 break;
55
56# ifndef NETXX_NO_INET6
57case Socket::TCP6:
58case Socket::UDP6:
59 setup(AF_INET6, port);
60 break;
61# endif
62
63# ifndef WIN32
64case Socket::LOCALSTREAM:
65case Socket::LOCALDGRAM:
66 setup(AF_LOCAL, 0);
67 break;
68# endif
69
70default:
71 setup(AF_INET, port);
72 break;
73 }
74}
75//####################################################################
76Netxx::SockAddr::SockAddr (int af_type, port_type port)
77 : sa_(0), sa_size_(0)
78{
79 setup(af_type, port);
80}
81//####################################################################
82sockaddr* Netxx::SockAddr::get_sa (void)
83{
84 return sa_;
85}
86//####################################################################
87Netxx::size_type Netxx::SockAddr::get_sa_size (void)
88{
89 return sa_size_;
90}
91//####################################################################
92void Netxx::SockAddr::setup (int af_type, port_type port)
93{
94 switch (af_type) {
95case AF_INET:
96 std::memset(&(sa_union_.sa_in), 0, sizeof(sa_union_.sa_in));
97 sa_union_.sa_in.sin_family = AF_INET;
98 if (port) sa_union_.sa_in.sin_port = htons(port);
99
100 sa_size_ = sizeof(sa_union_.sa_in);
101 sa_ = &sa_union_.sa;
102 break;
103
104
105# ifndef NETXX_NO_INET6
106case AF_INET6:
107 std::memset(&(sa_union_.sa_in6), 0, sizeof(sa_union_.sa_in6));
108 sa_union_.sa_in6.sin6_family = AF_INET6;
109 if (port) sa_union_.sa_in6.sin6_port = htons(port);
110
111 sa_size_ = sizeof(sa_union_.sa_in6);
112 sa_ = &sa_union_.sa;
113 break;
114# endif
115
116
117# ifndef WIN32
118case AF_LOCAL:
119 std::memset(&(sa_union_.sa_un), 0, sizeof(sa_union_.sa_un));
120 sa_union_.sa_un.sun_family = AF_LOCAL;
121
122 sa_size_ = sizeof(sa_union_.sa_un);
123 sa_ = &sa_union_.sa;
124 break;
125# endif
126
127
128default:
129 throw Exception("Netxx bug: bad socket type given to Netxx:SockAddr");
130 }
131}
132//####################################################################

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status