/****************************************************************************** * Log out and close the socket * */ int cimd_close(SMSCenter *smsc) { char *cbuff = NULL; int sum; int ret; if (smsc->socket == -1) { debug("bb.sms.cimd", 0, "Trying to close cimd while already closed!"); return 0; } cbuff = gw_malloc(2 * 1024); sprintf(cbuff, "%c%s%c%s%c%c", 0x02, "02", 0x09, "11", 0x03, 0x0A); sum = write_to_socket(smsc->socket, cbuff); if (sum < 0) goto error; /* this time we don't block waiting for acknowledge */ recv(smsc->socket, cbuff, 2*1024, 0); gw_free(cbuff); ret = close(smsc->socket); smsc->socket = -1; return ret; error: gw_free(cbuff); return -1; }
int udp_recvfrom(int s, Octstr **datagram, Octstr **addr) { struct sockaddr_in sa; socklen_t salen; char *buf; int bytes; buf = gw_malloc(UDP_PACKET_MAX_SIZE); salen = sizeof(sa); bytes = recvfrom(s, buf, UDP_PACKET_MAX_SIZE, 0, (struct sockaddr *) &sa, &salen); if (bytes == -1) { if (errno != EAGAIN) error(errno, "Couldn't receive UDP packet"); gw_free(buf); return -1; } *datagram = octstr_create_from_data(buf, bytes); *addr = octstr_create_from_data((char *) &sa, salen); gw_free(buf); return 0; }
void mms_queue_free_envelope(MmsEnvelope *e) { MmsEnvelopeTo *x; if (e == NULL) return; octstr_destroy(e->msgId); while ((x = gwlist_extract_first(e->to)) != NULL) { octstr_destroy(x->rcpt); gw_free(x); } gwlist_destroy(e->to, NULL); octstr_destroy(e->from); octstr_destroy(e->fromproxy); octstr_destroy(e->mdata); octstr_destroy(e->viaproxy); octstr_destroy(e->token); octstr_destroy(e->subject); octstr_destroy(e->vaspid); octstr_destroy(e->vasid); octstr_destroy(e->url1); octstr_destroy(e->url2); http_destroy_headers(e->hdrs); gw_free(e); }
Octstr *udp_create_address(Octstr *host_or_ip, int port) { struct sockaddr_in sa; struct hostent h; char *buff = NULL; Octstr *ret; sa = empty_sockaddr_in; sa.sin_family = AF_INET; sa.sin_port = htons(port); if (strcmp(octstr_get_cstr(host_or_ip), "*") == 0) { sa.sin_addr.s_addr = INADDR_ANY; } else { if (gw_gethostbyname(&h, octstr_get_cstr(host_or_ip), &buff) == -1) { error(0, "Couldn't find the IP number of `%s'", octstr_get_cstr(host_or_ip)); gw_free(buff); return NULL; } sa.sin_addr = *(struct in_addr *) h.h_addr; } ret = octstr_create_from_data((char *) &sa, sizeof(sa)); gw_free(buff); return ret; }
static void destroy_table(struct table *table) { /* No need to call octstr_destroy on immutable octstrs */ gw_free(table->strings); gw_free(table->numbers); gw_free(table->versions); }
void numhash_destroy(Numhash *table) { if (table == NULL) return; gw_free(table->numbers); gw_free(table->hash); gw_free(table); }
static void heap_destroy(TimerHeap *heap) { if (heap == NULL) return; gw_free(heap->tab); gw_free(heap); }
static void sqlite3_conf_destroy(DBConf *db_conf) { SQLite3Conf *conf = db_conf->sqlite3; octstr_destroy(conf->file); gw_free(conf); gw_free(db_conf); }
static void sdb_conf_destroy(DBConf *db_conf) { SDBConf *conf = db_conf->sdb; octstr_destroy(conf->url); gw_free(conf); gw_free(db_conf); }
static void oracle_conf_destroy(DBConf *theconf) { OracleConf *conf = theconf->oracle; octstr_destroy(conf->username); octstr_destroy(conf->password); octstr_destroy(conf->tnsname); gw_free(conf); gw_free(theconf); }
int make_server_socket(int port, const char *interface_name ) { struct sockaddr_in addr; int s; int reuse; struct hostent hostinfo; char *buff = NULL; s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) { error(errno, "socket failed"); goto error; } addr = empty_sockaddr_in; addr.sin_family = AF_INET; addr.sin_port = htons(port); if (interface_name == NULL || strcmp(interface_name, "*") == 0) addr.sin_addr.s_addr = htonl(INADDR_ANY); else { if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) { error(errno, "gethostbyname failed"); goto error; } addr.sin_addr = *(struct in_addr *) hostinfo.h_addr; } reuse = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) == -1) { error(errno, "setsockopt failed for server address"); goto error; } if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) { error(errno, "bind failed"); goto error; } if (listen(s, 10) == -1) { error(errno, "listen failed"); goto error; } gw_free(buff); return s; error: if (s >= 0) (void) close(s); gw_free(buff); return -1; }
void parse_context_destroy(ParseContext *context) { gw_assert(context != NULL); if (context->limit_stack) { while (gwlist_len(context->limit_stack) > 0) gw_free(gwlist_extract_first(context->limit_stack)); gwlist_destroy(context->limit_stack, NULL); } gw_free(context); }
static void *new_thread(void *arg) { int ret; struct new_thread_args *p = arg; /* Make sure we don't start until our parent has entered * our thread info in the thread table. */ lock(); /* check for initialization errors */ if (p->failed) { /* Must free p before signaling our exit, otherwise there is * a race with gw_check_leaks at shutdown. */ gw_free(p); delete_threadinfo(); unlock(); return NULL; } unlock(); /* This has to be done here, because pthread_setspecific cannot * be called by our parent on our behalf. That's why the ti * pointer is passed in the new_thread_args structure. */ /* Synchronization is not a problem, because the only thread * that relies on this call having been made is this one -- * no other thread can access our TSD anyway. */ ret = pthread_setspecific(tsd_key, p->ti); if (ret != 0) { panic(ret, "gwthread-pthread: pthread_setspecific failed"); } p->ti->pid = getpid(); debug("gwlib.gwthread", 0, "Thread %ld (%s) maps to pid %ld.", p->ti->number, p->ti->name, (long) p->ti->pid); (p->func)(p->arg); lock(); debug("gwlib.gwthread", 0, "Thread %ld (%s) terminates.", p->ti->number, p->ti->name); alert_joiners(); #ifdef HAVE_LIBSSL /* Clear the OpenSSL thread-specific error queue to avoid * memory leaks. */ ERR_remove_state(gwthread_self()); #endif /* HAVE_LIBSSL */ /* Must free p before signaling our exit, otherwise there is * a race with gw_check_leaks at shutdown. */ gw_free(p); delete_threadinfo(); unlock(); return NULL; }
static void pgsql_conf_destroy(DBConf *db_conf) { PgSQLConf *conf = db_conf->pgsql; octstr_destroy(conf->host); octstr_destroy(conf->username); octstr_destroy(conf->password); octstr_destroy(conf->database); gw_free(conf); gw_free(db_conf); }
static long spawn_thread(gwthread_func_t *func, const char *name, void *arg) { int ret; pthread_t id; struct new_thread_args *p = NULL; long new_thread_id; /* We want to pass both these arguments to our wrapper function * new_thread, but the pthread_create interface will only let * us pass one pointer. So we wrap them in a little struct. */ p = gw_malloc(sizeof(*p)); p->func = func; p->arg = arg; p->ti = gw_malloc(sizeof(*(p->ti))); p->failed = 0; /* Lock the thread table here, so that new_thread can block * on that lock. That way, the new thread won't start until * we have entered it in the thread table. */ lock(); if (active_threads >= THREADTABLE_SIZE) { unlock(); warning(0, "Too many threads, could not create new thread."); gw_free(p); return -1; } ret = pthread_create(&id, NULL, &new_thread, p); if (ret != 0) { unlock(); error(ret, "Could not create new thread."); gw_free(p); return -1; } ret = pthread_detach(id); if (ret != 0) { error(ret, "Could not detach new thread."); } new_thread_id = fill_threadinfo(id, name, func, p->ti); if (new_thread_id == -1) p->failed = 1; unlock(); if (new_thread_id != -1) debug("gwlib.gwthread", 0, "Started thread %ld (%s)", new_thread_id, name); else debug("gwlib.gwthread", 0, "Failed to start thread (%s)", name); return new_thread_id; }
/* Free memory allocated by MongoDB configuration */ static void mongodb_conf_destroy(DBConf *db_conf) { MongoDBConf *conf = db_conf->mongodb; octstr_destroy(conf->host); octstr_destroy(conf->username); octstr_destroy(conf->password); octstr_destroy(conf->database); gw_free(conf); gw_free(db_conf); mongo_conf = NULL; }
void load_destroy(Load *load) { int i; if (load == NULL) return; for (i = 0; i < load->len; i++) { gw_free(load->entries[i]); } gw_free(load->entries); gw_rwlock_destroy(load->lock); gw_free(load); }
static void cgwop_destroy(struct cgwop *cgwop) { int len; len = cgwop->num_fields; while (--len >= 0) { octstr_destroy(cgwop->name[len]); /* octstr_destroy(NULL) is ok */ octstr_destroy(cgwop->value[len]); } gw_free(cgwop->name); gw_free(cgwop->value); gw_free(cgwop); }
static Udpc *udpc_create(int port, char *interface_name) { Udpc *udpc; Octstr *os; int fl; udpc = gw_malloc(sizeof(Udpc)); udpc->fd = udp_bind(port, interface_name); os = octstr_create(interface_name); udpc->addr = udp_create_address(os, port); octstr_destroy(os); if (udpc->addr == NULL) { error(0, "updc_create: could not resolve interface <%s>", interface_name); close(udpc->fd); gw_free(udpc); return NULL; } fl = fcntl(udpc->fd, F_GETFL); fcntl(udpc->fd, F_SETFL, fl | O_NONBLOCK); os = udp_get_ip(udpc->addr); debug("bb.udp", 0, "udpc_create: Bound to UDP <%s:%d>", octstr_get_cstr(os), udp_get_port(udpc->addr)); octstr_destroy(os); udpc->outgoing_list = gwlist_create(); return udpc; }
void timers_shutdown(void) { if (initialized > 1) { initialized--; return; } /* Stop all timers. */ if (timers->heap->len > 0) warning(0, "Timers shutting down with %ld active timers.", timers->heap->len); while (timers->heap->len > 0) gwtimer_stop(timers->heap->tab[0]); /* Kill timer thread */ timers->stopping = 1; gwthread_wakeup(timers->thread); gwthread_join(timers->thread); initialized = 0; /* Free resources */ heap_destroy(timers->heap); mutex_destroy(timers->mutex); gw_free(timers); }
static void esme_destroy(ESME *esme) { if (esme != NULL) { conn_destroy(esme->conn); gw_free(esme); } }
void wap_event_destroy(WAPEvent *event) { if (event == NULL) return; wap_event_assert(event); switch (event->type) { #define WAPEVENT(name, prettyname, fields) \ case name: \ { struct name *p = &event->u.name; fields; } \ break; #define OCTSTR(name) octstr_destroy(p->name); #define OPTIONAL_OCTSTR(name) octstr_destroy(p->name); #define INTEGER(name) p->name = 0; #ifdef HAVE_WTLS_OPENSSL #define WTLSPDUS(name) wtls_pldList_destroy(p->name); #endif /* HAVE_WTLS_OPENSSL */ #define HTTPHEADER(name) http_destroy_headers(p->name); #define ADDRTUPLE(name) wap_addr_tuple_destroy(p->name); #define CAPABILITIES(name) wsp_cap_destroy_list(p->name); #include "wap_events.def" default: panic(0, "Unknown WAPEvent type %d", (int) event->type); } gw_free(event); }
SMASI_PDU *smasi_pdu_create(unsigned long type) { SMASI_PDU *pdu; pdu = gw_malloc(sizeof(*pdu)); pdu->type = type; switch (type) { #define NONTERMINATED(name) p->name = NULL; #define COMATERMINATED(name) p->name = NULL; #define PDU(name, id, fields) \ case id: { \ struct name *p = &pdu->u.name; \ pdu->type_name = #name; \ pdu->needs_hyphen = (id < SMASI_HYPHEN_ID ? 1 : 0); \ fields \ } break; #include "smasi_pdu.def" default: warning(0, "Unknown SMASI_PDU type, internal error."); gw_free(pdu); return NULL; } return pdu; }
static void smpp_tlv_destroy(struct smpp_tlv *tlv) { if (tlv == NULL) return; octstr_destroy(tlv->name); gw_free(tlv); }
Octstr *gw_regex_subst_real(const Octstr *re, const Octstr *os, const Octstr *rule, const char *file, long line, const char *func) { Octstr *result; regex_t *regexp; regmatch_t pmatch[REGEX_MAX_SUB_MATCH]; int rc; char *rsub; /* compile */ regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE, file, line, func); if (regexp == NULL) return 0; /* execute and match */ rc = gw_regex_exec_real(regexp, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0, file, line, func); gw_regex_destroy(regexp); /* substitute via rule if matched */ if (rc != 0) return NULL; rsub = gw_regex_sub(octstr_get_cstr(rule), octstr_get_cstr(os), REGEX_MAX_SUB_MATCH, &pmatch[0]); if (rsub == NULL) return NULL; result = octstr_create(rsub); gw_free(rsub); return result; }
Octstr *gw_regex_subst_pre_real(const regex_t *preg, const Octstr *os, const Octstr *rule, const char *file, long line, const char *func) { Octstr *result; regmatch_t pmatch[REGEX_MAX_SUB_MATCH]; int rc; char *rsub; gw_assert(preg != NULL); /* execute and match */ rc = gw_regex_exec_real(preg, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0, file, line, func); /* substitute via rule if matched */ if (rc != 0) return NULL; rsub = gw_regex_sub(octstr_get_cstr(rule), octstr_get_cstr(os), REGEX_MAX_SUB_MATCH, &pmatch[0]); if (rsub == NULL) return NULL; result = octstr_create(rsub); gw_free(rsub); return result; }
void mms_cfg_destroy(mCfg *cfg) { List *l; int i, n; gw_assert(cfg); for (i = 0, l = dict_keys(cfg->grps), n = gwlist_len(l); i < n; i++) { Octstr *grpname = gwlist_get(l, i); void *val = dict_get(cfg->grps, grpname); if (is_multigroup(grpname)) { /* item is a list. */ List *gl = val; int j, m = gwlist_len(gl); for (j = 0; j < m; j++) mGrp_destroy(gwlist_get(gl, j)); gwlist_destroy(gl, NULL); } else mGrp_destroy(val); } gwlist_destroy(l, (gwlist_item_destructor_t *)octstr_destroy); dict_destroy(cfg->grps); octstr_destroy(cfg->file); if (cfg->xcfg && cfg->cfg_funcs && cfg->cfg_funcs->destroy) cfg->cfg_funcs->destroy(cfg->xcfg); gw_free(cfg); }
/* linux version */ int gw_gethostbyname(struct hostent *ent, const char *name, char **buff) { struct hostent *tmphp, hp; int herr, res; size_t bufflen; tmphp = NULL; /* for compiler please */ bufflen = 1024; *buff = (char*) gw_malloc(bufflen); while ((res=gethostbyname_r(name, &hp,*buff, bufflen, &tmphp, &herr)) == ERANGE) { /* enlarge the buffer */ bufflen *= 2; *buff = (char*) gw_realloc(*buff, bufflen); } if (res != 0 || tmphp == NULL) { error(herr, "Error while gw_gethostbyname occurs."); gw_free(*buff); *buff = NULL; res = -1; } else { *ent = hp; } return res; }
SMPP_PDU *smpp_pdu_create(unsigned long type, unsigned long seq_no) { SMPP_PDU *pdu; pdu = gw_malloc(sizeof(*pdu)); pdu->type = type; switch (type) { #define OPTIONAL_BEGIN #define TLV_INTEGER(name, octets) p->name = -1; #define TLV_NULTERMINATED(name, max_len) p->name = NULL; #define TLV_OCTETS(name, min_len, max_len) p->name = NULL; #define OPTIONAL_END #define INTEGER(name, octets) \ if (strcmp(#name, "command_id") == 0) p->name = type; \ else if (strcmp(#name, "sequence_number") == 0) p->name = seq_no; \ else p->name = 0; #define NULTERMINATED(name, max_octets) p->name = NULL; #define OCTETS(name, field_giving_octetst) p->name = NULL; #define PDU(name, id, fields) \ case id: { \ struct name *p = &pdu->u.name; \ pdu->type_name = #name; \ fields \ } break; #include "smpp_pdu.def" default: error(0, "Unknown SMPP_PDU type, internal error."); gw_free(pdu); return NULL; } return pdu; }
RADIUS_PDU *radius_pdu_create(int type, RADIUS_PDU *req) { RADIUS_PDU *pdu; pdu = gw_malloc(sizeof(*pdu)); pdu->type = type; switch (type) { #define INTEGER(name, octets) \ if (strcmp(#name, "code") == 0) p->name = type; \ else p->name = 0; #define OCTETS(name, field_giving_octets) p->name = NULL; #define PDU(name, id, fields) \ case id: { \ struct name *p = &pdu->u.name; \ pdu->type_name = #name; \ fields \ } break; #include "radius_pdu.def" default: error(0, "Unknown RADIUS_PDU type, internal error."); gw_free(pdu); return NULL; } #define ATTR(attr, type, string, min, max) #define UNASSIGNED(attr) #define ATTRIBUTES(fields) \ pdu->attr = dict_create(20, (void (*)(void *))octstr_destroy); #include "radius_attributes.def" return pdu; }