BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
fb91a00
Branches
Tags
master
libetherpix
src
parse.c
v1.0.5
Stefan Schuermans
commited
fb91a00
at 2011-09-11 17:17:19
parse.c
Blame
History
Raw
/* * FlexiPix library * !version: 1.0.5! !date: 2010-12-22! * * Copyright 2010 Stefan Schuermans <stefan schuermans info> * * 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, version 3 of the License. * * * 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 Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <stdlib.h> #include <string.h> #include <intern/net.h> #include <intern/parse.h> /** * \brief parse two comma-separated numbers * * \param[in] text text to parse * \param[out] no1 first number parsed from text * \param[out] no2 second number parsed from text * \param[out] end location in text where parsing ended * \return 0 in case of success, -1 in case of error */ int flp_parse_two_nos(char *text, unsigned int *no1, unsigned int *no2, char **end) { char *ptr1, *ptr2; unsigned long val1, val2; /* parse first number */ val1 = strtoul(text, &ptr1, 0); if (ptr1 == text || *ptr1 != ',') return -1; /* skip comma */ ptr1++; /* parse second number */ val2 = strtoul(ptr1, &ptr2, 0); if (ptr2 == ptr1 || (*ptr2 != 0 && *ptr2 != ' ' && *ptr2 != '\t' && *ptr2 != '\r' && *ptr2 != '\n')) return -1; /* return numbers and pointer behind end */ *no1 = (unsigned int)val1; *no2 = (unsigned int)val2; *end = ptr2; return 0; } /** * \brief parse an address * * \param[in] test test to parse * \param[out] addr address parsed from text * \return 0 in case of success, -1 in case of error */ int flp_parse_addr(char *text, struct sockaddr_in *addr) { char *p_colon, *p_host, *p_port, *p_port_end; in_addr_t ip; unsigned long port; struct hostent *p_hostent; /* split address into IP and port */ p_colon = strchr(text, ':'); if (p_colon) { *p_colon = 0; p_host = text; p_port = p_colon + 1; } else { p_host = text; p_port = text + strlen(text); } /* parse host */ ip = inet_addr(p_host); if (ip == htonl(INADDR_NONE)) { /* try to resolve host */ p_hostent = gethostbyname(p_host); if (!p_hostent || !p_hostent->h_addr_list || !p_hostent->h_addr_list[0]) { /* restore text before leaving */ if (p_colon) *p_colon = ':'; return -1; } ip = *(in_addr_t *)p_hostent->h_addr_list[0]; } /* parse port */ port = strtoul(p_port, &p_port_end, 0); if (*p_port_end || /* not all characters of port were valid */ port > 65535) { /* invalid port number */ /* restore text before leaving */ if (p_colon) *p_colon = ':'; return -1; } /* construct address */ addr->sin_family = AF_INET; addr->sin_port = htons((in_port_t) port); addr->sin_addr.s_addr = ip; /* restore text before leaving */ if (p_colon) *p_colon = ':'; return 0; }