File: socket.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (872 lines) | stat: -rw-r--r-- 23,199 bytes parent folder | download
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
 * Copyright (c) 1998, 1999, 2000 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.
 */

/*
 * Implementation of socket factory and socket based on NATIVEOS() functions.
 *
 * This implementation is incomplete (asyncio listeners are missing),
 * untested and mostly likely not working.
 *
 * Use
 *
 *   oskit_error_t
 *   oskit_native_net_init(oskit_socket_factory_t **f);
 *
 * to access it.
 */
/*
 * OS Kit includes
 */
#include <oskit/dev/dev.h>
#include <oskit/net/socket.h>
#include <oskit/io/asyncio.h>
#include <oskit/io/bufio_stream.h>
#include <oskit/com/listener_mgr.h>
#include <oskit/clientos.h>
#include <oskit/net/socket.h>

#include "native.h"
#include "support.h"

/*
 * FreeBSD includes
 */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>

#include <errno.h>

/*
 * socket implementation
 */
struct oskit_sockimpl {
        oskit_socket_t  ioi;                    /* COM I/O interface Nr. 1 */
        oskit_stream_t  ios;                    /* COM I/O interface Nr. 2 */
        oskit_asyncio_t ioa;                    /* COM I/O interface Nr. 3 */
        unsigned        count;                  /* reference count */

        int 		fd;                    /* native OS fd */
	osenv_sleeprec_t    sleeprec;	/* For non-blocking I/O */
	oskit_u32_t	    selmask;	/* Select mask */
        struct listener_mgr *readers;   /* listeners for asyncio READ */
        struct listener_mgr *writers;   /* listeners for asyncio WRITE*/
};
typedef struct oskit_sockimpl oskit_sockimpl_t;

static oskit_sockimpl_t * create_sockimpl(int fd);

/*
 * implementations of query, addref and release dealing with
 * different COM interfaces
 */
/*
 * query
 */
static oskit_error_t
query(oskit_sockimpl_t *b, const struct oskit_guid *iid, void **out_ihandle)
{
        if (b == NULL)
                osenv_panic("%s:%d: null oskit_sockimpl_t", __FILE__, __LINE__);
        if (b->count == 0)
                osenv_panic("%s:%d: bad count", __FILE__, __LINE__);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_posixio_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_socket_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &b->ioi;
                ++b->count;
                return 0;
        }

        if (memcmp(iid, &oskit_stream_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &b->ios;
                ++b->count;
                return 0;
        }

        if (memcmp(iid, &oskit_asyncio_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &b->ioa;
                ++b->count;
                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL
socket_query(oskit_socket_t *f, const struct oskit_guid *iid, void **out_ihandle
)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)f;
        return query(b, iid, out_ihandle);
}

static OSKIT_COMDECL
opensocket_query(oskit_stream_t *f, const struct oskit_guid *iid,
                                 void **out_ihandle)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-1);
        return query(b, iid, out_ihandle);
}

static OSKIT_COMDECL
asyncio_query(oskit_asyncio_t *f, const struct oskit_guid *iid,
        void **out_ihandle)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-2);
        return query(b, iid, out_ihandle);
}

/*
 * addref
 */
static oskit_u32_t
addref(oskit_sockimpl_t *b)
{
        if (b == NULL)
                osenv_panic("%s:%d: null oskit_sockimpl_t", __FILE__, __LINE__);
        if (b->count == 0)
                osenv_panic("%s:%d: bad count", __FILE__, __LINE__);

        return ++b->count;
}

static OSKIT_COMDECL_U
socket_addref(oskit_socket_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)f;
        return addref(b);
}

static OSKIT_COMDECL_U
opensocket_addref(oskit_stream_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-1);
        return addref(b);
}

static OSKIT_COMDECL_U
asyncio_addref(oskit_asyncio_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-2);
        return addref(b);
}

/*
 * release
 */
static oskit_u32_t
release(oskit_sockimpl_t *si)
{
        unsigned newcount;

        if (si == NULL)
                osenv_panic("%s:%d: null sockimpl_t", __FILE__, __LINE__);
        if (si->count == 0)
                osenv_panic("%s:%d: bad count", __FILE__, __LINE__);

        if ((newcount = --si->count) == 0) {
		unregister_async_fd(si->fd);
		NATIVEOS(close)(si->fd);
                oskit_destroy_listener_mgr(si->readers);
                oskit_destroy_listener_mgr(si->writers);
                osenv_mem_free(si, 0, sizeof(*si));
        }
        return newcount;
}

static OSKIT_COMDECL_U
socket_release(oskit_socket_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)f;
        return release(b);
}

static OSKIT_COMDECL_U
opensocket_release(oskit_stream_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-1);
        return release(b);
}

static OSKIT_COMDECL_U
asyncio_release(oskit_asyncio_t *f)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)(f-2);
        return release(b);
}

/*******************************************************/
/******* Implementation of the oskit_socket_t if ********/
/*******************************************************/

/*** Operations inherited from oskit_posixio_t ***/
static OSKIT_COMDECL
socket_stat(oskit_socket_t *f, struct oskit_stat *out_stats)
{
	/* XXX do a fstat and convert the values */
        memset(out_stats, 0, sizeof *out_stats);
        out_stats->mode = OSKIT_S_IFSOCK;
        return 0;
}

static OSKIT_COMDECL
socket_setstat(oskit_socket_t *f, oskit_u32_t mask,
        const struct oskit_stat *stats)
{
        return OSKIT_ENOTSUP;
}

static OSKIT_COMDECL
socket_pathconf(oskit_socket_t *f, oskit_s32_t option, oskit_s32_t *out_val)
{
        return OSKIT_ENOTSUP;
}

#ifdef NATIVE_HAVE_SA_LEN
#define SA_FIX_OUT(sa, salen)	((void)0)
#define	SA_IN(sa, salen)	struct sockaddr *const SA = (void *)(sa);
#else
#define SA_FIX_OUT(sa, len) \
	((((struct oskit_sockaddr *) (sa))->sa_family = \
	  ((struct sockaddr *) (sa))->sa_family), \
	 ((struct oskit_sockaddr *) (sa))->sa_len = (len))
#define SA_IN(sa, salen)						      \
	struct sockaddr _sa, *const SA = &_sa;				      \
	assert((salen) <= sizeof _sa);					      \
	_sa.sa_family = (sa)->sa_family;				      \
	memcpy(_sa.sa_data, (sa)->sa_data,				      \
	       (salen) - offsetof(struct oskit_sockaddr, sa_data))
#endif

/*** Operations specific to oskit_socket_t ***/
static OSKIT_COMDECL
socket_accept(oskit_socket_t *s, struct oskit_sockaddr *name,
        oskit_size_t *anamelen, struct oskit_socket **newopenso)
{
        oskit_sockimpl_t *b;
	int	nfd;

	assert(offsetof(struct oskit_sockaddr, sa_data) ==
	       offsetof(struct sockaddr, sa_data));

	if ((nfd = threaded_accept(((oskit_sockimpl_t*)s)->fd,
				   (struct sockaddr *)name, anamelen)) < 0)
		return native_to_oskit_error(native_errno);

	SA_FIX_OUT(name, *anamelen);

        /* create a new sockimpl */
        b = create_sockimpl(nfd);               /* XXX error check */

        *newopenso = &b->ioi;
        return 0;
}

static OSKIT_COMDECL
socket_bind(oskit_socket_t *s, const struct oskit_sockaddr *name,
        oskit_size_t namelen)
{
	SA_IN(name, namelen);
        if (NATIVEOS(bind)(((oskit_sockimpl_t *)s)->fd,
			   SA, namelen) < 0)
		return native_to_oskit_error(native_errno);

	return 0;
}

static OSKIT_COMDECL
socket_connect(oskit_socket_t *s, const struct oskit_sockaddr *name,
        oskit_size_t namelen)
{
        oskit_sockimpl_t *b = (oskit_sockimpl_t *)s;
	SA_IN(name, namelen);

	if (threaded_connect(b->fd, SA, namelen) < 0)
		return native_to_oskit_error(native_errno);

        return 0;
}

