Here is the code for TeardownPacket, located in message.c
Code:
void TeardownPacket(char *input, int *type, char *username, char *message) {
/*given the packet as input, break it down and return the appropriate values for each parameter*/
int ulen=0;
int mlen=0;
/*type*/
*type = (int) *input;
printf("Msg received, type %i\n",*type);
/*username*/
ulen = (int) *(input+3);
memset(username,0,MAX_MLEN);
if (ulen>0) {
/*username specified*/
strncpy(username, input+4, ulen);
}
/*message*/
/*second verse, same as the first*/
mlen = (int) *(input+4+ulen);
memset(message,0,MAX_MLEN);
if (mlen>0) {
/*message specified*/
strncpy(message, input+5+ulen, mlen);
}
}
Here are some more sections of the code in main...
Code:
#include "message.c"
... some code ...
int main()
{
char servername[SHTSTR];
int lsocket;
pthread_t serverth;
struct sockaddr_in mysock;
socklen_t mysockLen;
struct sockaddr_in remoteAddr;
socklen_t remoteAddrLen;
struct pollfd skpoll;
struct hostent *hostcfg;
char myusername[MAX_ULEN];
int type;
char username[MAX_ULEN];
char message[MAX_MLEN];
char outputbuf[MAX_MSG];
char inputbuf[MAX_MSG];
... some more code
recvfrom(lsocket, &inputbuf, MAX_MSG, 0, (struct sockaddr *) &remoteAddr, &remoteAddrLen);
type=0;
TeardownPacket(inputbuf, &type, username, message);
printf("Type after TP: %i\n", type);
if (type==TYP_JOIN) {
/*connect successful, exit username input loop*/
break;
}
else if (type==TYP_MSG) {
/*unsuccessful join, display message*/
printf("%s\n", message);
}
else {
/*unknown response*/
printf("Server has responded with bad data, exiting program!\n");
exit(0);
}
}
The output I get in my shell is...
"Msg received, type 1
Type after TP: 0"
I didnt think I need to prototype it since its located in a different file that is included.
Maybe it has something to do with trying to cast a char onto an int? Would the different sizes make a problem? The value of the first byte of the input variable is 0x01, and thats why I get the 1 in the first line of output, but it goes back to zero when the function is returned.