WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sockutil.c
Go to the documentation of this file.
1 
12 //#define FILE_LOG_SILENCE
13 #include "common/common.h"
14 //#include "device/sockutil.h"
15 
16 
17 static watch_cbfunc watch_cb[TOTAL_SOCK_NUM] = {0,};
18 static uint8 watch_sock[TOTAL_SOCK_NUM] = {0,};
19 
20 
37 int8 network_init(uint8 dhcp_sock, void_func ip_update, void_func ip_conflict)
38 {
39 #define NETINIT_ADDR_SET(name_p) \
40 do { \
41  if(ip_check(DEFAULT_IP_ADDR, netinfo.ip) != RET_OK) { \
42  ERR("Default IP Addr set fail"); return RET_NOK; \
43  } else DBGA(name_p" IP Addr(%d.%d.%d.%d)", \
44  netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]); \
45  if(ip_check(DEFAULT_SN_MASK, netinfo.sn) != RET_OK) { \
46  ERR("Default SN Mask set fail"); return RET_NOK; \
47  } else DBGA(name_p" SN Mask(%d.%d.%d.%d)", \
48  netinfo.sn[0], netinfo.sn[1], netinfo.sn[2], netinfo.sn[3]); \
49  if(ip_check(DEFAULT_GW_ADDR, netinfo.gw) != RET_OK) { \
50  ERR("Default GW Addr set fail"); return RET_NOK; \
51  } else DBGA(name_p" GW Addr(%d.%d.%d.%d)", netinfo.gw[0], \
52  netinfo.gw[1], netinfo.gw[2], netinfo.gw[3]); \
53  if(ip_check(DEFAULT_DNS_ADDR, netinfo.dns) != RET_OK) { \
54  ERR("Default DNS Addr set fail"); return RET_NOK; \
55  } else DBGA(name_p" DNS Addr(%d.%d.%d.%d)", netinfo.dns[0], \
56  netinfo.dns[1], netinfo.dns[2], netinfo.dns[3]); \
57 } while(0)
58 
59  wiz_NetInfo netinfo;
60 
61  memset(&netinfo, 0, sizeof(netinfo));
62 
63  if(mac_check(DEFAULT_MAC_ADDR, netinfo.mac) != RET_OK) {
64  ERR("Default MAC Addr set fail");
65  return RET_NOK;
66  } else DBGA("Default MAC Addr(%02x:%02x:%02x:%02x:%02x:%02x)", netinfo.mac[0],
67  netinfo.mac[1], netinfo.mac[2], netinfo.mac[3], netinfo.mac[4], netinfo.mac[5]);
68 
69 #if (USE_DHCP == VAL_ENABLE)
70  NETINIT_ADDR_SET("Default"); // Set the addresses which will be used when DHCP failed
71  if(dhcp_init(dhcp_sock, ip_update, ip_conflict, &netinfo) != RET_OK)
72  return RET_NOK;
73 #else
74  NETINIT_ADDR_SET("Static");
75  netinfo.dhcp = NETINFO_STATIC;
76  SetNetInfo(&netinfo);
77  network_disp(NULL);
78 #endif
79 
80  return RET_OK;
81 }
82 
91 void network_disp(wiz_NetInfo *netinfo)
92 {
93  wiz_NetInfo cur;
94 
95  if(netinfo == NULL) {
96  GetNetInfo(&cur);
97  netinfo = &cur;
98  LOG("---------------------------------------");
99  LOG("Current Network Configuration ");
100  }
101 
102  LOG("---------------------------------------");
103  LOGA("MAC : %02X:%02X:%02X:%02X:%02X:%02X", netinfo->mac[0], netinfo->mac[1],
104  netinfo->mac[2], netinfo->mac[3], netinfo->mac[4], netinfo->mac[5]);
105  LOGA("IP : %d.%d.%d.%d", netinfo->ip[0], netinfo->ip[1], netinfo->ip[2], netinfo->ip[3]);
106  LOGA("SN : %d.%d.%d.%d", netinfo->sn[0], netinfo->sn[1], netinfo->sn[2], netinfo->sn[3]);
107  LOGA("GW : %d.%d.%d.%d", netinfo->gw[0], netinfo->gw[1], netinfo->gw[2], netinfo->gw[3]);
108  LOGA("DNS : %d.%d.%d.%d", netinfo->dns[0], netinfo->dns[1], netinfo->dns[2], netinfo->dns[3]);
109  LOGA("DHCP: %s", netinfo->dhcp==NETINFO_STATIC? "Static": "DHCP");
110  LOG("---------------------------------------");
111 }
112 
124 int8 sockwatch_open(uint8 sock, watch_cbfunc cb)
125 {
126  DBGA("WATCH Open - sock(%d), CB(%p)", sock, (void*)cb);
127  if(cb == NULL || sock >= TOTAL_SOCK_NUM) {
128  ERRA("wrong arg - sock(%d)", sock);
129  return RET_NOK;
130  }
131  if(watch_cb[sock] == NULL) watch_cb[sock] = cb;
132  else return RET_NOK;
133 
134  return RET_OK;
135 }
136 
144 int8 sockwatch_close(uint8 sock)
145 {
146  DBGA("WATCH Close - sock(%d)", sock);
147  if(sock >= TOTAL_SOCK_NUM) {
148  ERRA("wrong sock(%d)", sock);
149  return RET_NOK;
150  }
151 
153  watch_cb[sock] = NULL;
154 
155  return RET_OK;
156 }
157 
169 int8 sockwatch_set(uint8 sock, uint8 item)
170 {
171  DBGA("WATCH Set - sock(%d), item(0x%x)", sock, item);
172  if(sock >= TOTAL_SOCK_NUM) {
173  ERRA("wrong sock(%d)", sock);
174  return RET_NOK;
175  }
176 
177  BITSET(watch_sock[sock], 0x7F & item);
178 
179  return RET_OK;
180 }
181 
192 int8 sockwatch_clr(uint8 sock, uint8 item)
193 {
194  DBGA("WATCH Clear - sock(%d), item(0x%x)", sock, item);
195  if(sock >= TOTAL_SOCK_NUM) {
196  ERRA("wrong sock(%d)", sock);
197  return RET_NOK;
198  }
199 
200  BITCLR(watch_sock[sock], 0x7F & item);
201 
202  return RET_OK;
203 }
204 
213 int8 sockwatch_chk(uint8 sock, uint8 item)
214 {
215  if((sock < TOTAL_SOCK_NUM) && (watch_sock[sock] & item))
216  return RET_OK;
217 
218  return RET_NOK;
219 }
220 
225 void sockwatch_run(void)
226 {
227 #define WCF_HANDLE(item_v, ret_v) \
228 do { \
229  BITCLR(watch_sock[i], item_v); \
230  watch_cb[i](i, item_v, ret_v); \
231 } while(0)
232 
233  uint8 i;
234  int32 ret;
235 
236  for(i=0; i<TOTAL_SOCK_NUM; i++) {
237  if(watch_sock[i] == 0) continue;
238  if(watch_sock[i] & WATCH_SOCK_RECV) { // checked every time when 'connected' state
239  if(GetSocketRxRecvBufferSize(i) > 0) WCF_HANDLE(WATCH_SOCK_RECV, RET_OK);
240  }
241  if(watch_sock[i] & WATCH_SOCK_CLS_EVT) { // checked every time when 'connected' state
242  ret = TCPClsRcvCHK(i);
243  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_CLS_EVT, ret);
244  }
245  if(watch_sock[i] & WATCH_SOCK_CONN_EVT) { // checked every time when 'listen' state
246  ret = TCPConnChk(i);
247  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_CONN_EVT, ret);
248  }
249  if((watch_sock[i] & WATCH_SOCK_MASK_LOW) == 0) continue; // things which occurs occasionally will be checked all together
250  if(watch_sock[i] & WATCH_SOCK_CLS_TRY) {
251  ret = TCPCloseCHK(i);
252  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_CLS_TRY, ret);
253  }
254  if(watch_sock[i] & WATCH_SOCK_CONN_TRY) {
255  ret = TCPConnChk(i);
256  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_CONN_TRY, ret);
257  }
258  if(watch_sock[i] & WATCH_SOCK_TCP_SEND) {
259  ret = TCPSendCHK(i);
260  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_TCP_SEND, ret);
261  }
262  if(watch_sock[i] & WATCH_SOCK_UDP_SEND) {
263  ret = UDPSendCHK(i);
264  if(ret != SOCKERR_BUSY) WCF_HANDLE(WATCH_SOCK_UDP_SEND, ret);
265  }
266  }
267 
268  // ToDo: not socket part
269 
270 }
271 
280 int8 ip_check(int8 *str, uint8 *ip)
281 {
282  uint8_t cnt=0;
283  int8 tmp[16], *split;
284  int32 digit, sumchk = 0;
285 
286  digit = strlen((char*)str);
287  if(digit > 15 || digit < 7) {
288  return RET_NOK;
289  }
290 
291  strcpy((char*)tmp, (char*)str);
292  split = (int8*)strtok((char*)tmp, ".");
293  while(split != NULL && str_check(isdigit, split) == RET_OK) {
294  digit = atoi((char*)split);
295  if(digit > 255 || digit < 0) return RET_NOK;
296  if(ip) ip[cnt] = digit;
297  sumchk += digit;
298  cnt++;
299  split = (int8*)strtok(NULL, ".");
300  }
301 
302  if(cnt != 4 || sumchk == 0) { //printf("not 4 digit (%d)\r\n", cnt);
303  return RET_NOK;
304  }
305 
306  return RET_OK;
307 }
308 
317 int8 port_check(int8 *str, uint16 *port)
318 {
319  int8 *ptr;
320  uint32 val;
321 
322  val = strtol((char*)str, (char**)&ptr, 10); //printf("ptr(%p, %x), arg(%p), val(%d)\r\n", ptr, *ptr, str, val);
323 
324  if(val == 0 || val > 65535 || *ptr != 0) return RET_NOK;
325  if(port) *port = val;
326 
327  return RET_OK;
328 }
329 
338 int8 mac_check(int8 *str, uint8 *mac)
339 {
340  uint8_t cnt=0;
341  int8 tmp[18], *split;
342  int32 digit;
343 
344  if(strlen((char*)str) != 17) {
345  return RET_NOK;
346  }
347 
348  strcpy((char*)tmp, (char*)str);
349  split = (int8*)strtok((char*)tmp, ":");
350  while(split != NULL && str_check(isxdigit, split) == RET_OK) {
351  digit = strtol((char*)split, NULL, 16);
352  if(digit > 255 || digit < 0) return RET_NOK;
353  if(mac) mac[cnt] = digit;
354  cnt++;
355  split = (int8*)strtok(NULL, ":");
356  }
357 
358  if(cnt != 6) { //printf("not 6 digit (%d)\r\n", cnt);
359  return RET_NOK;
360  }
361 
362  return RET_OK;
363 }
364 
371 int8* inet_ntoa(uint32 addr)
372 {
373  static int8 addr_str[16];
374  memset(addr_str,0,16);
375  sprintf((char*)addr_str,"%d.%d.%d.%d",(int32)(addr>>24 & 0xFF),(int32)(addr>>16 & 0xFF),(int32)(addr>>8 & 0xFF),(int32)(addr & 0xFF));
376  return addr_str;
377 }
378 
386 int8* inet_ntoa_pad(uint32 addr)
387 {
388  static int8 addr_str[16];
389  memset(addr_str,0,16);
390  sprintf((char*)addr_str,"%03d.%03d.%03d.%03d",(int32)(addr>>24 & 0xFF),(int32)(addr>>16 & 0xFF),(int32)(addr>>8 & 0xFF),(int32)(addr & 0xFF));
391  return addr_str;
392 }
393 
400 uint32 inet_addr(uint8* addr)
401 {
402  int8 i;
403  uint32 inetaddr = 0;
404  int8 taddr[30];
405  int8 * nexttok;
406  int32 num;
407  strcpy((char*)taddr,(char*)addr);
408 
409  nexttok = taddr;
410  for(i = 0; i < 4 ; i++)
411  {
412  nexttok = (int8*)strtok((char*)nexttok,".");
413  if(nexttok[0] == '0' && nexttok[1] == 'x') num = strtol((char*)nexttok+2, NULL, 16);
414  else num = strtol((char*)nexttok, NULL, 10);
415  inetaddr = inetaddr << 8;
416  inetaddr |= (num & 0xFF);
417  nexttok = NULL;
418  }
419  return inetaddr;
420 }
421 
428 uint16 swaps(uint16 i)
429 {
430  uint16 ret=0;
431  ret = (i & 0xFF) << 8;
432  ret |= ((i >> 8)& 0xFF);
433  return ret;
434 }
435 
442 uint32 swapl(uint32 l)
443 {
444  uint32 ret=0;
445  ret = (l & 0xFF) << 24;
446  ret |= ((l >> 8) & 0xFF) << 16;
447  ret |= ((l >> 16) & 0xFF) << 8;
448  ret |= ((l >> 24) & 0xFF);
449  return ret;
450 }
451 
458 uint16 htons(uint16 hostshort)
459 {
460 #ifdef SYSTEM_LITTLE_ENDIAN
461  return swaps(hostshort);
462 #else
463  return hostshort;
464 #endif
465 }
466 
467 
474 uint32 htonl(uint32 hostlong)
475 {
476 #ifdef SYSTEM_LITTLE_ENDIAN
477  return swapl(hostlong);
478 #else
479  return hostlong;
480 #endif
481 }
482 
483 
491 uint32 ntohs(uint16 netshort)
492 {
493 #ifdef SYSTEM_LITTLE_ENDIAN
494  return htons(netshort);
495 #else
496  return netshort;
497 #endif
498 }
499 
507 uint32 ntohl(uint32 netlong)
508 {
509 #ifdef SYSTEM_LITTLE_ENDIAN
510  return swapl(netlong);
511 #else
512  return netlong;
513 #endif
514 }