functions.c

Ir para a documentação deste ficheiro.
00001 
00010 #include "functions.h"
00011 
00026 void trim_crlf(char *line)
00027   {
00028   char *aux = line;
00029   while(*aux != '\r' && *aux != '\n')
00030     aux++;
00031   *aux = '\0';
00032   } 
00033 
00046 int write_to_socket(int fd, char *mesg, char *error_mesg)
00047   {
00048   if(write(fd, mesg, strlen(mesg) + 1) < 0)
00049         {
00050     DEBUG(error_mesg);
00051     close(fd);
00052     return ERROR_VAL;
00053         }
00054   return ALL_GOES_WELL;
00055   }
00056 
00067 int readline (int fd, char *linha, int max)
00068   {
00069   int index = 0;
00070   char ch;
00071   memset (linha, 0, max);
00072   do
00073     {
00074     switch (read (fd, linha + index, sizeof (char)))
00075       {
00076           case 0:
00077             return index;
00078           case -1:
00079             switch (errno)
00080               {
00081               case EINTR:
00082                 return index;
00083               case EWOULDBLOCK:
00084                     continue;
00085               default:
00086                     return index;
00087               }
00088       }
00089     ch = linha[index++];
00090     }
00091   while (ch != '\n' && index < max - 1);
00092   return index;
00093   }
00094 
00104 int validate_ip(char *ip)
00105   {
00106   struct in_addr temp;
00107   int ip_size = strlen(ip);
00108   
00109   #ifdef SHOW_DEBUG
00110     DEBUG("A tentar validar o IP: %s", ip);
00111   #endif
00112   
00113   if(ip_size < MIN_IP_LEN || ip_size > MAX_IP_LEN)
00114     return ERROR_VAL;
00115   
00116   if((ip[ip_size] == '0' && ip[ip_size - 1] == '.') || (ip[ip_size] >= '5' && ip[ip_size - 1] >= '5' && ip[ip_size -2] >= '2'))
00117         return ERROR_VAL;
00118  
00119   if(!(inet_pton(AF_INET, ip, &temp)))
00120     return ERROR_VAL;
00121   
00122   return ALL_GOES_WELL;
00123   }
00124 
00130 int open_socket(uint16_t porto, PROCESSOR processa_pedido, void *args_processor, int *stoping_condition)
00131   {
00132   int server_fd, client_fd;
00133   socklen_t client_len;
00134   struct sockaddr_in server_addr, client_addr;
00135   int option = 1;
00136   
00137   /* Criar o Socket */
00138   if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
00139     return ERROR_VAL;
00140   /* Preencher estrutura */
00141   memset(&server_addr, 0, sizeof(server_addr));
00142   server_addr.sin_family = AF_INET;
00143   server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
00144   server_addr.sin_port = htons(porto);
00145     
00146   if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(int)) == -1)
00147     return ERROR_VAL;
00148   
00149   if (bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
00150     return ERROR_VAL;
00151   if (listen(server_fd, 5) < 0)
00152     return ERROR_VAL;
00153   client_len = sizeof(struct sockaddr);
00154   
00155   /* Ciclo para atender clientes */
00156   while (*stoping_condition)
00157     {
00158     /* Aceitar ligacoes */
00159     client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_len);
00160         if (client_fd < 0)
00161           switch(errno)
00162             {
00163             case EINTR:
00164             case ECONNABORTED:
00165               continue;
00166             default:
00167               return ERROR_VAL;
00168             }
00169             
00170         #ifdef SHOW_DEBUG
00171           DEBUG("cliente [%s@%d]", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));
00172         #endif
00173         
00174     processa_pedido(client_fd, args_processor, strdup(inet_ntoa(client_addr.sin_addr)));
00175     
00176     #ifdef SHOW_DEBUG
00177       DEBUG("Pedido do cliente processado");
00178     #endif
00179     }
00180   return ALL_GOES_WELL;
00181   }
00182 
00198 int parse_args(int argc, char *argv[], char **c_file, char **m_file, int *porto, int *tempo)
00199   {
00200   t_gen_args args;
00201   if(cmdline_parser (argc, argv, &args) != 0)
00202         return ERROR_VAL;
00203   *c_file = strdup(args.contas_arg);
00204   *m_file = strdup(args.maquinas_arg);
00205   *porto = args.porto_arg;
00206   *tempo = args.tempo_arg;
00207   return ALL_GOES_WELL;
00208   }
00209 
00222 void print_error_exit(char *user_mesg, char *error_mesg, int exit_code)
00223   {
00224   printf("%s", user_mesg);
00225   fflush(stdout);
00226   ERROR(exit_code, "%s.", error_mesg);
00227   }
00228 
00240 int validate_path(char *path)
00241   {
00242   int z = 0, size, value = -1;
00243   char *test;
00244   
00245   if(path == NULL)
00246     return 0;
00247   
00248   #ifdef SHOW_DEBUG
00249     DEBUG("Caminho a validar: ^%s^", path);
00250   #endif
00251   
00252   test = strdup(path);
00253   size = strlen(path); 
00254   for(;z < size; z++)
00255     if(test[z] == '/')
00256       {
00257       if(test[z + 1] == '/')
00258         return -1;
00259       test[z] = VALID_CHAR;
00260       }
00261   value = strpbrk(test, INVALID_CHARS) == NULL ? 0 : -1;
00262   free(test);
00263   return value;
00264   }

Gerado em Fri Nov 25 18:42:04 2005 para Securitas por  doxygen 1.4.4