OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [mw/] [src/] [demos/] [vnc/] [vncviewer/] [sockets.c] - Blame information for rev 1780

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 *  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
3
 *
4
 *  This is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This software 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
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this software; if not, write to the Free Software
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17
 *  USA.
18
 */
19
 
20
/*
21
 * sockets.c - functions to deal with sockets.
22
 */
23
 
24
#include <unistd.h>
25
#include <sys/socket.h>
26
#include <errno.h>
27
#include <netinet/in.h>
28
#include <netinet/tcp.h>
29
#include <arpa/inet.h>
30
#include <netdb.h>
31
#include <vncviewer.h>
32
 
33
/* fix bad MIPS sys headers...*/
34
#ifndef SOCK_STREAM
35
#define SOCK_STREAM     2       /* <asm/socket.h>*/
36
#endif
37
 
38
void PrintInHex(char *buf, int len);
39
 
40
Bool errorMessageFromReadExact = True;
41
 
42
/*
43
 * Read an exact number of bytes, and don't return until you've got them.
44
 */
45
 
46
Bool
47
ReadExact(int sock, char *buf, int n)
48
{
49
    int i = 0;
50
    int j;
51
 
52
    while (i < n) {
53
        j = read(sock, buf + i, (n - i));
54
        if (j <= 0) {
55
            if (j < 0) {
56
                fprintf(stderr,programName);
57
                perror(": read");
58
            } else {
59
                if (errorMessageFromReadExact) {
60
                    fprintf(stderr,"%s: read failed\n",programName);
61
                }
62
            }
63
            return False;
64
        }
65
        i += j;
66
    }
67
    if (debug)
68
        PrintInHex(buf,n);
69
    return True;
70
}
71
 
72
 
73
/*
74
 * Write an exact number of bytes, and don't return until you've sent them.
75
 */
76
 
77
Bool
78
WriteExact(int sock, char *buf, int n)
79
{
80
    int i = 0;
81
    int j;
82
 
83
    while (i < n) {
84
        j = write(sock, buf + i, (n - i));
85
        if (j <= 0) {
86
            if (j < 0) {
87
                fprintf(stderr,programName);
88
                perror(": write");
89
            } else {
90
                fprintf(stderr,"%s: write failed\n",programName);
91
            }
92
            return False;
93
        }
94
        i += j;
95
    }
96
    return True;
97
}
98
 
99
 
100
/*
101
 * ConnectToTcpAddr connects to the given TCP port.
102
 */
103
 
104
int
105
ConnectToTcpAddr(unsigned int host, int port)
106
{
107
    int sock;
108
    struct sockaddr_in addr;
109
    int one = 1;
110
 
111
    addr.sin_family = AF_INET;
112
    addr.sin_port = htons(port);
113
    addr.sin_addr.s_addr = host;
114
 
115
    sock = socket(AF_INET, SOCK_STREAM, 0);
116
    if (sock < 0) {
117
        fprintf(stderr,programName);
118
        perror(": ConnectToTcpAddr: socket");
119
        return -1;
120
    }
121
 
122
    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
123
        fprintf(stderr,programName);
124
        perror(": ConnectToTcpAddr: connect");
125
        close(sock);
126
        return -1;
127
    }
128
 
129
    if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
130
                   (char *)&one, sizeof(one)) < 0) {
131
        fprintf(stderr,programName);
132
        perror(": ConnectToTcpAddr: setsockopt");
133
        close(sock);
134
        return -1;
135
    }
136
 
137
    return sock;
138
}
139
 
140
 
141
 
142
/*
143
 * ListenAtTcpPort starts listening at the given TCP port.
144
 */
145
 
146
int
147
ListenAtTcpPort(int port)
148
{
149
    int sock;
150
    struct sockaddr_in addr;
151
    int one = 1;
152
 
153
    addr.sin_family = AF_INET;
154
    addr.sin_port = htons(port);
155
    addr.sin_addr.s_addr = INADDR_ANY;
156
 
157
    sock = socket(AF_INET, SOCK_STREAM, 0);
158
    if (sock < 0) {
159
        fprintf(stderr,programName);
160
        perror(": ListenAtTcpPort: socket");
161
        return -1;
162
    }
163
 
164
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
165
                   (const char *)&one, sizeof(one)) < 0) {
166
        fprintf(stderr,programName);
167
        perror(": ListenAtTcpPort: setsockopt");
168
        close(sock);
169
        return -1;
170
    }
171
 
172
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
173
        fprintf(stderr,programName);
174
        perror(": ListenAtTcpPort: bind");
175
        close(sock);
176
        return -1;
177
    }
178
 
179
    if (listen(sock, 5) < 0) {
180
        fprintf(stderr,programName);
181
        perror(": ListenAtTcpPort: listen");
182
        close(sock);
183
        return -1;
184
    }
185
 
186
    return sock;
187
}
188
 
189
 
190
/*
191
 * AcceptTcpConnection accepts a TCP connection.
192
 */
193
 
194
int
195
AcceptTcpConnection(int listenSock)
196
{
197
    int sock;
198
    struct sockaddr_in addr;
199
    int addrlen = sizeof(addr);
200
    int one = 1;
201
 
202
    sock = accept(listenSock, (struct sockaddr *) &addr, &addrlen);
203
    if (sock < 0) {
204
        fprintf(stderr,programName);
205
        perror(": AcceptTcpConnection: accept");
206
        return -1;
207
    }
208
 
209
    if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
210
                   (char *)&one, sizeof(one)) < 0) {
211
        fprintf(stderr,programName);
212
        perror(": AcceptTcpConnection: setsockopt");
213
        close(sock);
214
        return -1;
215
    }
216
 
217
    return sock;
218
}
219
 
220
 
221
/*
222
 * StringToIPAddr - convert a host string to an IP address.
223
 */
224
 
225
int
226
StringToIPAddr(const char *str, unsigned int *addr)
227
{
228
    struct hostent *hp;
229
 
230
    if ((*addr = inet_addr(str)) == -1)
231
    {
232
        if (!(hp = gethostbyname(str)))
233
            return 0;
234
 
235
        *addr = *(unsigned int *)hp->h_addr;
236
    }
237
 
238
    return 1;
239
}
240
 
241
 
242
/*
243
 * Test if the other end of a socket is on the same machine.
244
 */
245
 
246
Bool
247
SameMachine(int sock)
248
{
249
    struct sockaddr_in peeraddr, myaddr;
250
    int addrlen = sizeof(struct sockaddr_in);
251
 
252
    getpeername(sock, (struct sockaddr *)&peeraddr, &addrlen);
253
    getsockname(sock, (struct sockaddr *)&myaddr, &addrlen);
254
 
255
    return (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr);
256
}
257
 
258
 
259
/*
260
 * Print out the contents of a packet for debugging.
261
 */
262
 
263
void
264
PrintInHex(char *buf, int len)
265
{
266
    int i, j;
267
    char c, str[17];
268
 
269
    str[16] = 0;
270
 
271
    fprintf(stderr,"ReadExact: ");
272
 
273
    for (i = 0; i < len; i++)
274
    {
275
        if ((i % 16 == 0) && (i != 0)) {
276
            fprintf(stderr,"           ");
277
        }
278
        c = buf[i];
279
        str[i % 16] = (((c > 31) && (c < 127)) ? c : '.');
280
        fprintf(stderr,"%02x ",(unsigned char)c);
281
        if ((i % 4) == 3)
282
            fprintf(stderr," ");
283
        if ((i % 16) == 15)
284
        {
285
            fprintf(stderr,"%s\n",str);
286
        }
287
    }
288
    if ((i % 16) != 0)
289
    {
290
        for (j = i % 16; j < 16; j++)
291
        {
292
            fprintf(stderr,"   ");
293
            if ((j % 4) == 3) fprintf(stderr," ");
294
        }
295
        str[i % 16] = 0;
296
        fprintf(stderr,"%s\n",str);
297
    }
298
 
299
    fflush(stderr);
300
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.