📄 utils.c
字号:
/*
* Copyright (C) 2006-2007 by egnite Software GmbH. All rights reserved.
* Copyright (C) 2008 by egnite GmbH. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* For additional information see http://www.ethernut.de/
*/
/*!
* \file utils.c
* \brief Utility routines.
*
* \verbatim
*
* $Log$
*
* \endverbatim
*/
#include "config.h"
#include <sys/heap.h>
#include <sys/timer.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "logmsg.h"
#include "utils.h"
#ifndef MAX_HEADERLINE_SIZE
#define MAX_HEADERLINE_SIZE 255
#endif
/*!
* \brief Connect TCP host.
*/
int TcpHostConnect(TCPSOCKET * sock, CONST char * rhost, u_short port)
{
u_long rip;
if (proxy.proxy_port) {
port = proxy.proxy_port;
rhost = proxy.proxy_host;
}
/* Get remote host IP. */
rip = inet_addr(rhost);
if (rip == (u_long)-1 || rip == 0) {
rip = NutDnsGetHostByName((u_char *)rhost);
if (rip == 0) {
LogMsg(LOG_WARN, "DNS query for %s failed\n", rhost);
return -1;
}
}
if (NutTcpConnect(sock, rip, port)) {
LogMsg(LOG_WARN, "Connection failed with error %d\n", NutTcpError(sock));
return -1;
}
return 0;
}
FILE *TcpStreamAccept(TCPSOCKET * sock, u_short port, char *mode)
{
FILE *stream = NULL;
u_long avail;
int wcntr;
/* Wait for connecting client. */
if (NutTcpAccept(sock, port)) {
LogMsg(LOG_WARN, "Failed to listen at TCP port %u\n", port);
return NULL;
}
/* Wait for required resources. */
for (wcntr = 0; wcntr < 10; wcntr++) {
avail = NutHeapAvailable();
if (avail >= 8192) {
stream = _fdopen((int)((uptr_t) sock), mode);
if (stream) {
break;
}
LogMsg(LOG_WARN, "Failed to create stream\n");
}
else {
LogMsg(LOG_WARN, "Only %lu bytes free\n", avail);
}
NutSleep(500);
}
return stream;
}
/*!
* \brief Get a specified number of characters from TCP socket connection.
*
* The low level NutTcpReceive() routine may return less than the number
* of bytes specified. This routine makes sure, that the complete buffer
* is filled. However, an additional pointer to a status is passed to
* this routine. If the status changes, the routine will be aborted.
*
* Any TCP receive timeout is ignored, but required in order to detect
* status changes.
*
* \param sock Socket descriptor. This pointer must have been retrieved
* by calling NutTcpCreateSocket(). In addition a connection
* must have been established by calling NutTcpConnect() or
* NutTcpAccept().
* \param buff Pointer to the buffer.
* \param size Size of the buffer.
* \param status Pointer to a status.
*
* \return 0 if the buffer is filled. Otherwise -1 is returned.
*/
int TcpGetBuffer(TCPSOCKET * sock, char * buff, u_int size, u_int *status)
{
u_int initial = *status;
int got;
while (size) {
if (*status != initial || (got = NutTcpReceive(sock, buff, size)) < 0) {
/* Status change or receive error. */
return -1;
}
size -= got;
buff += got;
}
return 0;
}
int TcpGetTag(TCPSOCKET * sock, char * tag, int size)
{
int rc = 0;
int got;
char *cp = tag;
int in_tag = 0;
int in_spc = 0;
int in_qte = 0;
if (size > 0) {
for (;;) {
if ((got = NutTcpReceive(sock, cp, 1)) <= 0) {
rc = -1;
break;
}
if (!in_tag) {
if (!in_qte) {
if (*cp == '"') {
in_qte = 1;
}
else if (*cp == '<') {
in_tag = 1;
}
}
}
else if (in_qte) {
}
else if (isspace(*cp)) {
if (!in_spc) {
*cp++ = ' ';
rc++;
in_spc = 1;
}
}
else {
in_spc = 0;
if (*cp == '>') {
*cp = 0;
break;
}
if (rc < size) {
rc++;
cp++;
}
}
}
}
return rc;
}
char * LocateAttributeString(CONST char * tag, CONST char * name)
{
CONST char *cp;
size_t nlen = strlen(name);
int in_qte = 0;
cp = tag;
while (*cp) {
/* Skip leading spaces. */
if (isspace(*cp)) {
cp++;
continue;
}
if (strncmp(cp, name, nlen) == 0 && *(cp + nlen) == '=') {
return (char *)(cp + nlen + 2);
}
while (*cp) {
if (!in_qte) {
if (isspace(*cp)) {
break;
}
if (*cp == '"') {
in_qte = 1;
}
}
else {
if (*cp == '"') {
in_qte = 0;
}
}
cp++;
}
}
return NULL;
}
/*!
* \brief Get line from TCP socket connection.
*
* ASCII codes below 32 or above 127 are discarded.
*
* \param sock Socket descriptor. This pointer must have been retrieved
* by calling NutTcpCreateSocket(). In addition a connection
* must have been established by calling NutTcpConnect() or
* NutTcpAccept().
* \param line Pointer to the buffer that receives the string.
* \param size Size of the buffer. If the received line doesn't fit,
* then remaining characters will be discarded.
*
* \return Number of characters stored in the line buffer or -1 in case
* of an error or timeout.
*/
int TcpGetLine(TCPSOCKET * sock, char * line, u_short size)
{
int rc = 0;
int got;
char *cp = line;
if (size > 0) {
for (;;) {
if ((got = NutTcpReceive(sock, cp, 1)) <= 0) {
rc = -1;
break;
}
if (*cp == '\n') {
*cp = 0;
break;
}
if (*cp >= ' ' && rc < (int) size) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -