📄 parse.c
字号:
/* * Embedded SMTP Server Attachment Parsing Functions * * ./software/ch5/emsmtpd/parse.c * * mtj@cogitollc.com * * Copyright 2001,2002 M. Tim Jones <mtj@cogitollc.com> * * 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, 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. * */#include <stdio.h>#include <errno.h>#include "server.h"#include "client.h"/* * parseMail() * * Performs mail parsing for content after the mail header. This * includes the subject and a single file attachment of any kind. * The mail arguemnt is the incoming mail structure and len is the * length of the mail body. * * The subject line will contain the commmand: * * STATUS - Return a device status email * UPDATE - Parse out the attachment * * Returns -1 on failure or 0 on success. * */int parseMail(parseMailType *mail, int len){ int i = 0, index; int extractAttachment ( parseMailType *, int, int ); // First, grab the subject which is the location for the file for (i = 0 ; i < len ; i++) { if (mail->rawMail[i] == 'S') { if (!strncmp(&mail->rawMail[i], "Subject:", 8)) { i+= 9; index = 0; while ((mail->rawMail[i] != 0x0d) && (mail->rawMail[i] != 0x0a)) { mail->subject[index++] = mail->rawMail[i++]; } mail->subject[index] = 0; break; } } } if (i == len) { return(-1); } /* This is our parser. We look at the subject of the received email * and then determine how to deal with the email and its contents. */ if (!strncmp(mail->subject, "STATUS", 6)) { emitStatusResponse(mail); } else if (!strncmp(mail->subject, "UPDATE", 6)) { /* Extract the attachment from the email */ extractAttachment(mail, i, len); /* At this point, the mail structure will have the attachment stored * in the attachment array. The attachlen will contain the length of * the attachment, or -1 if an error occured. */ emitUpdateResponse(mail); } else { /* Don't understand the command, silently ignore... */ } return 0;}/* * extractAttachment() * * Extract a single attachement from the body of the email and store it * in the attachment array. The mail structure is the incoming email, * i is the current position in the rawMail array and len is the total * length of the email body. * * Returns -1 on failure, 0 on success. * */int extractAttachment ( parseMailType *mail, int i, int len ){ int index, status; // Next, grab the filename for ( ; i < len ; i++) { if (mail->rawMail[i] == 'f') { if (!strncmp(&mail->rawMail[i], "filename=", 9)) { i+= 10; index = 0; while ((mail->rawMail[i] != 0x0d) && (mail->rawMail[i] != 0x0a)) { if (mail->rawMail[i] == '"') i++; else mail->filename[index++] = mail->rawMail[i++]; } mail->filename[index] = 0; break; } } } if (i == len) { printf("Couldn't find the filename...\n"); return(-1); } else { printf("The filename was [%s]\n", mail->filename); } /* Finally, find the start of the Base64 encoded data and decode... */ for ( ; i < len ; i++) { /* We're looking for a CR/LF on a blank line after the * filename specification (since this is in the attachment * boundary). */ if ((mail->rawMail[i] == 0x0d) && (mail->rawMail[i+1] == 0x0a) && (mail->rawMail[i+2] == 0x0d) && (mail->rawMail[i+3] == 0x0a)) { i+=4; break; } } if (i == len) { printf("Couldn't find the Base64 MIME section...\n"); return(-1); } /* At this point, i is the index to the start of the Base64 * encoded section, start the decoding process... */ mail->attachlen = b64decode(&mail->rawMail[i], mail->attachment); return(mail->attachlen ? 0 : -1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -