diff --git a/serialsock/canserver.creator.user b/serialsock/canserver.creator.user index 2810e57..1573d70 100644 --- a/serialsock/canserver.creator.user +++ b/serialsock/canserver.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 0 + 0 ProjectExplorer.Project.EditorSettings @@ -28,15 +28,17 @@ QmlJSGlobal - 2 + 2 KOI8-R false 4 false + 0 80 true true 1 + 0 false false false @@ -54,16 +56,31 @@ *.md, *.MD, Makefile true true + true ProjectExplorer.Project.PluginSettings + + true + false + true + true + true + true + + false + + + 0 + true true true Builtin.DefaultTidyAndClazy 4 + false @@ -81,9 +98,9 @@ Desktop Desktop {91347f2c-5221-46a7-80b1-0a054ca02f79} - 0 - 0 - 0 + 0 + 0 + 0 /home/eddy/Docs/SAO/ELECTRONICS/CAN_controller/Socket_CANserver @@ -94,9 +111,9 @@ true GenericProjectManager.GenericMakeStep - 1 - Build - Build + 1 + Сборка + Сборка ProjectExplorer.BuildSteps.Build @@ -107,9 +124,9 @@ true GenericProjectManager.GenericMakeStep - 1 - Clean - Clean + 1 + Очистка + Очистка ProjectExplorer.BuildSteps.Clean 2 @@ -120,12 +137,12 @@ Default GenericProjectManager.GenericBuildConfiguration - 1 + 1 - 0 - Deploy - Deploy + 0 + Развёртывание + Развёртывание ProjectExplorer.BuildSteps.Deploy 1 @@ -133,24 +150,29 @@ false ProjectExplorer.DefaultDeployConfiguration - 1 + 1 + true + true + true 2 + false + -e cpu-cycles --call-graph dwarf,4096 -F 250 + ProjectExplorer.CustomExecutableRunConfiguration - false + false true - false true - 1 + 1 ProjectExplorer.Project.TargetCount - 1 + 1 ProjectExplorer.Project.Updater.FileVersion diff --git a/serialsock/cmdlnopts.h b/serialsock/cmdlnopts.h index 8592cd8..94041f5 100644 --- a/serialsock/cmdlnopts.h +++ b/serialsock/cmdlnopts.h @@ -27,8 +27,7 @@ typedef struct{ char *devpath; // path to serial device char *pidfile; // name of PID file char *logfile; // logging to this file - char *path; // path to socket file - // int port; + char *path; // path to socket file (UNIX-socket) or number of port (local INET) int speed; // connection speed int verbose; // verbose level: for messages & logging int client; // ==1 if application runs in client mode diff --git a/serialsock/sersock.c b/serialsock/sersock.c index 6e0fbdc..2b7ca78 100644 --- a/serialsock/sersock.c +++ b/serialsock/sersock.c @@ -308,15 +308,21 @@ static TTY_descr *openserialdev(char *path, int speed){ int start_socket(int server, char *path, TTY_descr **dev){ char apath[128]; DBG("path: %s", path); - if(*path == 0){ - DBG("convert name"); - apath[0] = 0; - strncpy(apath+1, path+1, 126); - }else if(strncmp("\\0", path, 2) == 0){ - DBG("convert name"); - apath[0] = 0; - strncpy(apath+1, path+2, 126); - }else strcpy(apath, path); + char *eptr; + long port = strtol(path, &eptr, 0); + if(eptr && *eptr){ + DBG("UNIX socket"); + port = -1; + if(*path == 0){ + DBG("convert name"); + apath[0] = 0; + strncpy(apath+1, path+1, 126); + }else if(strncmp("\\0", path, 2) == 0){ + DBG("convert name"); + apath[0] = 0; + strncpy(apath+1, path+2, 126); + }else strcpy(apath, path); + } if(server){ if(!dev) return 1; if(!(*dev = openserialdev(GP->devpath, GP->speed))){ @@ -327,28 +333,61 @@ int start_socket(int server, char *path, TTY_descr **dev){ } int sock = -1; int reuseaddr = 1; - struct sockaddr_un saddr = {0}; - saddr.sun_family = AF_UNIX; - memcpy(saddr.sun_path, apath, 106); // if sun_path[0] == 0 we don't create a file - if((sock = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0){ // or SOCK_STREAM? - LOGERR("socket()"); - ERR("socket()"); - } - if(server){ - if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){ - LOGWARN("setsockopt"); - WARN("setsockopt"); - } - if(bind(sock, &saddr, sizeof(saddr)) == -1){ - close(sock); - LOGERR("bind"); - ERR("bind"); + struct addrinfo hints = {0}, *res; + struct sockaddr_un unaddr = {0}; + unaddr.sun_family = AF_UNIX; + if(port > 0){ + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + if(getaddrinfo("127.0.0.1", path, &hints, &res) != 0){ + ERR("getaddrinfo"); } }else{ - if(connect(sock, &saddr, sizeof(saddr)) == -1){ - LOGERR("connect()"); - ERR("connect()"); + memcpy(unaddr.sun_path, apath, 106); // if sun_path[0] == 0 we don't create a file + hints.ai_addr = (struct sockaddr*) &unaddr; + hints.ai_addrlen = sizeof(unaddr); + hints.ai_family = AF_UNIX; + hints.ai_socktype = SOCK_SEQPACKET; + res = &hints; + } + for(struct addrinfo *p = res; p; p = p->ai_next){ + if((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0){ // or SOCK_STREAM? + LOGWARN("socket()"); + WARN("socket()"); + continue; } + if(server){ + int reuseaddr = 1; + if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1){ + LOGWARN("setsockopt()"); + WARN("setsockopt()"); + close(sock); sock = -1; + continue; + } + if(bind(sock, p->ai_addr, p->ai_addrlen) == -1){ + LOGWARN("bind()"); + WARN("bind()"); + close(sock); sock = -1; + continue; + } + int enable = 1; + if(ioctl(sock, FIONBIO, (void *)&enable) < 0){ // make socket nonblocking + LOGERR("Can't make socket nonblocking"); + ERRX("ioctl()"); + } + }else{ + if(connect(sock, p->ai_addr, p->ai_addrlen) == -1){ + LOGWARN("connect()"); + WARN("connect()"); + close(sock); sock = -1; + } + } + break; + } + if(sock < 0){ + LOGERR("Can't open socket"); + ERRX("Can't open socket"); } if(server) server_(sock, *dev); else client_(sock);