void remote_client_command (RemoteClient* self, RemoteCallback callback, gpointer user_data, const gchar* format, ...) { RemoteClosure *closure = g_new0(RemoteClosure, 1); gchar* full_message; gchar* line; va_list ap; /* Add the response callback to our queue */ closure->callback = callback; closure->user_data = user_data; g_queue_push_head(self->response_queue, closure); /* Assemble the caller's formatted string */ va_start(ap, format); full_message = g_strdup_vprintf(format, ap); va_end(ap); /* Send a one-line command */ line = g_strdup_printf("%s\n", full_message); gnet_conn_write(self->gconn, line, strlen(line)); g_free(full_message); g_free(line); }
static void ob_client_func (GConn* conn, GConnEvent* event, gpointer user_data) { switch (event->type) { case GNET_CONN_READ: { event->buffer[event->length-1] = '\n'; gnet_conn_write (conn, event->buffer, event->length); /* fwrite (event->buffer, event->length, 1, stdout); */ gnet_conn_readline (conn); break; } case GNET_CONN_WRITE: { ; /* Do nothing */ break; } case GNET_CONN_CLOSE: case GNET_CONN_TIMEOUT: case GNET_CONN_ERROR: { gnet_conn_delete (conn); break; } default: g_assert_not_reached (); } }
/* Read line from stdin asynchronously */ static gboolean ob_in_iofunc (GIOChannel* iochannel, GIOCondition condition, gpointer data) { GConn* conn = (GConn*) data; /* Check for socket error */ if (condition & G_IO_ERR) { fprintf (stderr, "Error: Socket error\n"); goto error; } /* Check for data to be read (or if the stdin was closed (?)) */ if (condition & G_IO_IN) { GIOError error; gchar buffer[1024]; gsize bytes_read; /* Read the data into our buffer */ error = gnet_io_channel_readline (iochannel, buffer, sizeof(buffer), &bytes_read); /* Check for stdin error */ if (error != G_IO_ERROR_NONE) { fprintf (stderr, "Read error (%d)\n", error); goto error; } /* Check for EOF */ else if (bytes_read == 0) { if (lines_pending == 0) exit (EXIT_SUCCESS); else read_eof = TRUE; return FALSE; } /* Otherwise, we read something */ else { gnet_conn_write (conn, buffer, bytes_read); lines_pending++; } } return TRUE; error: exit (EXIT_FAILURE); return FALSE; }
static void remote_server_send_binary (RemoteServerConn* self, unsigned char* data, unsigned long length) { int write_size; remote_server_send_response(self, FYRE_RESPONSE_BINARY, "%d byte binary response", length); while (length > 0) { write_size = MIN(length, 4096); gnet_conn_write(self->gconn, data, write_size); length -= write_size; data += write_size; } }
static void remote_server_send_response (RemoteServerConn* self, int response_code, const char* response_message, ...) { gchar* full_message; gchar* line; va_list ap; va_start(ap, response_message); full_message = g_strdup_vprintf(response_message, ap); va_end(ap); line = g_strdup_printf("%d %s\n", response_code, full_message); gnet_conn_write(self->gconn, line, strlen(line)); g_free(full_message); g_free(line); }
static void ob_server_func (GServer* server, GServerStatus status, struct _GConn* conn, gpointer user_data) { switch (status) { case GNET_SERVER_STATUS_CONNECT: { if (debug) { LOG(LOG_DEBUG, "New connection from %s", gnet_inetaddr_get_canonical_name(conn->inetaddr)); } conn->func = ob_client_func; conn->user_data = calloc(1, sizeof(struct conn_stat)); gnet_conn_write (conn, strdup(hello), strlen(hello), 0); gnet_conn_readline (conn, NULL, 1024, 30000); break; } case GNET_SERVER_STATUS_ERROR: { gnet_server_delete (server); exit (1); break; } } }
int ob_client_func (GConn* conn, GConnStatus status, gchar* buffer, gint length, gpointer user_data) { struct conn_stat *cs = (struct conn_stat *)user_data; int i = length-1; while (i > 0 && isspace(buffer[i])) { buffer[i--] = 0; } switch (status) { case GNET_CONN_STATUS_READ: { //fprintf(stderr, "user_data: %d %s %s\n", cs->state, cs->user, cs->pwd); if (cs->state != STATE_DATA && !strncasecmp(buffer, "QUIT", 4)) { gnet_conn_write (conn, strdup(bye), strlen(bye), 0); cs->state = STATE_EXIT; break; } switch (cs->state) { case STATE_INIT: cs->state = STATE_USER; case STATE_USER: if (strncasecmp(buffer, "USER ", 5)) { gnet_conn_write (conn, strdup(errhello), strlen(errhello), 0); } else { strncpy(cs->user, buffer+5, sizeof(cs->user)); cs->state = STATE_PWD; gnet_conn_write (conn, strdup(askpwd), strlen(askpwd), 0); } break; case STATE_PWD: if (strncasecmp(buffer, "PASS ", 5)) { gnet_conn_write (conn, strdup(erraskpwd), strlen(erraskpwd), 0); break; } strncpy(cs->pwd, buffer+5, sizeof(cs->pwd)); /* check the password first */ if (!uw_password_ok(cs->user, cs->pwd)) { LOG(LOG_NOTICE, "%s: password failure (%s/%s)", gnet_inetaddr_get_canonical_name(conn->inetaddr), cs->user, cs->pwd); gnet_conn_write (conn, strdup(errbye), strlen(errbye), 0); cs->state = STATE_EXIT; break; } cs->state = STATE_DATA; cs->sp_info = spool_open(OPT_ARG(SPOOLDIR), OPT_ARG(OUTPUT)); if (cs->sp_info == NULL) { gnet_conn_write (conn, strdup(spoolerr), strlen(spoolerr), 0); cs->state = STATE_EXIT; break; } gnet_conn_write (conn, strdup(askdata), strlen(askdata), 0); break; case STATE_DATA: if (!strncasecmp(buffer, ".", 1)) { char *targfilename = strdup(spool_targfilename(cs->sp_info)); if (!spool_close(cs->sp_info, TRUE)) { gnet_conn_write (conn, strdup(spoolerr), strlen(spoolerr), 0); } else { if (debug) LOG(LOG_DEBUG, "spooled to %s", targfilename); } free(targfilename); gnet_conn_write (conn, strdup(bye), strlen(bye), 0); cs->state = STATE_EXIT; } else { if (!spool_printf(cs->sp_info, "%s\n", buffer)) { spool_close(cs->sp_info, FALSE); cs->state = STATE_ERROR; } } break; case STATE_ERROR: if (!strncasecmp(buffer, ".", 1)) { gnet_conn_write (conn, strdup(errbye), strlen(errbye), 0); cs->state = STATE_EXIT; } } break; } case GNET_CONN_STATUS_WRITE: { g_free (buffer); if (cs->state == STATE_EXIT) { if (debug) { LOG(LOG_DEBUG, "%s closed connection", gnet_inetaddr_get_canonical_name(conn->inetaddr)); } free(conn->user_data); gnet_conn_delete (conn, TRUE); } break; } case GNET_CONN_STATUS_CLOSE: case GNET_CONN_STATUS_TIMEOUT: case GNET_CONN_STATUS_ERROR: { if (cs->state == STATE_DATA || cs->state == STATE_ERROR) { spool_close(cs->sp_info, FALSE); if (debug) { LOG(LOG_DEBUG, "%s unexpected end of input", gnet_inetaddr_get_canonical_name(conn->inetaddr)); } } else { if (debug && cs->state != STATE_DATA) { LOG(LOG_DEBUG, "%s unexpected close", gnet_inetaddr_get_canonical_name(conn->inetaddr)); } } free(conn->user_data); gnet_conn_delete (conn, TRUE); break; } default: g_assert_not_reached (); } return TRUE; /* TRUE means read more if status was read, otherwise its ignored */ }
void send_to(gpointer data, gpointer user_data){ struct _GConn* conn = (struct _GConn*) data; gchar* buf = (gchar*) user_data; gchar* buf2 = g_memdup (buf, (guint) strlen(buf)+1); gnet_conn_write( conn, buf2, (gint) strlen(buf2)); }