📄 parser.c
字号:
/* * Embedded SLP Message Parsers * * ./software/ch10/emslp/parser.c * * mtj@cogitollc.com * */#include <stdio.h>#include <string.h>#include "manip.h"#include "slppriv.h"/* * slpParseServiceReply() * * Parse a service reply message from an SLP message. * */int slpParseServiceReply( slpMsgType *slpMsg, slpParsedServiceReplyType *reply ){ uint8_t version, messageType; uint16_t xid, langLen, numUrls; int len; if ((slpMsg == NULL) || (reply == NULL)) { return -1; } /* * First, verify that the version is correct and that the response is * a service reply. */ getByte(slpMsg, &version); getByte(slpMsg, &messageType); if (version != 2) { return -2; } getTriOctet(slpMsg, (uint32_t *)&len); /* Skip some unused portions of the header */ getTriOctet(slpMsg, NULL); getShort(slpMsg, NULL); getShort(slpMsg, &xid); getShort(slpMsg, &langLen); getString(slpMsg, langLen, NULL); if (messageType == SRV_REPLY) { /* Store the error code and URL count into the reply */ getShort( slpMsg, &reply->errorcode ); getShort( slpMsg, (uint16_t *)&numUrls ); if ((reply->errorcode != 0) || (numUrls == 0)) return -3; while ((numUrls--) && (reply->urlCount < MAX_URLS)) { getByte( slpMsg, NULL ); // Reserved Field getShort( slpMsg, &reply->urls[reply->urlCount].lifetime ); getShort( slpMsg, (uint16_t *)&len ); getString( slpMsg, len, (char *)&reply->urls[reply->urlCount].url[0] ); getByte( slpMsg, NULL ); // Num Auth Blocks reply->urlCount++; } } else if (messageType == DA_ADVERT) { /* Store the error code count into the reply */ getShort( slpMsg, &reply->errorcode ); getShort( slpMsg, NULL ); getShort( slpMsg, NULL ); /* Get the directory-agent URL */ getShort( slpMsg, (uint16_t *)&len ); getString( slpMsg, len, (char *)&reply->urls[reply->urlCount].url[0] ); if (len) reply->urlCount++; } return 0;}/* * slpParseServiceTypesReply() * * Parse a service type reply message from an SLP message. * */int slpParseServiceTypesReply( slpMsgType *slpMsg, slpParsedServiceTypeReplyType *reply ){ uint8_t version, messageType; uint16_t xid, langLen, serviceOctets; int len, curOctet, srvIndex, srvChar; if ((slpMsg == NULL) || (reply == NULL)) { return -1; } /* * First, verify that the version is correct and that the response is * a service-type reply. */ getByte(slpMsg, &version); getByte(slpMsg, &messageType); if ((version != 2) || (messageType != SRV_TYPE_REPLY)) { return -2; } getTriOctet(slpMsg, (uint32_t *)&len); /* Skip some unused portions of the header */ getTriOctet(slpMsg, NULL); getShort(slpMsg, NULL); getShort(slpMsg, &xid); getShort(slpMsg, &langLen); getString(slpMsg, langLen, NULL); /* Store the error code and Services count into the reply */ getShort( slpMsg, &reply->errorcode ); getShort( slpMsg, (uint16_t *)&serviceOctets ); if ((reply->errorcode != 0) || (serviceOctets == 0)) return -3; curOctet = 0; srvIndex = 0; srvChar = 0; while ((curOctet < serviceOctets) && (reply->srvCount < MAX_SERVICE_TYPES)) { if (slpMsg->buffer[slpMsg->index] == ',') { reply->service[srvIndex][srvChar] = 0; slpMsg->index++; srvChar = 0; srvIndex++; } else if (slpMsg->buffer[slpMsg->index] == 0) { srvIndex++; break; } else { reply->service[srvIndex][srvChar++] = slpMsg->buffer[slpMsg->index++]; } } reply->srvCount = srvIndex; return 0;}/* * slpParseServiceURL() * * Parse a URL into its distinct elements. * */int slpParseServiceURL ( char *url, char *serviceType, char *addrSpec, char *port ){ int i, j, len; if ((url == NULL) || (serviceType == NULL) || (addrSpec == NULL) || (port == NULL)) { return -1; } serviceType[0] = 0; addrSpec[0] = 0; port[0] = 0; if (strncmp(url, "service:", 8)) return -1; len = strlen(url); j = 0; i = 8; while ((i < len) && (url[i] != ':') && (url[i] != 0)) { serviceType[j++] = url[i++]; } serviceType[j] = 0; if (!((url[i] == ':') && (url[i+1] == '/'))) { return -1; } j = 0; i+=3; while ((i < len) && (url[i] != ':') && (url[i] != 0)) { addrSpec[j++] = url[i++]; } addrSpec[j] = 0; if (url[i] == 0) return 0; j = 0; i++; while ((i < len) && (url[i] != 0)) { port[j++] = url[i++]; } port[j] = 0; return 0;}/* * Copyright (c) 2002 Charles River Media. All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, is hereby granted without fee provided * that the following conditions are met: * * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * 3. Neither the name of Charles River Media nor the names of * its contributors may be used to endorse or promote products * derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY CHARLES RIVER MEDIA AND CONTRIBUTERS * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHARLES * RIVER MEDIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARAY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -