From 29b331b4375c1e07fee0e943ec90c3a855b6d428 Mon Sep 17 00:00:00 2001 From: Guo Wenxue <guowenxue@gmail.com> Date: Fri, 08 Sep 2023 09:59:40 +0800 Subject: [PATCH] Update socketd example project --- project/socketd/booster/proc.c | 2 project/socketd/booster/database.c | 265 +++++++++++++ project/socketd/booster/packet.h | 70 +++ project/socketd/booster/socket.h | 100 +++++ project/socketd/sock_client.c | 21 project/socketd/makefile | 13 project/socketd/booster/packet.c | 95 ++++ project/socketd/booster/ds18b20.c | 1 project/socketd/booster/logger.h | 4 project/socketd/booster/database.h | 60 +++ project/socketd/booster/socket.c | 521 ++++++++++++++++++++++++++ project/socketd/booster/logger.c | 4 12 files changed, 1,133 insertions(+), 23 deletions(-) diff --git a/project/socketd/booster/database.c b/project/socketd/booster/database.c new file mode 100644 index 0000000..00b10cb --- /dev/null +++ b/project/socketd/booster/database.c @@ -0,0 +1,265 @@ +/******************************************************************************** + * Copyright: (C) 2020 LingYun IoT System Studio + * All rights reserved. + * + * Filename: database.c + * Description: This library used to operate blob packet in sqlite database. + * + * Version: 1.0.0(2020年05月13日) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2020年05月13日 12时14分23秒" + * + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include "database.h" +#include "logger.h" + +/* Blob packet table name */ +#define TABLE_NAME "PackTable" + +/* Use static global handler here in order to simplify the API, + * But it will make this library not thread safe + */ +static sqlite3 *s_clidb = NULL; + + +/* description: open or create sqlite database if not exist + * input args: + * $db_file: sqlite database file name + * return value: <0: failure 0:ok + * */ +int database_init(const char *db_file) +{ + char sql[SQL_COMMAND_LEN]={0}; + char *errmsg = NULL; + + if( !db_file ) + { + log_error("%s() Invalid input arguments\n", __func__); + return -1; + } + + /*+------------------------------------------+ + *| database already exist, just open it | + *+------------------------------------------+*/ + if( 0==access(db_file, F_OK) ) + { + if( SQLITE_OK != sqlite3_open(db_file, &s_clidb) ) + { + log_error("open database file '%s' failure\n", db_file); + return -2; + } + log_info("open database file '%s' ok\n", db_file); + return 0; + } + + /*+-----------------------------------------+ + *| database not exist, create and init it | + *+-----------------------------------------+*/ + + if( SQLITE_OK != sqlite3_open(db_file, &s_clidb) ) + { + log_error("create database file '%s' failure\n", db_file); + return -2; + } + + /* SQLite continues without syncing as soon as it has handed data off to the operating system */ + sqlite3_exec(s_clidb, "pragma synchronous = OFF; ", NULL, NULL, NULL); + + /* enable full auto vacuum, Auto increase/decrease */ + sqlite3_exec(s_clidb, "pragma auto_vacuum = 2 ; ", NULL, NULL, NULL); + + /* Create firehost table in the database */ + snprintf(sql, sizeof(sql), "CREATE TABLE %s(packet BLOB);", TABLE_NAME); + if( SQLITE_OK != sqlite3_exec(s_clidb, sql, NULL, NULL, &errmsg) ) + { + log_error("create data_table in database file '%s' failure: %s\n", db_file, errmsg); + sqlite3_free(errmsg); /* free errmsg */ + sqlite3_close(s_clidb); /* close databse */ + unlink(db_file); /* remove database file */ + return -3; + } + + log_info("create and init database file '%s' ok\n", db_file); + return 0; +} + + +/* description: close sqlite database handler + * return value: none + */ +void database_term(void) +{ + log_warn("close sqlite database now\n"); + sqlite3_close(s_clidb); + + return ; +} + + +/* description: push a blob packet into database + * input args: + * $pack: blob packet data address + * $size: blob packet data bytes + * return value: <0: failure 0:ok + */ +int database_push_packet(void *pack, int size) +{ + char sql[SQL_COMMAND_LEN]={0}; + int rv = 0; + sqlite3_stmt *stat = NULL; + + if( !pack || size<=0 ) + { + log_error("%s() Invalid input arguments\n", __func__); + return -1; + } + + if( ! s_clidb ) + { + log_error("sqlite database not opened\n"); + return -2; + } + + snprintf(sql, sizeof(sql), "insert into %s(packet) values(?)", TABLE_NAME); + rv = sqlite3_prepare_v2(s_clidb, sql, -1, &stat, NULL); + if(SQLITE_OK!=rv || !stat) + { + log_error("blob add sqlite3_prepare_v2 failure\n"); + rv = -2; + goto OUT; + } + + if( SQLITE_OK != sqlite3_bind_blob(stat, 1, pack, size, NULL) ) + { + log_error("blob add sqlite3_bind_blob failure\n"); + rv = -3; + goto OUT; + } + + rv = sqlite3_step(stat); + if( SQLITE_DONE!=rv && SQLITE_ROW!=rv ) + { + log_error("blob add sqlite3_step failure\n"); + rv = -4; + goto OUT; + } + +OUT: + sqlite3_finalize(stat); + + if( rv < 0 ) + log_error("add new blob packet into database failure, rv=%d\n", rv); + else + log_info("add new blob packet into database ok\n"); + + return rv; +} + + +/* description: pop the first blob packet from database + * input args: + * $pack: blob packet output buffer address + * $size: blob packet output buffer size + * $byte: blob packet bytes + * return value: <0: failure 0:ok + */ +int database_pop_packet(void *pack, int size, int *bytes) +{ + char sql[SQL_COMMAND_LEN]={0}; + int rv = 0; + sqlite3_stmt *stat = NULL; + const void *blob_ptr; + + if( !pack || size<=0 ) + { + log_error("%s() Invalid input arguments\n", __func__); + return -1; + } + + if( ! s_clidb ) + { + log_error("sqlite database not opened\n"); + return -2; + } + + /* Only query the first packet record */ + snprintf(sql, sizeof(sql), "select packet from %s limit 0,1;", TABLE_NAME); + rv = sqlite3_prepare_v2(s_clidb, sql, -1, &stat, NULL); + if(SQLITE_OK!=rv || !stat) + { + log_error("firehost sqlite3_prepare_v2 failure\n"); + rv = -3; + goto out; + } + + rv = sqlite3_step(stat); + if( SQLITE_DONE!=rv && SQLITE_ROW!=rv ) + { + log_error("firehost sqlite3_step failure\n"); + rv = -5; + goto out; + } + + /* 1rd argument<0> means first segement is packet */ + blob_ptr = sqlite3_column_blob(stat, 0); + if( !blob_ptr ) + { + rv = -6; + goto out; + } + + *bytes = sqlite3_column_bytes(stat, 0); + + if( *bytes > size ) + { + log_error("blob packet bytes[%d] larger than bufsize[%d]\n", *bytes, size); + *bytes = 0; + rv = -1; + } + + memcpy(pack, blob_ptr, *bytes); + rv = 0; + +out: + sqlite3_finalize(stat); + return rv; +} + + +/* description: remove the first blob packet from database + * input args: none + * return value: <0: failure 0:ok + */ +int database_del_packet(void) +{ + char sql[SQL_COMMAND_LEN]={0}; + char *errmsg = NULL; + + if( ! s_clidb ) + { + log_error("sqlite database not opened\n"); + return -2; + } + + /* remove packet from db */ + memset(sql, 0, sizeof(sql)); + snprintf(sql, sizeof(sql), "delete from %s limit 0,1;", TABLE_NAME); + if( SQLITE_OK != sqlite3_exec(s_clidb, sql, NULL, 0, &errmsg) ) + { + log_error("delete first blob packet from database failure: %s\n", errmsg); + sqlite3_free(errmsg); + return -2; + } + log_warn("delete first blob packet from database ok\n"); + + /* Vacuum the database */ + sqlite3_exec(s_clidb, "VACUUM;", NULL, 0, NULL); + + return 0; +} + diff --git a/project/socketd/booster/database.h b/project/socketd/booster/database.h new file mode 100644 index 0000000..f64771d --- /dev/null +++ b/project/socketd/booster/database.h @@ -0,0 +1,60 @@ +/******************************************************************************** + * Copyright: (C) 2020 LingYun IoT System Studio + * All rights reserved. + * + * Filename: database.h + * Description: This library used to operate blob packet in sqlite database. + * + * Version: 1.0.0(2020年05月13日) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2020年05月13日 12时14分23秒" + * + ********************************************************************************/ +#ifndef _DATABASE_H_ +#define _DATABASE_H_ + +#include "sqlite3.h" + +#define SQL_COMMAND_LEN 256 + +/* description: open or create sqlite database if not exist + * input args: + * $db_file: sqlite database file name + * return value: <0: failure 0:ok + * */ +extern int database_init(const char *db_file); + + +/* description: close sqlite database handler + * return value: none + */ +extern void database_term(void); + + +/* description: push a blob packet into database + * input args: + * $pack: blob packet data address + * $size: blob packet data bytes + * return value: <0: failure 0:ok + */ +extern int database_push_packet(void *pack, int size); + + +/* description: pop the first blob packet from database + * input args: + * $pack: blob packet output buffer address + * $size: blob packet output buffer size + * $byte: blob packet bytes + * return value: <0: failure 0:ok + */ +extern int database_pop_packet(void *pack, int size, int *bytes); + + +/* description: remove the first blob packet from database + * input args: none + * return value: <0: failure 0:ok + */ +extern int database_del_packet(void); + + +#endif /* ----- #ifndef _DATABASE_H_ ----- */ diff --git a/project/socketd/booster/ds18b20.c b/project/socketd/booster/ds18b20.c index 5f279fe..aa1e01b 100644 --- a/project/socketd/booster/ds18b20.c +++ b/project/socketd/booster/ds18b20.c @@ -49,7 +49,6 @@ struct dirent *direntp; int fd =-1; char *ptr; - float value; int found = 0; if( !temp ) diff --git a/project/socketd/booster/logger.c b/project/socketd/booster/logger.c index 217eb02..1af3b0e 100644 --- a/project/socketd/booster/logger.c +++ b/project/socketd/booster/logger.c @@ -4,11 +4,11 @@ * * Filename: logger.c * Description: This file is common logger API functions - * + * * Version: 1.0.0(11/08/23) * Author: Guo Wenxue <guowenxue@gmail.com> * ChangeLog: 1, Release initial version on "11/08/23 16:18:43" - * + * ********************************************************************************/ #include <stdio.h> diff --git a/project/socketd/booster/logger.h b/project/socketd/booster/logger.h index 484ac33..6f1f7e7 100644 --- a/project/socketd/booster/logger.h +++ b/project/socketd/booster/logger.h @@ -4,11 +4,11 @@ * * Filename: logger.h * Description: This file is common logger API functions - * + * * Version: 1.0.0(11/08/23) * Author: Guo Wenxue <guowenxue@gmail.com> * ChangeLog: 1, Release initial version on "11/08/23 16:18:43" - * + * ********************************************************************************/ #ifndef _LOGGER_H_ diff --git a/project/socketd/booster/packet.c b/project/socketd/booster/packet.c new file mode 100644 index 0000000..a6075fe --- /dev/null +++ b/project/socketd/booster/packet.c @@ -0,0 +1,95 @@ +/********************************************************************************* + * Copyright: (C) 2022 LingYun IoT System Studio + * All rights reserved. + * + * Filename: packet.c + * Description: This file is packet API functions + * + * Version: 1.0.0(18/04/22) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "18/04/22 16:30:25" + * + ********************************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <time.h> +#include "packet.h" +#include "logger.h" +#include "ds18b20.h" + +int get_devid(char *devid, int size, int sn) +{ + if( !devid || size<DEVID_LEN ) + { + log_error("Invalid input arugments\n"); + return -1; + } + + memset(devid, 0, size); + snprintf(devid, size, "rpi#%04d", sn); + return 0; +} + +int get_time(struct tm *ptm) +{ + if( !ptm ) + { + log_error("Invalid input arugments\n"); + return -1; + } + + + time_t now = time(NULL); + localtime_r(&now, ptm); + + return 0; +} + +int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size) +{ + char strtime[TIME_LEN] = {'\0'}; + struct tm *ptm; + + if( !pack_info || !pack_buf || size<=0 ) + { + log_error("Invalid input arguments\n"); + return -1; + } + + ptm = &pack_info->sample_time; + snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d", + ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + + + memset(pack_buf, 0, size); + snprintf(pack_buf, size, "%s|%s|%.3f", pack_info->devid, strtime, pack_info->temper); + + return strlen(pack_buf); +} + +int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size) +{ + char strtime[TIME_LEN] = {'\0'}; + struct tm *ptm; + + if( !pack_info || !pack_buf || size<=0 ) + { + log_error("Invalid input arguments\n"); + return -1; + } + + ptm = &pack_info->sample_time; + snprintf(strtime, sizeof(strtime), "%04d-%02d-%2d %02d:%02d:%02d", + ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min, ptm->tm_sec); + + memset(pack_buf, 0, size); + snprintf(pack_buf, size, "{\"devid\":\"%s\", \"time\":\"%s\",\"temperature\":\"%.3f\"}", + pack_info->devid, strtime, pack_info->temper); + + return strlen(pack_buf); +} + diff --git a/project/socketd/booster/packet.h b/project/socketd/booster/packet.h new file mode 100644 index 0000000..6344e22 --- /dev/null +++ b/project/socketd/booster/packet.h @@ -0,0 +1,70 @@ +/******************************************************************************** + * Copyright: (C) 2022 LingYun IoT System Studio + * All rights reserved. + * + * Filename: packet.h + * Description: This head file is packet API functions. + * + * Version: 1.0.0(18/04/22) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "18/04/22 16:24:40" + * + ********************************************************************************/ + + +#ifndef _PACKET_H_ +#define _PACKET_H_ + +#include <stdint.h> +#include <time.h> + +#define DEVID_LEN 16 +#define TIME_LEN 32 + +typedef struct pack_info_s +{ + char devid[DEVID_LEN]; /* device ID */ + struct tm sample_time; /* sample time */ + float temper; /* sample temperature */ +} pack_info_t; + +/* packet function pointer type */ +typedef int (* pack_proc_t)(pack_info_t *pack_info, char *pack_buf, int size); + +/* description: get device ID + * input args: + * $devid : device ID string + * $size : device ID output buffer size + * $sn : serial number + * return value: <0: failure 0:ok + */ +extern int get_devid(char *devid, int size, int sn); + +/* description: get current system in struct tm + * input args: + * $sample_time: sample time in struct tm + * return value: <0: failure 0:ok + */ +extern int get_time(struct tm *sample_time); + +/* description: package a string packet in format "devid|time|temper" + * input args: + * $pack_info: packet data contains devid, time and temperature + * $pack_buf : packet output buffer + * $size : packet output buffer size + * return value: <0: failure >0: packet bytes + */ +extern int packet_segmented_pack(pack_info_t *pack_info, char *pack_buf, int size); + + +/* description: package a json string packet: {"devid":"xxx", "time":"xxx", "temperature":"xxx"} + * input args: + * $pack_info: packet data contains devid, time and temperature + * $pack_buf : packet output buffer + * $size : packet output buffer size + * return value: <0: failure >0: packet bytes + */ +extern int packet_json_pack(pack_info_t *pack_info, char *pack_buf, int size); + + +#endif /* ----- #ifndef _PACKET_H_ ----- */ diff --git a/project/socketd/booster/proc.c b/project/socketd/booster/proc.c index d233d83..3fa8907 100644 --- a/project/socketd/booster/proc.c +++ b/project/socketd/booster/proc.c @@ -161,7 +161,7 @@ if( check_daemon_running(pidfile) ) { - log_error("Program already running, process exit now"); + log_error("Program already running, process exit now\n"); return -1; } diff --git a/project/socketd/booster/socket.c b/project/socketd/booster/socket.c new file mode 100644 index 0000000..0206bb9 --- /dev/null +++ b/project/socketd/booster/socket.c @@ -0,0 +1,521 @@ +/******************************************************************************** + * Copyright: (C) 2022 LingYun IoT System Studio + * All rights reserved. + * + * Filename: socket.c + * Description: This file is for socket API functions + * + * Version: 1.0.0(18/04/22) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "18/04/22 17:09:59" + * + ********************************************************************************/ +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <netdb.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/un.h> +#include <poll.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <linux/sockios.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <arpa/inet.h> +#include <sys/resource.h> + +#include "socket.h" +#include "logger.h" + +/* description: initial socket context + * input args: + * $sock: socket context pointer + * $host: connect server hostname for client mode, unused for server mode + * $port: connect server port for client mode or listen port for server mode + * return value: <0: failure 0:ok + */ +int socket_init(socket_ctx_t *sock, char *host, int port) +{ + if( !sock || port<=0 ) + return -1; + + memset( sock, 0, sizeof(*sock) ); + sock->fd = -1; + sock->port = port; + if( host ) /* server no need it */ + { + strncpy(sock->host, host, HOSTNAME_LEN); + } + + return 0; +} + +/* description: close socket + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +int socket_term(socket_ctx_t *sock) +{ + if( !sock ) + return -1; + + if( sock->fd > 0) + { + close(sock->fd); + sock->fd = -1; + } + + return 0; +} + +/* description: socket server start listen + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +#if 0 /* --TBD-- */ +int socket_listen(socket_ctx_t *sock) +{ + int rv = 0; + struct sockaddr_in addr; + int backlog = 13; + + if( !sock ) + return -1; + + set_socket_rlimit(); /* set max open socket count */ +} +#endif + +/* description: socket connect to server in block mode + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +int socket_connect(socket_ctx_t *sock) +{ + int rv = 0; + int sockfd = 0; + char service[20]; + struct addrinfo hints, *rp; + struct addrinfo *res = NULL; + struct in_addr inaddr; + struct sockaddr_in addr; + int len = sizeof(addr); + + if( !sock ) + return -1; + + socket_term(sock); + + /*+--------------------------------------------------+ + *| use getaddrinfo() to do domain name translation | + *+--------------------------------------------------+*/ + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_INET; /* Only support IPv4 */ + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; /* TCP protocol */ + + /* If $host is a valid IP address, then don't use name resolution */ + if( inet_aton(sock->host, &inaddr) ) + { + //log_info("%s is a valid IP address, don't use domain name resolution.\n", sock->host); + hints.ai_flags |= AI_NUMERICHOST; + } + + /* Obtain address(es) matching host/port */ + snprintf(service, sizeof(service), "%d", sock->port); + if( (rv=getaddrinfo(sock->host, service, &hints, &res)) ) + { + log_error("getaddrinfo() parser [%s:%s] failed: %s\n", sock->host, service, gai_strerror(rv)); + return -3; + } + + /* getaddrinfo() returns a list of address structures. Try each + address until we successfully connect or bind */ + for (rp=res; rp!=NULL; rp=rp->ai_next) + { +#if 0 + char ipaddr[INET_ADDRSTRLEN]; + struct sockaddr_in *sp = (struct sockaddr_in *) rp->ai_addr; + + /* print domain name translation result */ + memset( ipaddr, 0, sizeof(ipaddr) ); + if( inet_ntop(AF_INET, &sp->sin_addr, ipaddr, sizeof(ipaddr)) ) + { + log_info("domain name resolution [%s->%s]\n", sock->host, ipaddr); + } +#endif + + /* Create the socket */ + sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if( sockfd < 0) + { + log_error("socket() create failed: %s\n", strerror(errno)); + rv = -3; + continue; + } + + /* connect to server */ + rv = connect(sockfd, rp->ai_addr, len); + if( 0 == rv ) + { + sock->fd = sockfd; + log_info("Connect to server[%s:%d] on fd[%d] successfully!\n", sock->host, sock->port, sockfd); + break; + } + else + { + /* socket connect get error, try another IP address */ + close(sockfd); + continue; + } + } + + freeaddrinfo(res); + return rv; +} + +/* description: send data from the socket + * input args: + * $sock : socket context pointer + * $data : socket send data + * $bytes: socket send data bytes + * return value: <0: failure 0:ok + */ +int socket_send(socket_ctx_t *sock, char *data, int bytes) +{ + int rv = 0; + int i = 0; + int left_bytes = bytes; + + if( !sock || !data || bytes<= 0 ) + return -1; + + while( left_bytes > 0 ) + { + rv=write(sock->fd, &data[i], left_bytes); + if( rv < 0 ) + { + log_info("socket[%d] write() failure: %s, close socket now\n", sock->fd, strerror(errno)); + socket_term(sock); + return -2; + } + else if( rv == left_bytes ) + { + log_info("socket send %d bytes data over\n", bytes); + return 0; + } + else + { + /* not send over this time, continue to send left data */ + i += rv; + left_bytes -= rv; + continue; + } + } + + return 0; +} + +/* description: receive data from the socket + * input args: + * $sock : socket context pointer + * $buf : socket receive data buffer + * $size : socket receive data buffer size + * $timeout: receive data time, <=0 will don't timeout + * return value: <0: failure 0:ok + */ +int socket_recv(socket_ctx_t *sock, char *buf, int size, int timeout) +{ + int rv = 0; + fd_set rdset; + int maxfd; + + if( !sock || !buf || size<= 0 ) + return -1; + + memset(buf, 0, size); + + maxfd = sock->fd; + FD_ZERO(&rdset); + FD_SET(sock->fd, &rdset); + + if( timeout <= 0 ) /* no timeout */ + { + rv=select(maxfd+1, &rdset, NULL, NULL, NULL); + } + else + { + struct timeval tv; + tv.tv_sec = timeout; + tv.tv_usec = 0; + rv=select(maxfd+1, &rdset, NULL, NULL, &tv); + } + + if( rv < 0 ) + { + log_error("select() on socket[%d] got error: %s\n", sock->fd, strerror(errno)); + return -2; + } + else if( rv == 0 ) + { + log_error("select() on socket[%d] get timeout\n", sock->fd); + return 0; + } + else + { + rv = read(sock->fd, buf, size); + if( rv <= 0 ) + { + log_error("socket[%d] read() failure or got disconnected: %s, close socket now\n", sock->fd, strerror(errno)); + socket_term(sock); + return -2; + } + else + { + log_debug("socket[%d] receive %d bytes data\n", sock->fd, rv); + return rv; + } + } +} + + + +/*+-------------------------------------------------------------------+ + *| socket utils function | + *+-------------------------------------------------------------------+*/ + + +/* socket connected or not: <0: failure 0:ok */ +int sock_check_connect(int sockfd) +{ + struct tcp_info info; + int len=sizeof(info); + + if( sockfd < 0 ) + return -1; + + getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *)&len); + + if( TCP_CLOSE==info.tcpi_state || TCP_CLOSING==info.tcpi_state || TCP_CLOSE_WAIT==info.tcpi_state ) + { + return -3; + } + + return -0; +} + +/* description: set socket listen port as reusable, fix port already used bug */ +int socket_set_reuseaddr(int sockfd) +{ + int opt = 1; + int len = sizeof (int); + + if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, len)) + { + log_error("Set socket[%d] option SO_REUSEADDR failed:%s\n", sockfd, strerror(errno)); + return -1; + } + log_debug("Set socket[%d] option SO_REUSEADDR ok\n", sockfd); + + return 0; +} + +/* set socket as non-block mode, common socket default work as block mode */ +int socket_set_nonblock(int sockfd) +{ + int opts; + /* + * fcntl may set: + * + * EACCES, EAGAIN: Operation is prohibited by locks held by other + * processes. Or, operation is prohibited because the file has + * been memory-mapped by another process. + * EBADF: fd is not an open file descriptor, or the command was F_SETLK + * or F_SETLKW and the file descriptor open mode doesn't match + * with the type of lock requested. + * EDEADLK: It was detected that the specified F_SETLKW command would + * cause a deadlock. + * EFAULT: lock is outside your accessible address space. + * EINTR: For F_SETLKW, the command was interrupted by a signal. For + * F_GETLK and F_SETLK, the command was interrupted by a signal + * before the lock was checked or acquired. Most likely when + * locking a remote file (e.g. locking over NFS), but can + * sometimes happen locally. + * EINVAL: For F_DUPFD, arg is negative or is greater than the maximum + * allowable value. For F_SETSIG, arg is not an allowable signal + * number. + * EMFILE: For F_DUPFD, the process already has the maximum number of + * file descriptors open. + * ENOLCK: Too many segment locks open, lock table is full, or a remote + * locking protocol failed (e.g. locking over NFS). + * EPERM: Attempted to clear the O_APPEND flag on a file that has the + * append-only attribute set. + */ + opts = fcntl(sockfd, F_GETFL); + if (opts < 0) + { + log_warn("fcntl() get socket options failure: %s\n", strerror(errno)); + return -1; + } + + opts |= O_NONBLOCK; + + if (fcntl(sockfd, F_SETFL, opts) < 0) + { + log_warn("fcntl() set socket options failure: %s\n", strerror(errno)); + return -1; + } + + log_debug("Set socket[%d] none blocking\n", sockfd); + return opts; +} + + +/* set socket receive and send buffer size in linux kernel space */ +int socket_set_buffer(int sockfd, int rsize, int ssize) +{ + int opt; + socklen_t optlen = sizeof(opt); + + if(sockfd < 0) + return -1; + + /* Get system default receive buffer size, Linux X86: 85K */ + if (getsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, &optlen)) + { + log_warn("getsockopt() get receive buffer failure: %s\n", strerror(errno)); + return -2; + } + + /* Only when current receive buffer size larger than the default one will change it */ + if(rsize > opt) + { + opt = (int) rsize; + if (setsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, optlen)) + { + log_warn("setsockopt() set receive buffer to %d failure: %s\n", opt, strerror(errno)); + return -2; + } + } + + /* Get system default send buffer size, Linux X86: 16K */ + if (getsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, &optlen)) + { + log_warn("getsockopt() get send buffer failure: %s\n", strerror(errno)); + return -3; + } + + /* Only when current receive buffer size larger than the default one will change it */ + if(ssize > opt) + { + opt = (int) ssize; + if (setsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, optlen)) + { + log_warn("setsockopt() set send buffer to %d failure: %s\n", opt, strerror(errno)); + return -3; + } + } + + log_info("Set socket[%d] RCVBUF size:%d SNDBUF size:%d\n", sockfd, rsize, ssize); + return 0; +} + +/* + * Enable socket SO_KEEPALIVE, if the connection disconnected, any system call on socket + * will return immediately and errno will be set to "WSAENOTCONN" + * + * keepalive is not program related, but socket related, * so if you have multiple sockets, + * you can handle keepalive for each of them separately. + * + * Reference: http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/ + */ +int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt) +{ + int opt; + + if(sockfd < 0) + return -1; + + /* Enable the KEEPALIVE flag */ + opt = 1; + if (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof (opt))) + { + log_warn("setsockopt() enable SO_KEEPALIVE failure: %s\n", strerror(errno)); + return -2; + } + + if(keepintvl || keepcnt) + { + /* + * The tcp_keepidle parameter specifies the interval between the last data packet sent + * (simple ACKs are not considered data) and the first keepalive probe; after the + * connection is marked to need keepalive, this counter is not used any further. + * ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_time + * 7200 + */ + opt = 3; /* 3 seconds */ + if (setsockopt (sockfd, SOL_TCP, TCP_KEEPIDLE, (char *) &opt, sizeof (opt))) + { + log_error("setsockopt() set TCP_KEEPIDLE to %d seconds failure: %s\n", opt, strerror(errno)); + return -3; + } + + if((opt=keepintvl) > 0) + { + /* + * The tcp_keepintvl parameter specifies the interval between subsequential keepalive + * probes, regardless of what the connection has exchanged in the meantime. + * ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_intvl + * 75 + */ + if (setsockopt (sockfd, SOL_TCP, TCP_KEEPINTVL, (char *) &opt, sizeof (opt))) + { + log_error("setsockopt() set TCP_KEEPINTVL to %d failure: %s\n", opt, strerror(errno)); + return -4; + } + } + + if((opt=keepcnt) > 0) + { + /* + * The TCP_KEEPCNT option specifies the maximum number of unacknowledged probes to + * send before considering the connection dead and notifying the application layer + * probes to be sent. The value of TCP_KEEPCNT is an integer value between 1 and n, + * where n is the value of the systemwide tcp_keepcnt parameter. + * ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_probes + * 9 + */ + if (setsockopt (sockfd, SOL_TCP, TCP_KEEPCNT, (char *) &opt, sizeof (opt))) + { + log_error("setsockopt() set TCP_KEEPCNT to %d failure: %s\n", opt, strerror(errno)); + return -5; + } + } + } + + log_debug("Set socket[%d] KEEPINTVL:%d KEEPCNT:%d\n", sockfd, keepintvl, keepcnt); + return 0; +} + + +/* Set open file description count to max */ +void set_socket_rlimit(void) +{ + struct rlimit limit = {0}; + + getrlimit(RLIMIT_NOFILE, &limit ); + limit.rlim_cur = limit.rlim_max; + setrlimit(RLIMIT_NOFILE, &limit ); + + log_info("set socket open fd max count to %d\n", limit.rlim_max); +} + diff --git a/project/socketd/booster/socket.h b/project/socketd/booster/socket.h new file mode 100644 index 0000000..f0c5fb4 --- /dev/null +++ b/project/socketd/booster/socket.h @@ -0,0 +1,100 @@ +/******************************************************************************** + * Copyright: (C) 2022 LingYun IoT System Studio + * All rights reserved. + * + * Filename: socket.h + * Description: This head file is for socket API functions + * + * Version: 1.0.0(18/04/22) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "18/04/22 17:09:59" + * + ********************************************************************************/ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#define HOSTNAME_LEN 64 + +typedef struct socket_ctx_s +{ + char host[HOSTNAME_LEN]; /* CLIENT: Connect server hostname; SERVER: Unused */ + int port; /* CLIENT: Connect server port; SERVER: listen port */ + int fd; /* socket descriptor */ +} socket_ctx_t; + +/* description: initial socket context + * input args: + * $sock: socket context pointer + * $host: connect server hostname for client mode, unused for server mode + * $port: connect server port for client mode or listen port for server mode + * return value: <0: failure 0:ok + */ +extern int socket_init(socket_ctx_t *sock, char *host, int port); + +/* description: close socket + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +extern int socket_term(socket_ctx_t *sock); + +/* description: socket server start listen + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +extern int socket_listen(socket_ctx_t *sock); + +/* description: socket client connect to server + * input args: + * $sock: socket context pointer + * return value: <0: failure 0:ok + */ +extern int socket_connect(socket_ctx_t *sock); + +/* description: send data from the socket + * input args: + * $sock : socket context pointer + * $data : socket send data + * $bytes: socket send data bytes + * return value: <0: failure 0:ok + */ +extern int socket_send(socket_ctx_t *sock, char *data, int bytes); + +/* description: receive data from the socket + * input args: + * $sock : socket context pointer + * $buf : socket receive data buffer + * $size : socket receive data buffer size + * $timeout: receive data time, <=0 will don't timeout + * return value: <0: failure 0:ok + */ +#define TIMEOUT_NONE 0 +extern int socket_recv(socket_ctx_t *sock, char *buf, int size, int timeout); + +/*+-------------------------------------------------------------------+ + *| socket utils function | + *+-------------------------------------------------------------------+*/ + + +/* socket connected or not: <0: failure 0:ok */ +extern int sock_check_connect(int sockfd); + +/* description: set socket listen port as reusable, fix port already used bug */ +extern int socket_set_reuseaddr(int sockfd); + +/* set socket as non-block mode, common socket default work as block mode */ +extern int socket_set_nonblock(int sockfd); + +/* set socket receive and send buffer size in linux kernel space */ +extern int socket_set_buffer(int sockfd, int rsize, int ssize); + +/* set heartbeat keepalive */ +extern int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt); + +/* Set open file description count to max */ +extern void set_socket_rlimit(void); + +#endif /* ----- #ifndef _SOCKET_H_ ----- */ + diff --git a/project/socketd/makefile b/project/socketd/makefile index 2c72631..4036f46 100644 --- a/project/socketd/makefile +++ b/project/socketd/makefile @@ -12,7 +12,7 @@ #******************************************************************************* PRJ_PATH=$(shell pwd) -APP_NAME = client +APP_NAME = sock_client BUILD_ARCH=$(shell uname -m) ifneq ($(findstring $(BUILD_ARCH), "x86_64" "i386"),) @@ -24,9 +24,6 @@ # common CFLAGS for our source code CFLAGS = -Wall -Wshadow -Wundef -Wmaybe-uninitialized -D_GNU_SOURCE - -# sub-directory need to entry and compile -SUBDIR=booster sqlite # sub-directory compiled to a library and need to link SRCS=booster @@ -40,18 +37,20 @@ CFLAGS+=-I ${PRJ_PATH}/sqlite/install/include LDFLAGS+=-L ${PRJ_PATH}/sqlite/install/lib LDFLAGS+=-lsqlite3 - LDFLAGS+=-lpthread +# sub-directory need to entry and compile +SUBDIR=${SRCS} sqlite + all: entry subdir - ${CROSS_COMPILE}gcc ${CFLAGS} client.c -o client ${LDFLAGS} + ${CROSS_COMPILE}gcc ${CFLAGS} sock_client.c -o sock_client ${LDFLAGS} entry: @echo "Building ${APP_NAME} on ${BUILD_ARCH}" subdir: @for dir in ${SUBDIR} ; do if [ ! -e $${dir} ] ; then ln -s ../$${dir}; fi; done - @for dir in ${SUBDIR} ; do make -C $${dir} ; done + @for dir in ${SUBDIR} ; do make CFLAGS="${CFLAGS}" -C $${dir} ; done install: cp ${APP_NAME} /tftp diff --git a/project/socketd/client.c b/project/socketd/sock_client.c similarity index 85% rename from project/socketd/client.c rename to project/socketd/sock_client.c index e350bef..c9084cd 100644 --- a/project/socketd/client.c +++ b/project/socketd/sock_client.c @@ -46,9 +46,9 @@ int daemon = 1; int opt; char *progname=NULL; - char *logfile="client.log"; - int loglevel=LOG_LEVEL_INFO; - int logsize=10; /* logfile size max to 10K */ + char *logfile="client.log"; + int loglevel=LOG_LEVEL_INFO; + int logsize=10; /* logfile size max to 10K */ struct option long_options[] = { {"debug", no_argument, NULL, 'd'}, @@ -66,8 +66,8 @@ { case 'd': /* Set debug running */ daemon = 0; - logfile="console"; - loglevel=LOG_LEVEL_DEBUG; + logfile="console"; + loglevel=LOG_LEVEL_DEBUG; break; case 'v': /* Get software version */ @@ -84,11 +84,11 @@ } - if( log_open(logfile, loglevel, logsize, THREAD_LOCK_NONE) < 0 ) - { - fprintf(stderr, "Initial log system failed\n"); - return 1; - } + if( log_open(logfile, loglevel, logsize, THREAD_LOCK_NONE) < 0 ) + { + fprintf(stderr, "Initial log system failed\n"); + return 1; + } install_default_signal(); @@ -102,6 +102,7 @@ cleanup: log_close(); + unlink(DAEMON_PIDFILE); return 0; } -- Gitblit v1.9.1