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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Copyright (c) 1998 The University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit 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 GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* A send-only UDP interface without fragmentation.
*/
#include <oskit/net/socket.h>
#include <oskit/io/bufio.h>
#include <oskit/io/netio.h>
#include <oskit/c/netinet/in.h>
#include <oskit/c/netinet/udp.h>
#include <oskit/c/netinet/ip.h>
#include <oskit/c/netinet/udp.h>
#include <oskit/c/arpa/inet.h>
#include <oskit/c/string.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/assert.h>
#include <oskit/net/ether.h>
#include <oskit/fudp.h>
#include "fudp.h"
/*
* XXX This is actually not correct because
* in COM the callee is supposed to pop args.
*/
static OSKIT_COMDECL
notimpl(void)
{
return OSKIT_E_NOTIMPL;
}
/* Implementation of the COM socket interface for FakeUDP. */
struct sk_impl {
oskit_socket_t sk_ioi;
oskit_u32_t sk_count;
oskit_u16_t sk_port; /* net order */
struct in_addr sk_ipaddr; /* net order */
oskit_netio_t *sk_netio;
};
static OSKIT_COMDECL
sk_query(oskit_socket_t *sk, const struct oskit_guid *iid, void **out_ihandle)
{
struct sk_impl *ski = (void *)sk;
assert(ski && ski->sk_count);
if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_socket_iid, sizeof(*iid)) == 0) {
*out_ihandle = &ski->sk_ioi;
return 0;
}
*out_ihandle = NULL;
return OSKIT_E_NOINTERFACE;
}
static OSKIT_COMDECL_U
sk_addref(oskit_socket_t *sk)
{
struct sk_impl *ski = (void *)sk;
assert(ski && ski->sk_count);
return ++ski->sk_count;
}
static OSKIT_COMDECL_U
sk_release(oskit_socket_t *sk)
{
struct sk_impl *ski = (void *)sk;
assert(ski && ski->sk_count);
if (--ski->sk_count)
return ski->sk_count;
if (ski->sk_netio)
oskit_netio_release(ski->sk_netio);
free(ski);
return 0;
}
/*
* Assign the port and ipaddr for the socket to use.
*
* XXX In the future we could pick a port in `create' and then
* this routine will be only useful if you need a specific name
* for a socket.
* Maybe we could define a new family type and have the sockaddr
* contain the netio?
*/
static OSKIT_COMDECL
sk_bind(oskit_socket_t *sk,
const struct oskit_sockaddr *name,
oskit_size_t namelen)
{
struct sk_impl *ski = (void *)sk;
struct sockaddr_in *sin = (void *)name;
assert(ski && ski->sk_count);
if (sin->sin_family != OSKIT_AF_INET)
return OSKIT_E_INVALIDARG;
ski->sk_ipaddr.s_addr = sin->sin_addr.s_addr;
ski->sk_port = sin->sin_port;
return 0;
}
static OSKIT_COMDECL
sk_setsockopt(oskit_socket_t *sk, oskit_u32_t level,
oskit_u32_t name, const void *val,
oskit_size_t ignored_valsize)
{
struct sk_impl *ski = (void *)sk;
assert(ski && ski->sk_count);
if (level != OSKIT_SOL_SOCKET ||
name != OSKIT_SO_NETIO ||
val == NULL)
return OSKIT_E_INVALIDARG;
ski->sk_netio = (oskit_netio_t *)val;
oskit_netio_addref(ski->sk_netio);
return 0;
}
/*
* Send a message via UDP; no fragmentation.
* Fill in the UDP header and pass to ip_send.
*/
static OSKIT_COMDECL
sk_sendto(oskit_socket_t *sk,
const void *msg, oskit_size_t msglen,
oskit_u32_t ignored_flags,
const struct oskit_sockaddr *to, oskit_size_t tolen,
oskit_size_t *retval)
{
struct sk_impl *ski = (void *)sk;
oskit_bufio_t *b;
oskit_size_t blen;
struct udphdr *udp;
struct sockaddr_in *sin = (void *)to;
oskit_error_t err;
assert(ski && ski->sk_count);
if (ski->sk_port == 0 ||
ski->sk_ipaddr.s_addr == htonl(INADDR_ANY) ||
ski->sk_netio == NULL ||
sin->sin_family != OSKIT_AF_INET ||
ignored_flags)
return OSKIT_E_INVALIDARG;
/*
* Allocate the bufio.
* It contains the whole ethernet frame.
* The various layers below us fill in their respective parts.
*/
blen = sizeof(struct ether_header) +
sizeof(struct ip) +
sizeof(struct udphdr) +
msglen;
if (blen > ETH_MAX_PACKET)
return OSKIT_EMSGSIZE;
b = oskit_bufio_create(blen);
if (b == NULL)
return OSKIT_E_OUTOFMEMORY;
/*
* Map it in so `udp' points to the UDP part.
*/
err = oskit_bufio_map(b, (void **)&udp,
sizeof(struct ether_header) + sizeof(struct ip),
sizeof(struct udphdr) + msglen);
if (err)
goto done;
/*
* Fill in the UDP header and payload.
*/
udp->uh_sport = ski->sk_port; /* already in net order */
udp->uh_dport = sin->sin_port; /* already in net order */
udp->uh_ulen = htons(sizeof(struct udphdr) + msglen);
udp->uh_sum = 0;
memcpy((char *)udp + sizeof(struct udphdr), msg, msglen);
oskit_bufio_unmap(b, udp,
sizeof(struct ether_header) + sizeof(struct ip),
sizeof(struct udphdr) + msglen);
err = ip_send(ski->sk_netio, &ski->sk_ipaddr, &sin->sin_addr, b, blen);
if (! err)
*retval = msglen;
done:
oskit_bufio_release(b);
return err;
}
static struct oskit_socket_ops sk_ops = {
sk_query,
sk_addref,
sk_release,
(void *)notimpl, /* stat */
(void *)notimpl, /* setstat */
(void *)notimpl, /* pathconf */
(void *)notimpl, /* accept */
sk_bind,
(void *)notimpl, /* connect */
(void *)notimpl, /* shutdown */
(void *)notimpl, /* listen */
(void *)notimpl, /* getsockname */
(void *)notimpl, /* getpeername */
(void *)notimpl, /* getsockopt */
sk_setsockopt,
sk_sendto,
(void *)notimpl, /* recvfrom */
(void *)notimpl, /* sendmsg */
(void *)notimpl, /* recvmsg */
};
/* Implementation of the FakeUDP socket factory. */
struct sf_impl {
oskit_socket_factory_t sf_ioi;
oskit_u32_t sf_count;
};
static OSKIT_COMDECL
sf_query(oskit_socket_factory_t *sf, const struct oskit_guid *iid,
void **out_ihandle)
{
struct sf_impl *sfi = (void *)sf;
assert(sfi && sfi->sf_count);
if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_socket_factory_iid, sizeof(*iid)) == 0) {
*out_ihandle = &sfi->sf_ioi;
return 0;
}
*out_ihandle = NULL;
return OSKIT_E_NOINTERFACE;
}
static OSKIT_COMDECL_U
sf_addref(oskit_socket_factory_t *sf)
{
struct sf_impl *sfi = (void *)sf;
assert(sfi && sfi->sf_count);
return ++sfi->sf_count;
}
/*
* Roughly the opposite of fudp_init.
*/
static OSKIT_COMDECL_U
sf_release(oskit_socket_factory_t *sf)
{
struct sf_impl *sfi = (void *)sf;
assert(sfi && sfi->sf_count);
if (--sfi->sf_count)
return sfi->sf_count;
free(sfi);
return 0;
}
/*
* Function to create a send-only UDP socket - analog to socket(2).
* The socket created is not yet ready for use, it needs a
* bind() call to give it a port & ipaddr and a setsockopt() to give it
* the netio to use.
*/
static OSKIT_COMDECL
sf_create(oskit_socket_factory_t *sf,
oskit_u32_t domain, oskit_u32_t type, oskit_u32_t protocol,
oskit_socket_t **out_sock)
{
struct sf_impl *sfi = (void *)sf;
struct sk_impl *ski;
assert(sfi && sfi->sf_count);
if (domain != OSKIT_AF_INET ||
type != OSKIT_SOCK_DGRAM ||
protocol != IPPROTO_UDP)
return OSKIT_E_INVALIDARG;
ski = malloc(sizeof *ski);
if (ski == NULL)
return OSKIT_E_OUTOFMEMORY;
ski->sk_ioi.ops = &sk_ops;
ski->sk_count = 1;
ski->sk_port = 0;
ski->sk_ipaddr.s_addr = htonl(INADDR_ANY);
ski->sk_netio = NULL;
*out_sock = &ski->sk_ioi;
return 0;
}
static OSKIT_COMDECL
sf_create_pair(oskit_socket_factory_t *sf,
oskit_u32_t domain, oskit_u32_t type, oskit_u32_t protocol,
oskit_socket_t **out_sock1, oskit_socket_t **out_sock2)
{
return OSKIT_E_NOTIMPL;
}
static struct oskit_socket_factory_ops sf_ops = {
sf_query,
sf_addref,
sf_release,
sf_create,
sf_create_pair
};
/*
* Get a socket factory.
* Roughly the opposite of sf_release.
*/
oskit_error_t
fudp_init(oskit_socket_factory_t **out_factory)
{
struct sf_impl *sfi;
sfi = malloc(sizeof *sfi);
if (sfi == NULL)
return OSKIT_E_OUTOFMEMORY;
sfi->sf_ioi.ops = &sf_ops;
sfi->sf_count = 1;
*out_factory = &sfi->sf_ioi;
return 0;
}
|