2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
11 #include <string.h> // memcpy
12 #include <acess/sys.h>
15 UDP_IOCTL_GETSETLPORT = 4,
16 UDP_IOCTL_GETSETRPORT,
17 UDP_IOCTL_GETSETRMASK,
23 int Net_OpenSocket(int AddrType, const void *Addr, const char *Filename)
25 int addrLen = Net_GetAddressSize(AddrType);
26 char hexAddr[addrLen*2+1];
29 const uint8_t *addrBuffer = Addr;
30 for( unsigned int i = 0; i < addrLen; i ++ )
31 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
36 int len = snprintf(NULL, 0, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
38 snprintf(path, len+1, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
39 _SysDebug("%s", path);
40 return _SysOpen(path, OPENFLAG_READ|OPENFLAG_WRITE);
44 int len = snprintf(NULL, 0, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
46 snprintf(path, len+1, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
47 return _SysOpen(path, OPENFLAG_READ);
51 int Net_OpenSocket_TCPC(int AddrType, const void *Addr, int Port)
53 int fd = Net_OpenSocket(AddrType, Addr, "tcpc");
54 if( fd == -1 ) return -1;
56 if( _SysIOCtl(fd, 5, &Port) < 0 ) // Remote Port
58 if( _SysIOCtl(fd, 6, (void*)Addr) < 0 ) // Remote address (kernel shouldn't modify)
60 if( _SysIOCtl(fd, 7, NULL) < 0) // connect
68 int Net_OpenSocket_UDP(int AddrType, const void *Addr, int RPort, int LPort)
70 int fd = Net_OpenSocket(AddrType, Addr, "udp");
71 if( fd == -1 ) return -1;
73 if( _SysIOCtl(fd, UDP_IOCTL_GETSETLPORT, &LPort) < 0 )
75 if( _SysIOCtl(fd, UDP_IOCTL_GETSETRPORT, &RPort) < 0 )
77 int maskbits = Net_GetAddressSize(AddrType) * 8;
78 if( _SysIOCtl(fd, UDP_IOCTL_GETSETRMASK, &maskbits) < 0 )
80 if( _SysIOCtl(fd, UDP_IOCTL_SETRADDR, (void*)Addr) < 0 ) // Remote address (kernel shouldn't modify)
88 int Net_UDP_SendTo(int FD, int Port, int AddrType, const void *Addr, size_t Length, const void *Data)
96 ep.addr_type = AddrType;
97 memcpy(ep.addr, Addr, Net_GetAddressSize(AddrType));
102 } info = { .ep = &ep, .buf = Data, .len = Length };
104 return _SysIOCtl(FD, UDP_IOCTL_SENDTO, &info);
107 int Net_UDP_RecvFrom(int FD, int* Port, int* AddrType, void *Addr, size_t Length, void *Data)
118 } info = { .ep = &ep, .buf = Data, .len = Length };
120 int rv = _SysIOCtl(FD, UDP_IOCTL_RECVFROM, &info);
123 if(Port) *Port = ep.port;
124 if(AddrType) *AddrType = ep.addr_type;
125 if(Addr) memcpy(Addr, ep.addr, Net_GetAddressSize(ep.addr_type));