static void boxc_destroy(Boxc *boxc) { if (boxc == NULL) return; /* do nothing to the lists, as they are only references */ if (boxc->smsbox_connection) conn_destroy(boxc->smsbox_connection); if (boxc->bearerbox_connection) conn_destroy(boxc->bearerbox_connection); octstr_destroy(boxc->client_ip); octstr_destroy(boxc->boxc_id); gw_free(boxc); }
/* * XXX bad assumption here that conn_wrap_fd for SSL can only happens * for the server side!!!! FIXME !!!! */ Connection *conn_wrap_fd(int fd, int ssl) { Connection *conn; if (socket_set_blocking(fd, 0) < 0) return NULL; conn = gw_malloc(sizeof(*conn)); conn->inlock = mutex_create(); conn->outlock = mutex_create(); conn->claimed = 0; conn->outbuf = octstr_create(""); conn->outbufpos = 0; conn->inbuf = octstr_create(""); conn->inbufpos = 0; conn->fd = fd; conn->connected = yes; conn->read_eof = 0; conn->io_error = 0; conn->output_buffering = DEFAULT_OUTPUT_BUFFERING; conn->registered = NULL; conn->callback = NULL; conn->callback_data = NULL; conn->callback_data_destroyer = NULL; conn->listening_pollin = 0; conn->listening_pollout = 0; #ifdef HAVE_LIBSSL /* * do all the SSL magic for this connection */ if (ssl) { conn->ssl = SSL_new(global_server_ssl_context); conn->peer_certificate = NULL; /* SSL_set_fd can fail, so check it */ if (SSL_set_fd(conn->ssl, conn->fd) == 0) { /* SSL_set_fd failed, log error and return NULL */ error(errno, "SSL: OpenSSL: %.256s", ERR_error_string(ERR_get_error(), NULL)); conn_destroy(conn); return NULL; } /* SSL_set_verify(conn->ssl, 0, NULL); */ /* set read/write BIO layer to non-blocking mode */ BIO_set_nbio(SSL_get_rbio(conn->ssl), 1); BIO_set_nbio(SSL_get_wbio(conn->ssl), 1); /* set accept state , SSL-Handshake will be handled transparent while SSL_[read|write] */ SSL_set_accept_state(conn->ssl); } else { conn->ssl = NULL; conn->peer_certificate = NULL; } #endif /* HAVE_LIBSSL */ return conn; }
int main(int argc, char **argv) { Connection *conn; Octstr *host = NULL; int port, i; gwlib_init(); get_and_set_debugs(argc, argv, NULL); host = octstr_create(argv[1]); port = atoi(argv[2]); i = 50; debug("",0,"Connecting to host '%s', port %d, looping %i times.", octstr_get_cstr(host), port, i); for (i = 0; i <= 50; i++) { conn = conn_open_tcp(host, port, NULL); if (conn == NULL) { panic(0, "Couldn't connect."); } debug("",0,"%d: connected.", i); gwthread_sleep(0.2); debug("",0,"%d: closing.", i); conn_destroy(conn); } octstr_destroy(host); gwlib_shutdown(); return 0; }
static void esme_destroy(ESME *esme) { if (esme != NULL) { conn_destroy(esme->conn); gw_free(esme); } }
void rel_destroy (rel_t *r) { if (r->next) r->next->prev = r->prev; *r->prev = r->next; conn_destroy (r->c); /* Free any other allocated memory here */ if (r->ss) { free(r->ss); } if (r->sendWindow) { free(r->sendWindow); } if (r->recvWindow) { free(r->recvWindow); } if (r->sendState) { free(r->sendState); } if (r->recvState) { free(r->recvState); } if (r->sendTimer) { free(r->sendTimer); } free(r); }
static void smsbox_thread(void *arg) { Connection *conn; Msg *msg; Octstr *os; Octstr *reply_msg; unsigned long count; msg = msg_create(sms); msg->sms.sender = octstr_create("123"); msg->sms.receiver = octstr_create("456"); msg->sms.msgdata = octstr_create("hello world"); reply_msg = msg_pack(msg); msg_destroy(msg); gwthread_sleep(1.0); conn = conn_open_tcp(bearerbox_host, port_for_smsbox, NULL); if (conn == NULL) { gwthread_sleep(2.0); conn = conn_open_tcp(bearerbox_host, port_for_smsbox, NULL); if (conn == NULL) panic(0, "Couldn't connect to bearerbox as smsbox"); } while (!quitting && conn_wait(conn, -1.0) != -1) { for (;;) { os = conn_read_withlen(conn); if (os == NULL) { if (conn_eof(conn) || conn_error(conn)) goto error; break; } msg = msg_unpack(os); if (msg == NULL || msg->type == wdp_datagram) error(0, "Bearerbox sent garbage to smsbox"); if (msg->type == sms) { if (first_from_bb == (time_t) -1) time(&first_from_bb); count = counter_increase(num_from_bearerbox) + 1; debug("test.smpp", 0, "Bearerbox sent sms #%ld <%s> to smsbox, sending reply.", count, octstr_get_cstr(msg->sms.msgdata)); if (count == max_to_esme) info(0, "Bearerbox has sent all messages to smsbox."); conn_write_withlen(conn, reply_msg); counter_increase(num_to_bearerbox); } msg_destroy(msg); octstr_destroy(os); time(&last_to_bb); } } error: conn_destroy(conn); octstr_destroy(reply_msg); debug("test.smpp", 0, "%s terminates.", __func__); }
void kr_close(KrClient * client) { conn_send(client, KR_COMMAND_CLOSE, &client->db_id, sizeof(client->db_id)); conn_destroy(client); free(client->dev); free(client); }
/* user logout, client timeout or something error, we need to * close the connection and release resource */ void close_connection(struct conn_server *server, struct connection *conn) { /* remove from fd-conn hash map */ close(conn->sfd); struct fd_entry *fd_entry; HASH_FIND_INT(server->fd_conn_map, &conn->sfd, fd_entry); if (fd_entry) { HASH_DEL(server->fd_conn_map, fd_entry); free(fd_entry); } /* remove from uin-conn hash map */ struct uin_entry *uin_entry; HASH_FIND_INT(server->uin_conn_map, &conn->uin, uin_entry); if (uin_entry) { HASH_DEL(server->uin_conn_map, uin_entry); free(uin_entry); } /* remove from timer */ timer_del(conn); conn_destroy(server, conn); /* free the conn struct */ allocator_free(&server->conn_allocator, conn); }
extern int connlist_destroy(void) { t_connection * c; BEGIN_HASHTABLE_TRAVERSE_DATA(connlist_head, c) { conn_destroy(c); }
void rel_destroy(rel_t *r) { if (r->next) r->next->prev = r->prev; *r->prev = r->next; conn_destroy(r->c); //destroy the connection /* Free any other allocated memory here */ }
int bot_destroy(struct bot* b) { config_write(b->conf, "config.conf"); conn_destroy(b->conn); config_destroy(b->conf); free(b->conn); free(b->conf); b->conn=NULL; b->conf=NULL; if(!b->conn && !b->conf) return 0; else return -1; }
static Connection *cgw_open_send_connection(SMSCConn *conn) { PrivData *privdata = conn->data; int wait; Connection *server; Msg *msg; wait = 0; while (!privdata->shutdown) { /* Change status only if the first attempt to form a * connection fails, as it's possible that the SMSC closed the * connection because of idle timeout and a new one will be * created quickly. */ if (wait) { if (conn->status == SMSCCONN_ACTIVE) { mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_RECONNECTING; mutex_unlock(conn->flow_mutex); } while ((msg = gwlist_extract_first(privdata->outgoing_queue))) bb_smscconn_send_failed(conn, msg, SMSCCONN_FAILED_TEMPORARILY, NULL); info(0, "smsc_cgw: waiting for %d minutes before trying to connect again", wait); gwthread_sleep(wait * 60); wait = wait > 5 ? 10 : wait * 2; } else wait = 1; server = conn_open_tcp_with_port(privdata->host, privdata->port, privdata->our_port, conn->our_host); if (privdata->shutdown) { conn_destroy(server); return NULL; } if (server == NULL) { error(0, "smsc_cgw: opening TCP connection to %s failed", octstr_get_cstr(privdata->host)); continue; } if (conn->status != SMSCCONN_ACTIVE) { mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_ACTIVE; conn->connect_time = time(NULL); mutex_unlock(conn->flow_mutex); bb_smscconn_connected(conn); } return server; } return NULL; }
Connection *conn_open_ssl(Octstr *host, int port, Octstr *certkeyfile, Octstr *our_host) { Connection *ret; /* open the TCP connection */ if (!(ret = conn_open_tcp(host, port, our_host))) { return NULL; } if (conn_init_client_ssl(ret, certkeyfile) == -1) { conn_destroy(ret); return NULL; } return ret; }
static int sd_tcpoutput(int csocket, t_connection * c) { unsigned int currsize; unsigned int totsize; t_packet * packet; totsize = 0; for (;;) { currsize = conn_get_out_size(c); switch (net_send_packet(csocket,queue_peek_packet((t_queue const * const *)conn_get_out_queue(c)),&currsize)) /* avoid warning */ { case -1: conn_destroy(c); return -1; case 0: /* still working on it */ conn_set_out_size(c,currsize); return 0; /* bail out */ case 1: /* done sending */ packet = queue_pull_packet(conn_get_out_queue(c)); if (hexstrm) { fprintf(hexstrm,"%d: send class=%s[0x%02x] type=%s[0x%04x] length=%u\n", csocket, packet_get_class_str(packet),(unsigned int)packet_get_class(packet), packet_get_type_str(packet,packet_dir_from_server),packet_get_type(packet), packet_get_size(packet)); hexdump(hexstrm,packet_get_raw_data(packet,0),packet_get_size(packet)); } packet_del_ref(packet); conn_set_out_size(c,0); /* stop at about 2KB (or until out of packets or EWOULDBLOCK) */ if (totsize>2048 || queue_get_length((t_queue const * const *)conn_get_out_queue(c))<1) return 0; totsize += currsize; } } /* not reached */ }
void rel_destroy (rel_t *r) { assert(r); if (r->next) r->next->prev = r->prev; *r->prev = r->next; conn_destroy (r->c); /* Free the buffer queues */ bq_destroy(r->send_bq); bq_destroy(r->rec_bq); /* Free the rel_t block */ free(r); }
/** * Create a connection object without queueing * @retval 1 Success * @retval 0 failure, conslt errno for more details */ struct conn * conn_create_noq(int sock, struct sockaddr * addr) { struct conn * conn; conn = malloc(sizeof (struct conn)); if (!conn) return NULL; conn_init(conn); conn->sock=sock; if (addr) sock_copyaddr(&conn->addr,addr); /* create the CAPWAP framentation manager */ conn->fragman = fragman_create(); if (conn->fragman==NULL){ conn_destroy(conn); return NULL; } /* set packet recieve and send methods */ conn->recv_packet = conn_recv_packet; conn->recv_packet_peek = conn_recv_packet_peek; conn->send_packet = conn_send_packet; /* misc settings */ conn->last_seqnum_received=-1; conn->mtu=1500; conn->cur_packet=0; conn->recv_timeout=1; conn->seqnum=-1; conn->write = conn->send_packet; conn->read = conn->recv_packet; return conn; }
void rel_destroy (rel_t *r) { // printf("rel_destroy\n"); uint32_t curTime = getCurrentTime(); fprintf(stderr, "RECEIVED TIME: %u SENT TIME: %u\n", curTime, r->startTime); conn_destroy (r->c); /* Free any other allocated memory here */ int i; for (i = 0; i < r->ssThresh; i++) { free(r->sentPackets[i]->packet); free(r->sentPackets[i]); free(r->recvPackets[i]->packet); free(r->recvPackets[i]); } free(r->sentPackets); free(r->recvPackets); free(r); }
void rel_destroy (rel_t *r) { if (r->next) r->next->prev = r->prev; *r->prev = r->next; conn_destroy (r->c); /* Free any other allocated memory here */ int i; for (i = 0; i < r->windowSize; i++) { free(r->sentPackets[i]->packet); free(r->sentPackets[i]); free(r->recvPackets[i]->packet); free(r->recvPackets[i]); } free(r->sentPackets); free(r->recvPackets); free(r); }
void rel_destroy (rel_t *r) { long int end = stopTimer(r); conn_destroy (r->c); packetQueue* tempBuff = r->receiverBuff; packetQueue* next = r->receiverBuff; while(tempBuff){ next = tempBuff->next; dequeue(&(tempBuff)); tempBuff = next; } tempBuff = r->senderBuff; next = r->senderBuff; while(tempBuff){ next = tempBuff->next; dequeue(&(tempBuff)); tempBuff = next; } fprintf(stderr, "Time elapsed: \t%ld ms\n", (end - ((r->beginTime.tv_sec * 1000) + (r->beginTime.tv_usec / 1000)))); return; }
//主连接备的时候结构的销毁 void mb_conn_destroy(int fd, short event, void *arg) { WThread *wt; BackupInfo *binfo; BackupItem *bitem; MBconn *mbconn = (MBconn *)arg; zz_check(mbconn); /* if (mbconn->wbuf) { zz_free(mbconn->wbuf); mbconn->wbuf = NULL; } if (mbconn->rbuf) { zz_free(mbconn->rbuf); mbconn->rbuf = NULL; } */ /* event_del(&conn->evt); if (mbconn->sock > 0) close(mbconn->sock); */ wt = (WThread *)mbconn->thread; binfo = wt->backup_info; bitem = (BackupItem *)mbconn->item; DINFO("====================destroy in connect: %s:%d\n", bitem->ip, bitem->write_port); bitem->state = STATE_NOWRITE; //zz_free(mbconn); bitem->mbconn = NULL; //bitem->is_conn_destroy = TRUE; conn_destroy((Conn *)mbconn); return; }
static void cgw_sender(void *arg) { SMSCConn *conn = arg; PrivData *privdata = conn->data; Msg *msg = NULL; Connection *server = NULL; int l = 0; int ret = 0; conn->status = SMSCCONN_CONNECTING; /* Make sure we log into our own log-file if defined */ log_thread_to(conn->log_idx); while (!privdata->shutdown) { /* check that connection is active */ if (conn->status != SMSCCONN_ACTIVE) { if ((server = cgw_open_send_connection(conn)) == NULL) { privdata->shutdown = 1; error(0, "Unable to connect to CGW server"); return ; } conn->status = SMSCCONN_ACTIVE; bb_smscconn_connected(conn); } else { ret = 0; l = gwlist_len(privdata->outgoing_queue); if (l > 0) ret = cgw_send_loop(conn, server); /* send any messages in queue */ if (ret != -1) ret = cgw_wait_command(privdata, conn, server, 1); /* read ack's and delivery reports */ if (ret != -1) cgw_check_acks(privdata); /* check un-acked messages */ if (ret == -1) { mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_RECONNECTING; mutex_unlock(conn->flow_mutex); } } } conn_destroy(server); while ((msg = gwlist_extract_first(privdata->outgoing_queue)) != NULL) bb_smscconn_send_failed(conn, msg, SMSCCONN_FAILED_SHUTDOWN, NULL); mutex_lock(conn->flow_mutex); conn->status = SMSCCONN_DEAD; gwlist_destroy(privdata->outgoing_queue, NULL); octstr_destroy(privdata->host); octstr_destroy(privdata->allow_ip); octstr_destroy(privdata->deny_ip); gw_free(privdata); conn->data = NULL; mutex_unlock(conn->flow_mutex); debug("bb.sms", 0, "smsc_cgw connection has completed shutdown."); bb_smscconn_killed(); }
static int sd_tcpinput(int csocket, t_connection * c) { unsigned int currsize; t_packet * packet; currsize = conn_get_in_size(c); if (!*conn_get_in_queue(c)) { switch (conn_get_class(c)) { case conn_class_init: if (!(packet = packet_create(packet_class_init))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate init packet for input"); return -1; } break; case conn_class_d2cs_bnetd: if (!(packet = packet_create(packet_class_d2cs_bnetd))) { eventlog(eventlog_level_error,"server_process","could not allocate d2cs_bnetd packet"); return -1; } break; case conn_class_bnet: if (!(packet = packet_create(packet_class_bnet))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate bnet packet for input"); return -1; } break; case conn_class_file: if (!(packet = packet_create(packet_class_file))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate file packet for input"); return -1; } break; case conn_class_bits: if (!(packet = packet_create(packet_class_bits))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate BITS packet for input"); return -1; } break; case conn_class_defer: case conn_class_bot: case conn_class_irc: case conn_class_telnet: if (!(packet = packet_create(packet_class_raw))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate raw packet for input"); return -1; } packet_set_size(packet,1); /* start by only reading one char */ break; case conn_class_auth: if (!(packet = packet_create(packet_class_auth))) { eventlog(eventlog_level_error,"sd_tcpinput","could not allocate auth packet for input"); return -1; } break; default: eventlog(eventlog_level_error,"sd_tcpinput","[%d] connection has bad class (closing connection)",conn_get_socket(c)); conn_destroy(c); return -1; } queue_push_packet(conn_get_in_queue(c),packet); packet_del_ref(packet); if (!*conn_get_in_queue(c)) return -1; /* push failed */ currsize = 0; } packet = queue_peek_packet((t_queue const * const *)conn_get_in_queue(c)); /* avoid warning */ switch (net_recv_packet(csocket,packet,&currsize)) { case -1: eventlog(eventlog_level_debug,"sd_tcpinput","[%d] read FAILED (closing connection)",conn_get_socket(c)); conn_destroy(c); return -1; case 0: /* still working on it */ /* eventlog(eventlog_level_debug,"sd_tcpinput","[%d] still reading \"%s\" packet (%u of %u bytes so far)",conn_get_socket(c),packet_get_class_str(packet),conn_get_in_size(c),packet_get_size(packet)); */ conn_set_in_size(c,currsize); break; case 1: /* done reading */ switch (conn_get_class(c)) { case conn_class_defer: { unsigned char const * const temp=packet_get_raw_data_const(packet,0); eventlog(eventlog_level_debug,"sd_tcpinput","[%d] got first packet byte %02x",conn_get_socket(c),(unsigned int)temp[0]); if (temp[0]==(unsigned char)0xff) /* HACK: thankfully all bnet packet types end with ff */ { conn_set_class(c,conn_class_bnet); conn_set_in_size(c,currsize); packet_set_class(packet,packet_class_bnet); eventlog(eventlog_level_debug,"sd_tcpinput","[%d] defered connection class is bnet",conn_get_socket(c)); } else { conn_set_class(c,conn_class_auth); conn_set_in_size(c,currsize); packet_set_class(packet,packet_class_auth); eventlog(eventlog_level_debug,"sd_tcpinput","[%d] defered connection class is auth",conn_get_socket(c)); } } break; case conn_class_bot: case conn_class_telnet: if (currsize<MAX_PACKET_SIZE) /* if we overflow, we can't wait for the end of the line. handle_*_packet() should take care of it */ { char const * const temp=packet_get_raw_data_const(packet,0); if ((temp[currsize-1]=='\003')||(temp[currsize-1]=='\004')) { /* we have to ignore these special characters, since * some bots even send them after login (eg. UltimateBot) */ currsize--; break; } if (temp[currsize-1]!='\r' && temp[currsize-1]!='\n') { conn_set_in_size(c,currsize); packet_set_size(packet,currsize+1); break; /* no end of line, get another char */ } } /* got a complete line or overflow, so: */ /*FALLTHRU*/ default: packet = queue_pull_packet(conn_get_in_queue(c)); if (hexstrm) { fprintf(hexstrm,"%d: recv class=%s[0x%02x] type=%s[0x%04x] length=%u\n", csocket, packet_get_class_str(packet),(unsigned int)packet_get_class(packet), packet_get_type_str(packet,packet_dir_from_client),packet_get_type(packet), packet_get_size(packet)); hexdump(hexstrm,packet_get_raw_data_const(packet,0),packet_get_size(packet)); } if (conn_get_class(c)==conn_class_bot || conn_get_class(c)==conn_class_telnet) /* NUL terminate the line to make life easier */ { char * const temp=packet_get_raw_data(packet,0); if (temp[currsize-1]=='\r' || temp[currsize-1]=='\n') temp[currsize-1] = '\0'; /* have to do it here instead of above so everything is intact for the hexdump */ } { int ret; switch (conn_get_class(c)) { case conn_class_bits: #ifdef WITH_BITS ret = handle_bits_packet(c,packet); #else eventlog(eventlog_level_error,"sd_tcpinput","[%d] BITS not enabled (closing connection)",conn_get_socket(c)); ret = -1; #endif break; case conn_class_init: ret = handle_init_packet(c,packet); break; case conn_class_bnet: ret = handle_bnet_packet(c,packet); break; case conn_class_d2cs_bnetd: ret = handle_d2cs_packet(c,packet); break; case conn_class_bot: ret = handle_bot_packet(c,packet); break; case conn_class_telnet: ret = handle_telnet_packet(c,packet); break; case conn_class_file: ret = handle_file_packet(c,packet); break; case conn_class_auth: ret = handle_auth_packet(c,packet); break; case conn_class_irc: ret = handle_irc_packet(c,packet); break; default: eventlog(eventlog_level_error,"sd_tcpinput","[%d] bad packet class %d (closing connection)",conn_get_socket(c),(int)packet_get_class(packet)); ret = -1; } packet_del_ref(packet); if (ret<0) { conn_destroy(c); return -1; } } conn_set_in_size(c,0); } } return 0; }
void close_connection_to_bearerbox_real(Connection *conn) { conn_destroy(conn); }
int main(void) { struct vlc_http_stream *s; struct vlc_http_msg *m; struct block_t *b; /* Dummy */ conn_create(); conn_destroy(); /* Test rejected connection */ conn_create(); conn_shutdown(SHUT_RD); s = stream_open(); assert(s == NULL); conn_destroy(); /* Test rejected stream */ conn_create(); s = stream_open(); assert(s != NULL); conn_shutdown(SHUT_WR); m = vlc_http_stream_read_headers(s); assert(m == NULL); b = vlc_http_stream_read(s); assert(b == NULL); m = vlc_http_stream_read_headers(s); assert(m == NULL); b = vlc_http_stream_read(s); assert(b == NULL); m = vlc_http_msg_get_initial(s); assert(m == NULL); s = stream_open(); assert(s == NULL); conn_destroy(); /* Test garbage */ conn_create(); s = stream_open(); assert(s != NULL); conn_send("Go away!\r\n\r\n"); conn_shutdown(SHUT_WR); m = vlc_http_stream_read_headers(s); assert(m == NULL); b = vlc_http_stream_read(s); assert(b == NULL); conn_destroy(); vlc_http_stream_close(s, false); /* Test HTTP/1.0 stream */ conn_create(); s = stream_open(); assert(s != NULL); conn_send("HTTP/1.0 200 OK\r\n\r\n"); m = vlc_http_msg_get_initial(s); assert(m != NULL); conn_send("Hello world!"); conn_shutdown(SHUT_WR); b = vlc_http_msg_read(m); assert(b != NULL); assert(b->i_buffer == 12); assert(!memcmp(b->p_buffer, "Hello world!", 12)); block_Release(b); b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); conn_destroy(); /* Test HTTP/1.1 with closed connection */ conn_create(); s = stream_open(); assert(s != NULL); conn_send("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"); m = vlc_http_msg_get_initial(s); assert(m != NULL); conn_send("Hello again!"); conn_shutdown(SHUT_WR); b = vlc_http_msg_read(m); assert(b != NULL); assert(b->i_buffer == 12); assert(!memcmp(b->p_buffer, "Hello again!", 12)); block_Release(b); b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); conn_destroy(); /* Test HTTP/1.1 with chunked transfer encoding */ conn_create(); s = stream_open(); assert(s != NULL); conn_send("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" "Content-Length: 1000000\r\n\r\n"); /* length must be ignored */ m = vlc_http_msg_get_initial(s); assert(m != NULL); conn_send("C\r\nHello there!\r\n0\r\n\r\n"); b = vlc_http_msg_read(m); assert(b != NULL); assert(b->i_buffer == 12); assert(!memcmp(b->p_buffer, "Hello there!", 12)); block_Release(b); conn_destroy(); /* test connection release before closing stream */ b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); /* Test HTTP/1.1 with content length */ conn_create(); s = stream_open(); assert(s != NULL); conn_send("HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n"); m = vlc_http_msg_get_initial(s); assert(m != NULL); conn_send("Bye bye!"); b = vlc_http_msg_read(m); assert(b != NULL); assert(b->i_buffer == 8); assert(!memcmp(b->p_buffer, "Bye bye!", 8)); block_Release(b); b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); conn_destroy(); return 0; }
int main(void) { struct vlc_http_stream *s, *s2; struct vlc_http_msg *m; struct block_t *b; uint_fast32_t sid = -1; /* Second guessed stream IDs :-/ */ conn_create(); conn_destroy(); conn_create(); conn_send(vlc_h2_frame_ping(42)); conn_expect(PING); /* Test rejected stream */ sid += 2; s = stream_open(); assert(s != NULL); conn_expect(HEADERS); conn_send(vlc_h2_frame_rst_stream(sid, VLC_H2_REFUSED_STREAM)); m = vlc_http_stream_read_headers(s); assert(m == NULL); b = vlc_http_stream_read(s); assert(b == NULL); vlc_http_stream_close(s, false); conn_expect(RST_STREAM); /* Test accepted stream */ sid += 2; s = stream_open(); assert(s != NULL); stream_reply(sid, false); m = vlc_http_msg_get_initial(s); assert(m != NULL); vlc_http_msg_destroy(m); stream_data(3, "Hello ", false); /* late data */ stream_data(3, "world!", true); conn_expect(HEADERS); conn_expect(RST_STREAM); conn_expect(RST_STREAM); conn_expect(RST_STREAM); /* Test continuation then accepted stream */ sid += 2; s = stream_open(); assert(s != NULL); stream_continuation(sid); m = vlc_http_msg_get_initial(s); assert(m != NULL); assert(vlc_http_msg_get_status(m) == 100); stream_reply(sid, false); m = vlc_http_msg_iterate(m); assert(m != NULL); stream_data(sid, "Hello ", false); stream_data(sid, "world!", true); stream_data(sid, "Stray message", false); /* data after EOS */ b = vlc_http_msg_read(m); assert(b != NULL); block_Release(b); b = vlc_http_msg_read(m); assert(b != NULL); block_Release(b); b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); conn_expect(HEADERS); conn_expect(RST_STREAM); conn_expect(RST_STREAM); /* Test accepted stream after continuation */ sid += 2; s = stream_open(); assert(s != NULL); stream_continuation(sid); stream_reply(sid, true); sid += 2; s2 = stream_open(); /* second stream to enforce test timing/ordering */ assert(s2 != NULL); stream_reply(sid, true); m = vlc_http_msg_get_initial(s2); assert(m != NULL); vlc_http_msg_destroy(m); m = vlc_http_msg_get_initial(s); assert(m != NULL); assert(vlc_http_msg_get_status(m) == 200); b = vlc_http_msg_read(m); assert(b == NULL); vlc_http_msg_destroy(m); conn_expect(HEADERS); conn_expect(HEADERS); conn_expect(RST_STREAM); conn_expect(RST_STREAM); /* Test nonexistent stream reset */ conn_send(vlc_h2_frame_rst_stream(sid + 100, VLC_H2_REFUSED_STREAM)); /* Test multiple streams in non-LIFO order */ sid += 2; s = stream_open(); assert(s != NULL); sid += 2; s2 = stream_open(); assert(s2 != NULL); stream_reply(sid, false); stream_reply(sid - 2, true); stream_data(sid, "Discarded", false); /* not read data */ m = vlc_http_msg_get_initial(s); assert(m != NULL); vlc_http_msg_destroy(m); m = vlc_http_msg_get_initial(s2); assert(m != NULL); vlc_http_msg_destroy(m); conn_expect(HEADERS); conn_expect(HEADERS); conn_expect(RST_STREAM); conn_expect(RST_STREAM); /* might or might not seen one or two extra RST_STREAM now */ /* Test graceful connection termination */ sid += 2; s = stream_open(); assert(s != NULL); conn_send(vlc_h2_frame_goaway(sid - 2, VLC_H2_NO_ERROR)); m = vlc_http_stream_read_headers(s); assert(m == NULL); /* Test stream after connection shut down */ assert(stream_open() == NULL); /* Test releasing connection before stream */ conn_destroy(); vlc_http_stream_close(s, false); return 0; }
static void main_connection_loop(SMSCConn *conn, Connection *client) { PrivData *privdata = conn->data; Octstr *line; Msg *msg; double delay = 0; if (conn->throughput > 0) { delay = 1.0 / conn->throughput; } while (1) { while (!conn->is_stopped && !privdata->shutdown && (line = conn_read_line(client))) msg_to_bb(conn, line); if (conn_error(client)) goto error; if (conn_eof(client)) goto eof; /* * We won't get DLRs from fakesmsc itself, due that we don't have * corresponding message IDs etc. We threat the DLR receiving here. So * DLR "originate" from the protocol layer towards abstraction layer. * This is all for pure debugging and testing. */ while ((msg = gwlist_extract_first(privdata->outgoing_queue)) != NULL) { /* pass msg to fakesmsc daemon */ if (sms_to_client(client, msg) == 1) { Msg *copy = msg_duplicate(msg); /* * Actually no guarantee of it having been really sent, * but I suppose that doesn't matter since this interface * is just for debugging anyway. The upper layer will send * a SMSC success DLR if mask is set. Be aware that msg is * destroyed in abstraction layer, that's why we use a copy * afterwards to handle the final DLR. */ bb_smscconn_sent(conn, msg, NULL); /* and now the final DLR */ if (DLR_IS_SUCCESS_OR_FAIL(copy->sms.dlr_mask)) { Msg *dlrmsg; Octstr *tmp; int dlrstat = DLR_SUCCESS; char id[UUID_STR_LEN + 1]; uuid_unparse(copy->sms.id, id); tmp = octstr_create(id); dlrmsg = dlr_find(conn->id, tmp, /* smsc message id */ copy->sms.receiver, /* destination */ dlrstat, 0); if (dlrmsg != NULL) { /* XXX TODO: Provide a SMPP DLR text in msgdata */ bb_smscconn_receive(conn, dlrmsg); } else { error(0,"smsc_fale: got DLR but could not find message or " "was not interested in it"); } octstr_destroy(tmp); } msg_destroy(copy); } else { bb_smscconn_send_failed(conn, msg, SMSCCONN_FAILED_REJECTED, octstr_create("REJECTED")); goto error; } /* obey throughput speed limit, if any */ if (conn->throughput > 0) { gwthread_sleep(delay); } } if (privdata->shutdown) { debug("bb.sms", 0, "smsc_fake shutting down, closing client socket"); conn_destroy(client); return; } conn_wait(client, -1); if (conn_error(client)) goto error; if (conn_eof(client)) goto eof; } error: info(0, "IO error to fakesmsc client. Closing connection."); conn_destroy(client); return; eof: info(0, "EOF from fakesmsc client. Closing connection."); conn_destroy(client); return; }
void wtpman_destroy(struct wtpman *wtpman) { if (wtpman->conn) conn_destroy(wtpman->conn); free(wtpman); }
/** * Create a conn object * @param sock a socket * @param addr the address associated * @param qsize size of packet queue * @return A pointer to the created object * This function creates a conn obnject with queueing functionality * for asynchronous operation. * To create a conn object without queue functionallity use #conn_create_noq. */ struct conn * conn_create(int sock, struct sockaddr * addr, int qsize) { struct conn * conn; conn = malloc(sizeof (struct conn)); if (!conn) return NULL; conn_init(conn); conn->sock=sock; if (addr) sock_copyaddr(&conn->addr,addr); conn->fragman = fragman_create(); if (conn->fragman==NULL){ conn_destroy(conn); return NULL; } conn->qsize=qsize; if (qsize != 0){ if (!(conn->q=malloc( sizeof(uint8_t *) * qsize))){ conn_destroy(conn); return NULL; } conn->qrpos=-1; if (sem_init(&conn->q_sem,0,0)!=0){ cw_log(LOG_ERR,"Fatal- Can't init semaphore for conn object: %s",strerror(errno)); conn_destroy(conn); return NULL; }; conn->recv_packet=conn_q_recv_packet; conn->recv_packet_peek=conn_q_recv_packet_peek; } else{ conn->recv_packet = conn_recv_packet; conn->recv_packet_peek = conn_recv_packet_peek; } conn->send_packet = conn_send_packet; // conn->send_data_packet = conn_send_data_packet; conn->last_seqnum_received=-1; conn->mtu=1500; conn->cur_packet=0; conn->recv_timeout=1; conn->seqnum=-1; conn->write = conn->send_packet; conn->read = conn->recv_packet; // conn->write_data = conn->send_data_packet; conn->dtls_mtu = 1500; return conn; }
static void cgw_listener(void *arg) { SMSCConn *conn = arg; PrivData *privdata = conn->data; struct sockaddr_in server_addr; socklen_t server_addr_len; Octstr *ip; Connection *server; int s, ret; /* Make sure we log into our own log-file if defined */ log_thread_to(conn->log_idx); while (!privdata->shutdown) { server_addr_len = sizeof(server_addr); ret = gwthread_pollfd(privdata->listening_socket, POLLIN, -1); if (ret == -1) { if (errno == EINTR) continue; error(0, "Poll for cgw smsc connections failed, shutting down"); break; } if (privdata->shutdown) break; if (ret == 0) /* This thread was woken up from elsewhere, but * if we're not shutting down nothing to do here. */ continue; s = accept(privdata->listening_socket, (struct sockaddr *) & server_addr, &server_addr_len); if (s == -1) { warning(errno, "cgw_listener: accept() failed, retrying..."); continue; } ip = host_ip(server_addr); if (!is_allowed_ip(privdata->allow_ip, privdata->deny_ip, ip)) { info(0, "CGW smsc connection tried from denied host <%s>, disconnected", octstr_get_cstr(ip)); octstr_destroy(ip); close(s); continue; } server = conn_wrap_fd(s, 0); if (server == NULL) { error(0, "cgw_listener: conn_wrap_fd failed on accept()ed fd"); octstr_destroy(ip); close(s); continue; } conn_claim(server); info(0, "cgw: smsc connected from %s", octstr_get_cstr(ip)); octstr_destroy(ip); cgw_receiver(conn, server); conn_destroy(server); } if (close(privdata->listening_socket) == -1) warning(errno, "smsc_cgw: couldn't close listening socket at shutdown"); gwthread_wakeup(privdata->sender_thread); }
/* The main program. */ int main(int argc, char **argv) { Connection *server; Octstr *line; Octstr **msgs; int i; int mptr, num_msgs; long num_received, num_sent; double first_received_at, last_received_at; double first_sent_at, last_sent_at; double start_time, end_time; double delta; int interactive, maxfd; char *cptr; char buffer[IN_BUFSIZE]; fd_set rset; struct timeval alarm; FILE *fp; gwlib_init(); setup_signal_handlers(); host = octstr_create("localhost"); start_time = get_current_time(); mptr = get_and_set_debugs(argc, argv, check_args); num_msgs = argc - mptr; interactive = 0; msgs = NULL; fp = NULL; if (num_msgs <= 0) { interactive = 1; num_msgs = 0; info(0, "Entering interactive mode. Type your message on the command line"); /* set up file pointer to stdin */ fp = stdin; /* initialize set for select */ FD_ZERO(&rset); } else { msgs = gw_malloc(sizeof(Octstr *) * num_msgs); for (i = 0; i < num_msgs; i ++) { msgs[i] = octstr_create(argv[mptr + i]); octstr_append_char(msgs[i], 10); /* End of line */ } info(0, "Host %s Port %d interval %.3f max-messages %ld", octstr_get_cstr(host), port, interval, max_send); srand((unsigned int) time(NULL)); } info(0, "fakesmsc starting"); server = conn_open_tcp(host, port, NULL); if (server == NULL) panic(0, "Failed to open connection"); num_sent = 0; num_received = 0; first_received_at = 0; first_sent_at = 0; last_received_at = 0; last_sent_at = 0; /* infinitely loop */ while (1) { /* Are we on interactive mode? */ if (interactive == 1) { /* Check if we need to clean things up beforehand */ if ( num_msgs > 0 ) { for (i = 0; i < num_msgs; i ++) octstr_destroy(msgs[i]); gw_free(msgs); num_msgs = 0; } /* we want either the file pointer or timer */ FD_SET(fileno(fp), &rset); /* get the largest file descriptor */ maxfd = fileno(fp) + 1; /* set timer to go off in 3 seconds */ alarm.tv_sec = IN_TIMEOUT; alarm.tv_usec = 0; if (select(maxfd, &rset, NULL, NULL, &alarm) == -1) goto over; /* something went off, let's see if it's stdin */ if (FD_ISSET(fileno(fp), &rset)) { /* stdin is readable */ cptr = fgets(buffer, IN_BUFSIZE, stdin); if( strlen( cptr ) < 2 ) goto rcv; } else { /* timer kicked in */ goto rcv; } num_msgs = 1; msgs = gw_malloc(sizeof(Octstr*)); msgs[0] = octstr_create(cptr); } /* if we still have something to send as MO message */ if (num_sent < max_send) { Octstr *os = choose_message(msgs, num_msgs); Octstr *msg = rnd > 0 ? randomize(os) : os; if (conn_write(server, msg) == -1) panic(0, "write failed"); ++num_sent; if (num_sent == max_send) info(0, "fakesmsc: sent message %ld", num_sent); else debug("send", 0, "fakesmsc: sent message %ld", num_sent); if (rnd > 0) octstr_destroy(msg); last_sent_at = get_current_time(); if (first_sent_at == 0) first_sent_at = last_sent_at; } rcv: do { delta = interval * num_sent - (get_current_time() - first_sent_at); if (delta < 0) delta = 0; if (num_sent >= max_send) delta = -1; conn_wait(server, delta); if (conn_error(server) || conn_eof(server) || sigint_received) goto over; /* read as much as the smsc module provides us */ while ((line = conn_read_line(server))) { last_received_at = get_current_time(); if (first_received_at == 0) first_received_at = last_received_at; ++num_received; if (num_received == max_send) { info(0, "Got message %ld: <%s>", num_received, octstr_get_cstr(line)); } else { debug("receive", 0, "Got message %ld: <%s>", num_received, octstr_get_cstr(line)); } octstr_destroy(line); } } while (delta > 0 || num_sent >= max_send); } over: conn_destroy(server); /* destroy the MO messages */ for (i = 0; i < num_msgs; i ++) octstr_destroy(msgs[i]); gw_free(msgs); end_time = get_current_time(); info(0, "fakesmsc: %ld messages sent and %ld received", num_sent, num_received); info(0, "fakesmsc: total running time %.1f seconds", end_time - start_time); delta = last_sent_at - first_sent_at; if (delta == 0) delta = .01; if (num_sent > 1) info(0, "fakesmsc: from first to last sent message %.1f s, " "%.1f msgs/s", delta, (num_sent - 1) / delta); delta = last_received_at - first_received_at; if (delta == 0) delta = .01; if (num_received > 1) info(0, "fakesmsc: from first to last received message %.1f s, " "%.1f msgs/s", delta, (num_received - 1) / delta); info(0, "fakesmsc: terminating"); return 0; }