Exemple #1
0
void
send(pid_t dst , Message *m){
    assert(dst > 0);
    lock();
    if (wait_message(dst) == TRUE){//process dst are waiting for message
        Process_Info *pi = get_value(dst);
        pi->wait_for_pid = 0;
        PCB * receiver = pi->pcb;
        receiver->message_addr->dest = dst;
        receiver->message_addr->src = (in_irq_handle == TRUE)?MSG_HWINTR:current_pcb->pid;
        receiver->message_addr->type = m->type;
        memcpy(receiver->message_addr->payload , m->payload , MSG_SZ );
        //printk("2 in send %s addr %x \n",((DevMessage*)receiver->message_addr)->buf,((DevMessage*)receiver->message_addr)->buf);
        V(&receiver->message);
    } else {
        Message_Node *new_message = (Message_Node *)malloc(sizeof(Message_Node));
        assert(new_message != NULL);
        new_message->message.dest = dst;
        new_message->message.src = (in_irq_handle == TRUE)?MSG_HWINTR:current_pcb->pid;
        new_message->message.type = m->type;
        memcpy(new_message->message.payload , m->payload , MSG_SZ);
        list_add_after(get_msg_list(current_pcb->pid , dst) , &new_message->sys_queue);
        Process_Info *pi = get_value(dst);
        list_add_after(&pi->message_list , &new_message->pid_queue);
    }
    unlock();
}
Exemple #2
0
/*
 * Parse a line from the newsrc file and write it back out with updated
 * sequence information. Return number of lines written (ie, 0 or 1)
 */
static int
write_newsrc_line(
	FILE *fp,
	char *line)
{
	char *seq;
	int sub;
	struct t_group *group;

	seq = parse_newsrc_line(line, &sub);

	if (line[0] == '\0' || sub == 0)		/* Insurance against blank line */
		return 0;

	if (seq == NULL) {		/* line has no ':' or '!' in it */
		if (tinrc.strip_bogus == BOGUS_REMOVE)
			wait_message(2, _(txt_remove_bogus), line);
		return 0;
	}

	/*
	 * Find the group in active. If we cannot, then junk it if bogus groups
	 * are set to auto removal. Also check for bogus flag just in case
	 * strip_bogus was changed since tin started
	 */
	group = group_find(line);

	if (tinrc.strip_bogus == BOGUS_REMOVE) {
		if (group == NULL || group->bogus) { /* group dosen't exist */
			wait_message(2, _(txt_remove_bogus), line);
			return 0;
		}
	}

	if ((group && group->newsrc.present) && (group->subscribed || !tinrc.strip_newsrc)) {
		fprintf(fp, "%s%c ", group->name, SUB_CHAR(group->subscribed));
		print_bitmap_seq(fp, group);
		return 1;
	} else {
		if (sub == SUBSCRIBED || !tinrc.strip_newsrc) {
			fprintf(fp, "%s%c %s\n", line, sub, seq);
			return 1;
		}
	}
	return 0;
}
Exemple #3
0
/*
 * Read .newsrc into my_group[]. my_group[] ints point to active[] entries.
 * If allgroups is set, then my_group[] is completely overwritten,
 * otherwise, groups are appended. Any bogus groups will be handled
 * accordingly. Bogus groups will _not_ be subscribed to as a design
 * principle.
 *
 * Returns the numer of lines read(useful for a check newsrc >= oldnewsrc)
 * 	< 0 error
 * 	>=0 number of lines read
 */
signed long int
read_newsrc(
	char *newsrc_file,
	t_bool allgroups)
{
	FILE *fp;
	char *grp, *seq;
	int sub, i;
	signed long line_count = 0;
	struct stat statbuf;

	if (allgroups)
		selmenu.max = skip_newgroups();

	/*
	 * make a .newsrc if one doesn't exist & auto subscribe to set groups
	 */
	if (stat(newsrc_file, &statbuf) == -1) {
		if (!create_newsrc(newsrc_file))
			return -1L; /* ouch */
		auto_subscribe_groups(newsrc_file);
	} else
		newsrc_mode = statbuf.st_mode;

	if ((fp = fopen(newsrc_file, "r")) != NULL) {
		if (!batch_mode || verbose)
			wait_message(0, _(txt_reading_newsrc));

		while ((grp = tin_fgets(fp, FALSE)) != NULL) {
			seq = parse_newsrc_line(grp, &sub);
			line_count++;

			if (sub == SUBSCRIBED) {
				if ((i = my_group_add(grp)) >= 0) {
					if (!active[my_group[i]].bogus) {
						active[my_group[i]].subscribed = SUB_BOOL(sub);
						parse_bitmap_seq(&active[my_group[i]], seq);
					}
				} else
					process_bogus(grp);
			}
		}
		fclose(fp);
		/* If you aborted with 'q', then you get what you get. */

		if (cmd_line) {
			my_fputc('\n', stdout);
			my_flush();
		}
	}
	return line_count;
}
Exemple #4
0
void set_im_window(unsigned id, Rectangle rect)
{
    if (imfd == -1 || !im_active || id >= NR_IM_WINS) return;

    Message msg;
    msg.type = SetWin;
    msg.len = sizeof(msg);
    msg.win.winid = id;
    msg.win.rect = rect;

    int ret = write(imfd, (char *)&msg, sizeof(msg));
    (void) ret;
    wait_message(AckWin);
}
Exemple #5
0
void init_progression_raw_newt(const char *msg, int size)
{
	size_progress = size;
	if (size) {
		actually_drawn = 0;
		newtCenteredWindow(70, 5, "Please wait...");
		form = newtForm(NULL, NULL, 0);
		newtFormAddComponent(form, newtLabel(1, 1, msg));
		scale = newtScale(1, 3, 68, size);
		newtFormAddComponent(form, scale);
		newtDrawForm(form);
		newtRefresh();
	}
	else {
		wait_message("%s", msg);
		msg_progress = msg;
	}
}
Exemple #6
0
int main(int argc, char *argv[])
{
  Mem = new Memory();

  if ( Mem == NULL ){
    my_error("couldn't allocate Mem");
    exit(0);
  }

  Mem->GetOptions(argc,argv);

  Socket sock = init_connection(Mem->SP_host,Mem->SP_port);

  Mem->sock = &sock;

  if(Mem->sock->socketfd == -1) {
    cerr << "Can't open connection for player" << endl;
    exit(-1);
  }

  send_initialize_message();

  if ( wait_message(recvbuf, Mem->sock) == 0 )
    my_error("wait_message failed");

  parse_initialize_message(recvbuf);

  Mem->Initialize();
  score();  /* find out the score */

  if (Mem->IP_only_connect)
    return 0;

  sigset_t sigfullmask = init_handler();	      

  while ( Mem->ServerAlive == TRUE && wait_for_signals(&sigfullmask) );

  if (Mem->sock->socketfd != -1) close_connection(Mem->sock);

  printf("Shutting down player %d\n",Mem->MyNumber);
}
Exemple #7
0
/*
 * Automatically subscribe user to newsgroups specified in
 * NEWSLIBDIR/subscriptions (locally) or same file but from NNTP
 * server (LIST SUBSCRIPTIONS) and create .newsrc
 */
static void
auto_subscribe_groups(
	char *newsrc_file)
{
	FILE *fp_newsrc;
	FILE *fp_subs;
	char *ptr;

	/*
	 * If subscription file exists then first unsubscribe to all groups
	 * and then subscribe to just the auto specified groups.
	 */
	if ((fp_subs = open_subscription_fp()) == NULL)
		return;

	if (!batch_mode)
		wait_message(0, _(txt_autosubscribing_groups));

	if ((fp_newsrc = fopen(newsrc_file, "w" FOPEN_OPTS)) == NULL)
		return;

#ifndef M_OS2
	if (newsrc_mode)
		fchmod(fileno(fp_newsrc), newsrc_mode);
#endif /* !M_OS2 */

	/* TODO: test me! */
	while ((ptr = tin_fgets(fp_subs, FALSE)) != NULL) {
		if (ptr[0] != '#') {
			if (group_find(ptr) != 0)
				fprintf(fp_newsrc, "%s:\n", ptr);
		}
	}

	/* We ignore user 'q'uits here. They will get them next time in any case */

	if (ferror(fp_newsrc) || fclose(fp_newsrc))
		error_message(_(txt_filesystem_full), NEWSRC_FILE);

	TIN_FCLOSE(fp_subs);
}
Exemple #8
0
/*
 * Create a newsrc from active[] groups. Subscribe to all groups.
 */