static OSKIT_COMDECL
socket_listen(oskit_socket_t *s, oskit_u32_t n)
{
	if (NATIVEOS(listen)(((oskit_sockimpl_t *)s)->fd, n) < 0)
	    	return native_to_oskit_error(native_errno);

	return 0;
}

static OSKIT_COMDECL
socket_getsockname(oskit_socket_t *s, struct oskit_sockaddr *asa,
        oskit_size_t *alen)
{
    	if (NATIVEOS(getsockname)(((oskit_sockimpl_t *)s)->fd,
				  (struct sockaddr *)asa, alen) < 0)
	    	return native_to_oskit_error(native_errno);
	SA_FIX_OUT(asa, *alen);
	return 0;
}

/* XXX translate sockopts! */
static OSKIT_COMDECL
socket_setsockopt(oskit_socket_t *s, oskit_u32_t level, oskit_u32_t name,
                const void *val, oskit_size_t valsize)
{
    	if (NATIVEOS(setsockopt)(((oskit_sockimpl_t *)s)->fd,
				     level, name, val, valsize) < 0)
	    	return native_to_oskit_error(native_errno);

	return 0;

}

static OSKIT_COMDECL
socket_sendto(oskit_socket_t *s, const void *msg, oskit_size_t len,
                oskit_u32_t flags, const struct oskit_sockaddr *to,
                oskit_size_t tolen, oskit_size_t *retval)
{
	int		  count;
	SA_IN(to, tolen);

	if ((count = threaded_sendto(((oskit_sockimpl_t *)s)->fd,
                                     msg, len, flags,
                                     SA, tolen)) < 0)
		return native_to_oskit_error(native_errno);

	*retval = (oskit_size_t) count;
	return 0;
}

static OSKIT_COMDECL
socket_recvfrom(oskit_socket_t *s, void *buf,
                oskit_size_t len, oskit_u32_t flags,
                struct oskit_sockaddr *from, oskit_size_t *fromlen,
                oskit_size_t *retval)
{
	int		  count;

	if ((count = threaded_recvfrom(((oskit_sockimpl_t *)s)->fd,
                                       buf, len, flags,
                                       (struct sockaddr *)from, fromlen)) < 0)
		return native_to_oskit_error(native_errno);
	SA_FIX_OUT(from, *fromlen);
	*retval = (oskit_size_t) count;
	return 0;
}

static OSKIT_COMDECL
socket_getsockopt(oskit_socket_t *s, oskit_u32_t level, oskit_u32_t name,
                void *val, oskit_size_t *valsize)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
socket_sendmsg(oskit_socket_t *s, const struct oskit_msghdr *msg,
                oskit_u32_t flags, oskit_size_t *retval)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
socket_recvmsg(oskit_socket_t *s, struct oskit_msghdr *msg,
                oskit_u32_t flags, oskit_size_t *retval)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
socket_getpeername(oskit_socket_t *f,
        struct oskit_sockaddr *asa, oskit_size_t *alen)
{
        oskit_sockimpl_t *s = (oskit_sockimpl_t *)f;
        if (NATIVEOS(getpeername)(s->fd, (struct sockaddr *)asa, alen) < 0)
	    	return native_to_oskit_error(native_errno);
	SA_FIX_OUT(asa, *alen);
	return 0;
}

static OSKIT_COMDECL
socket_shutdown(oskit_socket_t *f, oskit_u32_t how)
{
        if (NATIVEOS(shutdown)(((oskit_sockimpl_t *)f)->fd, how) < 0)
	    	return native_to_oskit_error(native_errno);

	return 0;
}

/*******************************************************/
/******* Implementation of the oskit_stream_t if ********/
/*******************************************************/

/*** Operations inherited from oskit_stream interface ***/
static OSKIT_COMDECL
opensocket_read(oskit_stream_t *f, void *buf, oskit_u32_t len,
                                oskit_u32_t *out_actual)
{
        oskit_sockimpl_t *s = (oskit_sockimpl_t *)(f-1);
	int		  count;

	if ((count = threaded_read(s->fd, buf, len)) < 0)
		return native_to_oskit_error(native_errno);

	*out_actual = count;
	return 0;
}

