Coda Distributed File System
Inet.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2000 Philip A. Nelson
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details. (COPYING.LIB)
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to:
16
17 The Free Software Foundation, Inc.
18 59 Temple Place, Suite 330
19 Boston, MA 02111-1307 USA.
20
21 You may contact the author by:
22 e-mail: philnelson at acm.org
23 us-mail: Philip A. Nelson
24 Computer Science Department, 9062
25 Western Washington University
26 Bellingham, WA 98226-9062
27
28*************************************************************************/
29
30#ifndef INET_H
31#define INET_H
32
33#include <config.h>
34
35#ifndef WIN32
36
37#include <unistd.h>
38#include <sys/types.h>
39#include <sys/socket.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <errno.h>
43
44#define sock_close close
45#define last_error errno
46
47#if defined(__CYGWIN__) || defined(__Solaris__)
48#define REMLENTYPE int
49#else
50#define REMLENTYPE unsigned int
51#endif
52
53#else
54
55#include <winsock.h>
56
57#define sock_close closesocket
58#define last_error WSAGetLastError()
59#define ENAMETOOLONG WSAENAMETOOLONG
60
61#define REMLENTYPE int
62
63#endif
64
65#if defined(HAVE_SYS_UN_H) && !defined(WIN32)
66#include <sys/un.h>
67#endif
68
69class Inet {
70public:
71 // Constructor, destructor ...
72 Inet();
73 ~Inet();
74
75 // Weird assignment ... moves data and invalidates right side.
77 { // Copy ...
78 fd = From.fd;
79 neterr = From.neterr;
80 server = From.server;
81 remname = From.remname;
82 remaddr = From.remaddr;
83 remlen = From.remlen;
84 unixlines = From.unixlines;
85 // Invalidate from ...
86 From.fd = -1;
87 From.remname = (char *)0;
88 return *this;
89 }
90
91 // Open a tcp connection. Return false on failure.
92 bool TcpOpen(char *host, int port);
93
94#if defined(HAVE_SYS_UN_H) && !defined(WIN32)
95 bool TcpOpen(const char *socketpath);
96#endif
97
98 // Open a tcp server socket. Return false on failure.
99 bool TcpServer(int port, int qlen);
100
101 // Accept a connect and return a new Inet
102 bool Accept(Inet &newn);
103
104 // Close the connection
105 void Close();
106
107 // Read a line of data. (max characters is length);
108 // Returns number of characters read.
109 int Readline(char *data, int length);
110
111 // Write a line of data. (adds the \r \n.)
112 int Writeline(char *data);
113
114 // Write characters and integers ...
115 int Write(char *data);
116 int Write(int data);
117
118 // isOpen -- is a network connection open.
119 bool isOpen() { return fd >= 0; };
120
121 // ErrNo -- the error number
122 int ErrNo() { return neterr; }
123
124 // FileNo -- the file number
125 int FileNo() { return fd; }
126
127 // RemoteName -- the host name of the remote machine
128 char *RemoteName() { return remname; }
129
130 // RemoteAddr -- the text address form of the remote machine
132 {
133 struct sockaddr_in *addr = (struct sockaddr_in *)&remaddr;
134 return inet_ntoa(addr->sin_addr);
135 }
136
137 // Options set ..
138 void SetUnix() { unixlines = true; }
139 void SetNet() { unixlines = false; }
140
141private:
142 int fd;
143 int neterr;
144 bool server;
145 char *remname;
146 struct sockaddr remaddr;
147 REMLENTYPE remlen;
148
149 bool unixlines;
150};
151
152#endif
#define REMLENTYPE
Definition: Inet.h:50
Definition: Inet.h:69
Inet & operator=(Inet &From)
Definition: Inet.h:76
bool isOpen()
Definition: Inet.h:119
~Inet()
Definition: Inet.cc:84
void SetNet()
Definition: Inet.h:139
void Close()
Definition: Inet.cc:259
char * RemoteAddr()
Definition: Inet.h:131
int Write(char *data)
Definition: Inet.cc:302
int Readline(char *data, int length)
Definition: Inet.cc:272
char * RemoteName()
Definition: Inet.h:128
bool TcpOpen(char *host, int port)
Definition: Inet.cc:99
bool Accept(Inet &newn)
Definition: Inet.cc:228
void SetUnix()
Definition: Inet.h:138
int FileNo()
Definition: Inet.h:125
bool TcpServer(int port, int qlen)
Definition: Inet.cc:190
int ErrNo()
Definition: Inet.h:122
Inet()
Definition: Inet.cc:65
int Writeline(char *data)
Definition: Inet.cc:327
char * inet_ntoa(struct in_addr in)
Definition: compat.c:67
Definition: smon2.c:59