static t_bool
create_newsrc(
	char *newsrc_file)
{
	FILE *fp;
	int i;

	if ((fp = fopen(newsrc_file, "w")) != NULL) {
		wait_message(0, _(txt_creating_newsrc));

		for_each_group(i)
			fprintf(fp, "%s!\n", active[i].name);

		if (ferror(fp) || fclose(fp)) {
			error_message(_(txt_filesystem_full), NEWSRC_FILE);
			return FALSE;
		}
		return TRUE; /* newsrc created */
	}
	return FALSE;
}
Exemple #9
0
void update_progression_raw_newt(int current_size)
{
	if (size_progress) {
		if (current_size <= size_progress)
			newtScaleSet(scale, current_size);
		newtRefresh();
	}
	else {
		struct timeval t;
		int time;
		static int last_time = -1;
		gettimeofday(&t, NULL);
		time = t.tv_sec*3 + t.tv_usec/300000;
		if (time != last_time) {
			char msg_prog_final[500];
			sprintf(msg_prog_final, "%s (%d bytes read) ", msg_progress, current_size);
			remove_wait_message();
			wait_message("%s", msg_prog_final);
		}
		last_time = time;
	}
}
Exemple #10
0
/*
 * Perform authentication with AUTHINFO USER method. Return response
 * code from server.
 *
 * we don't handle ERR_ENCRYPT right now
 */
static int
do_authinfo_user(
	char *server,
	char *authuser,
	char *authpass)
{
	char line[PATH_LEN];
	int ret;

	snprintf(line, sizeof(line), "AUTHINFO USER %s", authuser);
#	ifdef DEBUG
	if (debug & DEBUG_NNTP)
		debug_print_file("NNTP", "authorization %s", line);
#	endif /* DEBUG */
	put_server(line);
	if ((ret = get_only_respcode(NULL, 0)) != NEED_AUTHDATA)
		return ret;

	if ((authpass == NULL) || (*authpass == '\0')) {
#	ifdef DEBUG
		if (debug & DEBUG_NNTP)
			debug_print_file("NNTP", "authorization failed: no password");
#	endif /* DEBUG */
		error_message(2, _(txt_auth_failed_nopass), server);
		return ERR_AUTHBAD;
	}

	snprintf(line, sizeof(line), "AUTHINFO PASS %s", authpass);
#	ifdef DEBUG
	if (debug & DEBUG_NNTP)
		debug_print_file("NNTP", "authorization %s", line);
#	endif /* DEBUG */
	put_server(line);
	ret = get_only_respcode(line, sizeof(line));
	if (!batch_mode || verbose || ret != OK_AUTH)
		wait_message(2, (ret == OK_AUTH ? _(txt_authorization_ok) : _(txt_authorization_fail)), authuser);
	return ret;
}
Exemple #11
0
static int
do_authinfo_sasl_plain(
	char *authuser,
	char *authpass)
{
	char line[PATH_LEN];
	char *foo;
	char *utf8user;
	char *utf8pass;
	int ret;
#		ifdef CHARSET_CONVERSION
	char *cp;
	int i, c = 0;
	t_bool contains_8bit = FALSE;
#		endif /* CHARSET_CONVERSION */

	utf8user = my_strdup(authuser);
	utf8pass = my_strdup(authpass);
#		ifdef CHARSET_CONVERSION
	/* RFC 4616 */
	if (!IS_LOCAL_CHARSET("UTF-8")) {
		for (cp = utf8user; *cp && !contains_8bit; cp++) {
			if (!isascii(*cp)) {
				contains_8bit = TRUE;
				break;
			}
		}
		for (cp = utf8pass; *cp && !contains_8bit; cp++) {
			if (!isascii(*cp)) {
				contains_8bit = TRUE;
				break;
			}
		}
		if (contains_8bit) {
			for (i = 0; txt_mime_charsets[i] != NULL; i++) {
				if (!strcasecmp("UTF-8", txt_mime_charsets[i])) {
					c = i;
					break;
				}
			}
			if (c == i) { /* should never fail */
				if (!buffer_to_network(utf8user, c)) {
					free(utf8user);
					free(utf8pass);
					utf8user = my_strdup(authuser);
					utf8pass = my_strdup(authpass);
				} else {
					if (!buffer_to_network(utf8pass, c)) {
						free(utf8user);
						free(utf8pass);
						utf8user = my_strdup(authuser);
						utf8pass = my_strdup(authpass);
					}
				}
			}
		}
	}
#		endif /* CHARSET_CONVERSION */

#		ifdef DEBUG
	if (debug & DEBUG_NNTP)
		debug_print_file("NNTP", "do_authinfo_sasl_plain(\"%s\", \"%s\")", BlankIfNull(authuser), BlankIfNull(authpass));
#		endif /* DEBUG */

	if ((foo = sasl_auth_plain(utf8user, utf8pass)) == NULL) {
		free(utf8user);
		free(utf8pass);
		return ERR_AUTHBAD;
	}

	free(utf8user);
	free(utf8pass);

	snprintf(line, sizeof(line), "AUTHINFO SASL PLAIN %s", foo);
	FreeIfNeeded(foo);
#		ifdef DEBUG
	if (debug & DEBUG_NNTP)
		debug_print_file("NNTP", "authorization %s", line);
#		endif /* DEBUG */
	put_server(line);
	ret = get_only_respcode(line, sizeof(line));
	if (!batch_mode || verbose || ret != OK_AUTH)
		wait_message(2, (ret == OK_AUTH ? _(txt_authorization_ok) : _(txt_authorization_fail)), authuser);
	return ret;
}
Exemple #12
0
/*
 * NNTP user authorization. Returns TRUE if authorization succeeded,
 * FALSE if not.
 *
 * tries AUTHINFO SASL PLAIN (if available) fist and if not succcessfull
 * AUTHINFO USER/PASS
 *
 * If username/passwd already given, and server wasn't changed, retry those.
 * Otherwise, read password from ~/.newsauth or, if not present or no matching
 * server found, from console.
 *
 * The ~/.newsauth authorization file has the format:
 *   nntpserver1 password [user]
 *   nntpserver2 password [user]
 *   etc.
 */
