/* applets */ static int _auth(void) { int ret = 0; const char path[] = "../src/auth"; #ifdef __APPLE__ const char ext[] = ".dylib"; #else const char ext[] = ".so"; #endif DIR * dir; struct dirent * de; size_t len; char * s; void * p; LockerAuthDefinition * lad; if((dir = opendir(path)) == NULL) return -_perror(path, 1); while((de = readdir(dir)) != NULL) { if((len = strlen(de->d_name)) < sizeof(ext)) continue; if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0) continue; if((s = malloc(sizeof(path) + len + 1)) == NULL) { ret += _perror(de->d_name, 1); continue; } snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name); if((p = dlopen(s, RTLD_LAZY)) == NULL) { ret += _dlerror(s, 1); free(s); continue; } if((lad = dlsym(p, "plugin")) == NULL) ret += _dlerror(s, 1); else { if(lad->icon == NULL) ret += _error(s, "No icon defined", 1); else printf("\n%s: %s (%s)\n%s\n", de->d_name, lad->name, lad->icon, (lad->description != NULL) ? lad->description : "(no description)"); if(lad->get_widget == NULL) ret += _error(s, "No widget available", 1); if(lad->action == NULL) ret += _error(s, "No action handler", 1); } free(s); dlclose(p); } closedir(dir); return ret; }
/* plugins */ static int _plugins(void) { int ret = 0; const char path[] = "../src/plugins"; /* FIXME broken with OBJDIR */ #ifdef __APPLE__ const char ext[] = ".dylib"; #else const char ext[] = ".so"; #endif DIR * dir; struct dirent * de; size_t len; char * s; void * p; BrowserPluginDefinition * bpd; if((dir = opendir(path)) == NULL) return -_perror(path, 1); while((de = readdir(dir)) != NULL) { if((len = strlen(de->d_name)) < sizeof(ext)) continue; if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0) continue; if((s = malloc(sizeof(path) + len + 1)) == NULL) { ret += _perror(de->d_name, 1); continue; } snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name); if((p = dlopen(s, RTLD_LAZY)) == NULL) { ret += _dlerror(s, 1); free(s); continue; } if((bpd = dlsym(p, "plugin")) == NULL) ret += _dlerror(s, 1); else if(bpd->icon == NULL) ret += _error(s, "No icon defined", 1); else printf("\n%s: %s (%s)\n%s\n", de->d_name, bpd->name, bpd->icon, (bpd->description != NULL) ? bpd->description : "(no description)"); free(s); dlclose(p); } closedir(dir); return ret; }
void thread_create(thread_t* thread, TH_HDL (*handler)(void *), void* args, int detach) { #ifdef __MINGW32__ //TODO: Attach mode *thread = _beginthread(handler, 0, args); // Start in detach mode #else if(pthread_create(thread, NULL, handler, args) < 0) _perror("Cannot create thread"); if(detach) { if(pthread_detach(*thread) < 0) _perror("Cannot detach thread"); } #endif }
void setLogFile(string filename) { if (filename != "-") { logFile = NULL; #ifdef _WIN32 fopen_s(&logFile, filename.c_str(), "w+"); #else logFile = fopen(filename.c_str(), "w+"); #endif if (!logFile) { logFile = stdout; _perror("fopen"); log_warn("%s", "open log file error"); } } }
TH_HDL server_handler(void* serv) { server_t* server = (server_t*)serv; server->running = true; #ifdef __MINGW32__ if(!WSAinit) { WSADATA wsadata; if (WSAStartup(MAKEWORD(2,2), &wsadata) == SOCKET_ERROR) { _log(LVL_FATAL, "Error creating socket."); TH_RETURN; } WSAinit = true; } #endif server->socket = socket(AF_INET, SOCK_STREAM, 0); #ifndef __MINGW32__ int val = 1; setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, (void*)&val, sizeof(val)); #endif struct sockaddr_in addr; addr.sin_port=htons(server->port); addr.sin_family = AF_INET; addr.sin_addr.s_addr = server->bind_addr; if(bind(server->socket, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) { _perror("Error Bind"); TH_RETURN; } if(listen(server->socket, server->max_client) != 0) { _perror("Error listen"); TH_RETURN; } struct sockaddr_in addr_client; socklen_t socklen = sizeof(struct sockaddr_in); _log(LVL_INFO, "Accepting connections\n"); while(server->running) { int client_sock = accept(server->socket, (struct sockaddr*)&addr_client, &socklen); if(!server->running) break; else if(client_sock < 0) _perror("Error accept"); else { client_t *client = client_create(server, client_sock, inet_ntoa(addr.sin_addr), addr.sin_port); if(client == NULL) continue; _log(LVL_DEBUG, "New connection from %s:%d\n", inet_ntoa(addr_client.sin_addr),client->port); thread_create(&(client->thread), &client_handler, client, 1); } } //Wait clients end int i; for(i = 0; i < server->max_client; i++) { if(server->clients[i] != NULL) thread_join(&(server->clients[i]->thread)); } close(server->socket); TH_RETURN; }
static int listen_for_IP() { struct sockaddr_nl addr; int sock, len; const int RECV_SIZE = 512; char recv_buffer[RECV_SIZE]; if ((sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) { _perror("couldn't open NETLINK_ROUTE socket"); _assert(false); return 1; } memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = RTMGRP_IPV6_IFADDR; if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { _perror("couldn't bind"); assert(false); return 1; } send_ip_list_request(sock); loop: while ((len = recv(sock, recv_buffer, RECV_SIZE, 0)) > 0) { struct nlmsghdr* nlh = (struct nlmsghdr*) recv_buffer; while (NLMSG_OK(nlh, len) && (nlh->nlmsg_type != NLMSG_DONE)) { if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_GETADDR || nlh->nlmsg_type == RTM_DELADDR) { struct ifaddrmsg *ifa = (struct ifaddrmsg *) NLMSG_DATA(nlh); if(verbose_mode) { log_time(); log("Message: [%d] flags: ", nlh->nlmsg_type); fprint_flags(global_output, ifa->ifa_flags); log(" scope: %d\n", ifa->ifa_scope); } if (ifa->ifa_family == AF_INET6) { void *address = NULL; //The IP Address: struct ifa_cacheinfo *ci = NULL; //Cache info for address: struct rtattr *rth = IFA_RTA(ifa); int rtl = IFA_PAYLOAD(nlh); while (rtl && RTA_OK(rth, rtl)) { if (rth->rta_type == IFA_ADDRESS) { address = RTA_DATA(rth); if(verbose_mode) { log("[IFA_ADDRESS]"); log_ipv6(address); log("\n"); } } else if (rth->rta_type == IFA_CACHEINFO) { ci = (struct ifa_cacheinfo *) RTA_DATA(rth); if(verbose_mode) { log("[IFA_CACHEINFO] valid: %d prefered: %d\n", ci->ifa_valid, ci->ifa_prefered); } } else { verbose_log("[?%d?]\n", rth->rta_type); } rth = RTA_NEXT(rth, rtl); } assert(address != NULL); assert(ci != NULL); filter_ip(address, ifa->ifa_flags, ifa->ifa_scope, ci->ifa_prefered); } } else if (nlh->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr*) NLMSG_DATA(nlh); if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { log("ERROR truncated\n"); } else { errno = -err->error; _perror("RTNETLINK answers"); } return -1; } nlh = NLMSG_NEXT(nlh, len); } fflush(global_output); } if (errno == EINTR || errno == EAGAIN) goto loop; //Would a do..while be cleaner? assert(len != 0); assert(len > 0); return 0; }