Classes | Macros | Functions | Variables
ytcp.c File Reference
#include "ydef.h"
#include "ytcp.h"
#include "yproto.h"
#include "yhash.h"
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
Include dependency graph for ytcp.c:

Go to the source code of this file.

Classes

struct  DnsCache
 

Macros

#define __FILE_ID__   "ytcp"
 
#define _WINSOCK_DEPRECATED_NO_WARNINGS
 
#define DEFAULT_TCP_MAX_WINDOW_SIZE   (4*65536)
 
#define DEFAULT_TCP_ROUND_TRIP_TIME   30
 
#define SSDP_HTTP   "HTTP/1.1 200 OK\r\n"
 
#define SSDP_LINE_MAX_LEN   80u
 
#define SSDP_M_SEARCH   "M-SEARCH * HTTP/1.1\r\n"
 
#define SSDP_NOTIFY   "NOTIFY * HTTP/1.1\r\n"
 
#define UDP_IN_FIFO   yFifoBuf
 
#define WS_CONNEXION_TIMEOUT   10000
 
#define WS_MAX_DATA_LEN   124
 
#define yclosesocket(skt)   closesocket(skt)
 
#define YDNS_CACHE_SIZE   16
 
#define YDNS_CACHE_VALIDITY   600000u
 
#define yNetLogErr()   yNetLogErrEx(__LINE__,SOCK_ERR)
 
#define YPERF_TCP_ENTER(NAME)
 
#define YPERF_TCP_LEAVE(NAME)
 
#define YRand32()   rand()
 
#define yrecv(skt, buf, len, flags)   recv(skt, buf, len, flags)
 
#define ysend(skt, buf, len, flags)   send(skt, buf, len, flags)
 
#define ysocket(domain, type, protocol)   socket(domain, type, protocol)
 

Functions

static u16 Base64Encode (const u8 *cSourceData, u16 wSourceLen, u8 *cDestData, u16 wDestLen)
 
static int GenereateWebSockeyKey (const u8 *url, u32 urllen, char *buffer)
 
static char hexatochar (char hi_c, char lo_c)
 
static u32 resolveDNSCache (yUrlRef url, char *errmsg)
 
static int uuidToSerial (const char *uuid, char *serial)
 
static int VerifyWebsocketKey (const char *data, u16 hdrlen, const char *reference, u16 reference_len)
 
static void ws_appendTCPData (RequestSt *req, u8 *buffer, int pktlen, int isClose)
 
static void ws_closeBaseSocket (struct _WSNetHubSt *base_req)
 
static int ws_openBaseSocket (struct _WSNetHubSt *hub, yUrlRef url, const char *request, int request_len, int mstimout, char *errmsg)
 
static int ws_parseIncommingFrame (HubSt *hub, u8 *buffer, int pktlen, char *errmsg)
 
static int ws_processRequests (HubSt *hub, char *errmsg)
 
static int ws_requestStillPending (HubSt *hub)
 
static int ws_sendAuthenticationMeta (HubSt *hub, char *errmsg)
 
static int ws_sendFrame (HubSt *hub, int stream, int tcpchan, const u8 *data, int datalen, char *errmsg)
 
void * ws_thread (void *ctx)
 
static int ws_thread_select (struct _WSNetHubSt *base_req, u64 ms, WakeUpSocket *wuce, char *errmsg)
 
static void ws_threadUpdateRetryCount (HubSt *hub)
 
int yConsumeWakeUpSocket (WakeUpSocket *wuce, char *errmsg)
 
YSTATIC int yDetectNetworkInterfaces (u32 only_ip)
 
int yDringWakeUpSocket (WakeUpSocket *wuce, u8 signal, char *errmsg)
 
void yDupSet (char **storage, const char *val)
 
void yFreeWakeUpSocket (WakeUpSocket *wuce)
 
static void yHTTPCloseReqEx (struct _RequestSt *req, int canReuseSocket)
 
static int yHTTPMultiSelectReq (struct _RequestSt **reqs, int size, u64 ms, WakeUpSocket *wuce, char *errmsg)
 
