summaryrefslogtreecommitdiff
path: root/src/net/isocket.h
blob: 5ca1ec39417e129bcf58b7f6ee2710c339af9894 (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
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * 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.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef ISOCKETH
#define ISOCKETH

#include <mt/mread.h>
#include <mt/mstring.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

class isocket : public pipe_stream {
public:
  isocket(const in_addr& ia,int port,int backlog);
  isocket(int f,const sockaddr_in& ia);
  ~isocket() { close(); }

  void close() { if(fd>=0) { ::close(fd); fd=-1; }}
  
  int getfd() const { return fd; }
  int family() const { return sa.sin_family; }
  int port() const { return ntohs(sa.sin_port); }
  in_addr address() const { return sa.sin_addr; }
  
  void reuse(int ok) { set(SOL_SOCKET,SO_REUSEADDR,ok); }
  void nodelay(int ok) { set(IPPROTO_TCP,TCP_NODELAY,ok); }
  void keepalive(int ok) { set(SOL_SOCKET,SO_KEEPALIVE,ok); }
  void sendbufsize(int s) { set(SOL_SOCKET,SO_SNDBUF,s); }
  void recvbufsize(int s) { set(SOL_SOCKET,SO_RCVBUF,s); }

  isocket* accept() throw(merror_t);

  int test() { return rd.poll(); }
  int getsome(char* s,int n) { return rd.read(s,n); }
  int gethead(char* s,int n) { return rd.readpara(s,n); }
  int getbody(char* s,int n) { return rd.readx(s,n); }

protected:
  void setflag(int f) { fcntl(fd,F_SETFL,f); }
  int getflag() const { return fcntl(fd,F_GETFL,0); }
  void set(int lev,int opt,int ok) throw(merror_t) {
    if(setsockopt(fd,lev,opt,(char*)&ok,sizeof(ok))<0)
      THROW("isocket: Could not set option");
  }
private:
  sockaddr_in sa;
  mread rd;
};

#endif