summaryrefslogtreecommitdiffstats
path: root/lskat/lskat/KRSocket.h
blob: 83969a4de24795e6b3e1683be28b53def82b84ac (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
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
/***************************************************************************
                          KRSocket.h  -  description
                             -------------------
    begin                : Tue May 2 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/*
 * Remark: This is a copy of the ksock library. It will be merged with
 *         the ksock file as soon as it supports the SOCK_REUSE_ADDR flag
*/


#ifndef KRSOCK_H
#define KRSOCK_H

#include <tqobject.h>
#include <sys/types.h>
// we define STRICT_ANSI to get rid of some warnings in glibc
#ifndef __STRICT_ANSI__
#define __STRICT_ANSI__
#define _WE_DEFINED_IT_
#endif
#include <sys/socket.h>
#ifdef _WE_DEFINED_IT_
#undef __STRICT_ANSI__
#undef _WE_DEFINED_IT_
#endif

#include <sys/un.h>

#include <netinet/in.h>
class TQSocketNotifier;

#ifdef INET6
typedef sockaddr_in6 ksockaddr_in;
#define KSOCK_DEFAULT_DOMAIN PF_INET6
#else
typedef sockaddr_in ksockaddr_in;
#define KSOCK_DEFAULT_DOMAIN PF_INET
#endif

#include <ksock.h>

class KRServerSocketPrivate;


/**
 * Monitor a port for incoming TCP/IP connections.
 *
 * You can use a KRServerSocket to listen on a port for incoming
 * connections. When a connection arrived in the port, a TDESocket
 * is created and the signal accepted is raised. Make sure you
 * always connect to this signal. If you dont the ServerSocket will
 * create new TDESocket's and no one will delete them!
 *
 * If socket() is -1 or less the socket was not created properly.
 *
 * @author Torben Weis <weis@stud.uni-frankfurt.de>
 * @version $Id$
 * @short Monitor a port for incoming TCP/IP connections.
*/
class KRServerSocket : public TQObject
{
    Q_OBJECT
  
public:
    /**
     * Constructor.
     * @param _port	the port number to monitor for incoming connections.
     */
    KRServerSocket( unsigned short int _port );
    KRServerSocket( unsigned short int _port, int optname, int value=1, int level=SOL_SOCKET);

    /**
     * Creates a UNIX domain server socket.
     */
    KRServerSocket( const char *_path );
    KRServerSocket( const char *_path, int optname, int value=1, int level=SOL_SOCKET);
  
    /** 
     * Destructor. Closes the socket if it was not already closed.
     */
    ~KRServerSocket();
    
    /** 
     * Get the file descriptor assoziated with the socket.
     */
    int socket() const { return sock; }

    /** 
     * Returns the port number which is being monitored.
     */
    unsigned short int port();

    /** 
     * The address.
     */
    unsigned long ipv4_addr();

public slots: 
    /** 
     * Called when someone connected to our port.
     */
    virtual void slotAccept( int );

signals:
    /**
     * A connection has been accepted.
     * It is your task to delete the TDESocket if it is no longer needed.
     */
    void accepted( TDESocket* );

protected:
    bool init( short unsigned int );
    bool init( const char *_path );
    bool init( const char *_path, int optname, int value=1, int level=SOL_SOCKET);
    bool init( short unsigned int, int optname, int value=1, int level=SOL_SOCKET);
  
    /** 
     * Notifies us when there is something to read on the port.
     */
    TQSocketNotifier *notifier;
    
    /** 
     * The file descriptor for this socket. sock may be -1.
     * This indicates that it is not connected.
     */    
    int sock;  

    int domain;

private:
    KRServerSocket(const KRServerSocket&);
    KRServerSocket& operator=(const KRServerSocket&);

    KRServerSocketPrivate *d;
};


// Here are a whole bunch of hackish macros that allow one to
// get at the correct member of ksockaddr_in

#ifdef INET6
#define get_sin_addr(x) x.sin6_addr
#define get_sin_port(x) x.sin6_port
#define get_sin_family(x) x.sin6_family
#define get_sin_paddr(x) x->sin6_addr
#define get_sin_pport(x) x->sin6_port
#define get_sin_pfamily(x) x->sin6_family
#else
#define get_sin_addr(x) x.sin_addr
#define get_sin_port(x) x.sin_port
#define get_sin_family(x) x.sin_family
#define get_sin_paddr(x) x->sin_addr
#define get_sin_pport(x) x->sin_port
#define get_sin_pfamily(x) x->sin_family
#endif

#endif