File: wvunixsocket.h

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (112 lines) | stat: -rw-r--r-- 3,512 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C++ -*-
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 */ 
#ifndef __WVUNIXSOCKET_H
#define __WVUNIXSOCKET_H

#include "wvfdstream.h"
#include "wvaddr.h"

class WvIStreamList;
class WvUnixListener;

/**
 * WvStream-based Unix domain socket connection class.
 * 
 * Unlike WvTCPConn, WvUnixConn makes connections synchronously because either
 * the remote server is there, or it isn't.  For convenience, we'll just
 * ignore situations where it's a local server but still slow.
 * 
 * FIXME: support SOCK_DGRAM mode somehow.  This is a bit tricky since the
 * listener/connection separation doesn't make as much sense then.  I guess we
 * could just ignore the listener or something...
 * 
 * FIXME: use the weird credential-passing stuff to exchange pid, uid, and gid
 * with the remote end of the socket.  See the unix(7) man page.  This would
 * be very cool for authentication purposes.
 */
class WvUnixConn : public WvFDStream
{
    friend class WvUnixListener;
protected:
    WvUnixAddr addr;
    
    /** connect an already-open socket (used by WvUnixListener) */
    WvUnixConn(int _fd, const WvUnixAddr &_addr);
    
public:
    /** connect a new socket */
    WvUnixConn(const WvUnixAddr &_addr);

    virtual ~WvUnixConn();
    
    /**
     * the local address of this socket (ie. from getsockname())
     * really useful only for transparent proxies, but always available.
     * may be 0.0.0.0 if we did not bind explicitly!
     */
    const WvUnixAddr &localaddr() { return addr; }
    
    /**
     * return the remote address (source of all incoming packets),
     * which is a constant for any given connection.
     * This doesn't make much sense in Unix domain sockets, so we just
     * return localaddr() instead.
     */
    virtual const WvUnixAddr *src() const;
};

/** Server end of a Unix Sockets stream */
class WvUnixListener : public WvFDStream
{
public:
    WvUnixListener(const WvUnixAddr &_addr, int create_mode);
    virtual ~WvUnixListener();
    
    virtual void close();
    
    /**
     * return a new WvUnixConn socket corresponding to a newly-accepted
     * connection.  If no connection is ready immediately, we wait for
     * one indefinitely.  You can use select(read=true) to check for a
     * waiting connection.
     */
    WvUnixConn *accept();
    
    /**
     * set a callback() function that automatically accepts new WvUnixConn
     * connections, assigning them their own callback function 'callfunc'
     * with parameter 'userdata.'  Pass list==NULL or define your own
     * own callback function to disable auto-accepting.
     *
     * Be careful not to accept() connections yourself if you do this,
     * or we may end up accept()ing twice, causing a hang the second time.
     */
    void auto_accept(WvIStreamList *list,
		     WvStreamCallback callfunc = NULL, void *userdata = NULL);

    /**
     * these don't do anything, but they confuse the socket, so we'll
     * ignore them on purpose.
     */
    virtual size_t uread(void *buf, size_t len);
    virtual size_t uwrite(const void *buf, size_t len);
    
    /** src() is a bit of a misnomer, but it returns the socket address. */
    virtual const WvUnixAddr *src() const;
    
protected:
    WvUnixAddr addr;
    bool bound_okay;
    WvIStreamList *auto_list;

    WvStreamCallback auto_callback;
    void *auto_userdata;

    static void accept_callback(WvStream &s, void *userdata);
};


#endif // __WVUNIXSOCKET_H