#include #include #include #include #include #include #include "../const.h" #include "../tools.h" #include "modules.h" int LoadAllModule (struct all_module *s_all_module, int nb_modules) { int i; char path[MAX_BUFFER]; char *erreur; for (i = 0; i < nb_modules; i++) { memset (path, 0, MAX_BUFFER); sprintf (path, "%s/%s.so", MODULES_PATH, s_all_module->s_module[i].name); printf ("Chargement du module <%s>... ", s_all_module->s_module[i].name); s_all_module->s_module[i].handler = dlopen (path, RTLD_LAZY); if (!s_all_module->s_module[i].handler) { fputs (dlerror (), stderr); return 1; } s_all_module->s_module[i].MainModule = dlsym (s_all_module->s_module[i].handler, "MainModule"); if ((erreur = dlerror ()) != NULL) return 1; s_all_module->s_module[i].NB_BIND = 0; s_all_module->NB_MODULES_LOAD = nb_modules; if (s_all_module->s_module[i].MainModule (&s_all_module->s_module[i]) == 0) printf ("[OK]\n"); else { printf ("[ERROR]\n"); return 1; } } return 0; } int BindModule (struct struct_module *s_module, void (*func) (struct struct_client * s_client, char *buf, int state), char *type, char *data) { s_module->s_bind_module[s_module->NB_BIND] = malloc (sizeof (struct bind_module)); if (s_module->s_bind_module[s_module->NB_BIND] == NULL) { perror ("malloc"); return 1; } if (strlen (type) < MAX_BUFFER) strcpy (s_module->s_bind_module[s_module->NB_BIND]->type, type); else { printf ("Erreur, 'type' trop long ! (%s)\n", s_module->name); s_module->NB_BIND++; return 1; } if (strlen (data) < MAX_BUFFER) strcpy (s_module->s_bind_module[s_module->NB_BIND]->data, data); else { printf ("Erreur, 'data' trop long ! (%s)\n", s_module->name); s_module->NB_BIND++; return 1; } s_module->s_bind_module[s_module->NB_BIND]->bind = func; s_module->NB_BIND++; return 0; } int SendBindAllModule (void *buf, struct struct_client *s_client, int state) { int a, i; int nb = 0; if (s_all_module.NB_MODULES_LOAD != 0) { for (i = 0; i < s_all_module.NB_MODULES_LOAD; i++) { for (a = 0; a < s_all_module.s_module[i].NB_BIND; a++) { if (strcmp (s_all_module.s_module[i].s_bind_module[a]->type, "Content-type") == 0) { if (strcmp (s_all_module.s_module[i].s_bind_module[a]->data, s_client->content_type) == 0) { s_all_module.s_module[i].s_bind_module[a]-> bind (s_client, buf, state); nb++; } } } } } return nb; } int CheckIfModuleBind(struct struct_client *s_client) { int mod_bind = 0; int i, a; if (s_all_module.NB_MODULES_LOAD != 0) { for (i = 0; i < s_all_module.NB_MODULES_LOAD; i++) { for (a = 0; a < s_all_module.s_module[i].NB_BIND; a++) { if (strcmp (s_all_module.s_module[i].s_bind_module[a]->type, "Content-type") == 0) if (strcmp (s_all_module.s_module[i].s_bind_module[a]->data, s_client->content_type) == 0) mod_bind++; } } } return mod_bind; } int SendClient (struct struct_client *s_client,unsigned char *buf, int size) { char envoi[MAX_BUFFER_LARGE]; int size_tmp = 0; int i, a; unsigned short control = 0; i = 0; while (1) { memset (envoi, 0, MAX_BUFFER_LARGE); if (size <= MAX_BUFFER_LARGE) { if(control == 0) { sprintf (envoi, "%x\r\n", size); send (s_client->sock, envoi, strlen (envoi), 0); send (s_client->sock, buf, size, 0); } else { sprintf (envoi, "%x\r\n%s", size, envoi); send (s_client->sock, envoi, strlen (envoi), 0); } printf("envoi !\n"); break; } else { sprintf (envoi, "%x\r\n", MAX_BUFFER_LARGE); size_tmp = strlen (envoi); for (a = size_tmp; a < (MAX_BUFFER_LARGE - size_tmp); a++) { envoi[a] = buf[i]; i++; } send (s_client->sock, envoi, strlen (envoi), 0); size = size - strlen(envoi); control = 1; printf("envoi !\n"); } } send (s_client->sock, "\r\n0\r\n\r\n", strlen ("\r\n0\r\n\r\n"), 0); return 0; }