GhtErr ght_writer_new_mem(GhtWriter **writer) { GhtWriter *w; w = ght_malloc(sizeof(GhtWriter)); memset(w, 0,sizeof(GhtWriter)); w->bytebuffer = bytebuffer_create(); w->type = GHT_IO_MEM; *writer = w; return GHT_OK; }
/** * Convert LWGEOM to a char* in TWKB format. Caller is responsible for freeing * the returned array. */ uint8_t* lwgeom_to_twkb_with_idlist(const LWGEOM *geom, int64_t *idlist, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size) { LWDEBUGF(2, "Entered %s", __func__); LWDEBUGF(2, "variant value %x", variant); TWKB_GLOBALS tg; TWKB_STATE ts; uint8_t *twkb; memset(&ts, 0, sizeof(TWKB_STATE)); memset(&tg, 0, sizeof(TWKB_GLOBALS)); tg.variant = variant; tg.prec_xy = precision_xy; tg.prec_z = precision_z; tg.prec_m = precision_m; if ( idlist && ! lwgeom_is_collection(geom) ) { lwerror("Only collections can support ID lists"); return NULL; } if ( ! geom ) { LWDEBUG(4,"Cannot convert NULL into TWKB."); lwerror("Cannot convert NULL into TWKB"); return NULL; } ts.idlist = idlist; ts.header_buf = NULL; ts.geom_buf = bytebuffer_create(); lwgeom_write_to_buffer(geom, &tg, &ts); if ( twkb_size ) *twkb_size = bytebuffer_getlength(ts.geom_buf); twkb = ts.geom_buf->buf_start; lwfree(ts.geom_buf); return twkb; }
/** * initialize and startup of {@link ::_l2tp_ctrl L2TP LNS control connection} * instance */ static int l2tp_ctrl_init(l2tp_ctrl *_this, l2tpd *_l2tpd, struct sockaddr *peer, struct sockaddr *sock, void *nat_t_ctx) { int tunid, i; bytebuffer *bytebuf; time_t curr_time; memset(_this, 0, sizeof(l2tp_ctrl)); curr_time = get_monosec(); _this->l2tpd = _l2tpd; _this->state = L2TP_CTRL_STATE_IDLE; _this->last_snd_ctrl = curr_time; slist_init(&_this->call_list); /* seek a free tunnel ID */ i = 0; _this->id = ++l2tp_ctrl_id_seq; for (i = 0, tunid = _this->id; ; i++, tunid++) { tunid &= 0xffff; _this->tunnel_id = l2tp_ctrl_id_seq & 0xffff; if (tunid == 0) continue; if (l2tpd_get_ctrl(_l2tpd, tunid) == NULL) break; if (i > 80000) { /* this must be happen, just log it. */ l2tpd_log(_l2tpd, LOG_ERR, "Too many l2tp controls"); return -1; } } _this->tunnel_id = tunid; L2TP_CTRL_ASSERT(peer != NULL); L2TP_CTRL_ASSERT(sock != NULL); memcpy(&_this->peer, peer, peer->sa_len); memcpy(&_this->sock, sock, sock->sa_len); /* prepare send buffer */ _this->winsz = L2TPD_DEFAULT_SEND_WINSZ; if ((_this->snd_buffers = calloc(_this->winsz, sizeof(bytebuffer *))) == NULL) { l2tpd_log(_l2tpd, LOG_ERR, "calloc() failed in %s(): %m", __func__); goto fail; } for (i = 0; i < _this->winsz; i++) { if ((bytebuf = bytebuffer_create(L2TPD_SND_BUFSIZ)) == NULL) { l2tpd_log(_l2tpd, LOG_ERR, "bytebuffer_create() failed in %s(): %m", __func__); goto fail; } _this->snd_buffers[i] = bytebuf; } if ((_this->zlb_buffer = bytebuffer_create(sizeof(struct l2tp_header) + 128)) == NULL) { l2tpd_log(_l2tpd, LOG_ERR, "bytebuffer_create() failed in %s(): %m", __func__); goto fail; } #if defined(USE_LIBSOCKUTIL) || defined(USE_SA_COOKIE) if (nat_t_ctx != NULL) { if ((_this->sa_cookie = malloc( sizeof(struct in_ipsec_sa_cookie))) != NULL) { *(struct in_ipsec_sa_cookie *)_this->sa_cookie = *(struct in_ipsec_sa_cookie *)nat_t_ctx; } else { l2tpd_log(_l2tpd, LOG_ERR, "creating sa_cookie failed: %m"); goto fail; } } #endif _this->hello_interval = L2TP_CTRL_DEFAULT_HELLO_INTERVAL; _this->hello_timeout = L2TP_CTRL_DEFAULT_HELLO_TIMEOUT; _this->hello_io_time = curr_time; /* initialize timeout timer */ l2tp_ctrl_reset_timeout(_this); /* register l2tp context */ l2tpd_add_ctrl(_l2tpd, _this); return 0; fail: l2tp_ctrl_stop(_this, 0); return -1; }