Example #1
0
/*	Function invoked for sub-station (i.e., child process of the main server process)	*/
int sub_station() {
	
	int tcp_sock, sub_sock, s;
	in_port_t self_port;
	struct sockaddr_in sub_self, curr_client;
	Message from_msg, to_msg;
	
	self_port = getpid();
	
	printf("[Initiating sub-station %d]\n", self_port);
	
	memset((char*)&sub_self, 0, sizeof(struct sockaddr_in));
	memset((char*)&curr_client, 0, sizeof(struct sockaddr_in));
	sub_self.sin_family = AF_INET;
	sub_self.sin_port = htons(self_port);
	sub_self.sin_addr.s_addr = inet_addr(LOOPBACK_ADDR);
	
	// Let the main station(parent process) transmit the necessary infos to the client first
	//sleep(2);

	if((tcp_sock=socket(AF_INET, SOCK_STREAM, 0)) == -1)
		goto err_ret;
	if(bind(tcp_sock, (struct sockaddr*)&sub_self, sizeof(sub_self)) == -1)
		goto err_ret;
	if(listen(tcp_sock, 1) == -1)
		goto err_ret;
	s = sizeof(curr_client);
	if((sub_sock=accept(tcp_sock, (struct sockaddr*)&curr_client, &s)) == -1)
		goto err_ret;
	
	printf("[Sub-station %d]: Connection with host %d was successful\n", htons(self_port), htons(curr_client.sin_port));

	// Start communication with the host('A' or 'B')
	printf("[Begin communication]\n");
	while(1) {
		// readn(sub_sock, (Message*)&from_msg, sizeof(Message));
		readn(sub_sock, (char*)&from_msg, sizeof(Message));
		if(from_msg.header.type == MSG_DELIVER) {	//Deliver mesg to host B
			msg_deliver(&from_msg);
		} else {	// Else consider illegal message
			msg_reject(&curr_client);
		}
	}
	
	close(sub_sock);
	close(tcp_sock);
err_ret:
	fprintf(stderr, "[ERROR: Sub-station failure]\n");
	return EXIT_FAILURE;
}
Example #2
0
int reply_sendend(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
{
	int outsize = 0;
	START_PROFILE(SMBsendend);

	if (! (*lp_msg_command())) {
		END_PROFILE(SMBsendend);
		return(ERROR_DOS(ERRSRV,ERRmsgoff));
	}

	outsize = set_message(outbuf,0,0,True);

	DEBUG(3,("SMBsendend\n"));

	msg_deliver();

	END_PROFILE(SMBsendend);
	return(outsize);
}
Example #3
0
/****************************************************************************
  reply to a sends
****************************************************************************/
int reply_sends(connection_struct *conn,
		char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
{
  int len;
  char *msg;
  int outsize = 0;
  char *p;

  START_PROFILE(SMBsends);

  msgpos = 0;

  if (! (*lp_msg_command())) {
    END_PROFILE(SMBsends);
    return(ERROR_DOS(ERRSRV,ERRmsgoff));
  }

  outsize = set_message(outbuf,0,0,True);

  p = smb_buf(inbuf)+1;
  p += srvstr_pull_buf(inbuf, msgfrom, p, sizeof(msgfrom), STR_TERMINATE) + 1;
  p += srvstr_pull_buf(inbuf, msgto, p, sizeof(msgto), STR_TERMINATE) + 1;

  msg = p;

  len = SVAL(msg,0);
  len = MIN(len,sizeof(msgbuf)-msgpos);

  memset(msgbuf,'\0',sizeof(msgbuf));

  memcpy(&msgbuf[msgpos],msg+2,len);
  msgpos += len;

  msg_deliver();

  END_PROFILE(SMBsends);
  return(outsize);
}