-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-utils.h
145 lines (122 loc) · 4.54 KB
/
tcp-utils.h
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
/*
* Miscelanious TCP (and general) functions. Contains functions that
* connect active sockets, put passive sockets in listening mode, and
* read from streams.
*
* By Stefan Bruda, using the textbook as inspiration.
*/
#ifndef __TCP_UTILS_H
#define __TCP_UTILS_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
// extern int errno;
/*** Error codes: ***/
/* See below for what they mean. */
const int err_host = -1;
const int err_sock = -2;
const int err_connect = -3;
const int err_proto = -4;
const int err_bind = -5;
const int err_listen = -6;
/*** Client: ***/
/*
* Example: connectbyport("cs-linux.ubishops.ca","21");
*
* Receives a host name and a port number, the latter as string;
* attempts then to open a connection to that host on the specified
* port. When successful, returns a socket descriptor. Otherwise
* returns just as connectbyportint (which see).
*/
int connectbyport(const char*, const char*);
/*
* Example: connectbyservice("cs-linux.ubishops.ca","ftp");
*
* Receives a host name and a service name, and attempts then to open
* a connection to that host for the specified service. When
* successful, returns a socket descriptor. Otherwise returns just as
* connectbyportint (which see), plus
* err_proto: no port found for the specified service
*/
int connectbyservice(const char*, const char*);
/*
* Example: connectbyportint("cs-linux.ubishops.ca",21);
*
* Receives a host name and a port number, attempts to open a
* connection to that host on the specified port. When successful,
* returns a socket descriptor. Otherwise returns:
* err_host: error in obtaining host address (h_errno set accordingly)
* err_sock: error in creating socket (errno set accordingly)
* err_connect: connection error (errno set accordingly)
*/
int connectbyportint(const char*, const unsigned short);
/*** Server: ***/
/*
* Example: passivesocketstr("21",10);
*
* Receives a port number as a string as well as the maximum length of
* the queue of pending connections (backlog), and attempts to bind a
* socket to the given port. When successful, returns a socket
* descriptor. Otherwise returns just as passivesocket (which see).
*/
int passivesocketstr(const char*, const int);
/*
* Example: passivesocketserv("ftp",10);
*
* Receives a service name as well as the maximum length of the queue
* of pending connections (backlog), and attempts to bind a socket to
* the port corresponding to the given service. When successful,
* returns a socket descriptor. Otherwise returns just as
* passivesocket (which see), plus:
* err_proto: no port found for the specified service
*/
int passivesocketserv(const char*, const int);
/*
* Example: passivesocket(21,10);
*
* Receives a port number as well as the maximum length of the queue
* of pending connections, and attempts to bind a socket to the given
* port. When successful, returns a socket descriptor. Otherwise
* returns:
* err_sock: error in creating socket (errno set accordingly)
* err_bind: bind error (errno set accordingly)
* err_listen: error while putting the socket in listening mode
* (errno set accordingly)
*/
int passivesocket(const unsigned short, const int);
/*
* Example: controlsocket(21,10);
*
* Behaves just like passivesocket (above), but the resulting socket
* listent only for local connections (i.e., 127.0.0.1).
*/
int controlsocket(const unsigned short, const int);
/*** Receive stuff: ***/
const int recv_nodata = -2;
/*
* Behaves just like recv with the flags argument 0, except that it
* does not block more than the number of miliseconds specified by its
* last argument. Returns the number of characters read, or
* recv_nodata when no data is available, or -1 on error (errno is set
* accordingly).
*/
int recv_nonblock (const int, char*, const size_t, const int);
/*
* readline(fd,buf,max) reads a ('\n'-terminated) line from device fd
* and puts the result in buf. Does not read more than max
* bytes. Returns the number of bytes actually read, recv_nodata when
* no data is available, or -1 on error. This is a general function
* but can be of course used with sockets too.
*
* Note: the function will return 0 when an empty line is encountered,
* so a return of 0 is no longer an indication that the end of the
* file has been reached (check for a recv_nodata return instead).
*/
int readline(const int, char*, const size_t);
#endif /* __TCP_UTILS_H */