📄 simusock.c
字号:
#include "simu112.h"#include <stdio.h>#include <fcntl.h>#include <errno.h>#include <memory.h>#include <sys/types.h>#ifndef WIN32_ENV#include <sys/socket.h>#include <netdb.h>#include <unistd.h>#include <sys/uio.h>#include <netinet/in.h>#include <arpa/inet.h>#else#include <WINSOCK.H>#include <io.h>#endif/* AIX defines this but NCR does not */#ifndef INADDR_NONE#define INADDR_NONE 0xffffffff /* -1 return */#endifchar *normalizetid(char *tid);char *normalizeTCPtid(char *tid);static long getipaddr(char *pc);u_long GetHostAddress(char *pszHostName);u_long GetLocalHostAddress();int connectToSIMdrv(char *host, char *service, int port){ int sockfd; unsigned long inaddr;#ifdef NO_LINGER struct linger slinger;#endif struct sockaddr_in server_addr; struct servent *sp; struct hostent *hp; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons((u_short) port); if (service != NULL) { if ((sp = getservbyname(service, "tcp")) == NULL) return -1; if (port <= 0) server_addr.sin_port = sp->s_port; } else if (port <= 0) return -1; /* converts a IP addres to a long */ if ((inaddr = inet_addr(host)) != INADDR_NONE) memcpy(&server_addr.sin_addr, &inaddr, sizeof(inaddr)); /* if failed then must be a host name */ else { if ((hp = gethostbyname(host)) == NULL) return -1; memcpy(&server_addr.sin_addr, hp->h_addr, (size_t) hp->h_length); } if (port < 0 || (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* set local IP address to be reused so can do connects * one after the other */#ifdef NO_LINGER slinger.l_onoff = 1; slinger.l_linger = 0; setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char *) &slinger, sizeof(slinger));#endif /* Make the socket non-blocking so other processing can continue */#ifndef WIN32_ENV fcntl(sockfd, F_SETFL, O_NONBLOCK);#endif /* connect the socket */ if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {#ifndef WIN32_ENV if (errno == EINPROGRESS) { return sockfd; }#endif#ifdef WIN32_ENV closesocket(sockfd);#else close(sockfd);#endif return -1; } return sockfd;}int startSIMsrv(char *service, int port){ int sockfd; struct sockaddr_in server_addr; struct servent *sp;#ifndef WIN32_ENV int flag;#endif memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; /* server_addr.sin_addr.s_addr = GetLocalHostAddress(); */ server_addr.sin_addr.s_addr = htons(INADDR_ANY); /* will accept connect from any IP */ server_addr.sin_port = htons((u_short) port); /* if there is a service specified try to find it */ if (service != NULL) { if ((sp = getservbyname(service, "tcp")) == NULL) { if (port == 0) { return -1; } else { /* could not find service and port != 0 use port */ } } else { /* use port from service call */ server_addr.sin_port = sp->s_port; } } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* now set local IP address to be reused so can do connects * one after the other */#ifndef WIN32_ENV flag = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &flag, sizeof(flag)); /* Make the socket non-blocking so other processing can continue */ fcntl(sockfd, F_SETFL, O_NONBLOCK);#endif /* bind our port to the TCP i.e. idenify what our port number is. If it * is not unique then this will fail. */ if (bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {#ifdef WIN32_ENV closesocket(sockfd);#else close(sockfd);#endif return -1; } listen(sockfd, 5); return sockfd;}int readSocket(int sock_fd, char *data, int nbytes){/* int nleft, nread; nleft = nbytes; while (nleft > 0) { nread = read(sock_fd, data, nleft); if (nread < 0) return nread; else if (nread == 0) break; nleft -= nread; data += nread; } return (nbytes - nleft); */#ifdef WIN32_ENV return recv(sock_fd, data, nbytes, 0);#else return read(sock_fd, data, nbytes);#endif}int writeSocket(int sock_fd, char *data, int nbytes){/* int nleft, nwrite; nleft = nbytes; while (nleft > 0) { nwrite = write(sock_fd, data, nleft); if (nwrite < 0) return nwrite; else if (nwrite == 0) break; nleft -= nwrite; data += nwrite; } return (nbytes - nleft); */#ifdef WIN32_ENV return send(sock_fd, data, nbytes, 0);#else return write(sock_fd, data, nbytes);#endif}static long getipaddr(char *pc){ struct hostent *psh; unsigned char *pa; unsigned long lt; psh = gethostbyname(pc); if (psh == NULL) return ntohl(inet_addr(pc)); if (psh->h_addrtype != AF_INET) return -1l; /* not an internet name */ pa = (unsigned char *) psh->h_addr; lt = (unsigned long) *pa++ << 24; lt += (unsigned long) *pa++ << 16; lt += (unsigned long) *pa++ << 8; lt += (unsigned long) *pa; return (long) lt;}char *normalizeTCP(char *host, char *service){ static char result[25]; long li; unsigned port; struct servent *sp; li = getipaddr(host); if (li == (-1l)) return ""; if (service != NULL) { if ((sp = getservbyname(service, "tcp")) != NULL) { port = ntohs(sp->s_port); } else { if (!(isdigit(*service))) return ""; port = atoi(service); } } sprintf(result, "%d.%d.%d.%d,%u", (li >> 24) & 0xff, (li >> 16) & 0xff, (li >> 8) & 0xff, li & 0xff, port); return result;}char *normalizeTCPtid(char *tid){ char tmptid[90]; char *host, *service; strncpy(tmptid, tid, sizeof(tmptid)); host = tmptid; for (service = tmptid; *service; service++) { if (*service == ',') { *service++ = '\000'; break; } } return normalizeTCP(host, service);}char *normalizetid(char *tid){ char tmptid[90]; char *ptid; strncpy(tmptid, tid, sizeof(tmptid)); switch (tid[0]) { case 'S': case 's': ptid = normalizeTCPtid(&tid[2]); if (ptid) sprintf(tid, "S,%s", ptid); else tid = NULL; break; case 'M': /* future modem communications */ case 'm': tid = NULL; break; case 'X': /* future X.25 communications */ case 'x': tid = NULL; break; default: /* assume a TCP/IP address is passed in with no S, */ ptid = normalizeTCPtid(tid); if (ptid) sprintf(tid, "S,%s", ptid); else tid = NULL; break; } return (tid);}u_long GetHostAddress(char *pszHostName){ struct hostent *pHostAddr; /* * get local host address */ pHostAddr = gethostbyname(pszHostName); if (pHostAddr == NULL) { return (0); } else return *((u_long *) pHostAddr->h_addr);}u_long GetLocalHostAddress(){ char szHostName[64]; u_long ulHostAddr; if (gethostname(szHostName, sizeof(szHostName)) != -1) { ulHostAddr = GetHostAddress(szHostName); } else { return (0); } return (ulHostAddr);}/*********************************************************Function Name: char *fGetLocalHostIP()Narrative: This function get local host IP addressParam: noneReturn: if succeed, return IP Address else return NULL**********************************************************/char *fGetLocalHostIP(){ struct hostent *hp; char ServerName[64+1]; struct in_addr InAddr; if (gethostname(ServerName, 64)<0) { return NULL; } fLogInfo("ServerName=%s\n",ServerName); hp = (struct hostent *)gethostbyname(ServerName); if (hp == 0) { fLogInfo("hp=NULL\n"); return NULL; } memcpy((void *)&InAddr,hp->h_addr,hp->h_length); return ((char *)inet_ntoa(InAddr));}int fKeyDecrypt(char *Input, char *Output, int len){ static char ENCRYPTCODE[]={0x7a,0x1f,0x3e,0x8b,0xa1,0x69,0xf6,0xc3,0x89,0x5d,0x80,0x2f,0x6c,0xb2,0xd2,0x3c,0x11,0x20,0x00}; int i,j,k; int len0,len1; unsigned char TempStr[256]; int ch; *Output = '\0'; if (len <= 0) return 1; /* if input is null, then we do nothing */ len0 = strlen(ENCRYPTCODE); if ((len % 2) != 0) return 0; /* filter */ i=j=0; while (i<len) { if ( !((Input[i]>='0' && Input[i]<='?')) ) return 0; /* Invalid String, Fail! */ if ( !((Input[i+1]>='0' && Input[i+1]<='?')) ) return 0; /* Invalid String, Fail! */ TempStr[j] = Input[i] - '0' + ((Input[i+1] - '0')<<4); i += 2; ++j; } len = j; /* reverse the TempStr */ len1 = len/2; for (i=0; i<len1; ++i) { ch = TempStr[i]; TempStr[i] = TempStr[len-i-1]; TempStr[len-i-1] = ch; } for (i=0,j=0,k=0; i<len; ++i) { Output[k] = TempStr[i] ^ ENCRYPTCODE[j]; ++k; ++j; if (j >= len0) j=0; } Output[k] = '\0'; return 1;}int fGetKeyFromFile(char *FileName, char *Name, char *License, char *IPAddress1, char *IPAddress2){ char OutBuffer[10240+1]; char InBuffer[161]; short int StartPos; unsigned char *p,*q; char cc; int i; unsigned short int n,n1; FILE *Infp; Name[0]=0; License[0]=0; IPAddress1[0]=0; IPAddress2[0]=0; /* Decrypt file to OutBuffer */ Infp=fopen(FileName,"rb"); if (Infp==NULL) return 0; i=0; while (i<20480) { fread(InBuffer,1,160,Infp); fKeyDecrypt(InBuffer,&OutBuffer[i/2],160); i += 160; } fclose(Infp); /* check the CheckSum */ n=0; p=(unsigned char *)OutBuffer; i=0; while (i<10238) { n += p[i] + p[i+1]*256; i += 2; } n = ~ (n%65536); q = (unsigned char *)&OutBuffer[10238]; n1 = *q + (*(q+1))*256; /* if fail, then abort */ if (n1 != n) return 0; /* Get start position*/ q = (unsigned char *)&OutBuffer[0x112]; StartPos=*q + (*(q+1))*256; /* Get fields */ strncpy(Name,&OutBuffer[StartPos],60); Name[61]=0; strncpy(License,&OutBuffer[StartPos+60],60); License[61]=0; strncpy(IPAddress1,&OutBuffer[StartPos+60*2],60); IPAddress1[61]=0; strncpy(IPAddress2,&OutBuffer[StartPos+60*3],60); IPAddress2[61]=0; return 1;}extern char LIC_USERNAME[61];extern int LIC_DRIVERNUMBER;/*********************************************************Function Name: int fGetLicInfo()Narrative: This function get license informationParam: noneReturn: if succeed, return 1 else return 0**********************************************************/int fGetLicInfo(){ char *HostIP; char KeyFileName[]="simu112.lic"; char LicenseNumStr[61]; char IPStr1[61]; char IPStr2[61]; HostIP=fGetLocalHostIP(); if (!fGetKeyFromFile(KeyFileName, LIC_USERNAME, LicenseNumStr, IPStr1, IPStr2)) { printf("Can not get license information, system abort!\n"); fLogInfo("Can not get license information, system abort!\n"); return (0); } LIC_DRIVERNUMBER=atoi(LicenseNumStr); if (LIC_DRIVERNUMBER<=0) { printf("Invalid Maximum drivers, system abort!\n"); fLogInfo("Invalid Maximum drivers, system abort!\n"); return (0); } if (strcmp(HostIP,IPStr1)!=0 && strcmp(HostIP,IPStr2)!=0) { printf("Invalid IP address, system abort!\n"); fLogInfo("Invalid IP address, system abort!\n"); fLogInfo("HostIP=%s,LicIP1=%s,LicIP2=%s!\n",HostIP,IPStr1,IPStr2); return (0); } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -