File: tcp.c

package info (click to toggle)
postit 0.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 160 kB
  • ctags: 218
  • sloc: ansic: 753; sh: 243; makefile: 61
file content (231 lines) | stat: -rw-r--r-- 5,313 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* tcp.c  TCP communications routines for HERMES */

#ifdef TERM
#include <termnet.h>
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>

#include "tcp.h"
#include "postit.h"

#define DONT_HOG_CPU


#ifdef DONT_HOG_CPU
#ifdef usleep
#define NAP usleep(100000)
#else
#define NAP sleep(1)
#endif
#else
#define NAP
#endif


#ifdef DEBUG
void dump(unsigned char *what, int howmany)
{
  int i;

  for (i=0; i<howmany; i++) printf("%2.2X ",what[i]);
  printf("\n");
}
#endif

/* Open a socket and connect to remote host on a given port. Return value
is the socket handle if positive or an error if negative. */
int tcp_open_connection(char *hostname, unsigned short port)
{
  struct protoent *proto;
  struct hostent *host;
  struct sockaddr_in server;
  int socknr;
  unsigned char ip[4];
  int i;
  unsigned char *c;

  proto=getprotobyname("tcp");
  if (!proto) return -1;	/* no such protocol */
  socknr=socket(AF_INET,SOCK_STREAM,proto->p_proto);
  if (socknr==-1) return -2; /* no socket available */
  /* We have a socket on our machine; now let's find out how to
     connect to the remote host */
  if (isdigit(*hostname)) {
    /* We are given a string with four decimal numbers, separated
       by dots */
    c=hostname;
    for (i=0; i<4; i++) {
      ip[i]=0;
      if (!*c) return -3;	/* bad hostname */
      while (*c!='.'&&*c) {
	ip[i]=ip[i]*10+(*c&15);
	c++;
      }
      if (*c=='.') c++;
    }
    host=(struct hostent *)gethostbyaddr(ip,4,AF_INET);
  }
  else	{
    /* it's a real hostname */
    host=(struct hostent *)gethostbyname(hostname);
  }
  if (!host) return -4;	/* unknown host */
  bcopy(host->h_addr,ip,4);	
  bzero((char *)&server,sizeof(server));
  bcopy(host->h_addr,(char *)&server.sin_addr,host->h_length);
  server.sin_family=host->h_addrtype;
  server.sin_port=htons(port);	/* note that this is high-byte first!! */
  /* OK, now let's connect! */
  if (verbosity>=3) printf("Connecting to %u.%u.%u.%u, port %hu on socket %d...\n",
			   ip[0],ip[1],ip[2],ip[3],port,socknr);
  if (connect(socknr,(void *)&server,sizeof(server))==-1) return -6;	/* connection refused */
  return socknr;
}

/* This function closes a connection on a given socket for both rx and tx.
Returns 0 on success, -1 on error */
int tcp_close_connection(int socknr)
{
  return shutdown(socknr,2);
}

/* Read a line from the socket. If no data available, return 0, else return
number of characters read (including linefeed) */
int tcp_receive_line(int socknr, char *line, int maxsize)
{
  char *c;
  int flags;
  int x;

  c=line; *c=0; maxsize--;
  flags=fcntl(socknr,F_GETFL);	/* remember previous state */
  fcntl(socknr,F_SETFL,(long)(flags|O_NONBLOCK));	/* make sure read returns if no data available */
  while (1) {
    x=read(socknr,c,1);
    if (x==-1) {
      if (errno==EAGAIN) {
	if (c==line) break;
	continue;
      }
      break;
    }
    if (x==0) continue;
    c+=x;
    if (*(c-1)=='\n'||--maxsize<=0) {
      *c=0;
      break;
    }
  }
  fcntl(socknr,F_SETFL,flags);
  return c-line;
}

static char timeoutflag;
static void sigalarm(int x)
{
  timeoutflag=1;
#ifdef DEBUG
  printf("Timeout");
#endif
}

/* Read a line from the socket with timeout. If no data available, return 0, else return
number of characters read (including linefeed) */
int tcp_receive_line_to(int socknr, char *line, int maxsize, long timeout)
{
  char *c;
  int flags;
  int x;

  c=line; *c=0; maxsize--;
  flags=fcntl(socknr,F_GETFL);	/* remember previous state */
  fcntl(socknr,F_SETFL,(long)(flags|O_NONBLOCK));	/* make sure read returns if no data available */
  signal(SIGALRM,sigalarm);
  timeoutflag=0;
  alarm(timeout);
  while (1) {
    if (timeoutflag) {
      *c=0;
      break;
    }
    x=read(socknr,c,1);
    if (x==0) {
      NAP;
      continue;
    }
    if (x==-1) {
      if (errno==EAGAIN) {
	continue;
      }
      break;
    }
    c+=x;
    if (*(c-1)=='\n'||--maxsize<=0) {
      *c=0;
      break;
    }
  }
  fcntl(socknr,F_SETFL,flags);
  return c-line;
}

/* Read a block from the socket with timeout. If no data available, return 0, else return
number of characters read */
int tcp_receive_block_to(int socknr, char *block, int maxsize, long timeout)
{
  char *c;
  int flags;
  int x;

  c=block; *c=0;
  flags=fcntl(socknr,F_GETFL);	/* remember previous state */
  fcntl(socknr,F_SETFL,(long)(flags|O_NONBLOCK));	/* make sure read returns if no data available */
  signal(SIGALRM,sigalarm);
  timeoutflag=0;
  alarm(timeout);
  while (1) {
    if (timeoutflag) {
      *c=0;
      break;
    }
    x=read(socknr,c,maxsize);
    if (!x) {
      NAP;
      continue;
    }
    if (x==-1) {
      if (errno==EAGAIN) {
	continue;
      }
      break;
    }
    c+=x; maxsize-=x;
    break;
  }
  fcntl(socknr,F_SETFL,flags);
  return c-block;
}

/* Write a null-terminated string to a socket. Returns: # of chars written or
-1 on error */
int tcp_send_line(int socknr, char *line)
{
  return write(socknr,line,strlen(line));
}

/* Write a block to a socket. Returns # of bytes written or -1 on error */
int tcp_send_block(int socknr, char *block, int size)
{
  return write(socknr,block,size);
}