/* *This is the root component of the SqueakEasy. *[Implementation] *netcmd encapsulates the actual wire protocol away from the server. * * Copyright 2003 Joseph Pingenot This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * */ #include #include "netcmd.h" #include "readline.h" /*Simple routine to tell the user that the protocol is bad.*/ void nak_subj_proto(int proto, int fd){ write(fd, "I'm sorry; I don't understand your customs, stranger. Good day, dear subject.\n", 64); } /*Simple routine to tell the user that the request was not understood.*/ void nak_subj_cmd(int proto, int fd){ write(fd, "I'm sorry; I didn't understand your petition, dear subject.\n", 62); } /*Get the command. If it's bad, return the special "bad" command.*/ int get_subj_cmd(int proto, char *line, long int len){ /*We're going to have a sequence of comparisons.*/ //printf("get_subj_cmd(%s)\n", line); if((strcmp(line, "I must be leaving, my Queen. Good day.\r") == 0) || (strcmp(line, "I must be leaving, my Queen. Good day.") == 0)){ //printf(" cmd=quit\n"); return SQKRT_SUBJ_QUIT; }else{ //printf(" unknown command 1\n"); return SQKRT_SUBJ_BADCMD; } //printf(" unknown command 2\n"); return SQKRT_SUBJ_BADCMD; } /*Verify that the protocol is good.*/ int subjectproto_isvalid(int proto){ /*As of now, we know only protocol version 1.*/ if(proto == 1) { return 1; }else{ /*Unknown protocol!*/ return 0; } } void ack_subj_quit(proto, fd){ write(fd, "You may leave. Good day, loyal subject.\n", 41); } int do_subject_pleasantries(fd){ char *line; /*the line the user sent us.*/ long int linelen; /*line length*/ /*Alright. We need to set up the link with the subject.*/ line = readline(fd, &linelen); if(line == NULL){ /*client left.*/ return SQKRT_SUBJ_BADPROTO; } /*Check the line.*/ if((strcmp(line, "Good day, Your Majesty.\r") != 0) && (strcmp(line, "Good day, Your Majesty.") != 0)){ /*Broke protocol.*/ return SQKRT_SUBJ_BADPROTO; } /*Free the line, 'cause we don't need it anymore.*/ free(line); /*Reply.*/ write(fd, "Good day, my subject.\n", 22); /*Get the next line. This cues the protocol version.*/ line = readline(fd, &linelen); if(line == NULL){ /*client left.*/ return SQKRT_SUBJ_BADPROTO; } if((strcmp(line, "Pleasant weather we're having, isn't it, Your Majesty?\r") != 0) && (strcmp(line, "Pleasant weather we're having, isn't it, Your Majesty?") != 0)){ /*Broke protocol.*/ return SQKRT_SUBJ_BADPROTO; } /*Free the line, 'cause we don't need it anymore.*/ free(line); /*Is good protocol version 1*/ /*Reply back.*/ write(fd, "Why yes it is. But come, let us get down to business. What do you seek, dear subject?\n", 88); return SQKRT_SUBJ_PROTOV1; } /*This is where we actually do something with the command.*/ int process_subj_cmd(int proto, int cmd_type, int fd){ /*Switch on the command type.*/ switch (cmd_type){ case SQKRT_SUBJ_QUIT: return SQKRT_SUBJ_QUIT; case SQKRT_SUBJ_BADCMD: /*Same thing as an unknown command.*/ default: /*Print out a nak and try again.*/ nak_subj_cmd(proto, fd); /*Return a bad command.*/ return SQKRT_SUBJ_BADCMD; } }