| 1 |
/* Sgets.c |
| 2 |
* Syntax : Sgets(buf,maxbuf,skt) |
| 3 |
* Returns: buf if it worked, a NULL pointer otherwise |
| 4 |
* |
| 5 |
* buf[maxbuf] |
| 6 |
* Authors: Charles E. Campbell, Jr. |
| 7 |
* Terry McRoberts |
| 8 |
* Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1 |
| 9 |
* Permission is hereby granted to use and distribute this code, |
| 10 |
* with or without modifications, provided that this copyright |
| 11 |
* notice is copied with it. Like anything else that's free, |
| 12 |
* Sgets.c is provided *as is* and comes with no warranty |
| 13 |
* of any kind, either expressed or implied. By using this |
| 14 |
* software, you agree that in no event will the copyright |
| 15 |
* holder be liable for any damages resulting from the use |
| 16 |
* of this software. |
| 17 |
* Date: Aug 22, 2005 |
| 18 |
*/ |
| 19 |
#include <stdio.h> |
| 20 |
#include "sockets.h" |
| 21 |
|
| 22 |
/* --------------------------------------------------------------------------- |
| 23 |
* Local Definitions: |
| 24 |
*/ |
| 25 |
|
| 26 |
/* --------------------------------------------------------------------------- |
| 27 |
* Function Declarations: |
| 28 |
*/ |
| 29 |
|
| 30 |
/* --------------------------------------------------------------------------- |
| 31 |
* Source Code: |
| 32 |
*/ |
| 33 |
|
| 34 |
/* Sgets: |
| 35 |
* Gets a null-byte terminated string from the specified Socket. |
| 36 |
* Note that the maxbuf *must be* >= 2 (one byte needed for the |
| 37 |
* null byte termination, and one byte for a minimal amount of data). |
| 38 |
* This function blocks and will fill the buffer "buf" with up |
| 39 |
* to maxbuf characters. The output from this function will |
| 40 |
* be null byte terminated when successful, even on a full buffer condition. |
| 41 |
* |
| 42 |
* Returns: buf : on success |
| 43 |
* null ptr: on failure |
| 44 |
*/ |
| 45 |
#ifdef __PROTOTYPE__ |
| 46 |
char *Sgets( |
| 47 |
char *buf, /* holds null-byte terminated string from Socket */ |
| 48 |
int maxbuf, /* maximum size of string allowed in buf */ |
| 49 |
Socket *skt) /* Socket from which data is to be read */ |
| 50 |
#else |
| 51 |
char *Sgets(buf, maxbuf, skt) |
| 52 |
char *buf; |
| 53 |
int maxbuf; |
| 54 |
Socket *skt; |
| 55 |
#endif |
| 56 |
{ |
| 57 |
int buflen; |
| 58 |
int mesglen; |
| 59 |
char *b = NULL; |
| 60 |
#ifndef SSLNOPEEK |
| 61 |
char *nb = NULL; |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
/* error or whatever, until the buffer actually gets something its empty */ |
| 66 |
buf[0] = '\0'; |
| 67 |
|
| 68 |
/* sanity check */ |
| 69 |
if (!skt) { |
| 70 |
return (char *) NULL; |
| 71 |
} |
| 72 |
|
| 73 |
#ifdef SSLNOPEEK /* stupid, so it receives one byte at a time */ |
| 74 |
for (b = buf, buflen = maxbuf-1; buflen; --buflen, ++b) { |
| 75 |
if (recv(skt->skt, b, (unsigned) 1, 0) <= 0) { |
| 76 |
return (char *) NULL; |
| 77 |
} |
| 78 |
if (*b == '\0') break; |
| 79 |
} |
| 80 |
|
| 81 |
#else |
| 82 |
|
| 83 |
/* receive bytes until a nullbyte is received */ |
| 84 |
buflen = maxbuf - 2; /* leave space for trailing null byte */ |
| 85 |
buf[buflen + 1] = '\0'; /* so strchr will find a null byte somewhere */ |
| 86 |
for (b = buf, mesglen = 0; buflen > 0; b += mesglen, buflen -= mesglen) { |
| 87 |
|
| 88 |
/* block until something shows up on the Socket */ |
| 89 |
if (!Swait(skt)) { /* null == error - should be at least 1 null byte */ |
| 90 |
return (char *) NULL; |
| 91 |
} |
| 92 |
|
| 93 |
/* peek at contents of socket queue */ |
| 94 |
mesglen = recv(skt->skt, b, buflen, MSG_PEEK); |
| 95 |
|
| 96 |
if (mesglen <= 0) { /* socket error - bad receive */ |
| 97 |
return (char *) NULL; |
| 98 |
} |
| 99 |
else if (mesglen > 0) { /* check if null byte received yet */ |
| 100 |
nb = strchr(b, '\0'); /* find null byte in buffer */ |
| 101 |
|
| 102 |
if (nb < b + mesglen) { /* null byte inside peeked-at message */ |
| 103 |
mesglen = nb - b + 1; /* read up to and including null byte */ |
| 104 |
if (Sreadbytes(skt, b, mesglen) <= 0) { |
| 105 |
return buf; |
| 106 |
} |
| 107 |
break; |
| 108 |
} |
| 109 |
else if (mesglen >= buflen) { /* whoops - full buffer */ |
| 110 |
if (Sreadbytes(skt, b, buflen) <= 0) { |
| 111 |
return buf; |
| 112 |
} |
| 113 |
b[buflen] = '\0'; /* end-of-buffer termination */ |
| 114 |
break; |
| 115 |
} |
| 116 |
else { |
| 117 |
if (Sreadbytes(skt, b, mesglen) <= 0) { /* partial read */ |
| 118 |
return buf; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#endif /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 125 |
|
| 126 |
/* return message */ |
| 127 |
return buf; |
| 128 |
} |
| 129 |
|
| 130 |
/* --------------------------------------------------------------------- |
| 131 |
* vim: ts=4 |
| 132 |
*/ |