static t_bool
authinfo_plain(
	char *server,
	char *authuser,
	t_bool startup)
{
	char *authpass;
	int ret = ERR_AUTHBAD, changed;
	static char authusername[PATH_LEN] = "";
	static char authpassword[PATH_LEN] = "";
	static char last_server[PATH_LEN] = "";
	static t_bool already_failed = FALSE;
	static t_bool initialized = FALSE;

	changed = strcmp(server, last_server);	/* do we need new auth values? */
	strncpy(last_server, server, PATH_LEN - 1);
	last_server[PATH_LEN - 1] = '\0';

	/*
	 * Let's try the previous auth pair first, if applicable.
	 * Else, proceed to the other mechanisms.
	 */
	if (initialized && !changed && !already_failed) {
#	ifdef USE_SASL
		if (nntp_caps.sasl & SASL_PLAIN)
			ret = do_authinfo_sasl_plain(authusername, authpassword);
		if (ret != OK_AUTH)
#	endif /* USE_SASL */
		{
			if (nntp_caps.type != CAPABILITIES || nntp_caps.authinfo_user)
				ret = do_authinfo_user(server, authusername, authpassword);
		}
		return (ret == OK_AUTH);
	}

	authpassword[0] = '\0';
	authuser = strncpy(authusername, authuser, sizeof(authusername) - 1);
	authpass = authpassword;

	/*
	 * No username/password given yet.
	 * Read .newsauth only if we had not failed authentication yet for the
	 * current server (we don't want to try wrong username/password pairs
	 * more than once because this may lead to an infinite loop at connection
	 * startup: nntp_open tries to authenticate, it fails, server closes
	 * connection; next time tin tries to access the server it will do
	 * nntp_open again ...). This means, however, that if configuration
	 * changed on the server between two authentication attempts tin will
	 * prompt you the second time instead of reading .newsauth (except when
	 * at startup time; in this case, it will just leave); you have to leave
	 * and restart tin or change to another server and back in order to get
	 * it read again.
	 */
	if ((changed || !initialized) && !already_failed) {
		if (read_newsauth_file(server, authuser, authpass)) {
#	ifdef USE_SASL
			if (nntp_caps.sasl & SASL_PLAIN)
				ret = do_authinfo_sasl_plain(authuser, authpass);

			if (ret != OK_AUTH)
#	endif /* USE_SASL */
			{
				if (force_auth_on_conn_open || nntp_caps.type != CAPABILITIES || (nntp_caps.type == CAPABILITIES && nntp_caps.authinfo_user))
					ret = do_authinfo_user(server, authuser, authpass);
			}
			already_failed = (ret != OK_AUTH);

			if (ret == OK_AUTH) {
#	ifdef DEBUG
				if (debug & DEBUG_NNTP)
					debug_print_file("NNTP", "authorization succeeded");
#	endif /* DEBUG */
				initialized = TRUE;
				return TRUE;
			}
		}
	}

	/*
	 * At this point, either authentication with username/password pair from
	 * .newsauth has failed or there's no .newsauth file respectively no
	 * matching username/password for the current server. If we are not at
	 * startup we ask the user to enter such a pair by hand. Don't ask him
	 * at startup except if requested by -A option because if he doesn't need
	 * to authenticate (we don't know), the "Server expects authentication"
	 * messages are annoying (and even wrong).
	 * UNSURE: Maybe we want to make this decision configurable in the
	 * options menu, too, so that the user doesn't need -A.
	 * TODO: Put questions into do_authinfo_user() because it is possible
	 * that the server doesn't want a password; so only ask for it if needed.
	 */
	if (force_auth_on_conn_open || !startup) {
		if (batch_mode) { /* no interactive username/password prompting */
			error_message(0, _(txt_auth_needed));
			return (ret == OK_AUTH);
		}
		if (nntp_caps.type != CAPABILITIES || (nntp_caps.type == CAPABILITIES && !nntp_caps.authinfo_state && ((nntp_caps.sasl & SASL_PLAIN) || nntp_caps.authinfo_user || (!nntp_caps.authinfo_user && !(nntp_caps.sasl & SASL_PLAIN))))) {
#	ifdef USE_CURSES
			int state = RawState();
#	endif /* USE_CURSES */

			wait_message(0, _(txt_auth_needed));
#	ifdef USE_CURSES
			Raw(TRUE);
#	endif /* USE_CURSES */
			if (!prompt_default_string(_(txt_auth_user), authuser, PATH_LEN, authusername, HIST_NONE)) {
#	ifdef DEBUG
				if (debug & DEBUG_NNTP)
					debug_print_file("NNTP", "authorization failed: no username");
#	endif /* DEBUG */
				return FALSE;
			}

#	ifdef USE_CURSES
			Raw(state);
			my_printf("%s", _(txt_auth_pass));
			wgetnstr(stdscr, authpassword, sizeof(authpassword));
			Raw(TRUE);
#	else
			/*
			 * on some systems (i.e. Solaris) getpass(3) is limited to 8 chars ->
			 * we use tin_getline()
			 */
			authpass = strncpy(authpassword, tin_getline(_(txt_auth_pass), FALSE, NULL, PATH_LEN, TRUE, HIST_NONE), sizeof(authpassword) - 1);
#	endif /* USE_CURSES */

#	ifdef USE_SASL
			if (nntp_caps.sasl & SASL_PLAIN)
				ret = do_authinfo_sasl_plain(authuser, authpass);
			if (ret != OK_AUTH)
#	endif /* USE_SASL */
			{
				if (nntp_caps.type != CAPABILITIES || (nntp_caps.authinfo_user || !nntp_caps.authinfo_sasl)) {
#	ifdef DEBUG
					if (debug & DEBUG_NNTP) {
						if (nntp_caps.type == CAPABILITIES && !nntp_caps.authinfo_sasl && !nntp_caps.authinfo_user)
							debug_print_file("NNTP", "!!! No supported authmethod available, trying AUTHINFO USER/PASS");
					}
#	endif /* DEBUG */
					ret = do_authinfo_user(server, authuser, authpass);
					if (ret != OK_AUTH)
						already_failed = TRUE;
					/*
					 * giganews once responded to CAPABILITIES with just
					 * "VERSION 2", no mode-switching indication, no reader
					 * indication, no post indication, no authentication
					 * indication, ... so in case AUTHINFO USER/PASS succeeds
					 * if not advertized we simply go on but fully ignore
					 * CAPABILITIES
					 */
					if (nntp_caps.type == CAPABILITIES && !nntp_caps.authinfo_user && !nntp_caps.authinfo_sasl && ret == OK_AUTH)
						nntp_caps.type = BROKEN;
				}
			}
			initialized = TRUE;
			my_retouch();			/* Get rid of the chaff */
		} else {
			/*
			 * TODO:
			 * nntp_caps.type == CAPABILITIES && nntp_caps.authinfo_state
			 * can we change the sate here? and if so how? SARTTLS? MODE
			 * READER?
			 */
#	ifdef DEBUG
			if (debug & DEBUG_NNTP)
				debug_print_file("NNTP", "authorization not allowed in current sate");
#	endif /* DEBUG */
			/*
			 * we return OK_AUTH here once so tin doesn't exit just because a
			 * single command requested auth ...
			 */
			if (!already_failed)
				ret = OK_AUTH;
		}
	}

#	ifdef DEBUG
	if (debug & DEBUG_NNTP)
		debug_print_file("NNTP", "authorization %s", (ret == OK_AUTH ? "succeeded" : "failed"));
#	endif /* DEBUG */

	return (ret == OK_AUTH);
}
Exemple #13
0
/*
 * Subscribe/unsubscribe to a group in .newsrc.
 * This involves rewriting the .newsrc with the new info
 * If get_info is set we are allowed to issue NNTP commands if needed
 */