static OSKIT_COMDECL
opensocket_write(oskit_stream_t *f, const void *buf,
                                 oskit_u32_t len, oskit_u32_t *out_actual)
{
        oskit_sockimpl_t *s = (oskit_sockimpl_t *)(f-1);
	int		  count;

	if ((count = threaded_write(s->fd, buf, len)) < 0)
		return native_to_oskit_error(native_errno);

	*out_actual = count;
	return 0;
}

static OSKIT_COMDECL
opensocket_seek(oskit_stream_t *f, oskit_s64_t ofs,
        oskit_seek_t whence, oskit_u64_t *out_newpos)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_setsize(oskit_stream_t *f, oskit_u64_t new_size)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_copyto(oskit_stream_t *f, oskit_stream_t *dst,
        oskit_u64_t size,
        oskit_u64_t *out_read,
        oskit_u64_t *out_written)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_commit(oskit_stream_t *f, oskit_u32_t commit_flags)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_revert(oskit_stream_t *f)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_lock_region(oskit_stream_t *f,
        oskit_u64_t offset, oskit_u64_t size, oskit_u32_t lock_type)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_unlock_region(oskit_stream_t *f,
        oskit_u64_t offset, oskit_u64_t size, oskit_u32_t lock_type)
{
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_stat(oskit_stream_t *f, oskit_stream_stat_t *out_stat,
        oskit_u32_t stat_flags)
{
        /* this should be implemented soon */
        return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
opensocket_clone(oskit_stream_t *f, oskit_stream_t **out_stream)
{
        return OSKIT_E_NOTIMPL;
}

/*******************************************************/
/******* Implementation of the oskit_asyncio_t if *******/
/*******************************************************/

/*
 * return a mask with all conditions that currently apply to that socket
 * must be called with splnet()!
 */
static oskit_u32_t
get_socket_conditions(int fd)
{
        oskit_u32_t res = 0;
	int r;
	struct timeval nulltime = { 0, 0 };
	fd_set	in, out, err;

	FD_ZERO(&in);
	FD_ZERO(&out);
	FD_ZERO(&err);
	FD_SET(fd, &in);
	FD_SET(fd, &out);
	FD_SET(fd, &err);

	/* poll socket */
	r = NATIVEOS(select) (fd + 1, &in, &out, &err, &nulltime);
	if (r < 1)
		return res;

	if (FD_ISSET(fd, &in))
                res |= OSKIT_ASYNCIO_READABLE;
	if (FD_ISSET(fd, &out))
                res |= OSKIT_ASYNCIO_WRITABLE;
	if (FD_ISSET(fd, &err))
                res |= OSKIT_ASYNCIO_EXCEPTION;

        return res;
}

/*
 * Callback for asyncio interface. Determine which conditions are
 * ready and requested, and initiate those callbacks.
 */
static void
asyncio_callback(void *arg)
{
        oskit_sockimpl_t *si = (oskit_sockimpl_t *) arg;
	oskit_u32_t	 ready;
	unsigned	 iotype = 0;

	ready = si->selmask & get_socket_conditions(si->fd);

        /* for read and exceptional conditions */
        if (ready & (OSKIT_ASYNCIO_READABLE | OSKIT_ASYNCIO_EXCEPTION)) {
                oskit_listener_mgr_notify(si->readers);
		iotype |= IOTYPE_READ;
	}

        /* for write */
        if (ready & OSKIT_ASYNCIO_WRITABLE) {
                oskit_listener_mgr_notify(si->writers);
		iotype |= IOTYPE_WRITE;
        }
}

/*
 * Poll for currently pending asynchronous I/O conditions.
 * If successful, returns a mask of the OSKIT_ASYNC_IO_* flags above,
 * indicating which conditions are currently present.
 */
static OSKIT_COMDECL
asyncio_poll(oskit_asyncio_t *f)
{
        oskit_sockimpl_t *si = (oskit_sockimpl_t *)(f-2);
	oskit_u32_t	 cond;
	int		 enabled = osenv_intr_enabled();

	if (enabled)
		osenv_intr_disable();
	cond = get_socket_conditions(si->fd);
	if (enabled)
		osenv_intr_enable();

	return cond;
}

/*
 * Add a callback object (a "listener" for async I/O events).
 * When an event of interest occurs on this I/O object
 * (i.e., when one of the three I/O conditions becomes true),
 * all registered listeners will be called.
 * Also, if successful, this method returns a mask
 * describing which of the OSKIT_ASYNC_IO_* conditions are already true,
 * which the caller must check in order to avoid missing events
 * that occur just before the listener is registered.
 */
static OSKIT_COMDECL
asyncio_add_listener(oskit_asyncio_t *f, struct oskit_listener *l,
        oskit_s32_t mask)
{
        oskit_sockimpl_t *si = (oskit_sockimpl_t *)(f-2);
        oskit_s32_t     cond;
	int		enabled = osenv_intr_enabled();
	unsigned	iotype = 0;

	if (enabled)
		osenv_intr_disable();

        cond = get_socket_conditions(si->fd);

        /* for read and exceptional conditions */
        if (mask & (OSKIT_ASYNCIO_READABLE | OSKIT_ASYNCIO_EXCEPTION)) {
                oskit_listener_mgr_add(si->readers, l);
		iotype |= IOTYPE_READ;
	}

        /* for write */
        if (mask & OSKIT_ASYNCIO_WRITABLE) {
                oskit_listener_mgr_add(si->writers, l);
		iotype |= IOTYPE_WRITE;
        }

	si->selmask |= mask;
	register_async_fd(si->fd, iotype, asyncio_callback, si);

	if (enabled)
		osenv_intr_enable();
        return cond;
}

/*
 * Remove a previously registered listener callback object.
 * Returns an error if the specified callback has not been registered.
 */
static OSKIT_COMDECL
asyncio_remove_listener(oskit_asyncio_t *f, struct oskit_listener *l0)
{
        oskit_sockimpl_t *si = (oskit_sockimpl_t *)(f-2);
	int		 enabled = osenv_intr_enabled();
	oskit_error_t	 rc1, rc2;

        /*
         * protect against interrupts, since clients might call
         * remove_listener out of the asynciolistener callback
         */
	if (enabled)
		osenv_intr_disable();

        /*
         * we don't know where was added - if at all - so let's check
         * both lists
         *
         * turn off notifications if no listeners left
         */
        rc1 = oskit_listener_mgr_remove(si->readers, l0);
        if (oskit_listener_mgr_count(si->readers) == 0)
		si->selmask &=
			~(OSKIT_ASYNCIO_READABLE | OSKIT_ASYNCIO_EXCEPTION);

        rc2 = oskit_listener_mgr_remove(si->writers, l0);
        if (oskit_listener_mgr_count(si->writers) == 0)
		si->selmask &= ~(OSKIT_ASYNCIO_WRITABLE);

	if (! si->selmask)
		unregister_async_fd(si->fd);

	if (enabled)
		osenv_intr_enable();

        /* flag error if both removes failed */
        return (rc1 && rc2) ? OSKIT_E_INVALIDARG : 0;   /* is that right ? */
}

/*
 * return the number of bytes that can be read, basically ioctl(FIONREAD)
 */
static OSKIT_COMDECL
asyncio_readable(oskit_asyncio_t *f)
{
        oskit_sockimpl_t *si = (oskit_sockimpl_t *)(f-2);
	int b;
	int rc;
	rc = NATIVEOS(ioctl)(si->fd, FIONREAD, &b);
	return b;	/* XXX ??? */
}

/***************************************************************************/
/*
 * vtables for interfaces exported by sockets
 */
static struct oskit_socket_ops sockops =
{
        socket_query,
        socket_addref,
        socket_release,
        socket_stat,
        socket_setstat,
        socket_pathconf,
        socket_accept,
        socket_bind,
        socket_connect,
        socket_shutdown,
        socket_listen,
        socket_getsockname,
        socket_getpeername,
        socket_getsockopt,
        socket_setsockopt,
        socket_sendto,
        socket_recvfrom,
        socket_sendmsg,
        socket_recvmsg
};

static struct oskit_stream_ops opensockops =
{
        opensocket_query,
        opensocket_addref,
        opensocket_release,
        opensocket_read,
        opensocket_write,
        opensocket_seek,
        opensocket_setsize,
        opensocket_copyto,
        opensocket_commit,
        opensocket_revert,
        opensocket_lock_region,
        opensocket_unlock_region,
        opensocket_stat,
        opensocket_clone
};

static struct oskit_asyncio_ops asyncioops =
{
        asyncio_query,
        asyncio_addref,
        asyncio_release,
        asyncio_poll,
        asyncio_add_listener,
        asyncio_remove_listener,
        asyncio_readable
};

/*
 * create the COM part of a new sockimpl object
 */
static oskit_sockimpl_t *
create_sockimpl(int fd)
{
        oskit_sockimpl_t *si = osenv_mem_alloc(sizeof(*si), 0, 0);

        if (si == NULL)
                return NULL;
        memset(si, 0, sizeof(*si));

        si->count = 1;
        si->ioi.ops = &sockops;
        si->ios.ops = &opensockops;
        si->ioa.ops = &asyncioops;

        si->fd = fd;
        si->readers = oskit_create_listener_mgr((oskit_iunknown_t *)&si->ioa);
        si->writers = oskit_create_listener_mgr((oskit_iunknown_t *)&si->ioa);

	set_async_fd(fd);

        return si;
}

/***************************************************************************/
/*
 * methods of socket_factories
 */
static OSKIT_COMDECL
socket_factory_query(oskit_socket_factory_t *b,
        const struct oskit_guid *iid, void **out_ihandle)
{
        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_socket_factory_iid, sizeof(*iid)) == 0) {
                *out_ihandle = b;
                return 0;
        }
        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
socket_factory_addref(oskit_socket_factory_t *b)
{
        return 1;
}

static OSKIT_COMDECL_U
socket_factory_release(oskit_socket_factory_t *b)
{
        return 1;
}

/*
 * function to create a socket - analog to socket(2)
 */
static OSKIT_COMDECL
socket_factory_create(
        oskit_socket_factory_t *factory,
        oskit_u32_t domain,
        oskit_u32_t type, oskit_u32_t protocol, oskit_socket_t **aso)
{
        oskit_sockimpl_t *b;
	int fd;

        fd = NATIVEOS(socket)(domain, type, protocol);
        if (fd < 0)
                return native_to_oskit_error(native_errno);

        b = create_sockimpl(fd);
        *aso = &b->ioi;
        return 0;
}

/*
 * function to create a pair of connected sockets - analog to socketpair(2)
 */
static OSKIT_COMDECL
socket_factory_create_pair(
        oskit_socket_factory_t *factory,
        oskit_u32_t domain,
        oskit_u32_t type, oskit_u32_t protocol,
	oskit_socket_t **aso1, oskit_socket_t **aso2)
{
        oskit_sockimpl_t *b;
	int fds[2];

        if (NATIVEOS(socketpair)(domain, type, protocol, fds) < 0);
                return native_to_oskit_error(native_errno);

        b = create_sockimpl(fds[0]);
        *aso1 = &b->ioi;
        b = create_sockimpl(fds[1]);
        *aso2 = &b->ioi;
        return 0;
}

static struct oskit_socket_factory_ops sf_ops = {
        socket_factory_query,
        socket_factory_addref,
        socket_factory_release,
        socket_factory_create,
        socket_factory_create_pair
};

static
struct oskit_socket_factory oskit_native_socket_factory = { &sf_ops };

oskit_error_t
oskit_native_net_init(oskit_socket_factory_t **f)
{
	char hostname[256];

	NATIVEOS(gethostname)(hostname, 256);
	oskit_clientos_sethostname(hostname, 256);
        *f = &oskit_native_socket_factory;
        return 0;
}