init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /******************************************************************
  2. ** Copyright (c) 2007-2008 Wuhan Mozit Technology Co., Ltd .
  3. ** FileName: sdk.c
  4. ** Author: BigHead
  5. ** Mail: jsrenyw@sohu.com
  6. ** Date : 2007-5-31 下午07:17:32
  7. ** Version: 2007-5-31
  8. ** File Description:道具系统的SDK 代码
  9. //2010-11-24 bighead 增加对老socket索引 扔弃的处理
  10. socketIndex[oldPlayer->tcp_sock]=0;
  11. ******************************************************************/
  12. #include <iconv.h>
  13. #include <string.h>
  14. #include "../include/init.h"
  15. /******************************************************************
  16. * Function Name: getIPlist
  17. * Arguments: int[], int*, char*
  18. * Return Value: int
  19. * Date: 2007-6-2
  20. * Description: 读取白名单配置,如果未设置白名单,返回0;反之返回读取的条数
  21. * ip 转换为 int 存储,方便比较
  22. * 注意 数值最大长度为 MAX_IPLIST_NUM = 256
  23. ******************************************************************/
  24. /****
  25. int getIPlist(int IPlist[MAX_IPLIST_NUM],
  26. int *num,
  27. char *ipFile)
  28. {
  29. int ret = 0;
  30. ((void)(IPlist));
  31. ((void)(ipFile));
  32. *num = 0;
  33. return (ret);
  34. }
  35. ***/
  36. /******************************************************************
  37. * Function Name: checkIP
  38. * Arguments: int, int[], int
  39. * Return Value: int
  40. * Date: 2007-6-2
  41. * Description: 检查ip是否在白名单中,成功返回 True=1,失败返回False=0
  42. * 如果 _IPnum = 0,返回 True
  43. ******************************************************************/
  44. /***
  45. int checkIP(int _ip,
  46. int _IPlist[MAX_IPLIST_NUM],
  47. int _IPnum)
  48. {
  49. int ret = 0;
  50. int i;
  51. for (i = 0; i < _IPnum; ++i)
  52. {
  53. if (_ip == _IPlist[i])
  54. {
  55. ret = 1;
  56. break;
  57. } // if (_ip == _IPlist[i])
  58. } //for (i = 0; i < _IPnum; ++i)
  59. //如果白名单为空,默认允许所有连接请求 2007-6-2 下午02:58:24
  60. ret = _IPnum ? ret : 1;
  61. return (ret);
  62. }
  63. *****/
  64. /******************************************************************
  65. * Function Name: initRecordLogin
  66. * Arguments: FILE *logfile,char *fileDir
  67. * Return Value: return_type
  68. * Date: 2007-3-10
  69. * Description: 打开文件,准备保存日志
  70. ******************************************************************/
  71. int initRecordLogin(FILE **logfile, char *fileDir)
  72. {
  73. time_t timep;
  74. struct tm *p = NULL;
  75. char fileName[1024];
  76. *logfile = NULL;
  77. timep = time(NULL);
  78. p = localtime(&timep);
  79. //bzero(fileName,sizeof(fileName));
  80. memset(fileName, 0, sizeof(fileName));
  81. sprintf(fileName, "%s/loginInfo.%04d%02d%02d.log",
  82. fileDir,
  83. p->tm_year + 1900,
  84. p->tm_mon + 1,
  85. p->tm_mday);
  86. *logfile = fopen(fileName, "a");
  87. if (*logfile)
  88. {
  89. //fprintf(*logfile,"登录时间\t登录用户名\t登录金币\t登录金条\t登录积分\t登录IP\n");
  90. printf("初始化登录日志文件成功:%s\n", fileName);
  91. } // if (logfile)
  92. return (0);
  93. }
  94. #define MAXINTERFACES 16
  95. /******************************************************************
  96. * Function Name: getInternetIP
  97. * Arguments: int, void*, int, int
  98. * Return Value: int
  99. * Date: 2006-8-17
  100. * Description: 获取本机外网IP,eth0 = 0 eth1 =1
  101. ******************************************************************/
  102. int getInternetIP(char *localIP, int ethIndex)
  103. {
  104. strcpy(localIP, "127.0.0.1");
  105. ethIndex = 1;
  106. /***
  107. register int fd, intrface;
  108. struct ifreq buf[MAXINTERFACES];
  109. struct ifconf ifc;
  110. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  111. {
  112. return -1;
  113. }
  114. ifc.ifc_len = sizeof(buf);
  115. ifc.ifc_buf = (caddr_t)buf;
  116. if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0)
  117. {
  118. return -1;
  119. }
  120. intrface = ifc.ifc_len / sizeof(struct ifreq);
  121. printf("number of Network interface is: %d\n", intrface);
  122. while (intrface-- > 0)
  123. {
  124. printf("net device %s\n", buf[intrface].ifr_name);
  125. if ((ioctl(fd, SIOCGIFFLAGS, (char *)&buf[intrface])) < 0)
  126. {
  127. continue;
  128. }
  129. if (buf[intrface].ifr_flags & IFF_PROMISC)
  130. {
  131. puts("the interface is PROMISC");
  132. }
  133. else
  134. {
  135. if (buf[intrface].ifr_flags & IFF_UP)
  136. {
  137. puts("the interface status is UP");
  138. }
  139. else
  140. {
  141. if (buf[intrface].ifr_flags & IFF_RUNNING)
  142. puts("the interface status is RUNNING");
  143. }
  144. }
  145. if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
  146. {
  147. puts("IP address is:");
  148. puts((char *)inet_ntoa(((struct sockaddr_in *)(&buf[intrface].ifr_addr))->sin_addr));
  149. puts("");
  150. }
  151. else
  152. {
  153. char str[256];
  154. sprintf(str, "cpm: ioctl device %s", buf[intrface].ifr_name);
  155. perror(str);
  156. }
  157. }
  158. close(fd);
  159. //eth0
  160. strcpy(localIP, (char *)inet_ntoa(((struct sockaddr_in *)(&buf[ethIndex + 1].ifr_addr))->sin_addr));
  161. printf("Local IP:%s\n\n", localIP);
  162. ***/
  163. return 0;
  164. }
  165. /*
  166. * char* GetInitKey(FileName, Section, Key)
  167. * Return Key=>Value
  168. * Ex:
  169. *
  170. * config.ini
  171. * [config]
  172. * dbhost=localhost
  173. *
  174. * Usage:
  175. * char dbhost[20];
  176. * strcpy(dbhost,GetInitKey("config.ini", "config", "dbhost"));
  177. *
  178. * History:
  179. * Chen QunShan@20061010 (chqs_at_sina.com)
  180. * Handle comment line, duplicate section,
  181. * similar key, partly same key, key string existing
  182. * in value, trim left space char
  183. * and the last char is EOF.
  184. */
  185. /******************************************************************
  186. * Function Name: GetInitKey
  187. * Arguments: char*, char*, char*
  188. * Return Value: char*
  189. * Date: 2007-1-17
  190. * Description: 读取配置文件,支持单独一行写注释 以"#"开头
  191. ******************************************************************/
  192. char *GetInitKey(char *filename, char *title, char *key)
  193. {
  194. FILE *fp;
  195. char tmpLine[1024];
  196. int rtnval;
  197. int i = 0;
  198. int flag = 0;
  199. char *tmp;
  200. static char tmpstr[1024];
  201. if ((fp = fopen(filename, "r")) == NULL)
  202. {
  203. printf("出错: 严重错误 *** FILE NO EXIST: %s ***\n", filename);
  204. return "have no such file";
  205. }
  206. while (!feof(fp))
  207. {
  208. rtnval = fgetc(fp);
  209. if (rtnval == EOF)
  210. {
  211. tmpLine[i++] = 0;
  212. }
  213. else
  214. {
  215. tmpLine[i++] = rtnval;
  216. }
  217. if (rtnval == '\n' || rtnval == EOF)
  218. {
  219. tmpLine[--i] = 0;
  220. i = 0;
  221. if (tmpLine[0] == '\0' || strchr("#;", tmpLine[0]) != NULL)
  222. continue; // Skip null line or comment line
  223. if (tmpLine[0] == '[')
  224. {
  225. tmp = NULL;
  226. if (flag == 1)
  227. {
  228. // Meet next section
  229. #ifdef PROCESS_DUP_SECTION // INI file exist duplicate section
  230. flag = 0;
  231. #else
  232. break; // Skip other context
  233. #endif
  234. }
  235. }
  236. else
  237. {
  238. tmp = strchr(tmpLine, '=');
  239. }
  240. if ((tmp != NULL) && (flag == 1))
  241. {
  242. char *p;
  243. *tmp = '\0'; // erase '=', spilte Key and Value
  244. p = strstr(tmpLine, key);
  245. if (p != NULL)
  246. {
  247. if (p > tmpLine && strchr(" \t", *(p - 1)) == NULL)
  248. continue; // exist prefix, mishit key
  249. p += strlen(key);
  250. if (*p == '\0' || strchr(" \t", *p) != NULL)
  251. {
  252. tmp++; // Skip '='
  253. while (*tmp == ' ' || *tmp == '\t')
  254. tmp++; // Skip left lead ' ' or '\t'
  255. strcpy(tmpstr, tmp);
  256. fclose(fp);
  257. printf("[%s]->%s.value=%s\n", title, key, tmpstr);
  258. return tmpstr;
  259. }
  260. }
  261. }
  262. else
  263. {
  264. strcpy(tmpstr, "[");
  265. strcat(tmpstr, title);
  266. strcat(tmpstr, "]");
  267. if (strcmp(tmpstr, tmpLine) == 0)
  268. {
  269. flag = 1;
  270. }
  271. }
  272. }
  273. if (rtnval == EOF)
  274. {
  275. break;
  276. }
  277. } // end of while
  278. fclose(fp);
  279. return "";
  280. }
  281. /*--------------*
  282. * Test Case: *
  283. *--------------*
  284. config.ini
  285. ;[test]
  286. #p2= 789
  287. ;p2= 9
  288. p2 = 456
  289. [test]
  290. p3 = p2
  291. pp2= 45
  292. p2 = "456"
  293. [ip]
  294. portexam=777
  295. [user]
  296. port=666
  297. *--------------*/
  298. //gbk->utf-8
  299. int code_convert(char *inbuf, int inlen, char *outbuf, int outlen)
  300. {
  301. iconv_t cd;
  302. //int rc;
  303. char **pin = &inbuf;
  304. char **pout = &outbuf;
  305. cd = iconv_open("UTF-8", "GB2312");
  306. if (cd == 0)
  307. return -1;
  308. memset(outbuf, 0, outlen);
  309. if ((int)iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen) == -1)
  310. return -1;
  311. iconv_close(cd);
  312. return 0;
  313. }