Debian Codegraph @

COSMIC/Speek.c

raw
1 /* Speek.c:
2 * Authors: Charles E. Campbell, Jr.
3 * Terry McRoberts
4 * Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1
5 * Permission is hereby granted to use and distribute this code,
6 * with or without modifications, provided that this copyright
7 * notice is copied with it. Like anything else that's free,
8 * Speek.c is provided *as is* and comes with no warranty
9 * of any kind, either expressed or implied. By using this
10 * software, you agree that in no event will the copyright
11 * holder be liable for any damages resulting from the use
12 * of this software.
13 * Date: Aug 22, 2005
14 */
15 #include <stdio.h>
16 #define SSLNEEDTIME
17 #include "sockets.h"
18
19 /* ------------------------------------------------------------------------- */
20
21 /* Speek: this function peeks at the socket and returns a buffer full of
22 * whatever is on it
23 *
24 * Returns: qty of data in buf
25 * 1 (SSLNOPEEK) something on buffer, but cannot peek
26 * 0 if nothing
27 * EOF on bad select (socket error)
28 * or on select == 1 (something there) but recv indicates
29 * that nothing is there
30 */
31 #ifdef __PROTOTYPE__
32 int Speek(
33 Socket *skt, /* socket handle */
34 void *buf, /* socket character buffer */
35 int buflen) /* max length of buffer */
36 #else
37 int Speek(
38 skt, /* socket handle */
39 buf, /* socket character buffer */
40 buflen) /* max length of buffer */
41 Socket *skt;
42 void *buf;
43 int buflen;
44 #endif
45 {
46 short result;
47 struct timeval timeout;
48 fd_set rmask;
49 fd_set wmask;
50 fd_set emask;
51
52
53 /* insure a properly null-byte terminated buffer if nothing is returned */
54 ((char *) buf)[0]= '\0';
55
56 /* sanity check */
57 if(!skt) {
58 return 0;
59 }
60
61 FD_ZERO(&rmask);
62 FD_SET(skt->skt,&rmask);
63 FD_ZERO(&wmask);
64 FD_ZERO(&emask);
65
66 timeout.tv_sec = 0;
67 timeout.tv_usec= 0;
68
69 /* select checks if
70 * the file descriptor set (fds) has something ready for reading
71 */
72 #ifdef SSLNOPEEK
73 result = select(skt->skt+1,rmask.fds_bits,wmask.fds_bits,emask.fds_bits,
74 &timeout);
75 #else
76 result = select(skt->skt+1, &rmask,&wmask,&emask, &timeout);
77 #endif
78
79 if(result < 0) {
80 return EOF;
81 }
82
83 if(result == 0) { /* no descriptors ready for reading */
84 return 0;
85 }
86
87 #ifdef SSLNOPEEK
88
89 return 1;
90
91 #else
92
93 /* test if message available from socket, return qty bytes avail */
94 if(FD_ISSET(skt->skt,&rmask)) {
95 buflen= recv(skt->skt,buf,buflen,MSG_PEEK);
96 if(result == 1 && buflen == 0) buflen= EOF;
97 return buflen;
98 }
99
100 /* socket is empty */
101 return 0;
102
103 #endif /* #ifdef SSLNOPEEK ... #else ... #endif */
104 }
105
106 /* ---------------------------------------------------------------------
107 * vim: ts=4
108 */