123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /******************************************************************
- ** Copyright (c) 2007-2008 Wuhan Mozit Technology Co., Ltd .
- ** FileName: sdk.c
- ** Author: BigHead
- ** Mail: jsrenyw@sohu.com
- ** Date : 2007-5-31 下午07:17:32
- ** Version: 2007-5-31
- ** File Description:道具系统的SDK 代码
- //2010-11-24 bighead 增加对老socket索引 扔弃的处理
- socketIndex[oldPlayer->tcp_sock]=0;
- ******************************************************************/
- #include <iconv.h>
- #include <string.h>
- #include "../include/init.h"
- /******************************************************************
- * Function Name: getIPlist
- * Arguments: int[], int*, char*
- * Return Value: int
- * Date: 2007-6-2
- * Description: 读取白名单配置,如果未设置白名单,返回0;反之返回读取的条数
- * ip 转换为 int 存储,方便比较
- * 注意 数值最大长度为 MAX_IPLIST_NUM = 256
- ******************************************************************/
- /****
- int getIPlist(int IPlist[MAX_IPLIST_NUM],
- int *num,
- char *ipFile)
- {
- int ret = 0;
- ((void)(IPlist));
- ((void)(ipFile));
- *num = 0;
- return (ret);
- }
- ***/
- /******************************************************************
- * Function Name: checkIP
- * Arguments: int, int[], int
- * Return Value: int
- * Date: 2007-6-2
- * Description: 检查ip是否在白名单中,成功返回 True=1,失败返回False=0
- * 如果 _IPnum = 0,返回 True
- ******************************************************************/
- /***
- int checkIP(int _ip,
- int _IPlist[MAX_IPLIST_NUM],
- int _IPnum)
- {
- int ret = 0;
- int i;
- for (i = 0; i < _IPnum; ++i)
- {
- if (_ip == _IPlist[i])
- {
- ret = 1;
- break;
- } // if (_ip == _IPlist[i])
- } //for (i = 0; i < _IPnum; ++i)
- //如果白名单为空,默认允许所有连接请求 2007-6-2 下午02:58:24
- ret = _IPnum ? ret : 1;
- return (ret);
- }
- *****/
- /******************************************************************
- * Function Name: initRecordLogin
- * Arguments: FILE *logfile,char *fileDir
- * Return Value: return_type
- * Date: 2007-3-10
- * Description: 打开文件,准备保存日志
- ******************************************************************/
- int initRecordLogin(FILE **logfile, char *fileDir)
- {
- time_t timep;
- struct tm *p = NULL;
- char fileName[1024];
- *logfile = NULL;
- timep = time(NULL);
- p = localtime(&timep);
- //bzero(fileName,sizeof(fileName));
- memset(fileName, 0, sizeof(fileName));
- sprintf(fileName, "%s/loginInfo.%04d%02d%02d.log",
- fileDir,
- p->tm_year + 1900,
- p->tm_mon + 1,
- p->tm_mday);
- *logfile = fopen(fileName, "a");
- if (*logfile)
- {
- //fprintf(*logfile,"登录时间\t登录用户名\t登录金币\t登录金条\t登录积分\t登录IP\n");
- printf("初始化登录日志文件成功:%s\n", fileName);
- } // if (logfile)
- return (0);
- }
- #define MAXINTERFACES 16
- /******************************************************************
- * Function Name: getInternetIP
- * Arguments: int, void*, int, int
- * Return Value: int
- * Date: 2006-8-17
- * Description: 获取本机外网IP,eth0 = 0 eth1 =1
- ******************************************************************/
- int getInternetIP(char *localIP, int ethIndex)
- {
- strcpy(localIP, "127.0.0.1");
- ethIndex = 1;
- /***
- register int fd, intrface;
- struct ifreq buf[MAXINTERFACES];
- struct ifconf ifc;
- if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- {
- return -1;
- }
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = (caddr_t)buf;
- if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0)
- {
- return -1;
- }
- intrface = ifc.ifc_len / sizeof(struct ifreq);
- printf("number of Network interface is: %d\n", intrface);
- while (intrface-- > 0)
- {
- printf("net device %s\n", buf[intrface].ifr_name);
- if ((ioctl(fd, SIOCGIFFLAGS, (char *)&buf[intrface])) < 0)
- {
- continue;
- }
- if (buf[intrface].ifr_flags & IFF_PROMISC)
- {
- puts("the interface is PROMISC");
- }
- else
- {
- if (buf[intrface].ifr_flags & IFF_UP)
- {
- puts("the interface status is UP");
- }
- else
- {
- if (buf[intrface].ifr_flags & IFF_RUNNING)
- puts("the interface status is RUNNING");
- }
- }
- if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
- {
- puts("IP address is:");
- puts((char *)inet_ntoa(((struct sockaddr_in *)(&buf[intrface].ifr_addr))->sin_addr));
- puts("");
- }
- else
- {
- char str[256];
- sprintf(str, "cpm: ioctl device %s", buf[intrface].ifr_name);
- perror(str);
- }
- }
- close(fd);
- //eth0
- strcpy(localIP, (char *)inet_ntoa(((struct sockaddr_in *)(&buf[ethIndex + 1].ifr_addr))->sin_addr));
- printf("Local IP:%s\n\n", localIP);
- ***/
- return 0;
- }
- /*
- * char* GetInitKey(FileName, Section, Key)
- * Return Key=>Value
- * Ex:
- *
- * config.ini
- * [config]
- * dbhost=localhost
- *
- * Usage:
- * char dbhost[20];
- * strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));
- *
- * History:
- * Chen QunShan@20061010 (chqs_at_sina.com)
- * Handle comment line, duplicate section,
- * similar key, partly same key, key string existing
- * in value, trim left space char
- * and the last char is EOF.
- */
- /******************************************************************
- * Function Name: GetInitKey
- * Arguments: char*, char*, char*
- * Return Value: char*
- * Date: 2007-1-17
- * Description: 读取配置文件,支持单独一行写注释 以"#"开头
- ******************************************************************/
- char *GetInitKey(char *filename, char *title, char *key)
- {
- FILE *fp;
- char tmpLine[1024];
- int rtnval;
- int i = 0;
- int flag = 0;
- char *tmp;
- static char tmpstr[1024];
- if ((fp = fopen(filename, "r")) == NULL)
- {
- printf("出错: 严重错误 *** FILE NO EXIST: %s ***\n", filename);
- return "have no such file";
- }
- while (!feof(fp))
- {
- rtnval = fgetc(fp);
- if (rtnval == EOF)
- {
- tmpLine[i++] = 0;
- }
- else
- {
- tmpLine[i++] = rtnval;
- }
- if (rtnval == '\n' || rtnval == EOF)
- {
- tmpLine[--i] = 0;
- i = 0;
- if (tmpLine[0] == '\0' || strchr("#;", tmpLine[0]) != NULL)
- continue; // Skip null line or comment line
- if (tmpLine[0] == '[')
- {
- tmp = NULL;
- if (flag == 1)
- {
- // Meet next section
- #ifdef PROCESS_DUP_SECTION // INI file exist duplicate section
- flag = 0;
- #else
- break; // Skip other context
- #endif
- }
- }
- else
- {
- tmp = strchr(tmpLine, '=');
- }
- if ((tmp != NULL) && (flag == 1))
- {
- char *p;
- *tmp = '\0'; // erase '=', spilte Key and Value
- p = strstr(tmpLine, key);
- if (p != NULL)
- {
- if (p > tmpLine && strchr(" \t", *(p - 1)) == NULL)
- continue; // exist prefix, mishit key
- p += strlen(key);
- if (*p == '\0' || strchr(" \t", *p) != NULL)
- {
- tmp++; // Skip '='
- while (*tmp == ' ' || *tmp == '\t')
- tmp++; // Skip left lead ' ' or '\t'
- strcpy(tmpstr, tmp);
- fclose(fp);
- printf("[%s]->%s.value=%s\n", title, key, tmpstr);
- return tmpstr;
- }
- }
- }
- else
- {
- strcpy(tmpstr, "[");
- strcat(tmpstr, title);
- strcat(tmpstr, "]");
- if (strcmp(tmpstr, tmpLine) == 0)
- {
- flag = 1;
- }
- }
- }
- if (rtnval == EOF)
- {
- break;
- }
- } // end of while
- fclose(fp);
- return "";
- }
- /*--------------*
- * Test Case: *
- *--------------*
- config.ini
- ;[test]
- #p2= 789
- ;p2= 9
- p2 = 456
- [test]
- p3 = p2
- pp2= 45
- p2 = "456"
- [ip]
- portexam=777
- [user]
- port=666
- *--------------*/
- //gbk->utf-8
- int code_convert(char *inbuf, int inlen, char *outbuf, int outlen)
- {
- iconv_t cd;
- //int rc;
- char **pin = &inbuf;
- char **pout = &outbuf;
- cd = iconv_open("UTF-8", "GB2312");
- if (cd == 0)
- return -1;
- memset(outbuf, 0, outlen);
- if ((int)iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen) == -1)
- return -1;
- iconv_close(cd);
- return 0;
- }
|