static int yHTTPOpenReqEx (struct _RequestSt *req, u64 mstimout, char *errmsg)
 
void yInitWakeUpSocket (WakeUpSocket *wuce)
 
static int yNetLogErrEx (u32 line, unsigned err)
 
int yNetSetErrEx (u32 line, unsigned err, char *errmsg)
 
struct _RequestStyReqAlloc (struct _HubSt *hub)
 
void yReqClose (struct _RequestSt *req)
 
void yReqFree (struct _RequestSt *req)
 
int yReqGet (struct _RequestSt *req, u8 **buffer)
 
int yReqHasPending (struct _HubSt *hub)
 
int yReqIsAsync (struct _RequestSt *req)
 
int yReqIsEof (struct _RequestSt *req, char *errmsg)
 
int yReqMultiSelect (struct _RequestSt **tcpreq, int size, u64 ms, WakeUpSocket *wuce, char *errmsg)
 
int yReqOpen (struct _RequestSt *req, int wait_for_start, int tcpchan, const char *request, int reqlen, u64 mstimeout, yapiRequestAsyncCallback callback, void *context, RequestProgress progress_cb, void *progress_ctx, char *errmsg)
 
int yReqRead (struct _RequestSt *req, u8 *buffer, int len)
 
int yReqSelect (struct _RequestSt *tcpreq, u64 ms, char *errmsg)
 
u32 yResolveDNS (const char *name, char *errmsg)
 
static void ySSDP_parseSSPDMessage (SSDPInfos *SSDP, char *message, int msg_len)
 
static void * ySSDP_thread (void *ctx)
 
static void ySSDPCheckExpiration (SSDPInfos *SSDP)
 
int ySSDPDiscover (SSDPInfos *SSDP, char *errmsg)
 
int ySSDPStart (SSDPInfos *SSDP, ssdpHubDiscoveryCallback callback, char *errmsg)
 
void ySSDPStop (SSDPInfos *SSDP)
 
static void ySSDPUpdateCache (SSDPInfos *SSDP, const char *uuid, const char *url, int cacheValidity)
 
int yStartWakeUpSocket (WakeUpSocket *wuce, char *errmsg)
 
static int yTcpCheckReqTimeout (struct _RequestSt *req, char *errmsg)
 
static int yTcpCheckSocketStillValid (YSOCKET skt, char *errmsg)
 
static void yTcpClose (YSOCKET skt)
 
int yTcpDownload (const char *host, const char *url, u8 **out_buffer, u32 mstimeout, char *errmsg)
 
int yTcpInit (char *errmsg)
 
static int yTcpOpen (YSOCKET *newskt, u32 ip, u16 port, u64 mstimeout, char *errmsg)
 
static int yTcpRead (YSOCKET skt, u8 *buffer, int len, char *errmsg)
 
void yTcpShutdown (void)
 
static int yTcpWrite (YSOCKET skt, const char *buffer, int len, char *errmsg)
 
static void yWSCloseReqEx (struct _RequestSt *req, int takeCS)
 
static int yWSOpenReqEx (struct _RequestSt *req, int tcpchan, u64 mstimeout, char *errmsg)
 
static int yWSSelectReq (struct _RequestSt *req, u64 mstimeout, char *errmsg)
 

Variables

os_ifaces detectedIfaces [NB_OS_IFACES]
 
static const char * discovery
 
DnsCache dnsCache [YDNS_CACHE_SIZE]
 
int nbDetectedIfaces = 0
 
static const char * ws_header_end = "\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\n\r\n"
 
static const char * ws_header_start = " HTTP/1.1\r\nSec-WebSocket-Version: 13\r\nUser-Agent: Yoctopuce\r\nSec-WebSocket-Key: "
 

Macro Definition Documentation

#define __FILE_ID__   "ytcp"

Definition at line 40 of file ytcp.c.

#define _WINSOCK_DEPRECATED_NO_WARNINGS

Definition at line 41 of file ytcp.c.

#define DEFAULT_TCP_MAX_WINDOW_SIZE   (4*65536)

Definition at line 396 of file ytcp.c.

#define DEFAULT_TCP_ROUND_TRIP_TIME   30

Definition at line 395 of file ytcp.c.

#define SSDP_HTTP   "HTTP/1.1 200 OK\r\n"

Definition at line 2937 of file ytcp.c.

#define SSDP_LINE_MAX_LEN   80u

Definition at line 2938 of file ytcp.c.

#define SSDP_M_SEARCH   "M-SEARCH * HTTP/1.1\r\n"

Definition at line 2936 of file ytcp.c.

#define SSDP_NOTIFY   "NOTIFY * HTTP/1.1\r\n"

Definition at line 2935 of file ytcp.c.

#define UDP_IN_FIFO   yFifoBuf

Definition at line 2940 of file ytcp.c.

#define WS_CONNEXION_TIMEOUT   10000

Definition at line 1836 of file ytcp.c.

#define WS_MAX_DATA_LEN   124

Definition at line 1837 of file ytcp.c.

#define yclosesocket (   skt)    closesocket(skt)

Definition at line 212 of file ytcp.c.

#define YDNS_CACHE_SIZE   16

Definition at line 318 of file ytcp.c.

#define YDNS_CACHE_VALIDITY   600000u

Definition at line 319 of file ytcp.c.

#define yNetLogErr ( )    yNetLogErrEx(__LINE__,SOCK_ERR)

Definition at line 166 of file ytcp.c.

#define YPERF_TCP_ENTER (   NAME)

Definition at line 122 of file ytcp.c.

#define YPERF_TCP_LEAVE (   NAME)

Definition at line 123 of file ytcp.c.

#define YRand32 ( )    rand()

Definition at line 1672 of file ytcp.c.

#define yrecv (   skt,
  buf,
  len,
  flags 
)    recv(skt, buf, len, flags)

Definition at line 215 of file ytcp.c.

#define ysend (   skt,
  buf,
  len,
  flags 
)    send(skt, buf, len, flags)

Definition at line 214 of file ytcp.c.

#define ysocket (   domain,
  type,
  protocol 
)    socket(domain, type, protocol)

Definition at line 213 of file ytcp.c.

Function Documentation

static u16 Base64Encode ( const u8 *  cSourceData,
u16  wSourceLen,
u8 *  cDestData,
u16  wDestLen 
)
static

Definition at line 1702 of file ytcp.c.

static int GenereateWebSockeyKey ( const u8 *  url,
u32  urllen,
char *  buffer 
)
static

Definition at line 1783 of file ytcp.c.

static char hexatochar ( char  hi_c,
char  lo_c 
)
static

Definition at line 2943 of file ytcp.c.

static u32 resolveDNSCache ( yUrlRef  url,
char *  errmsg 
)
static

Definition at line 329 of file ytcp.c.

static int uuidToSerial ( const char *  uuid,
char *  serial 
)
static

Definition at line 2953 of file ytcp.c.

static int VerifyWebsocketKey ( const char *  data,
u16  hdrlen,
const char *  reference,
u16  reference_len 
)
static

Definition at line 1800 of file ytcp.c.

static void ws_appendTCPData ( RequestSt req,
u8 *  buffer,
int  pktlen,
int  isClose 
)
static

Definition at line 1923 of file ytcp.c.

static void ws_closeBaseSocket ( struct _WSNetHubSt base_req)
static

Definition at line 2439 of file ytcp.c.

static int ws_openBaseSocket ( struct _WSNetHubSt hub,
yUrlRef  url,
const char *  request,
int  request_len,
int  mstimout,
char *  errmsg 
)
static

Definition at line 2353 of file ytcp.c.

static int ws_parseIncommingFrame ( HubSt hub,
u8 *  buffer,
int  pktlen,
char *  errmsg 
)
static

Definition at line 1954 of file ytcp.c.

static int ws_processRequests ( HubSt hub,
char *  errmsg 
)
static

Definition at line 2213 of file ytcp.c.

static int ws_requestStillPending ( HubSt hub)
static

Definition at line 2189 of file ytcp.c.

static int ws_sendAuthenticationMeta ( HubSt hub,
char *  errmsg 
)
static

Definition at line 1896 of file ytcp.c.

static int ws_sendFrame ( HubSt hub,
int  stream,
int  tcpchan,
const u8 *  data,
int  datalen,
char *  errmsg 
)
static

Definition at line 1843 of file ytcp.c.

void* ws_thread ( void *  ctx)

Background thread for WebSocket Hub

Definition at line 2537 of file ytcp.c.

static int ws_thread_select ( struct _WSNetHubSt base_req,
u64  ms,
WakeUpSocket wuce,
char *  errmsg 
)
static

Definition at line 2455 of file ytcp.c.

static void ws_threadUpdateRetryCount ( HubSt hub)
static

Definition at line 2521 of file ytcp.c.

int yConsumeWakeUpSocket ( WakeUpSocket wuce,
char *  errmsg 
)

Definition at line 271 of file ytcp.c.

YSTATIC int yDetectNetworkInterfaces ( u32  only_ip)

Definition at line 2858 of file ytcp.c.

int yDringWakeUpSocket ( WakeUpSocket wuce,
u8  signal,
char *  errmsg 
)

Definition at line 263 of file ytcp.c.

void yDupSet ( char **  storage,
const char *  val 
)

Definition at line 128 of file ytcp.c.

void yFreeWakeUpSocket ( WakeUpSocket wuce)

Definition at line 281 of file ytcp.c.

static void yHTTPCloseReqEx ( struct _RequestSt req,
int  canReuseSocket 
)
static

Definition at line 959 of file ytcp.c.

static int yHTTPMultiSelectReq ( struct _RequestSt **  reqs,
int  size,
u64  ms,
WakeUpSocket wuce,
char *  errmsg 
)
static

Definition at line 990 of file ytcp.c.

static int yHTTPOpenReqEx ( struct _RequestSt req,
u64  mstimout,
char *  errmsg 
)
static

Definition at line 838 of file ytcp.c.

void yInitWakeUpSocket ( WakeUpSocket wuce)

Definition at line 218 of file ytcp.c.

static int yNetLogErrEx ( u32  line,
unsigned  err 
)
static

Definition at line 167 of file ytcp.c.

int yNetSetErrEx ( u32  line,
unsigned  err,
char *  errmsg 
)

Definition at line 141 of file ytcp.c.

struct _RequestSt* yReqAlloc ( struct _HubSt hub)

Definition at line 1337 of file ytcp.c.

void yReqClose ( struct _RequestSt req)

Definition at line 1561 of file ytcp.c.

void yReqFree ( struct _RequestSt req)

Definition at line 1606 of file ytcp.c.

int yReqGet ( struct _RequestSt req,
u8 **  buffer 
)

Definition at line 1504 of file ytcp.c.

int yReqHasPending ( struct _HubSt hub)

Definition at line 1629 of file ytcp.c.

int yReqIsAsync ( struct _RequestSt req)

Definition at line 1596 of file ytcp.c.

int yReqIsEof ( struct _RequestSt req,
char *  errmsg 
)

Definition at line 1486 of file ytcp.c.

int yReqMultiSelect ( struct _RequestSt **  tcpreq,
int  size,
u64  ms,
WakeUpSocket wuce,
char *  errmsg 
)

Definition at line 1479 of file ytcp.c.

int yReqOpen ( struct _RequestSt req,
int  wait_for_start,
int  tcpchan,
const char *  request,
int  reqlen,
u64  mstimeout,
yapiRequestAsyncCallback  callback,
void *  context,
RequestProgress  progress_cb,
void *  progress_ctx,
char *  errmsg 
)

Definition at line 1362 of file ytcp.c.

int yReqRead ( struct _RequestSt req,
u8 *  buffer,
int  len 
)

Definition at line 1525 of file ytcp.c.

int yReqSelect ( struct _RequestSt tcpreq,
u64  ms,
char *  errmsg 
)

Definition at line 1470 of file ytcp.c.

u32 yResolveDNS ( const char *  name,
char *  errmsg 
)

Definition at line 296 of file ytcp.c.

static void ySSDP_parseSSPDMessage ( SSDPInfos SSDP,
char *  message,
int  msg_len 
)
static

Definition at line 3060 of file ytcp.c.

static void* ySSDP_thread ( void *  ctx)
static

Definition at line 3159 of file ytcp.c.

static void ySSDPCheckExpiration ( SSDPInfos SSDP)
static

Definition at line 3039 of file ytcp.c.

int ySSDPDiscover ( SSDPInfos SSDP,
char *  errmsg 
)

Definition at line 3233 of file ytcp.c.

int ySSDPStart ( SSDPInfos SSDP,
ssdpHubDiscoveryCallback  callback,
char *  errmsg 
)

Definition at line 3253 of file ytcp.c.

void ySSDPStop ( SSDPInfos SSDP)

Definition at line 3330 of file ytcp.c.

static void ySSDPUpdateCache ( SSDPInfos SSDP,
const char *  uuid,
const char *  url,
int  cacheValidity 
)
static

Definition at line 2993 of file ytcp.c.

int yStartWakeUpSocket ( WakeUpSocket wuce,
char *  errmsg 
)

Definition at line 225 of file ytcp.c.

static int yTcpCheckReqTimeout ( struct _RequestSt req,
char *  errmsg 
)
static

Definition at line 792 of file ytcp.c.

static int yTcpCheckSocketStillValid ( YSOCKET  skt,
char *  errmsg 
)
static

Definition at line 569 of file ytcp.c.

static void yTcpClose ( YSOCKET  skt)
static

Definition at line 560 of file ytcp.c.

int yTcpDownload ( const char *  host,
const char *  url,
u8 **  out_buffer,
u32  mstimeout,
char *  errmsg 
)

Definition at line 702 of file ytcp.c.

int yTcpInit ( char *  errmsg)

Definition at line 364 of file ytcp.c.

static int yTcpOpen ( YSOCKET newskt,
u32  ip,
u16  port,
u64  mstimeout,
char *  errmsg 
)
static

Definition at line 398 of file ytcp.c.

static int yTcpRead ( YSOCKET  skt,
u8 *  buffer,
int  len,
char *  errmsg 
)
static

Definition at line 679 of file ytcp.c.

void yTcpShutdown ( void  )

Definition at line 382 of file ytcp.c.

static int yTcpWrite ( YSOCKET  skt,
const char *  buffer,
int  len,
char *  errmsg 
)
static

Definition at line 626 of file ytcp.c.

static void yWSCloseReqEx ( struct _RequestSt req,
int  takeCS 
)
static

Definition at line 1215 of file ytcp.c.

static int yWSOpenReqEx ( struct _RequestSt req,
int  tcpchan,
u64  mstimeout,
char *  errmsg 
)
static

Definition at line 1146 of file ytcp.c.

static int yWSSelectReq ( struct _RequestSt req,
u64  mstimeout,
char *  errmsg 
)
static

Definition at line 1202 of file ytcp.c.

Variable Documentation

os_ifaces detectedIfaces[NB_OS_IFACES]

Definition at line 2813 of file ytcp.c.

const char* discovery
static
Initial value:
=
"M-SEARCH * HTTP/1.1\r\n"
"MAN:\"ssdp:discover\"\r\n"
"MX:5\r\n"
"ST:" YSSDP_URN_YOCTOPUCE"\r\n"
"\r\n"
#define YSSDP_PORT
Definition: ydef.h:858
#define YSSDP_MCAST_ADDR_STR
Definition: ydef.h:859
#define YSSDP_URN_YOCTOPUCE
Definition: ydef.h:861
#define TOSTRING(x)
Definition: yproto.h:423

Definition at line 2926 of file ytcp.c.

Definition at line 326 of file ytcp.c.

int nbDetectedIfaces = 0

Definition at line 2814 of file ytcp.c.

const char* ws_header_end = "\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\n\r\n"
static

Definition at line 1670 of file ytcp.c.

const char* ws_header_start = " HTTP/1.1\r\nSec-WebSocket-Version: 13\r\nUser-Agent: Yoctopuce\r\nSec-WebSocket-Key: "
static

Definition at line 1669 of file ytcp.c.



yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:13