static void errorOnResponse(MSGPTR pMsg, u_char *msg, int len, size_t err) { u_short errmsg[6] ; u_short flags ; u_short *ps ; TRACE("errorOnResponse %d\n", err) ; ps = (u_short *) msg ; flags = ntohs(ps[1]) ; flags = ((flags & 0x7fff) | 0x8000) ; /* QR to response */ flags = ((flags & 0xfff8) | (err & 0x0007)) ; /* set RCODE */ memset(errmsg, 0, sizeof(errmsg)) ; errmsg[0] = htons(pMsg->orgId) ; errmsg[1] = htons(flags) ; server_response(&pMsg->from, pMsg->proto, (u_char *) errmsg, sizeof(errmsg)) ; }
/* * TASK to handle an incoming request */ int server_request (void *parm) { SERVERPARM *s; DBUF *req, *res; char *curl; s = (SERVERPARM *) parm; curl = xml_get_text (s->xml, "Phineas.Console.Url"); res = NULL; while ((req = server_receive (s->conn)) != NULL) { debug ("received %d bytes\n", dbuf_size (req)); if (dbuf_size (req) == 0) { dbuf_free (req); net_close (s->conn); return (-1); } dbuf_putc (req, 0); /* * log the request, but filter out GET requests for the console... * noise */ if (!(*curl && strstarts (dbuf_getbuf (req) + 4, curl))) server_logrequest (s->conn, dbuf_size (req), dbuf_getbuf (req)); if ((res = server_response (s->xml, dbuf_getbuf (req))) == NULL) { res = server_respond (500, "<h3>Failure processing ebXML request</h3>"); } server_header (res); net_write (s->conn, dbuf_getbuf (res), dbuf_size (res)); dbuf_free (res); dbuf_free (req); } net_close (s->conn); debug ("request completed\n"); return (0); }
/* This is the main fuction where server starts and initializes the socket * for listening the client. * Input: * inputs are specied through commnad line arguments in which * -p : specifies the port_no * -d : specifies the export directory * * Output: * int : return value: */ int main (int argc, char* argv []) { int socket_fd = 0; int ret = -1; int client_fd = 0; int port = 0; int max_thread = 3; int next_option; pthread_t pid, t_id; struct sockaddr_in client_addr; C_Request* c_req; /* Get the linked list and initialize it */ fd_node = NULL; /*Command line options*/ const char* const short_option = "p:d:"; const struct option long_option [] = { { "port", 1, NULL, 'p'}, { "destination",1, NULL, 'd'}, { "NULL", 0, NULL, 0 } }; do { next_option = getopt_long (argc, argv, short_option, long_option, NULL); switch (next_option) { case 'p': port = atoi (optarg); break; case 'd': exp_dir = (char*)malloc(strlen (optarg + 1)); strcpy (exp_dir, optarg); break; case -1: break; default: abort (); } } while (next_option != -1); printf ("\tPress ctrl+c to exit\n"); /*Creates the server socket*/ pthread_create (&pid, NULL, &sftp_rpc_server_init, &port); pthread_join (pid, (void*) &socket_fd); if (socket_fd < 0) { ret = -1; goto out; } /* Create the thread_pool and initialize the list */ pthread_create (&t_id, NULL, &create_thread_pool, &max_thread); pthread_join (t_id, (void*) &pool); while (1) { /*Accept the incomming client connection*/ client_fd = sftp_rpc_server_start (socket_fd); if (client_fd < 0) { ret = -1; goto out; } printf ("\tClient : %d \t had connected\n", client_fd); /*Giving the service to client*/ c_req = (C_Request*) calloc (1, sizeof (C_Request)); server_response (client_fd, pool, c_req); } ret = 0; out: close (socket_fd); return ret; }
static void messageResponse(MSGPTR pMsg, u_char *msg, int len) { u_short *p ; u_char buff[1024] ; u_char *bbase ; size_t bleng ; size_t cleng ; size_t cstat ; TRACE("messageResponse - %04x <- %04x\n", pMsg->orgId, pMsg->newId) ; /* * prepare conversion buffer */ if (len < sizeof(buff) / 2) { bbase = buff ; bleng = sizeof(buff) ; } else { bbase = malloc(len * 2) ; bleng = len * 2 ; } if (bbase == NULL) { WARN("messageResponse - cannot prepare conversion buffer\n") ; return ; } /* * translate message (domain names) */ TRACE("messageResponse - translate response\n") ; cstat = translate_reply(&pMsg->trctx, msg, len, bbase, bleng, &cleng) ; TRACE("messageResponse - translated status %d length %d\n", cstat, cleng) ; if (cstat != 0) { /* error on conversion */ WARN("messageResponse - translation error %d\n", cstat) ; errorOnResponse(pMsg, msg, len, cstat) ; return ; } if (pMsg->proto == SOCK_DGRAM && cleng > 512) { WARN("messageResponse - translation overflow %d\n", cleng) ; errorOnResponse(pMsg, msg, len, 2) ; return ; } /* * reply back to requester */ p = (u_short *) bbase ; p[0] = htons(pMsg->orgId) ; server_response(&pMsg->from, pMsg->proto, bbase, cleng) ; /* * cleanup buffer */ if (bbase != buff) { free(bbase) ; } }