int process_xover_msg(struct DayDream_Message* dd_msg) { int number_of_lines; int number_of_bytes; struct tm* t; char tmpbuf[128]; generate_temp_message(dd_msg->MSG_NUMBER); number_of_lines = count_lines_in_temp_message(); number_of_bytes = count_bytes_in_temp_message(); t = gmtime(&dd_msg->MSG_CREATION); strftime(tmpbuf, 128, "%a, %d %b %Y %H:%M:%S -0000", t); printf("%d\t%s\t%s <dummy@localhost>\t%s\t%s\t%s\t%d\t%d\t\r\n", dd_msg->MSG_NUMBER, dd_msg->MSG_SUBJECT, strip_hibit((unsigned char*) dd_msg->MSG_AUTHOR), tmpbuf, generate_message_id(dd_msg->MSG_NUMBER), "", number_of_bytes, number_of_lines ); return 0; }
int print_message_headers(int messagenumber) { int fd; int ok; int number_of_lines; char tmpbuf[128]; struct DayDream_Message dd_msg; struct tm* t; fd = ddmsg_open_base(current_conf->CONF_PATH, current_base->MSGBASE_NUMBER, O_RDONLY, 0660); if(fd < 0) { return -1; } ok = 0; while(read(fd, &dd_msg, sizeof(struct DayDream_Message)) == sizeof(struct DayDream_Message)) { if(dd_msg.MSG_NUMBER == messagenumber) { ok = 1; break; } } ddmsg_close_base(fd); if(!ok) { return -1; } generate_temp_message(messagenumber); number_of_lines = count_lines_in_temp_message(); t = gmtime(&dd_msg.MSG_CREATION); strftime(tmpbuf, 128, "%a, %d %b %Y %H:%M:%S GMT", t); strip_hibit((unsigned char*) dd_msg.MSG_AUTHOR); strip_hibit((unsigned char*) dd_msg.MSG_RECEIVER); printf("Path: localhost!not-for-mail\r\n"); printf("From: %s <dummy@localhost>\r\n", dd_msg.MSG_AUTHOR); printf("Newsgroups: %s.%s\r\n", current_conf->CONF_NAME, current_base->MSGBASE_FN_TAG); printf("Message-ID: %s\r\n", generate_message_id(messagenumber)); printf("Subject: %s\r\n", dd_msg.MSG_SUBJECT); printf("Date: %s\r\n", tmpbuf); printf("Lines: %d\r\n", number_of_lines); printf("X-FTN-TO: %s\r\n", dd_msg.MSG_RECEIVER); return 0; }
void send_private_message(char* message, int sock_fd) { //message body char package[TOKEN_LENTH + 1] = {'\0'};memcpy(package, message, TOKEN_LENTH); //header message_header m_header; m_header.type = PRIVATE; generate_message_id(m_header.id); //convert the local variable to network m_header.payload_length = htons(strlen(package) + 1); if (sizeof(m_header) != 12) { printf("message length is %d", sizeof(m_header)); throw_exception(FATAL_ERROR, "sizeof(m_header) != 12\n"); } //send the message to all connections send_message(sock_fd, &m_header, package); }
void send_udp_message() { // double check for network variables inet_pton(AF_INET, local_ip, &network_ip); self_peer_msg.ip = network_ip; printf("sender ip: %s", local_ip); self_peer_msg.udp_port = network_udp_port; add_peer_token_to_container(self_peer_msg.peer_token, self_peer_msg.udp_port, self_peer_msg.ip, tcp_port); //message body char package[TOKEN_LENTH + 7] = {'\0'};memcpy (package, self_peer_msg.peer_token, TOKEN_LENTH); memcpy(package + TOKEN_LENTH, &network_ip, 4); memcpy(package + TOKEN_LENTH + 4, &self_peer_msg.udp_port, 2); /* begin broadcasting */ int i, sock_fd = -1; for (i = 0; i < MAX_CONNECTIONS; i++) { sock_fd = get_conn_fd(i); if (sock_fd != -1) { /* construct message header */ message_header mh; init_header(&mh); generate_message_id(mh.id); mh.type = BROADCAST; mh.payload_length = htons(strlen(package) + 1); if (sizeof(mh) != 12) { printf("mh length is %d\n", sizeof(mh)); throw_exception(ERROR, "sizeof(mh) != 12\n"); } send_message(sock_fd, &mh, package); /* add msg_id to msg_bag */ if (is_new_msg(mh.id)) add_msg_to_container(mh.id); } } }
int main(int argc, char *argv[]) { char buf[128]; char filename[128]; char dd[128]; char user_str[128]; char* ptr; int tmp; int first_arg; int second_arg; int i; auth_ok = 0; for(i=0; i < argc; ++i) { if(!strcmp(argv[i], "-a")) { auth_ok = 1; } } current_conf = NULL; current_base = NULL; ptr = getenv("DAYDREAM"); if(!ptr) { syslog(0, "Can't get DAYDREAM env"); exit(1); } strncpy(dd, ptr, 128); datadir = dd; printf("200 DDNNTP ready to serve\r\n"); fflush(stdout); while(1) { if(fgets(buf, 128, stdin) == NULL) { break; } strip_crlf(buf); if(!strncasecmp(buf, "LIST", 4)) { if(check_auth()) { if(strlen(buf) > 5) { if(!strncasecmp(buf+5, "NEWSGROUPS", 10)) { generate_list_with_desc(); } else { generate_list(); } } else { generate_list(); } } } else if(!strncasecmp(buf, "GROUP", 5)) { if(check_auth()) { if(strlen(buf) < 7) { printf("411 no such news group\r\n"); } else { if(do_group_command(buf+6, 1) < 0) { printf("411 no such news group\r\n"); } } } } else if(!strncasecmp(buf, "HEAD", 4) || !strncasecmp(buf, "BODY", 4) || !strncasecmp(buf, "ARTICLE", 7)) { if(check_auth()) { if(!current_base || !current_conf) { printf("412 No newsgroup has been selected.\r\n"); } else { if(!strncasecmp(buf, "ARTICLE", 7)) { parse_input_message_id(buf+8, &tmp); } else { parse_input_message_id(buf+5, &tmp); } if(message_num_ok(tmp)) { if(!strncasecmp(buf, "HEAD", 4)) { printf("221 %d %s article retrieved - head follows\r\n", tmp, generate_message_id(tmp)); print_message_headers(tmp); printf(".\r\n"); } else if(!strncasecmp(buf, "BODY", 4)) { printf("222 %d %s article retrieved - body follows\r\n", tmp, generate_message_id(tmp)); print_message(tmp); printf(".\r\n"); } else { printf("220 %d %s article retrieved - head and body follow\r\n", tmp, generate_message_id(tmp)); print_message_headers(tmp); printf("\r\n"); print_message(tmp); printf(".\r\n"); } } else { printf("423 No such article number in this group.\r\n"); } } } } else if(!strncasecmp(buf, "QUIT", 4)) { printf("205 Closing connection - Goodbye!\r\n"); break; } else if(!strncasecmp(buf, "MODE READER", 11)) { printf("200 Hello, you can post.\r\n"); } else if(!strncasecmp(buf, "XOVER", 5)) { if(check_auth()) { if(!current_base || !current_conf) { printf("412 No news group current selected.\r\n"); } else { ptr = strchr(buf+5, '-'); if(ptr == NULL) { printf("420 No article(s) selected.\r\n"); } else { *ptr = '\0'; first_arg = atoi(buf+6); second_arg = atoi(ptr+1); printf("224 %d-%d fields follow\r\n", first_arg, second_arg); process_xover(first_arg, second_arg); printf(".\r\n"); } } } } else if(!strncasecmp(buf, "POST", 4)) { if(check_auth()) { printf("340 Ok\r\n"); fflush(stdout); if(!process_post()) { printf("240 Article posted.\r\n"); syslog(LOG_INFO, "Message posted\n"); } else { printf("441 posting failed.\r\n"); syslog(LOG_INFO, "Message post failed\n"); } } } else if(!strncasecmp(buf, "IHAVE", 5)) { if(check_auth()) { printf("335 Go ahead\r\n"); fflush(stdout); if(!process_post()) { printf("235 Article posted.\r\n"); } else { printf("435 posting failed.\r\n"); } } } else if(!strncasecmp(buf, "AUTHINFO", 8)) { if(!strncasecmp(buf+9, "USER", 4)) { strncpy(user_str, buf+14, 128); printf("381 More authentication information required.\r\n"); } else if(!strncasecmp(buf+9, "PASS", 4)) { struct userbase* u = ddgetpwnam(user_str); if(!u) { printf("482 Authentication rejected.\r\n"); } else if(cmppasswds(buf+14, u->user_password)) { syslog(LOG_INFO, "User %s logged in", user_str); printf("281 Authentication accepted.\r\n"); auth_ok = 1; } else { printf("482 Authentication rejected.\r\n"); } } } else { printf("500 Command not recognized\r\n"); syslog(LOG_INFO, "Unknown command: %s", buf); } fflush(stdout); } sprintf(filename, "/tmp/msg.%d", getpid()); unlink(filename); return 0; }