fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define CSV_LINE_MAX 128 /** maximum charactors per line **/
  7. #define MAX_LINES 66 /** maximum lines of CSV file(1..66) **/
  8. #define COLUMUN_DATE 28
  9. #define COLUMUN_VERSION 55
  10. #define FILTER_RULE_FILE_NAME "/export/http/import/iPASOLINK-filter.csv"
  11.  
  12. const char ccMTYPE[] = "iPASOLINK UX";
  13. const char ccKIND[] = "Filtering Rule";
  14. const char ccLINE2[] = "Rule ID";
  15.  
  16. typedef struct { /** CSVファイルから読み込んだFilter Rule **/
  17. unsigned char ucExportTime[26];
  18. unsigned char ucFVersion[8];
  19. struct {
  20. int iRuleID;
  21. int iSourcePort;
  22. int iAction;
  23. int iFilterType;
  24. int iTagMask;
  25. int iVlanID;
  26. int iMacAddressMask;
  27. unsigned char cMacAddress[17]; /** xx:xx:xx:xx:xx:xx **/
  28. unsigned char dummy;
  29. } stFilterRules[MAX_LINES-2]; /** 64record maximum **/
  30. } FilterDataFmt;
  31.  
  32. FilterDataFmt FilterRule;
  33.  
  34. /**************************************************************************
  35. *
  36. * read from CSV file (Filter Rules defined)
  37. *
  38. * return: 0:OK,
  39. * -1:NG(no file, file format error)
  40. *
  41. **************************************************************************/
  42. int LoadFilterRuleFile( FilterDataFmt *Save_p )
  43. {
  44. char *cRet;
  45. int iRet,iLine;
  46. int i;
  47. FILE *File_h; /** file handle **/
  48. char ucBuff[CSV_LINE_MAX]; /** temporary data buffer from CSV file **/
  49. char *ary[8]; /** for after 3line CSV **/
  50. const int dTbl[] = { 2,5,8,11,14 }; /** ":"/"-"charactor offset within MAC address **/
  51. const int nTbl[] = { 0,3,6,9,12,15 }; /** Hex data of octet **/
  52.  
  53. File_h = fopen( FILTER_RULE_FILE_NAME, "r" ); /** Open CSV file **/
  54. if( File_h == NULL ) {
  55. /** file not found */
  56. return -1;
  57. }
  58.  
  59. iRet = 0;
  60. for( iLine = 0; iLine < MAX_LINES; iLine++ ) {
  61. cRet = fgets( ucBuff, CSV_LINE_MAX, File_h ); /** read 1 line from CSV file **/
  62. if(cRet == NULL) { /** EOF **/
  63. if( iLine < 3 ) {
  64. iRet = -1; /** CSV file format error **/
  65. }
  66. else {
  67. iRet = 0; /** read from CSV file complete **/
  68. }
  69. iLine = MAX_LINES; /** end of "for loop" **/
  70. }
  71. switch( iLine ) {
  72. case 0: /** 1st line **/
  73. /** check ',' charactor **/
  74. if(( strncmp( ucBuff, ccMTYPE, strlen(ccMTYPE) ) != 0 ) || ( ucBuff[12] != ',') ||
  75. ( strncmp( &ucBuff[13], ccKIND, strlen(ccKIND) ) != 0 ) || ( ucBuff[27] != ',' ) || ( ucBuff[54] != ',' )) {
  76. iLine = MAX_LINES;
  77. iRet = -1; /** NG - CSV file format error **/
  78. }
  79. else { /** format ok **/
  80. /** copy last export date & time **/
  81. memcpy( Save_p->ucExportTime, &ucBuff[COLUMUN_DATE], sizeof(Save_p->ucExportTime) );
  82. memcpy( Save_p->ucFVersion, &ucBuff[COLUMUN_VERSION], sizeof(Save_p->ucFVersion) );
  83. }
  84. break;
  85. case 1: /** 2nd line **/
  86. if( strncmp( ucBuff, ccLINE2, strlen(ccLINE2) ) != 0 ) {/** NG - format error **/
  87. iLine = MAX_LINES;
  88. iRet = -1;
  89. }
  90. break;
  91. case MAX_LINES: /** error or EOF **/
  92. break;
  93. default: /** from 3rd to last line **/
  94. ary[0] = strtok( ucBuff, "," ); /** Rule ID(「1~100」であること) **/
  95. Save_p->stFilterRules[iLine].iRuleID = atoi( ary[0] );
  96. if(( Save_p->stFilterRules[iLine].iRuleID < 1 ) || ( Save_p->stFilterRules[iLine].iRuleID > 100 )) {
  97. iLine = MAX_LINES;
  98. iRet = -1;
  99. break;
  100. }
  101. ary[1] = strtok( NULL, "," ); /** Source Port(「1~4」であること) **/
  102. Save_p->stFilterRules[iLine].iSourcePort = atoi( ary[1] );
  103. if(( Save_p->stFilterRules[iLine].iSourcePort < 1 ) || ( Save_p->stFilterRules[iLine].iSourcePort > 4 )) {
  104. iLine = MAX_LINES;
  105. iRet = -1;
  106. break;
  107. }
  108. ary[2] = strtok( NULL, "," ); /** Action(「1~2」であること) **/
  109. Save_p->stFilterRules[iLine].iAction = atoi( ary[2] );
  110. if(( Save_p->stFilterRules[iLine].iAction < 1 ) || ( Save_p->stFilterRules[iLine].iAction > 2 )) {
  111. iLine = MAX_LINES;
  112. iRet = -1;
  113. break;
  114. }
  115. ary[3] = strtok( NULL, "," ); /** Filter Type(「1」であること) **/
  116. Save_p->stFilterRules[iLine].iFilterType = atoi( ary[3] );
  117. if( Save_p->stFilterRules[iLine].iFilterType != 1 ) {
  118. iLine = MAX_LINES;
  119. iRet = -1;
  120. break;
  121. }
  122. ary[4] = strtok( NULL, "," ); /** VLAN Tag Mask(「1~3」であること) **/
  123. Save_p->stFilterRules[iLine].iTagMask = atoi( ary[4] );
  124. if(( Save_p->stFilterRules[iLine].iTagMask < 1 ) || ( Save_p->stFilterRules[iLine].iTagMask > 3 )) {
  125. iLine = MAX_LINES;
  126. iRet = -1;
  127. break;
  128. }
  129. ary[5] = strtok( NULL, "," ); /** VLAN ID(「1~4094」であること) **/
  130. Save_p->stFilterRules[iLine].iVlanID = atoi( ary[5] );
  131. if(( Save_p->stFilterRules[iLine].iVlanID < 1 ) || ( Save_p->stFilterRules[iLine].iVlanID > 4094 )) {
  132. iLine = MAX_LINES;
  133. iRet = -1;
  134. break;
  135. }
  136. ary[6] = strtok( NULL, "," ); /** Source MAC Address Mask(「1~2」であること)**/
  137. Save_p->stFilterRules[iLine].iMacAddressMask = atoi( ary[6] );
  138. if(( Save_p->stFilterRules[iLine].iMacAddressMask < 1 ) || ( Save_p->stFilterRules[iLine].iMacAddressMask > 2 )) {
  139. iLine = MAX_LINES;
  140. iRet = -1;
  141. break;
  142. }
  143. ary[7] = strtok( NULL, "," ); /** Source MAC Address(MAC Address形式であること)**/
  144. strcpy( Save_p->stFilterRules[iLine].cMacAddress, ary[7] );
  145. for( i = 0; i < sizeof(dTbl); i++ ) {
  146. if(( isxdigit( (int)Save_p->stFilterRules[iLine].cMacAddress[nTbl[i]]) != 0 ) ||
  147. ( isxdigit( (int)Save_p->stFilterRules[iLine].cMacAddress[nTbl[i+1]]) != 0 )) {
  148. /** charactor is not hexdecimal digit **/
  149. iLine = MAX_LINES;
  150. iRet = -1;
  151. break;
  152. }
  153. if(( Save_p->stFilterRules[iLine].cMacAddress[dTbl[i]] != ":" ) &&
  154. ( Save_p->stFilterRules[iLine].cMacAddress[dTbl[i]] != "-" )) {
  155. /** delimitter is not MAC address format **/
  156. iLine = MAX_LINES;
  157. iRet = -1;
  158. break;
  159. }
  160. }
  161. break;
  162. } /** end of switch **/
  163. }/** enf of for loop **/
  164.  
  165. fclose( File_h );
  166. return iRet;
  167.  
  168. }
  169.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘LoadFilterRuleFile’:
prog.c:144:13: warning: pointer targets in passing argument 1 of ‘strcpy’ differ in signedness [-Wpointer-sign]
     strcpy( Save_p->stFilterRules[iLine].cMacAddress, ary[7] );
             ^~~~~~
In file included from prog.c:3:0:
/usr/include/string.h:125:14: note: expected ‘char * restrict’ but argument is of type ‘unsigned char *’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
prog.c:153:61: warning: comparison between pointer and integer
      if(( Save_p->stFilterRules[iLine].cMacAddress[dTbl[i]] != ":" ) &&
                                                             ^~
prog.c:153:61: warning: comparison with string literal results in unspecified behavior [-Waddress]
prog.c:154:61: warning: comparison between pointer and integer
         ( Save_p->stFilterRules[iLine].cMacAddress[dTbl[i]] != "-" )) {
                                                             ^~
prog.c:154:61: warning: comparison with string literal results in unspecified behavior [-Waddress]
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty