int writemessage(int sock,char *buffer,int msglen,int to,int fromchild) { int retcode ; char ent[40] ; int nfds ; fd_set selectmask ; /* Select mask */ struct timeval wait_timer; int msgsize ; int msgsend ; int nbsent ; if ( fromchild == 1 ) { sprintf(ent,"Child %06d : ",getpid()) ; } else { strcpy(ent,"") ; } nbsent = 0 ; msgsize = msglen ; /* ** start the writing loop */ while ( nbsent < msgsize ) { nfds = sysconf(_SC_OPEN_MAX) ; FD_ZERO(&selectmask) ; FD_SET(sock,&selectmask) ; wait_timer.tv_sec = to ; wait_timer.tv_usec = 0 ; if ( (retcode = select(FD_SETSIZE,0,&selectmask,0,&wait_timer) ) == -1 ) { /* ** Select error */ if (debug ) printmessage(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Select error : MSG (%d,%d)\n",ent,msglen,nbsent) ; return -1 ; } else if ( retcode == 0 ) { if (debug ) printmessage(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Time Out : MSG (%d,%d)\n",ent,msglen,nbsent) ; return -1 ; } else { msgsend = write(sock,&buffer[nbsent],msgsize-nbsent) ; if ( msgsend < 0 ) { if (debug ) printmessage(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Send error %d(%s) : MSG (%d,%d)\n",ent,errno,strerror(errno),msglen,nbsent) ; return -1 ; } else if (msgsend == 0 ) { if (debug ) printmessage(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Connection breaks : MSG (%d,%d)\n",ent,msglen,nbsent) ; return -1 ; } else { nbsent = nbsent + msgsend ; } } } return(0) ; }
/** * \brief Start the minstack_tcp in parameter * \param mt the minstack_tcp that have to be started * \return 0 if the minstack_tcp stack started correctly */ int minstack_tcp_start(minstack_tcp *mt) { int retval = 0; if (!mt || mt->status != IDLE) { printerror("Cannot start while already started\n"); return -1; } switch (mt->type) { case NONE: printwarning("the minstack_tcp is not initialized\n"); return -1; case SERVER: retval = minstack_tcp_boot_server(mt); break; case CLIENT: retval = minstack_tcp_boot_client(mt); break; default: printerror("Unknow enum type %d\n",mt->type); break; } if (!retval) { mt->status = STARTED; printmessage("%s is now started\n",mt->name); } return retval; }
int minstack_default_new_connection_callback(int cid, struct sockaddr_in *cli_addr) { printmessage("Accepted client: %s:%d socket[%d]\n", inet_ntoa(cli_addr->sin_addr), ntohs(cli_addr->sin_port),cid); return 0; }
int main() { system ("title "); system("color f0"); char nextcheque, compname[50], name[50]; int randNo; do { system ("cls"); printmessage (); // function call srand (time (NULL)); randNo = 100000 + rand () % (1000000-100000+1); // generate random numbers printcheque (compname, randNo, name); // function call printf ("\n\n\n"); printf ("Anymore cheque? > "); fflush (stdin); scanf ("%c", &nextcheque); printf ("\n"); } while (nextcheque == 'Y' || nextcheque == 'y'); printf ("Thank you for your cooperation.\n\n"); system("pause"); return 0; }
/** * \brief Stop the minstack_tcp in parameter * \param mt the minstack_tcp that have to be stopped * \return 0 if the minstack_tcp stack stopped correctly */ int minstack_tcp_stop(minstack_tcp *mt) { if (!mt || mt->status == IDLE) { printerror("Cannot stop while not started\n"); return -1; } #ifdef NOT_CANCEL_THREAD_WHEN_STOPPING if (&mt->pthread_accept_thread && !mt->pthread_accept_thread_stop) { printdebug("The accepting thread is asked to stop\n"); mt->pthread_accept_thread_stop = 1; pthread_join(mt->pthread_accept_thread, NULL); printmessage("The accepting thread stopped\n"); } if (&mt->pthread_reading_thread && !mt->pthread_reading_thread_stop) { printdebug("The reading thread is asked to stop\n"); mt->pthread_reading_thread_stop = 1; pthread_join(mt->pthread_reading_thread, NULL); printmessage("The reading thread stopped\n"); } #else if (&mt->pthread_accept_thread && !mt->pthread_accept_thread_stop) { printdebug("The accepting thread is asked to stop\n"); mt->pthread_accept_thread_stop = 1; //Check if & or not after if(&mt->pthread_accept_thread) pthread_cancel(mt->pthread_accept_thread); printmessage("The accepting thread stopped\n"); } if (&mt->pthread_reading_thread && !mt->pthread_reading_thread_stop) { printdebug("The reading thread is asked to stop\n"); mt->pthread_reading_thread_stop = 1; //Check if & or not after if(&mt->pthread_reading_thread) pthread_cancel(mt->pthread_reading_thread); printmessage("The reading thread stopped\n"); } #endif if (mt->listen_socket_fd) minstack_close(mt->listen_socket_fd); pthread_mutex_destroy(&mt->mutex); mt->status = IDLE; printmessage("%s is now stopped\n",mt->name); return 0; }
/* * take ressources because pthread_cancel do not free the memory allocation * but we have to cancel the thread because we can stay in accept wait event if we try to use a non blocked socket */ void *minstack_tcp_accept_thread(void *ptr) { minstack_tcp *mt = (minstack_tcp *) ptr; pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); printdebug("starting %s\n",__FUNCTION__); while (!mt->pthread_accept_thread_stop) { int tmp_clisockfd; unsigned int cli_len; struct sockaddr_in cli_addr; memset((char *) &cli_addr, 0, sizeof(cli_addr)); cli_len = sizeof(cli_addr); #ifdef WIN32 tmp_clisockfd = accept(mt->listen_socket_fd, (struct sockaddr *) &cli_addr,(int *) &cli_len); #else tmp_clisockfd = accept(mt->listen_socket_fd, (struct sockaddr *) &cli_addr, &cli_len); #endif if (tmp_clisockfd == -1) { usleep(mt->receive_loop_usleep); continue; } else if (tmp_clisockfd <= 0) { printerror("error :%d\n",tmp_clisockfd); printmoreerror("ERROR on accept:"); break; } pthread_mutex_lock(&mt->mutex); mt->sockets.client_socket_fd[mt->sockets.connected_client_nb] = tmp_clisockfd; mt->sockets.connected_client_nb++; pthread_mutex_unlock(&mt->mutex); mt->new_connection_callback(tmp_clisockfd, &cli_addr); if (mt->pthread_reading_thread_stop && pthread_create( &mt->pthread_reading_thread, NULL, minstack_tcp_reading_thread, (void *) mt)) { printwarning("pthread_create minstack_tcp_accept_thread error\n"); continue; } } //TODO what is this ?!!! //before was //if (mt->pthread_reading_thread && !mt->pthread_reading_thread) { if (&mt->pthread_reading_thread && !mt->pthread_reading_thread_stop){ mt->pthread_reading_thread_stop = 1; pthread_join(mt->pthread_reading_thread, NULL); printmessage("The reading thread stopped\n"); } printdebug("stopping %s\n",__FUNCTION__); pthread_exit(NULL); return NULL; }
int main(void) { int counter; int maxval=-1; datastruct* svalues[200]; for(counter=0;counter<200;counter++) { svalues[counter]=getinput(); if(!svalues[counter]) break; maxval=counter; } printmessage(svalues[maxval/2]); return 0; }
/** * \brief Uninitialized a minstack_tcp * \param mt is the minstack_tcp stack */ void minstack_tcp_uninit(minstack_tcp *mt) { if (!mt) { printwarning("Trying to uninit a NULL minstack_tcp\n"); return; } if (mt->status == STARTED) { minstack_tcp_stop(mt); printdebug("%s minstack has TCP been stopped before uninit\n",mt->name); } printmessage("%s minstack TCP has been uninitialized\n",mt->name); free(mt); #ifdef WIN32 win32_uninit_socket_api(); #endif }
/** * \brief Generate a minstack_tcp * \param nickname is the name given to the minstack_tcp could be NULL * \return the minstack_tcp generated */ minstack_tcp * minstack_tcp_init(const char *nickname) { #ifdef WIN32 win32_init_socket_api(); #endif minstack_tcp *mt = (minstack_tcp *) calloc(1, sizeof(minstack_tcp)); if (mt == NULL) { printerror("Could not allocate a minstack_tcp\n"); return NULL; } mt->name = nickname; mt->type = NONE; mt->status = IDLE; mt->pthread_reading_thread_stop = 1; printmessage("%s minstack TCP has been initialized\n",mt->name); return mt; }
/** * \brief Write the message with the length len_message to the cid * \param mt is the minstack_tcp stack * \param cid is the client ID to whom send the message * \param message the message to send * \param len_message the message length * \return 0 if the minstack_tcp stack stopped correctly */ int minstack_tcp_write(minstack_tcp *mt, int cid, char *message, int len_message) { if (!mt) { printerror("the minstack_tcp is NULL\n"); return -100; } //if (write(cid, message, len_message) < 0) { if(send(cid, message, len_message,0) < 0){ #ifdef WIN32 printerror("ERROR writing to socket %d with erro :%d\n",cid,WSAGetLastError()); #else printerror("ERROR writing to socket %d\n",cid); #endif return -1; } printmessage("%s sent:%s length:%d to cid:%d\n",mt->name,message,len_message,cid); return 0; }
void ep_app_error( const char *fmt, ...) { va_list av; va_start(av, fmt); printmessage("ERROR", EpVid->vidfgwhite, EpVid->vidbgred, fmt, av); va_end(av); if (EP_UT_BITSET(EP_APP_FLAG_LOGERRORS, OperationFlags)) { va_start(av, fmt); ep_logv(EP_STAT_ERROR, fmt, av); va_end(av); } }
void ep_app_warn( const char *fmt, ...) { va_list av; va_start(av, fmt); printmessage("WARNING", EpVid->vidfgblack, EpVid->vidbgyellow, fmt, av); va_end(av); if (EP_UT_BITSET(EP_APP_FLAG_LOGWARNINGS, OperationFlags)) { va_start(av, fmt); ep_logv(EP_STAT_WARN, fmt, av); va_end(av); } }
int main(int argc, char *argv[]) { char *message; if (argc < 2) { printf ("usage: %s <message>\n", argv[0]); return(1); } message = argv[1]; if (!printmessage(message)) { printf("%s: No puedo imprimir su Mensaje \n", argv[0]); return(1); } printf("Mensaje entregado\n"); return(0); }
void getinput(){ fgets(message, sizeof message, stdin); if (*message == '\n'){ fputs("enter message: ", stdout); fflush(stdout); return; } strcpy(concatenated, nick); strcat(concatenated, ": "); strcat(concatenated, message); /* sends the package a bunch of times, this is just fire and forget */ for (i = 10; i > 0; i--){ send_beaconpacket(concatenated); } printmessage(); return; }
void ep_app_info( const char *fmt, ...) { va_list av; errno = 0; va_start(av, fmt); printmessage("INFO", EpVid->vidfgblack, EpVid->vidbgcyan, fmt, av); va_end(av); if (EP_UT_BITSET(EP_APP_FLAG_LOGINFOS, OperationFlags)) { va_start(av, fmt); ep_logv(EP_STAT_OK, fmt, av); va_end(av); } }
int main(int argc, char *argv[]) { char *mes; retorno *vuelta; if (argc < 2) { printf( "uso: %s <message>\n", argv[0]); return(1); } mes=argv[1]; vuelta = printmessage(mes); if (! vuelta->cod_ret ) { printf("%s: No puedo grabar su Mensaje \n", argv[0]); return(1); } printf("Mensaje entregado\n"); printf("Respuesta: %s \n",vuelta->retorno_u.message); return(0); }
void ep_app_fatal( const char *fmt, ...) { va_list av; va_start(av, fmt); printmessage("FATAL", EpVid->vidfgyellow, EpVid->vidbgred, fmt, av); va_end(av); if (EP_UT_BITSET(EP_APP_FLAG_LOGFATALS, OperationFlags)) { va_start(av, fmt); ep_logv(EP_STAT_SEVERE, fmt, av); va_end(av); } fprintf(stderr, "\t(exiting)\n"); exit(1); /*NOTREACHED*/ }
void ep_app_abort( const char *fmt, ...) { va_list av; va_start(av, fmt); printmessage("ABORT", EpVid->vidfgyellow, EpVid->vidbgred, fmt, av); va_end(av); if (EP_UT_BITSET(EP_APP_FLAG_LOGABORTS, OperationFlags)) { va_start(av, fmt); ep_logv(EP_STAT_ABORT, fmt, av); va_end(av); } fprintf(stderr, "\n\t(exiting)\n"); abort(); /*NOTREACHED*/ }
int sendproto() { char buffer[8] ; struct message *msg ; int remoteprotomin ; int remoteprotomax ; fd_set selectmask ; /* Select mask */ int nfds ; /* Max number of file descriptor */ int code ; int retcode ; int msglen ; if ( debug ) printmessage(stdout,CASE_NORMAL,0,timestamp,"Sending protocol request\n") ; /* ** Now send the control message : First part */ msg = (struct message *)buffer ; msg->code = MSG_PROT ; msg->msglen = 0 ; if ( writemessage(outcontrolsock,buffer,MINMESSLEN,sendcontrolto,0) < 0 ) { /* ** We were not able to send the minimum message so ** we are going to close the control socket and to ** tell the calling program to restart a connection */ printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT"); return -1 ; /* restart connection */ } if ( debug ) printmessage(stdout,CASE_NORMAL,0,timestamp,"Sent message %x\n", msg->code) ; /* ** Now we are going to wait for the message on the control ** connection */ waitcontrol: nfds = sysconf(_SC_OPEN_MAX) ; FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; retcode = select(FD_SETSIZE,&selectmask,0,0,0) ; if ( retcode < 0 ) { /* ** Select error */ if ( errno != EINTR ) { /* ** we have got an error so close the connection ** and restart */ printmessage(stderr,CASE_ERROR,66,timestamp,"Error select on control connection : %s\n",strerror(errno)); return -1 ; } else { /* ** Interrupted by a signal */ FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; goto waitcontrol ; } } else if ( retcode == 0 ) { /* ** Impossible we do not set any timer */ FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; goto waitcontrol ; } else { /* ** read the message */ if ( readmessage(incontrolsock,buffer,MINMESSLEN,recvcontrolto,0) < 0 ) { printmessage(stderr,CASE_ERROR,61,timestamp,"Error waiting %s message\n","MSG_PROT_ANS"); return -1 ; } msg = (struct message *)buffer ; code = msg->code ; if ( code == MSG_BAD || code == MSG_BAD_NO_RETRY) { /* ** The server does not understand protocol */ printmessage(stderr,CASE_FATAL_ERROR,101,timestamp,"Incompatible deamon and client (remote protocol version (%d,%d), local (%d,%d))\n",1,1,protocolmin,protocolmax); } else if (msg->code == MSG_PROT_ANS ) { /* ** At this stage */ #ifndef WORDS_BIGENDIAN msglen = ntohl(msg->msglen) ; #else msglen = msg->msglen ; #endif if ( msglen != 8 ) { printmessage(stderr,CASE_ERROR,63,timestamp,"Unexpected message length while waiting for %s message\n","MSG_PROT_ANS"); return -1 ; } if ( readmessage(incontrolsock,buffer,msglen,recvcontrolto,0) < 0) { printmessage(stderr,CASE_ERROR,67,timestamp,"Error reading data for %s message\n","MSG_PROT_ANS"); return -1 ; } #ifndef WORDS_BIGENDIAN remoteprotomin = ntohl(msg->code) ; remoteprotomax = ntohl(msg->msglen) ; #else remoteprotomin = msg->code ; remoteprotomax = msg->msglen ; #endif if ( (remoteprotomax < protocolmin) || (remoteprotomin > protocolmax) ) { /* ** Imcompatible version */ msg->code = MSG_BAD_NO_RETRY ; msg->msglen = 0 ; writemessage(outcontrolsock,buffer,MINMESSLEN,recvcontrolto,0); printmessage(stderr,CASE_FATAL_ERROR,101,timestamp,"Incompatible deamon and client (remote protocol version (%d,%d), local (%d,%d))\n",remoteprotomin,remoteprotomax,protocolmin,protocolmax); } else if (remoteprotomax <= protocolmax ) { protocol = remoteprotomax ; } else { protocol = protocolmax ; } msg->code = MSG_PROT_ANS ; #ifndef WORDS_BIGENDIAN msg->msglen = ntohl(4) ; #else msg->msglen = 4 ; #endif if ( writemessage(outcontrolsock,buffer,MINMESSLEN,recvcontrolto,0) < 0 ){ printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT_ANS"); return -1 ; /* restart connection */ } #ifndef WORDS_BIGENDIAN msg->code = ntohl(protocol) ; #else msg->code = protocol ; #endif if ( writemessage(outcontrolsock,buffer,4,recvcontrolto,0) < 0 ){ printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_PROT_ANS"); return -1 ; /* restart connection */ } return 0 ; } else { /* ** Receive unkwown message so something is ** going wrong. close the control socket ** and restart */ printmessage(stderr,CASE_ERROR,62,timestamp,"Unknown message while waiting for %s message\n","MSG_PROT_ANS"); return -1 ; } } /* ** Never reach this point but to in order to avoid stupid messages ** from IRIX compiler set a return code to -1 */ return -1 ; }
void DreamGenContext::printmessage() { printmessage(di, bx, al, dl, (bool)(dl & 1)); }
int bbftp_list(char *line,char **filelist,int *filelistlen,int *errcode) { char minbuffer[MINMESSLEN] ; int msglen ; int code ; struct message *msg ; struct mess_integer *msg_integer ; fd_set selectmask ; /* Select mask */ int nfds ; /* Max number of file descriptor */ char *buffer ; int retcode ; if ( verbose) printmessage(stdout,CASE_NORMAL,0,timestamp,">> COMMAND : list %s\n",line) ; msg = (struct message *)minbuffer ; msg->code = MSG_LIST_V2 ; #ifndef WORDS_BIGENDIAN msg->msglen = ntohl(strlen(line)+sizeof(int)) ; #else msg->msglen = strlen(line)+sizeof(int) ; #endif if ( writemessage(outcontrolsock,minbuffer,MINMESSLEN,sendcontrolto,0) < 0 ) { /* ** We were not able to send the minimum message so ** we are going to close the control socket and to ** tell the calling program to restart a connection */ printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_LIST_V2"); *errcode = 64 ; bbftp_free_all_var() ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; /* restart connection */ } /* ** Sending transfer options */ msg_integer = (struct mess_integer*)minbuffer ; msg_integer->myint = transferoption ; if ( writemessage(outcontrolsock,minbuffer,sizeof(int),sendcontrolto,0) < 0 ) { printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_LIST_V2 (transferoption)"); *errcode = 64 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; /* restart connection */ } /* ** Directory name */ if ( writemessage(outcontrolsock,line,strlen(line),sendcontrolto,0) < 0 ) { printmessage(stderr,CASE_ERROR,64,timestamp,"Error sending %s message\n","MSG_LIST_V2 (directory name)"); *errcode = 64 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; /* restart connection */ } /* ** Now we are going to wait for the message on the control ** connection */ waitcontrol: nfds = sysconf(_SC_OPEN_MAX) ; FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; retcode = select(FD_SETSIZE,&selectmask,0,0,0) ; if ( retcode < 0 ) { /* ** Select error */ if ( errno != EINTR ) { /* ** we have got an error so close the connection ** and restart */ printmessage(stderr,CASE_ERROR,66,timestamp,"Error select on control connection : %s\n",strerror(errno)); *errcode = 66 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; } else { /* ** Interrupted by a signal */ FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; goto waitcontrol ; } } else if ( retcode == 0 ) { /* ** Impossible we do not set any timer */ FD_ZERO(&selectmask) ; FD_SET(incontrolsock,&selectmask) ; goto waitcontrol ; } else { /* ** read the message */ if ( readmessage(incontrolsock,minbuffer,MINMESSLEN,recvcontrolto,0) < 0 ) { printmessage(stderr,CASE_ERROR,61,timestamp,"Error waiting %s message\n","MSG_OK (on MSG_LIST_V2)"); *errcode = 61 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; } code = msg->code ; if ( code == MSG_BAD || code == MSG_BAD_NO_RETRY) { /* ** The change directory has failed so ... */ #ifndef WORDS_BIGENDIAN msglen = ntohl(msg->msglen) ; #else msglen = msg->msglen ; #endif if ( (buffer = (char *) malloc(msglen+1) ) == NULL) { printmessage(stderr,CASE_ERROR,35,timestamp,"Error allocating memory for %s : %s\n","buffer (bbftp_list)",strerror(errno)) ; *errcode = 35 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; } if ( readmessage(incontrolsock,buffer,msglen,recvcontrolto,0) < 0 ) { printmessage(stderr,CASE_ERROR,67,timestamp,"Error reading data for %s message\n","MSG_BAD (on MSG_LIST_V2)"); *errcode = 67 ; bbftp_close_control() ; free(buffer) ; if ( code == MSG_BAD ) { return BB_RET_CONN_BROKEN ; } else { return BB_RET_FT_NR_CONN_BROKEN ; } } else { buffer[msglen] = '\0' ; printmessage(stderr,CASE_ERROR,100,timestamp,"%s\n",buffer) ; if (verbose) printmessage(stdout,CASE_NORMAL,0,timestamp,"<< %s\n",buffer) ; free(buffer) ; if ( code == MSG_BAD ) { *errcode = 100 ; return BB_RET_ERROR; } else { /* ** In case of no retry we are going to wait 2 seconds ** in order for the server to finish its cleaning (in ** fact if the error message is du to a server child ** abnormal termination, the message is sent on the death ** of the first child and the server has some cleaning ** to do). */ sleep(2) ; *errcode = 100 ; return BB_RET_FT_NR ; } } } else if (msg->code == MSG_LIST_REPL_V2 ) { /* ** At this stage the transfer is OK but we need ** the answer because it gives all file names */ #ifndef WORDS_BIGENDIAN msglen = ntohl(msg->msglen) ; #else msglen = msg->msglen ; #endif if ( msglen == 0 ) { /* ** There was no file corresponding */ if ( verbose) printmessage(stdout,CASE_NORMAL,0,timestamp,"<< OK : no file\n") ; *filelistlen = 0 ; return BB_RET_OK ; } else { if ( ((*filelist) = (char *) malloc(msglen+1) ) == NULL) { printmessage(stderr,CASE_ERROR,35,timestamp,"Error allocating memory for %s : %s\n","filelist (bbftp_list)",strerror(errno)) ; *errcode = 35 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; } if ( readmessage(incontrolsock,*filelist,msglen,recvcontrolto,0) < 0) { printmessage(stderr,CASE_ERROR,67,timestamp,"Error reading data for %s message\n","MSG_LIST_REPL_V2 (on MSG_LIST_V2)"); *errcode = 67 ; bbftp_close_control() ; free(*filelist) ; return BB_RET_CONN_BROKEN ; } (*filelist)[msglen] = '\0' ; *filelistlen = msglen ; if ( verbose ) printmessage(stdout,CASE_NORMAL,0,timestamp,"<< OK\n") ; return BB_RET_OK ; } } else { /* ** Receive unkwown message so something is ** going wrong. close the control socket ** and restart */ printmessage(stderr,CASE_ERROR,62,timestamp,"Unknown message while waiting for %s message\n","MSG_OK (on MSG_MKDIR_V2)"); *errcode = 62 ; bbftp_close_control() ; return BB_RET_CONN_BROKEN ; } } }
int main(int argc, char** args) { int id = printmessage(); return 0; }
int minstack_default_connection_closed_server_callback(sockets *socket, int cid) { printmessage("The client closed the socket %d\n",cid); minstack_tcp_delete_cid(socket, cid); return 0; }
static void process_message(isc_buffer_t *source) { dns_message_t *message; isc_result_t result; int i; message = NULL; result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message); CHECKRESULT(result, "dns_message_create failed"); result = dns_message_parse(message, source, parseflags); if (result == DNS_R_RECOVERABLE) result = ISC_R_SUCCESS; CHECKRESULT(result, "dns_message_parse failed"); result = printmessage(message); CHECKRESULT(result, "printmessage() failed"); if (printmemstats) isc_mem_stats(mctx, stdout); if (dorender) { unsigned char b2[64 * 1024]; isc_buffer_t buffer; dns_compress_t cctx; isc_buffer_init(&buffer, b2, sizeof(b2)); /* * XXXMLG * Changing this here is a hack, and should not be done in * reasonable application code, ever. */ message->from_to_wire = DNS_MESSAGE_INTENTRENDER; for (i = 0; i < DNS_SECTION_MAX; i++) message->counts[i] = 0; /* Another hack XXX */ result = dns_compress_init(&cctx, -1, mctx); CHECKRESULT(result, "dns_compress_init() failed"); result = dns_message_renderbegin(message, &cctx, &buffer); CHECKRESULT(result, "dns_message_renderbegin() failed"); result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0); CHECKRESULT(result, "dns_message_rendersection(QUESTION) failed"); result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0); CHECKRESULT(result, "dns_message_rendersection(ANSWER) failed"); result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0); CHECKRESULT(result, "dns_message_rendersection(AUTHORITY) failed"); result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0); CHECKRESULT(result, "dns_message_rendersection(ADDITIONAL) failed"); dns_message_renderend(message); dns_compress_invalidate(&cctx); message->from_to_wire = DNS_MESSAGE_INTENTPARSE; dns_message_destroy(&message); printf("Message rendered.\n"); if (printmemstats) isc_mem_stats(mctx, stdout); result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message); CHECKRESULT(result, "dns_message_create failed"); result = dns_message_parse(message, &buffer, parseflags); CHECKRESULT(result, "dns_message_parse failed"); result = printmessage(message); CHECKRESULT(result, "printmessage() failed"); } dns_message_destroy(&message); }
ATF_TC_BODY(tsig_tcp, tc) { const dns_name_t *tsigowner = NULL; dns_fixedname_t fkeyname; dns_message_t *msg = NULL; dns_name_t *keyname; dns_tsig_keyring_t *ring = NULL; dns_tsigkey_t *key = NULL; isc_buffer_t *buf = NULL; isc_buffer_t *querytsig = NULL; isc_buffer_t *tsigin = NULL; isc_buffer_t *tsigout = NULL; isc_result_t result; unsigned char secret[16] = { 0 }; dst_context_t *tsigctx = NULL; dst_context_t *outctx = NULL; UNUSED(tc); result = dns_test_begin(stderr, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); /* isc_log_setdebuglevel(lctx, 99); */ dns_fixedname_init(&fkeyname); keyname = dns_fixedname_name(&fkeyname); result = dns_name_fromstring(keyname, "test", 0, NULL); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = dns_tsigkeyring_create(mctx, &ring); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = dns_tsigkey_create(keyname, dns_tsig_hmacsha256_name, secret, sizeof(secret), ISC_FALSE, NULL, 0, 0, mctx, ring, &key); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); /* * Create request. */ result = isc_buffer_allocate(mctx, &buf, 65535); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); render(buf, 0, key, &tsigout, &querytsig, NULL); isc_buffer_free(&buf); /* * Create response message 1. */ result = isc_buffer_allocate(mctx, &buf, 65535); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); render(buf, DNS_MESSAGEFLAG_QR, key, &querytsig, &tsigout, NULL); /* * Process response message 1. */ result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &msg); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_create: %s", dns_result_totext(result)); result = dns_message_settsigkey(msg, key); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_settsigkey: %s", dns_result_totext(result)); result = dns_message_parse(msg, buf, 0); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_parse: %s", dns_result_totext(result)); printmessage(msg); result = dns_message_setquerytsig(msg, querytsig); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_setquerytsig: %s", dns_result_totext(result)); result = dns_tsig_verify(buf, msg, NULL, NULL); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_tsig_verify: %s", dns_result_totext(result)); ATF_CHECK_EQ(msg->verified_sig, 1); ATF_CHECK_EQ(msg->tsigstatus, dns_rcode_noerror); /* * Check that we have a TSIG in the first message. */ ATF_REQUIRE(dns_message_gettsig(msg, &tsigowner) != NULL); result = dns_message_getquerytsig(msg, mctx, &tsigin); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_getquerytsig: %s", dns_result_totext(result)); tsigctx = msg->tsigctx; msg->tsigctx = NULL; isc_buffer_free(&buf); dns_message_destroy(&msg); result = dst_context_create3(key->key, mctx, DNS_LOGCATEGORY_DNSSEC, ISC_FALSE, &outctx); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); /* * Start digesting. */ result = add_mac(outctx, tsigout); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); /* * Create response message 2. */ result = isc_buffer_allocate(mctx, &buf, 65535); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx); /* * Process response message 2. */ result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &msg); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_create: %s", dns_result_totext(result)); msg->tcp_continuation = 1; msg->tsigctx = tsigctx; tsigctx = NULL; result = dns_message_settsigkey(msg, key); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_settsigkey: %s", dns_result_totext(result)); result = dns_message_parse(msg, buf, 0); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_parse: %s", dns_result_totext(result)); printmessage(msg); result = dns_message_setquerytsig(msg, tsigin); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_setquerytsig: %s", dns_result_totext(result)); result = dns_tsig_verify(buf, msg, NULL, NULL); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_tsig_verify: %s", dns_result_totext(result)); ATF_CHECK_EQ(msg->verified_sig, 1); ATF_CHECK_EQ(msg->tsigstatus, dns_rcode_noerror); /* * Check that we don't have a TSIG in the second message. */ tsigowner = NULL; ATF_REQUIRE(dns_message_gettsig(msg, &tsigowner) == NULL); tsigctx = msg->tsigctx; msg->tsigctx = NULL; isc_buffer_free(&buf); dns_message_destroy(&msg); /* * Create response message 3. */ result = isc_buffer_allocate(mctx, &buf, 65535); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx); result = add_tsig(outctx, key, buf); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "add_tsig: %s", dns_result_totext(result)); /* * Process response message 3. */ result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &msg); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_create: %s", dns_result_totext(result)); msg->tcp_continuation = 1; msg->tsigctx = tsigctx; tsigctx = NULL; result = dns_message_settsigkey(msg, key); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_settsigkey: %s", dns_result_totext(result)); result = dns_message_parse(msg, buf, 0); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_parse: %s", dns_result_totext(result)); printmessage(msg); /* * Check that we had a TSIG in the third message. */ ATF_REQUIRE(dns_message_gettsig(msg, &tsigowner) != NULL); result = dns_message_setquerytsig(msg, tsigin); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_setquerytsig: %s", dns_result_totext(result)); result = dns_tsig_verify(buf, msg, NULL, NULL); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_tsig_verify: %s", dns_result_totext(result)); ATF_CHECK_EQ(msg->verified_sig, 1); ATF_CHECK_EQ(msg->tsigstatus, dns_rcode_noerror); if (tsigin != NULL) isc_buffer_free(&tsigin); result = dns_message_getquerytsig(msg, mctx, &tsigin); ATF_CHECK_EQ_MSG(result, ISC_R_SUCCESS, "dns_message_getquerytsig: %s", dns_result_totext(result)); isc_buffer_free(&buf); dns_message_destroy(&msg); if (outctx != NULL) dst_context_destroy(&outctx); if (querytsig != NULL) isc_buffer_free(&querytsig); if (tsigin != NULL) isc_buffer_free(&tsigin); if (tsigout != NULL) isc_buffer_free(&tsigout); if (buf != NULL) isc_buffer_free(&buf); if (msg != NULL) dns_message_destroy(&msg); if (key != NULL) dns_tsigkey_detach(&key); if (ring != NULL) dns_tsigkeyring_detach(&ring); dns_test_end(); }
void *minstack_tcp_reading_thread(void *ptr) { minstack_tcp *mt = (minstack_tcp *) ptr; fd_set fds_read; int fdmax; struct timeval tv; printdebug("starting %s\n",__FUNCTION__); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); mt->pthread_reading_thread_stop = 0; while (!mt->pthread_reading_thread_stop) { int i, retval, socket_to_read = -1; fdmax = 0; tv.tv_sec = 0; tv.tv_usec = (mt->receive_loop_usleep); FD_ZERO(&fds_read); pthread_mutex_lock(&mt->mutex); if (mt->sockets.connected_client_nb == 0) { pthread_mutex_unlock(&mt->mutex); usleep(mt->receive_loop_usleep); //printdebug("there is no client\n"); #ifdef STOP_READING_THREAD_WITHOUT_CLIENTS break; #else continue; #endif } for (i = 0; i < mt->sockets.connected_client_nb; i++) { printdebug("Stay socket %d\n",mt->sockets.client_socket_fd[i]); FD_SETMAX(mt->sockets.client_socket_fd[i],&fds_read,fdmax); } fdmax++; pthread_mutex_unlock(&mt->mutex); retval = select(fdmax, &fds_read, NULL, NULL, &tv); pthread_mutex_lock(&mt->mutex); if (retval == EBADF) { printmoreerror("Select Bad FD\n"); continue; } if (retval < 0) { printerror("Select: got an error %d\n",retval); printmoreerror("Select:"); break; } if (retval) { printdebug("there is something to read\n"); for (i = 0; i < mt->sockets.connected_client_nb; i++) { if (FD_ISSET(mt->sockets.client_socket_fd[i],&fds_read)) { socket_to_read = mt->sockets.client_socket_fd[i]; } } } if (!retval) { //printmoreerror("Nothing much from the select\n"); } pthread_mutex_unlock(&mt->mutex); if (socket_to_read > 0) { char from[16]; char *buffer=NULL; int buffer_size = 0; pthread_mutex_lock(&mt->mutex); pthread_mutex_unlock(&mt->mutex); buffer_size = mt->read_socket(socket_to_read, from,&buffer); if (buffer_size <= 0) { pthread_mutex_lock(&mt->mutex); if (mt && mt->connection_closed_callback && (mt->type == SERVER)) { mt->connection_closed_callback(&mt->sockets, socket_to_read); minstack_close(socket_to_read); printdebug("Closing the socket %d\n",socket_to_read); } pthread_mutex_unlock(&mt->mutex); if (mt && (mt->type == CLIENT)) { printmessage("The server do not exists anymore we will close the connection\n"); minstack_close(socket_to_read); minstack_tcp_stop(mt); } } else { printmessage("%s received from %s(%d):(%u)=>%s\n",mt->name,from, socket_to_read,buffer_size,buffer); if (mt->external_read_socket) mt->external_read_socket(socket_to_read, from, buffer, buffer_size); //else free(buffer); } } } mt->pthread_reading_thread_stop = 1; printdebug("stopping %s\n",__FUNCTION__); pthread_exit(NULL); return NULL; }
int main(int argc,char* argv[]) { if(argc != 2) { printf("usage: dnsclient <host_name>\n"); return -1; } time_t ident; int fd; int rc; int serveraddrlent; char *q; unsigned char *p; unsigned char *countp; unsigned char reqBuf[512] = {0}; unsigned char rplBuf[512] = {0}; struct sockaddr_in serveraddr; //udp fd = socket(AF_INET, SOCK_DGRAM, 0); if(fd == -1) { perror("error create udp socket"); return -1; } time(&ident); //copy p = reqBuf; //Transaction ID *(p++) = ident; *(p++) = ident>>8; //Header section //flag word = 0x0100 *(p++) = 0x01; *(p++) = 0x00; //Questions = 0x0001 //just one query *(p++) = 0x00; *(p++) = 0x01; //Answer RRs = 0x0000 //no answers in this message *(p++) = 0x00; *(p++) = 0x00; //Authority RRs = 0x0000 *(p++) = 0x00; *(p++) = 0x00; //Additional RRs = 0x0000 *(p++) = 0x00; *(p++) = 0x00; //Query section countp = p; *(p++) = 0; for(q=argv[1]; *q!=0; q++) { if(*q != '.') { (*countp)++; *(p++) = *q; } else if(*countp != 0) { countp = p; *(p++) = 0; } } if(*countp != 0) *(p++) = 0; //Type=1(A):host address *(p++)=0; *(p++)=1; //Class=1(IN):internet *(p++)=0; *(p++)=1; printf("\nRequest:\n"); printmessage(reqBuf); //fill bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(53); serveraddr.sin_addr.s_addr = inet_addr("192.168.1.1"); //send to DNS Serv if(sendto(fd,reqBuf,p-reqBuf,0,(void *)&serveraddr,sizeof(serveraddr)) < 0) { perror("error sending request"); return -1; } //recev the reply bzero(&serveraddr,sizeof(serveraddr)); serveraddrlent = sizeof(serveraddr); rc = recvfrom(fd,&rplBuf,sizeof(rplBuf),0,(void *)&serveraddr,&serveraddrlent); if(rc < 0) { perror("error receiving request\n"); return -1; } //print out results printf("\nReply:\n"); printmessage(rplBuf); //exit printf("Program Exit\n"); return 0; }