2 * Acess2 Networking Test Suite (NetTest)
3 * - By John Hodge (thePowersGang)
6 * - General purpose helper functions
10 #include <IPStack/ipstack.h>
11 #include <IPStack/interface.h>
14 //extern tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name);
15 extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
18 int Net_ParseAddress(const char *String, void *Addr);
21 int NetTest_AddAddress(char *SetAddrString)
24 int addrtype, netmask;
25 char *ifend, *addrend, *end;
28 // <interface>,<ip>/<netmask>
29 ifend = strchr(SetAddrString, ',');
31 addrend = strchr(ifend+1, '/');
34 addrtype = Net_ParseAddress(ifend+1, addr);
35 netmask = strtol(addrend+1, &end, 10);
40 if( *end != '\0' || addrtype == 0 )
45 tInterface *iface = IPStack_AddInterface(SetAddrString, addrtype, "");
51 // Set interface address
52 iface->Node.Type->IOCtl(&iface->Node, 6, addr);
53 iface->Node.Type->IOCtl(&iface->Node, 7, &netmask);
54 // iface->Node.Type->Close(&iface->Node);
59 // ----------------------------------
60 // Copy-pasta'd from userland libnet
61 // ----------------------------------
63 * \brief Read an IPv4 Address
64 * \param String IPv4 dotted decimal address
65 * \param Addr Output 32-bit representation of IP address
66 * \return Boolean success
68 static int Net_ParseIPv4Addr(const char *String, uint8_t *Addr)
74 for( j = 0; String[i] && j < 4; j ++ )
77 for( ; String[i] && String[i] != '.'; i++ )
79 if('0' > String[i] || String[i] > '9') {
82 val = val*10 + String[i] - '0';
95 if(String[i] != '\0') {
102 * \brief Read an IPv6 Address
103 * \param String IPv6 colon-hex representation
104 * \param Addr Output 128-bit representation of IP address
105 * \return Boolean success
107 static int Net_ParseIPv6Addr(const char *String, uint8_t *Addr)
111 int val, split = -1, end;
112 uint16_t hi[8], low[8];
114 for( j = 0; String[i] && j < 8; j ++ )
116 if(String[i] == ':') {
126 for( k = 0; String[i] && String[i] != ':'; i++, k++ )
129 if('0' <= String[i] && String[i] <= '9')
130 val += String[i] - '0';
131 else if('A' <= String[i] && String[i] <= 'F')
132 val += String[i] - 'A' + 10;
133 else if('a' <= String[i] && String[i] <= 'f')
134 val += String[i] - 'a' + 10;
149 if( String[i] == ':' ) {
155 // Create final address
157 for( j = 0; j < split; j ++ )
159 Addr[j*2] = hi[j]>>8;
160 Addr[j*2+1] = hi[j]&0xFF;
163 for( ; j < 8 - (end - split); j++ )
170 for( ; j < 8; j ++, k++)
172 Addr[j*2] = low[k]>>8;
173 Addr[j*2+1] = low[k]&0xFF;
180 * \brief Parse an address from a string
181 * \param String String containing an IPv4/IPv6 address
182 * \param Addr Buffer for the address (must be >= 16 bytes)
183 * \return Address type
184 * \retval 0 Unknown address type
185 * \retval 4 IPv4 Address
186 * \retval 6 IPv6 Address
188 int Net_ParseAddress(const char *String, void *Addr)
190 if( Net_ParseIPv4Addr(String, Addr) )
193 if( Net_ParseIPv6Addr(String, Addr) )
199 int Net_OpenSocket(int AddrType, void *Addr, const char *Filename)
201 int addrLen = IPStack_GetAddressSize(AddrType);
203 uint8_t *addrBuffer = Addr;
204 char hexAddr[addrLen*2+1];
206 for( i = 0; i < addrLen; i ++ )
207 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
211 int len = snprintf(NULL, 0, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
213 snprintf(path, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
214 return VFS_Open(path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
218 int len = snprintf(NULL, 0, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
220 snprintf(path, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
221 return VFS_Open(path, VFS_OPENFLAG_READ);
225 int Net_OpenSocket_TCPC(int AddrType, void *Addr, int Port)
227 int fd = Net_OpenSocket(AddrType, Addr, "tcpc");
228 if( fd == -1 ) return -1;
230 VFS_IOCtl(fd, 5, &Port); // Remote Port
231 VFS_IOCtl(fd, 6, Addr); // Remote address
232 VFS_IOCtl(fd, 7, NULL); // connect