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

util_net.c

Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include <stdio.h>
00006 
00007 #ifndef WIN32
00008 /* for inet_ntoa */
00009 #include <sys/types.h>
00010 #include <sys/socket.h>
00011 #include <netinet/in.h>
00012 #include <arpa/inet.h>
00013 #endif /* WIN32 */
00014 
00015 #include <string.h>
00016 
00017 #include "util_net.h"
00018 
00019 /** 
00020  * give a textual representation of tcp flags
00021  * 
00022  * @param flags tcph->flags
00023  * 
00024  * @return ptr to a static buffer w/ the string represented
00025  */
00026 char * mktcpflag_str(int flags)
00027 {
00028     static char buf[9];    
00029     const int fin      = 0x01;
00030     const int syn      = 0x02;
00031     const int rst      = 0x04;
00032     const int psh      = 0x08;
00033     const int ack      = 0x10;
00034     const int urg      = 0x20;
00035     const int cwr      = 0x40;
00036     const int ecn_echo = 0x80;
00037     
00038     memset(buf, '-', 9);
00039     
00040     if(flags & fin)
00041         buf[0] = 'F';
00042 
00043     if(flags & syn)
00044         buf[1] = 'S';
00045     
00046     if(flags & rst)
00047         buf[2] = 'R';
00048 
00049     if(flags & psh)
00050         buf[3] = 'P';
00051 
00052     if(flags & ack)
00053         buf[4] = 'A';
00054 
00055     if(flags & urg)
00056         buf[5] = 'U';
00057     
00058     if(flags & cwr)
00059         buf[6] = 'C';
00060 
00061     if(flags & ecn_echo)
00062         buf[7] = 'E';
00063 
00064     buf[8] = '\0';
00065 
00066     return buf;
00067 }
00068 
00069 /** 
00070  * A inet_ntoa that has 2 static buffers that are changed between
00071  * subsequent calls
00072  * 
00073  * @param ip ip in NETWORK BYTE ORDER
00074  */
00075 char *inet_ntoax(u_int32_t ip)
00076 {
00077     static char s_buf1[16];
00078     static char s_buf2[16];
00079     static int which = 0;
00080     char *buf;
00081     char *net_str;
00082 
00083     net_str = inet_ntoa(*(struct in_addr *)&ip);
00084 
00085     if(which)
00086     {
00087         buf = s_buf2;
00088         which = 0;
00089     }
00090     else
00091     {
00092         buf = s_buf1;
00093         which = 1;
00094     }
00095 
00096     snprintf(buf, 16, "%s", net_str);
00097 
00098     return buf;    
00099 }
00100 
00101 
00102 #ifdef TEST_UTIL_NET
00103 int main(void)
00104 {
00105     u_int32_t ip1 = htonl(0xFF00FF00);
00106     u_int32_t ip2 = htonl(0xFFAAFFAA);
00107         
00108     printf("%s -> %s\n", inet_ntoax(ip1), inet_ntoax(ip2));
00109 
00110     /* the following one is invalid and will break the first one*/
00111     printf("%s -> %s -> %s\n", inet_ntoax(ip1), inet_ntoax(ip2), inet_ntoax(ip2));
00112     return 0;
00113 }
00114 #endif /* TEST_UTIL_NET */

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