Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

aton.c

Go to the documentation of this file.
00001 /*
00002  * aton.c
00003  *
00004  * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: aton.c,v 1.3 2002/10/14 15:42:10 dugsong Exp $
00007  */
00008 
00009 #include "config.h"
00010 
00011 #include <sys/types.h>
00012 
00013 #include <ctype.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #include "dnet.h"
00020 #include "aton.h"
00021 
00022 int
00023 type_aton(char *string, uint16_t *type)
00024 {
00025         u_int i;
00026 
00027         if (strcmp(string, "ip") == 0)
00028                 *type = htons(ETH_TYPE_IP);
00029         else if (strcmp(string, "arp") == 0)
00030                 *type = htons(ETH_TYPE_ARP);
00031         else {
00032                 if (sscanf(string, "%i", &i) != 1 || i > 0xffff)
00033                         return (-1);
00034                 *type = htons(i & 0xffff);
00035         }
00036         return (0);
00037 }
00038 
00039 int
00040 op_aton(char *string, uint16_t *op)
00041 {
00042         u_int i;
00043 
00044         if (strncasecmp(string, "req", 3) == 0)
00045                 *op = htons(ARP_OP_REQUEST);
00046         else if (strncasecmp(string, "rep", 3) == 0)
00047                 *op = htons(ARP_OP_REPLY);
00048         else if (strncasecmp(string, "revreq", 6) == 0)
00049                 *op = htons(ARP_OP_REVREQUEST);
00050         else if (strncasecmp(string, "revrep", 6) == 0)
00051                 *op = htons(ARP_OP_REVREPLY);
00052         else {
00053                 if (sscanf(string, "%i", &i) != 1 || i > 0xffff)
00054                         return (-1);
00055                 *op = htons(i & 0xffff);
00056         }
00057         return (0);
00058 }
00059 
00060 int
00061 proto_aton(char *string, uint8_t *proto)
00062 {
00063         struct protoent *pp;
00064         u_int i;
00065         
00066         if ((pp = getprotobyname(string)) != NULL)
00067                 *proto = pp->p_proto;
00068         else {
00069                 if (sscanf(string, "%i", &i) != 1 || i > 0xff)
00070                         return (-1);
00071                 *proto = i & 0xff;
00072         }
00073         return (0);
00074 }
00075 
00076 int
00077 off_aton(char *string, uint16_t *off)
00078 {
00079         int i;
00080         char *p;
00081 
00082         if (strncmp(string, "0x", 2) == 0) {
00083                 if (sscanf(string, "%i", &i) != 1 || i > IP_OFFMASK)
00084                         return (-1);
00085                 *off = htons(i);
00086         } else {
00087                 i = strtol(string, &p, 10);
00088                 if (*string == '\0' || (*p != '\0' && *p != '+') ||
00089                     i > IP_OFFMASK)
00090                         return (-1);
00091                 *off = htons(((*p == '+') ? IP_MF : 0) | (i >> 3));
00092         }
00093         return (0);
00094 }
00095 
00096 int
00097 port_aton(char *string, uint16_t *port)
00098 {
00099         struct servent *sp;
00100         u_int i;
00101         
00102         /* XXX */
00103         if ((sp = getservbyname(string, "tcp")) != NULL) {
00104                 *port = sp->s_port;
00105         } else if ((sp = getservbyname(string, "udp")) != NULL) {
00106                 *port = sp->s_port;
00107         } else {
00108                 if (sscanf(string, "%i", &i) != 1 || i > 0xffff)
00109                         return (-1);
00110                 *port = htons(i & 0xffff);
00111         }
00112         return (0);
00113 }
00114 
00115 int
00116 seq_aton(char *string, uint32_t *seq)
00117 {
00118         char *p;
00119         
00120         *seq = strtoul(string, &p, 10);
00121         if (*string == '\0' || *p != '\0')
00122                 return (-1);
00123 
00124         *seq = htonl(*seq);
00125         return (0);
00126 }
00127 
00128 int
00129 flags_aton(char *string, uint8_t *flags)
00130 {
00131         char *p;
00132         u_int i;
00133 
00134         *flags = 0;
00135         
00136         if (strncmp(string, "0x", 2) == 0) {
00137                 if (sscanf(string, "%i", &i) != 1 || i > 0xff)
00138                         return (-1);
00139                 *flags = i & 0xff;
00140                 
00141                 return (0);
00142         }
00143         for (p = string; *p != '\0'; p++) {
00144                 switch (*p) {
00145                 case 'S':
00146                         *flags |= TH_SYN;
00147                         break;
00148                 case 'A':
00149                         *flags |= TH_ACK;
00150                         break;
00151                 case 'F':
00152                         *flags |= TH_FIN;
00153                         break;
00154                 case 'R':
00155                         *flags |= TH_RST;
00156                         break;
00157                 case 'P':
00158                         *flags |= TH_PUSH;
00159                         break;
00160                 case 'U':
00161                         *flags |= TH_URG;
00162                         break;
00163                 default:
00164                         return (-1);
00165                 }
00166         }
00167         return (0);
00168 }
00169 
00170 static u_char
00171 hex2num(char ch, char cl)
00172 {
00173         ch = tolower(ch), cl = tolower(cl);
00174         
00175         if (ch >= '0' && ch <= '9')
00176                 ch -= '0';
00177         else if (ch >= 'a' && ch <= 'f')
00178                 ch -= 'a' - 10;
00179         else
00180                 return (0);
00181         
00182         if (cl >= '0' && cl <= '9')
00183                 cl -= '0';
00184         else if (cl >= 'a' && cl <= 'f')
00185                 cl -= 'a' - 10;
00186         else
00187                 return (0);
00188         
00189         return ((u_char)((ch << 4) | cl));
00190 }
00191 
00192 int
00193 fmt_aton(char *string, u_char *buf)
00194 {
00195         u_char *u;
00196         char *p;
00197 
00198         for (u = buf, p = string; *p != '\0'; p++) {
00199                 if (*p != '\\') {
00200                         *u++ = *p;
00201                         continue;
00202                 }
00203                 if (*++p == '\0')
00204                         break;
00205 
00206                 if (*p == '\\')         *u++ = '\\';
00207                 else if (*p == '\'')    *u++ = '\'';
00208                 else if (*p == '"')     *u++ = '"';
00209                 else if (*p == 'a')     *u++ = '\a';
00210                 else if (*p == 'b')     *u++ = '\b';
00211                 else if (*p == 'e')     *u++ = 033;
00212                 else if (*p == 'f')     *u++ = '\f';
00213                 else if (*p == 'n')     *u++ = '\n';
00214                 else if (*p == 'r')     *u++ = '\r';
00215                 else if (*p == 't')     *u++ = '\t';
00216                 else if (*p == 'v')     *u++ = '\v';
00217                 else if (*p == 'x' && p[1] != '\0' && p[2] != '\0') {
00218                         *u++ = hex2num(p[1], p[2]);
00219                         p += 2;
00220                 } else
00221                         return (-1);
00222         }
00223         return (u - buf);
00224 }

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2