void
subscribe(
	struct t_group *group,
	int sub_state,
	t_bool get_info)
{
	FILE *fp;
	FILE *newfp;
	char *line;
	char *seq;
	int sub;
	t_bool found = FALSE;

	if (no_write)
		return;

	if ((newfp = fopen(newnewsrc, "w" FOPEN_OPTS)) == NULL)
		return;

#ifndef M_OS2
	if (newsrc_mode)
		fchmod(fileno(newfp), newsrc_mode);
#endif /* !M_OS2 */

	if ((fp = fopen(newsrc, "r")) != NULL) {
		while ((line = tin_fgets(fp, FALSE)) != NULL) {
			seq = parse_newsrc_line(line, &sub);
			if (STRCMPEQ(line, group->name)) {
				fprintf(newfp, "%s%c %s\n", line, sub_state, seq);
				group->subscribed = SUB_BOOL(sub_state);

				/* If previously subscribed to in .newsrc, load up any existing information */
				if (sub_state == SUBSCRIBED)
					parse_bitmap_seq(group, seq);

				found = TRUE;
			} else
				fprintf(newfp, "%s%c %s\n", line, sub, seq);
		}

		fclose(fp);

		if (!found) {
			wait_message(0, _(txt_subscribing));
			group->subscribed = SUB_BOOL(sub_state);
			if (sub_state == SUBSCRIBED) {
				fprintf(newfp, "%s%c ", group->name, sub_state);
				if (get_info) {
					vGet1GrpArtInfo(group);
					print_bitmap_seq(newfp, group);
				} else /* we are not allowed to issue NNTP cmds during AUTOSUBSCRIBE loop */
					fprintf(newfp, "1\n");
			} else
				fprintf(newfp, "%s%c\n", group->name, sub_state);
		}
	}

	if (ferror(newfp) || fclose(newfp)) {
		error_message(_(txt_filesystem_full), NEWSRC_FILE);
		unlink(newnewsrc);
	} else
		rename_file(newnewsrc, newsrc);
}