sal_socket.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-23 ChenYong First version
  9. * 2018-11-12 ChenYong Add TLS support
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <string.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <sal_socket.h>
  19. #include <sal_netdb.h>
  20. #ifdef SAL_USING_TLS
  21. #include <sal_tls.h>
  22. #endif
  23. #include <sal_low_lvl.h>
  24. #include <netdev.h>
  25. #ifdef SAL_INTERNET_CHECK
  26. #include <ipc/workqueue.h>
  27. #endif
  28. #ifdef RT_USING_LWP
  29. #include <lwp_sys_socket.h>
  30. #endif
  31. /* check system workqueue stack size */
  32. #if defined(SAL_INTERNET_CHECK) && RT_SYSTEM_WORKQUEUE_STACKSIZE < 1536
  33. #error "The system workqueue stack size must more than 1536 bytes"
  34. #endif
  35. #define DBG_TAG "sal.skt"
  36. #define DBG_LVL DBG_INFO
  37. #include <rtdbg.h>
  38. #define VALID_PROTOCOL(protocol) ((protocol) >= 0 && (protocol) <= IPPROTO_RAW)
  39. #define VALID_COMBO(domain, type, protocol) \
  40. ( \
  41. (((domain) == AF_INET || (domain) == AF_INET6) && \
  42. (((type) == SOCK_STREAM && ((protocol) == 0 || (protocol) == IPPROTO_TCP)) || \
  43. ((type) == SOCK_DGRAM && ((protocol) == 0 || (protocol) == IPPROTO_UDP)) || \
  44. ((type) == SOCK_RAW && ((protocol) == IPPROTO_RAW)) \
  45. )) || \
  46. ((domain) == AF_UNIX && (type) == SOCK_STREAM && (protocol) == 0) || \
  47. ((domain) == AF_NETLINK && (type) == SOCK_RAW && (protocol) == 0) \
  48. )
  49. /* the socket table used to dynamic allocate sockets */
  50. struct sal_socket_table
  51. {
  52. uint32_t max_socket;
  53. struct sal_socket **sockets;
  54. };
  55. /* record the netdev and res table*/
  56. struct sal_netdev_res_table
  57. {
  58. struct addrinfo *res;
  59. struct netdev *netdev;
  60. };
  61. struct ifconf
  62. {
  63. int ifc_len; /* Size of buffer. */
  64. union
  65. {
  66. char *ifcu_buf;
  67. struct sal_ifreq *ifcu_req;
  68. } ifc_ifcu;
  69. };
  70. #ifdef SAL_USING_TLS
  71. /* The global TLS protocol options */
  72. static struct sal_proto_tls *proto_tls;
  73. #endif
  74. /* The global socket table */
  75. static struct sal_socket_table socket_table;
  76. static struct rt_mutex sal_core_lock;
  77. static rt_bool_t init_ok = RT_FALSE;
  78. static struct sal_netdev_res_table sal_dev_res_tbl[SAL_SOCKETS_NUM];
  79. #define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \
  80. ((sock)->protocol == PROTOCOL_DTLS))
  81. #define SAL_SOCKOPS_PROTO_TLS_VALID(sock, name) (proto_tls && (proto_tls->ops->name) && IS_SOCKET_PROTO_TLS(sock))
  82. #define SAL_SOCKOPT_PROTO_TLS_EXEC(sock, name, optval, optlen) \
  83. do \
  84. { \
  85. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)) \
  86. { \
  87. return proto_tls->ops->name((sock)->user_data_tls, (optval), (optlen)); \
  88. } \
  89. } while (0)
  90. #define SAL_SOCKET_OBJ_GET(sock, socket) \
  91. do \
  92. { \
  93. (sock) = sal_get_socket(socket); \
  94. if ((sock) == RT_NULL) \
  95. { \
  96. return -1; \
  97. } \
  98. } while (0)
  99. #define SAL_NETDEV_IS_UP(netdev) \
  100. do \
  101. { \
  102. if (!netdev_is_up(netdev)) \
  103. { \
  104. return -1; \
  105. } \
  106. } while (0)
  107. #define SAL_NETDEV_SOCKETOPS_VALID(netdev, pf, ops) \
  108. do \
  109. { \
  110. (pf) = (struct sal_proto_family *)netdev->sal_user_data; \
  111. if ((pf)->skt_ops->ops == RT_NULL) \
  112. { \
  113. return -1; \
  114. } \
  115. } while (0)
  116. #define SAL_NETDEV_NETDBOPS_VALID(netdev, pf, ops) \
  117. ((netdev) && netdev_is_up(netdev) && \
  118. ((pf) = (struct sal_proto_family *)(netdev)->sal_user_data) != RT_NULL && \
  119. (pf)->netdb_ops->ops)
  120. #define SAL_NETDBOPS_VALID(netdev, pf, ops) \
  121. ((netdev) && \
  122. ((pf) = (struct sal_proto_family *)(netdev)->sal_user_data) != RT_NULL && \
  123. (pf)->netdb_ops->ops)
  124. /**
  125. * SAL (Socket Abstraction Layer) initialize.
  126. *
  127. * @return result 0: initialize success
  128. * -1: initialize failed
  129. */
  130. int sal_init(void)
  131. {
  132. int cn;
  133. if (init_ok)
  134. {
  135. LOG_D("Socket Abstraction Layer is already initialized.");
  136. return 0;
  137. }
  138. /* init sal socket table */
  139. cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
  140. socket_table.max_socket = cn;
  141. socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
  142. if (socket_table.sockets == RT_NULL)
  143. {
  144. LOG_E("No memory for socket table.\n");
  145. return -1;
  146. }
  147. /*init the dev_res table */
  148. rt_memset(sal_dev_res_tbl, 0, sizeof(sal_dev_res_tbl));
  149. /* create sal socket lock */
  150. rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_PRIO);
  151. LOG_I("Socket Abstraction Layer initialize success.");
  152. init_ok = RT_TRUE;
  153. return 0;
  154. }
  155. INIT_COMPONENT_EXPORT(sal_init);
  156. #ifdef SAL_INTERNET_CHECK
  157. /* check SAL network interface device internet status */
  158. static void check_netdev_internet_up_work(struct rt_work *work, void *work_data)
  159. {
  160. #define SAL_INTERNET_VERSION 0x00
  161. #define SAL_INTERNET_BUFF_LEN 12
  162. #define SAL_INTERNET_TIMEOUT (2)
  163. #define SAL_INTERNET_HOST "link.rt-thread.org"
  164. #define SAL_INTERNET_PORT 8101
  165. #define SAL_INTERNET_MONTH_LEN 4
  166. #define SAL_INTERNET_DATE_LEN 16
  167. unsigned int index;
  168. int sockfd = -1, result = 0;
  169. struct sockaddr_in server_addr;
  170. struct hostent *host;
  171. struct timeval timeout;
  172. struct netdev *netdev = (struct netdev *)work_data;
  173. socklen_t addr_len = sizeof(struct sockaddr_in);
  174. char send_data[SAL_INTERNET_BUFF_LEN], recv_data = 0;
  175. const char month[][SAL_INTERNET_MONTH_LEN] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  176. char date[SAL_INTERNET_DATE_LEN];
  177. unsigned int moth_num = 0;
  178. struct sal_proto_family *pf = (struct sal_proto_family *)netdev->sal_user_data;
  179. const struct sal_socket_ops *skt_ops;
  180. if (work)
  181. {
  182. rt_free(work);
  183. }
  184. /* get network interface socket operations */
  185. if (pf == RT_NULL || pf->skt_ops == RT_NULL)
  186. {
  187. result = -RT_ERROR;
  188. goto __exit;
  189. }
  190. host = (struct hostent *)pf->netdb_ops->gethostbyname(SAL_INTERNET_HOST);
  191. if (host == RT_NULL)
  192. {
  193. result = -RT_ERROR;
  194. goto __exit;
  195. }
  196. skt_ops = pf->skt_ops;
  197. if ((sockfd = skt_ops->socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  198. {
  199. result = -RT_ERROR;
  200. goto __exit;
  201. }
  202. server_addr.sin_family = AF_INET;
  203. server_addr.sin_port = htons(SAL_INTERNET_PORT);
  204. server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  205. rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
  206. timeout.tv_sec = SAL_INTERNET_TIMEOUT;
  207. timeout.tv_usec = 0;
  208. /* set receive and send timeout */
  209. skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
  210. skt_ops->setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));
  211. /* get build moth value*/
  212. rt_memset(date, 0x00, SAL_INTERNET_DATE_LEN);
  213. rt_snprintf(date, SAL_INTERNET_DATE_LEN, "%s", __DATE__);
  214. for (index = 0; index < sizeof(month) / SAL_INTERNET_MONTH_LEN; index++)
  215. {
  216. if (rt_memcmp(date, month[index], SAL_INTERNET_MONTH_LEN - 1) == 0)
  217. {
  218. moth_num = index + 1;
  219. break;
  220. }
  221. }
  222. /* not find build month */
  223. if (moth_num == 0 || moth_num > sizeof(month) / SAL_INTERNET_MONTH_LEN)
  224. {
  225. result = -RT_ERROR;
  226. goto __exit;
  227. }
  228. rt_memset(send_data, 0x00, SAL_INTERNET_BUFF_LEN);
  229. send_data[0] = SAL_INTERNET_VERSION;
  230. for (index = 0; index < netdev->hwaddr_len; index++)
  231. {
  232. send_data[index + 1] = netdev->hwaddr[index] + moth_num;
  233. }
  234. send_data[9] = RT_VERSION_MAJOR;
  235. send_data[10] = RT_VERSION_MINOR;
  236. send_data[11] = RT_VERSION_PATCH;
  237. skt_ops->sendto(sockfd, send_data, SAL_INTERNET_BUFF_LEN, 0,
  238. (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
  239. result = skt_ops->recvfrom(sockfd, &recv_data, sizeof(recv_data), 0, (struct sockaddr *)&server_addr, &addr_len);
  240. if (result < 0)
  241. {
  242. goto __exit;
  243. }
  244. if (recv_data == RT_FALSE)
  245. {
  246. result = -RT_ERROR;
  247. goto __exit;
  248. }
  249. __exit:
  250. if (result > 0)
  251. {
  252. LOG_D("Set network interface device(%s) internet status up.", netdev->name);
  253. netdev_low_level_set_internet_status(netdev, RT_TRUE);
  254. }
  255. else
  256. {
  257. LOG_D("Set network interface device(%s) internet status down.", netdev->name);
  258. netdev_low_level_set_internet_status(netdev, RT_FALSE);
  259. }
  260. if (sockfd >= 0)
  261. {
  262. skt_ops->closesocket(sockfd);
  263. }
  264. }
  265. #endif /* SAL_INTERNET_CHECK */
  266. /**
  267. * This function will check SAL network interface device internet status.
  268. *
  269. * @param netdev the network interface device to check
  270. */
  271. int sal_check_netdev_internet_up(struct netdev *netdev)
  272. {
  273. RT_ASSERT(netdev);
  274. #ifdef SAL_INTERNET_CHECK
  275. /* workqueue for network connect */
  276. struct rt_work *net_work = RT_NULL;
  277. net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
  278. if (net_work == RT_NULL)
  279. {
  280. LOG_W("No memory for network interface device(%s) delay work.", netdev->name);
  281. return -1;
  282. }
  283. rt_work_init(net_work, check_netdev_internet_up_work, (void *)netdev);
  284. rt_work_submit(net_work, RT_TICK_PER_SECOND);
  285. #endif /* SAL_INTERNET_CHECK */
  286. return 0;
  287. }
  288. /**
  289. * This function will register TLS protocol to the global TLS protocol.
  290. *
  291. * @param pt TLS protocol object
  292. *
  293. * @return 0: TLS protocol object register success
  294. */
  295. #ifdef SAL_USING_TLS
  296. int sal_proto_tls_register(const struct sal_proto_tls *pt)
  297. {
  298. RT_ASSERT(pt);
  299. proto_tls = (struct sal_proto_tls *)pt;
  300. return 0;
  301. }
  302. #endif
  303. /**
  304. * This function will get sal socket object by sal socket descriptor.
  305. *
  306. * @param socket sal socket index
  307. *
  308. * @return sal socket object of the current sal socket index
  309. */
  310. struct sal_socket *sal_get_socket(int socket)
  311. {
  312. struct sal_socket_table *st = &socket_table;
  313. socket = socket - SAL_SOCKET_OFFSET;
  314. if (socket < 0 || socket >= (int)st->max_socket)
  315. {
  316. return RT_NULL;
  317. }
  318. /* check socket structure valid or not */
  319. RT_ASSERT(st->sockets[socket]->magic == SAL_SOCKET_MAGIC);
  320. return st->sockets[socket];
  321. }
  322. /**
  323. * This function will lock sal socket.
  324. *
  325. * @note please don't invoke it on ISR.
  326. */
  327. static void sal_lock(void)
  328. {
  329. rt_err_t result;
  330. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  331. if (result != RT_EOK)
  332. {
  333. RT_ASSERT(0);
  334. }
  335. }
  336. /**
  337. * This function will lock sal socket.
  338. *
  339. * @note please don't invoke it on ISR.
  340. */
  341. static void sal_unlock(void)
  342. {
  343. rt_mutex_release(&sal_core_lock);
  344. }
  345. /**
  346. * This function will clean the netdev.
  347. *
  348. * @note please don't invoke it on ISR.
  349. */
  350. int sal_netdev_cleanup(struct netdev *netdev)
  351. {
  352. uint32_t idx = 0;
  353. int find_dev;
  354. do
  355. {
  356. find_dev = 0;
  357. sal_lock();
  358. for (idx = 0; idx < socket_table.max_socket; idx++)
  359. {
  360. if (socket_table.sockets[idx] && socket_table.sockets[idx]->netdev == netdev)
  361. {
  362. find_dev = 1;
  363. break;
  364. }
  365. }
  366. sal_unlock();
  367. if (find_dev)
  368. {
  369. rt_thread_mdelay(100);
  370. }
  371. } while (find_dev);
  372. return 0;
  373. }
  374. /**
  375. * This function will initialize sal socket object and set socket options
  376. *
  377. * @param family protocol family
  378. * @param type socket type
  379. * @param protocol transfer Protocol
  380. * @param res sal socket object address
  381. *
  382. * @return 0 : socket initialize success
  383. * -1 : input the wrong family
  384. * -2 : input the wrong socket type
  385. * -3 : get network interface failed
  386. * -4 : invalid protocol or combo
  387. */
  388. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  389. {
  390. struct sal_socket *sock;
  391. struct sal_proto_family *pf;
  392. struct netdev *netdv_def = netdev_default;
  393. struct netdev *netdev = RT_NULL;
  394. rt_bool_t flag = RT_FALSE;
  395. /* Existing range checks for family and type */
  396. if (family < 0 || family > AF_MAX)
  397. {
  398. LOG_E("Invalid family: %d (must be 0 ~ %d)", family, AF_MAX);
  399. return -1;
  400. }
  401. if (type < 0 || type > SOCK_MAX)
  402. {
  403. LOG_E("Invalid type: %d (must be 0 ~ %d)", type, SOCK_MAX);
  404. return -2;
  405. }
  406. /* Range check for protocol */
  407. if (!VALID_PROTOCOL(protocol))
  408. {
  409. LOG_E("Invalid protocol: %d (must be 0 ~ %d)", protocol, IPPROTO_RAW);
  410. rt_set_errno(EINVAL);
  411. return -4;
  412. }
  413. sock = *res;
  414. sock->domain = family;
  415. sock->type = type;
  416. sock->protocol = protocol;
  417. /* Combo compatibility check */
  418. if (!VALID_COMBO(family, type, protocol))
  419. {
  420. LOG_E("Invalid combo: domain=%d, type=%d, protocol=%d", family, type, protocol);
  421. rt_set_errno(EINVAL);
  422. return -4;
  423. }
  424. /* Existing netdev selection logic */
  425. if (netdv_def && netdev_is_up(netdv_def))
  426. {
  427. /* check default network interface device protocol family */
  428. pf = (struct sal_proto_family *)netdv_def->sal_user_data;
  429. if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family))
  430. {
  431. sock->netdev = netdv_def;
  432. flag = RT_TRUE;
  433. }
  434. }
  435. if (flag == RT_FALSE)
  436. {
  437. /* get network interface device by protocol family */
  438. netdev = netdev_get_by_family(family);
  439. if (netdev == RT_NULL)
  440. {
  441. LOG_E("not find network interface device by protocol family(%d).", family);
  442. return -3;
  443. }
  444. sock->netdev = netdev;
  445. }
  446. LOG_D("Socket init success: domain=%d, type=%d, protocol=%d, netdev=%s",
  447. family, type, protocol, sock->netdev ? sock->netdev->name : "default");
  448. return 0;
  449. }
  450. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  451. {
  452. int idx;
  453. /* find an empty socket entry */
  454. for (idx = f_socket; idx < (int)st->max_socket; idx++)
  455. {
  456. if (st->sockets[idx] == RT_NULL)
  457. {
  458. break;
  459. }
  460. }
  461. /* allocate a larger sockte container */
  462. if (idx == (int)st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  463. {
  464. int cnt, index;
  465. struct sal_socket **sockets;
  466. /* increase the number of socket with 4 step length */
  467. cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
  468. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  469. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  470. if (sockets == RT_NULL)
  471. goto __result; /* return st->max_socket */
  472. /* clean the new allocated fds */
  473. for (index = st->max_socket; index < cnt; index++)
  474. {
  475. sockets[index] = RT_NULL;
  476. }
  477. st->sockets = sockets;
  478. st->max_socket = cnt;
  479. }
  480. /* allocate 'struct sal_socket' */
  481. if (idx < (int)st->max_socket && st->sockets[idx] == RT_NULL)
  482. {
  483. st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
  484. if (st->sockets[idx] == RT_NULL)
  485. {
  486. idx = st->max_socket;
  487. }
  488. }
  489. __result:
  490. return idx;
  491. }
  492. static void socket_free(struct sal_socket_table *st, int idx)
  493. {
  494. struct sal_socket *sock;
  495. sock = st->sockets[idx];
  496. st->sockets[idx] = RT_NULL;
  497. rt_free(sock);
  498. }
  499. static int socket_new(void)
  500. {
  501. struct sal_socket *sock;
  502. struct sal_socket_table *st = &socket_table;
  503. int idx;
  504. sal_lock();
  505. /* find an empty sal socket entry */
  506. idx = socket_alloc(st, 0);
  507. /* can't find an empty sal socket entry */
  508. if (idx == (int)st->max_socket)
  509. {
  510. idx = -(1 + SAL_SOCKET_OFFSET);
  511. goto __result;
  512. }
  513. sock = st->sockets[idx];
  514. sock->socket = idx + SAL_SOCKET_OFFSET;
  515. sock->magic = SAL_SOCKET_MAGIC;
  516. sock->netdev = RT_NULL;
  517. sock->user_data = RT_NULL;
  518. #ifdef SAL_USING_TLS
  519. sock->user_data_tls = RT_NULL;
  520. #endif
  521. __result:
  522. sal_unlock();
  523. return idx + SAL_SOCKET_OFFSET;
  524. }
  525. static void socket_delete(int socket)
  526. {
  527. struct sal_socket *sock;
  528. struct sal_socket_table *st = &socket_table;
  529. int idx;
  530. idx = socket - SAL_SOCKET_OFFSET;
  531. if (idx < 0 || idx >= (int)st->max_socket)
  532. {
  533. return;
  534. }
  535. sal_lock();
  536. sock = sal_get_socket(socket);
  537. RT_ASSERT(sock != RT_NULL);
  538. sock->magic = 0;
  539. sock->netdev = RT_NULL;
  540. socket_free(st, idx);
  541. sal_unlock();
  542. }
  543. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  544. {
  545. int new_socket;
  546. struct sal_socket *sock;
  547. struct sal_proto_family *pf;
  548. /* get the socket object by socket descriptor */
  549. SAL_SOCKET_OBJ_GET(sock, socket);
  550. /* check the network interface is up status */
  551. SAL_NETDEV_IS_UP(sock->netdev);
  552. /* check the network interface socket operations */
  553. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, accept);
  554. new_socket = pf->skt_ops->accept((int)(size_t)sock->user_data, addr, addrlen);
  555. if (new_socket != -1)
  556. {
  557. int retval;
  558. int new_sal_socket;
  559. struct sal_socket *new_sock;
  560. /* allocate a new socket structure and registered socket options */
  561. new_sal_socket = socket_new();
  562. new_sock = sal_get_socket(new_sal_socket);
  563. if (new_sock == RT_NULL)
  564. {
  565. pf->skt_ops->closesocket(new_socket);
  566. return -1;
  567. }
  568. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  569. if (retval < 0)
  570. {
  571. pf->skt_ops->closesocket(new_socket);
  572. rt_memset(new_sock, 0x00, sizeof(struct sal_socket));
  573. /* socket init failed, delete socket */
  574. socket_delete(new_sal_socket);
  575. LOG_E("New socket registered failed, return error %d.", retval);
  576. return -1;
  577. }
  578. /* new socket create by accept should have the same netdev with server*/
  579. new_sock->netdev = sock->netdev;
  580. /* socket structure user_data used to store the acquired new socket */
  581. new_sock->user_data = (void *)(size_t)new_socket;
  582. return new_sal_socket;
  583. }
  584. return -1;
  585. }
  586. static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr)
  587. {
  588. const struct sockaddr_in *svr_addr = (const struct sockaddr_in *)name;
  589. #if NETDEV_IPV4 && NETDEV_IPV6
  590. local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr;
  591. local_ipaddr->type = IPADDR_TYPE_V4;
  592. #elif NETDEV_IPV4
  593. local_ipaddr->addr = svr_addr->sin_addr.s_addr;
  594. #elif NETDEV_IPV6
  595. #error "not only support IPV6"
  596. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  597. }
  598. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  599. {
  600. struct sal_socket *sock;
  601. struct sal_proto_family *pf;
  602. ip_addr_t input_ipaddr;
  603. RT_ASSERT(name);
  604. /* get the socket object by socket descriptor */
  605. SAL_SOCKET_OBJ_GET(sock, socket);
  606. #define IS_INET_ADDR_FAMILY(_af) ((_af) == AF_INET) || ((_af) == AF_INET6)
  607. if (IS_INET_ADDR_FAMILY(name->sa_family))
  608. {
  609. /* bind network interface by ip address */
  610. sal_sockaddr_to_ipaddr(name, &input_ipaddr);
  611. /* check input ipaddr is default netdev ipaddr */
  612. if (!ip_addr_isany_val(input_ipaddr))
  613. {
  614. struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL;
  615. struct netdev *new_netdev = RT_NULL;
  616. new_netdev = netdev_get_by_ipaddr(&input_ipaddr);
  617. if (new_netdev == RT_NULL)
  618. {
  619. return -1;
  620. }
  621. /* get input and local ip address proto_family */
  622. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind);
  623. SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind);
  624. /* check the network interface protocol family type */
  625. if (input_pf->family != local_pf->family)
  626. {
  627. int new_socket = -1;
  628. /* protocol family is different, close old socket and create new socket by input ip address */
  629. local_pf->skt_ops->closesocket(socket);
  630. new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol);
  631. if (new_socket < 0)
  632. {
  633. return -1;
  634. }
  635. sock->netdev = new_netdev;
  636. sock->user_data = (void *)(size_t)new_socket;
  637. }
  638. }
  639. }
  640. /* check and get protocol families by the network interface device */
  641. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind);
  642. return pf->skt_ops->bind((int)(size_t)sock->user_data, name, namelen);
  643. }
  644. int sal_shutdown(int socket, int how)
  645. {
  646. struct sal_socket *sock;
  647. struct sal_proto_family *pf;
  648. int error = 0;
  649. /* get the socket object by socket descriptor */
  650. SAL_SOCKET_OBJ_GET(sock, socket);
  651. /* shutdown operation not need to check network interface status */
  652. /* check the network interface socket opreation */
  653. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, shutdown);
  654. if (pf->skt_ops->shutdown((int)(size_t)sock->user_data, how) == 0)
  655. {
  656. #ifdef SAL_USING_TLS
  657. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  658. {
  659. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  660. {
  661. return -1;
  662. }
  663. }
  664. #endif
  665. error = 0;
  666. }
  667. else
  668. {
  669. error = -1;
  670. }
  671. return error;
  672. }
  673. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  674. {
  675. struct sal_socket *sock;
  676. struct sal_proto_family *pf;
  677. /* get the socket object by socket descriptor */
  678. SAL_SOCKET_OBJ_GET(sock, socket);
  679. /* check the network interface socket opreation */
  680. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getpeername);
  681. return pf->skt_ops->getpeername((int)(size_t)sock->user_data, name, namelen);
  682. }
  683. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  684. {
  685. struct sal_socket *sock;
  686. struct sal_proto_family *pf;
  687. /* get socket object by socket descriptor */
  688. SAL_SOCKET_OBJ_GET(sock, socket);
  689. /* check the network interface socket opreation */
  690. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockname);
  691. return pf->skt_ops->getsockname((int)(size_t)sock->user_data, name, namelen);
  692. }
  693. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  694. {
  695. struct sal_socket *sock;
  696. struct sal_proto_family *pf;
  697. /* get the socket object by socket descriptor */
  698. SAL_SOCKET_OBJ_GET(sock, socket);
  699. /* check the network interface socket opreation */
  700. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockopt);
  701. return pf->skt_ops->getsockopt((int)(size_t)sock->user_data, level, optname, optval, optlen);
  702. }
  703. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  704. {
  705. struct sal_socket *sock;
  706. struct sal_proto_family *pf;
  707. /* get the socket object by socket descriptor */
  708. SAL_SOCKET_OBJ_GET(sock, socket);
  709. /* check the network interface socket opreation */
  710. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, setsockopt);
  711. #ifdef SAL_USING_TLS
  712. if (level == SOL_TLS)
  713. {
  714. switch (optname)
  715. {
  716. case TLS_CRET_LIST:
  717. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen);
  718. break;
  719. case TLS_CIPHERSUITE_LIST:
  720. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen);
  721. break;
  722. case TLS_PEER_VERIFY:
  723. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen);
  724. break;
  725. case TLS_DTLS_ROLE:
  726. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen);
  727. break;
  728. default:
  729. return -1;
  730. }
  731. return 0;
  732. }
  733. else
  734. {
  735. return pf->skt_ops->setsockopt((int)sock->user_data, level, optname, optval, optlen);
  736. }
  737. #else
  738. return pf->skt_ops->setsockopt((int)(size_t)sock->user_data, level, optname, optval, optlen);
  739. #endif /* SAL_USING_TLS */
  740. }
  741. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  742. {
  743. struct sal_socket *sock;
  744. struct sal_proto_family *pf;
  745. int ret;
  746. /* get the socket object by socket descriptor */
  747. SAL_SOCKET_OBJ_GET(sock, socket);
  748. /* check the network interface is up status */
  749. SAL_NETDEV_IS_UP(sock->netdev);
  750. /* check the network interface socket opreation */
  751. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect);
  752. ret = pf->skt_ops->connect((int)(size_t)sock->user_data, name, namelen);
  753. #ifdef SAL_USING_TLS
  754. if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect))
  755. {
  756. if (proto_tls->ops->connect(sock->user_data_tls) < 0)
  757. {
  758. return -1;
  759. }
  760. return ret;
  761. }
  762. #endif
  763. return ret;
  764. }
  765. int sal_listen(int socket, int backlog)
  766. {
  767. struct sal_socket *sock;
  768. struct sal_proto_family *pf;
  769. /* get the socket object by socket descriptor */
  770. SAL_SOCKET_OBJ_GET(sock, socket);
  771. /* check the network interface socket opreation */
  772. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, listen);
  773. return pf->skt_ops->listen((int)(size_t)sock->user_data, backlog);
  774. }
  775. int sal_sendmsg(int socket, const struct msghdr *message, int flags)
  776. {
  777. struct sal_socket *sock;
  778. struct sal_proto_family *pf;
  779. /* get the socket object by socket descriptor */
  780. SAL_SOCKET_OBJ_GET(sock, socket);
  781. /* check the network interface is up status */
  782. SAL_NETDEV_IS_UP(sock->netdev);
  783. /* check the network interface socket opreation */
  784. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendmsg);
  785. #ifdef SAL_USING_TLS
  786. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  787. {
  788. int ret;
  789. if ((ret = proto_tls->ops->send(sock->user_data_tls, message, flags)) < 0)
  790. {
  791. return -1;
  792. }
  793. return ret;
  794. }
  795. else
  796. {
  797. return pf->skt_ops->sendmsg((int)(size_t)sock->user_data, message, flags);
  798. }
  799. #else
  800. return pf->skt_ops->sendmsg((int)(size_t)sock->user_data, message, flags);
  801. #endif
  802. }
  803. int sal_recvmsg(int socket, struct msghdr *message, int flags)
  804. {
  805. struct sal_socket *sock;
  806. struct sal_proto_family *pf;
  807. /* get the socket object by socket descriptor */
  808. SAL_SOCKET_OBJ_GET(sock, socket);
  809. /* check the network interface is up status */
  810. SAL_NETDEV_IS_UP(sock->netdev);
  811. /* check the network interface socket opreation */
  812. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvmsg);
  813. #ifdef SAL_USING_TLS
  814. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  815. {
  816. int ret;
  817. if ((ret = proto_tls->ops->recv(sock->user_data_tls, message, flags)) < 0)
  818. {
  819. return -1;
  820. }
  821. return ret;
  822. }
  823. else
  824. {
  825. return pf->skt_ops->recvmsg((int)(size_t)sock->user_data, message, flags);
  826. }
  827. #else
  828. return pf->skt_ops->recvmsg((int)(size_t)sock->user_data, message, flags);
  829. #endif
  830. }
  831. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  832. struct sockaddr *from, socklen_t *fromlen)
  833. {
  834. struct sal_socket *sock;
  835. struct sal_proto_family *pf;
  836. /* get the socket object by socket descriptor */
  837. SAL_SOCKET_OBJ_GET(sock, socket);
  838. /* check the network interface is up status */
  839. SAL_NETDEV_IS_UP(sock->netdev);
  840. /* check the network interface socket opreation */
  841. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom);
  842. #ifdef SAL_USING_TLS
  843. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  844. {
  845. int ret;
  846. if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0)
  847. {
  848. return -1;
  849. }
  850. return ret;
  851. }
  852. else
  853. {
  854. return pf->skt_ops->recvfrom((int)(size_t)sock->user_data, mem, len, flags, from, fromlen);
  855. }
  856. #else
  857. return pf->skt_ops->recvfrom((int)(size_t)sock->user_data, mem, len, flags, from, fromlen);
  858. #endif
  859. }
  860. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  861. const struct sockaddr *to, socklen_t tolen)
  862. {
  863. struct sal_socket *sock;
  864. struct sal_proto_family *pf;
  865. /* get the socket object by socket descriptor */
  866. SAL_SOCKET_OBJ_GET(sock, socket);
  867. /* check the network interface is up status */
  868. SAL_NETDEV_IS_UP(sock->netdev);
  869. /* check the network interface socket opreation */
  870. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto);
  871. #ifdef SAL_USING_TLS
  872. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  873. {
  874. int ret;
  875. if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0)
  876. {
  877. return -1;
  878. }
  879. return ret;
  880. }
  881. else
  882. {
  883. return pf->skt_ops->sendto((int)sock->user_data, dataptr, size, flags, to, tolen);
  884. }
  885. #else
  886. return pf->skt_ops->sendto((int)(size_t)sock->user_data, dataptr, size, flags, to, tolen);
  887. #endif
  888. }
  889. int sal_socket(int domain, int type, int protocol)
  890. {
  891. int retval;
  892. int socket, proto_socket;
  893. struct sal_socket *sock;
  894. struct sal_proto_family *pf;
  895. /* allocate a new socket and registered socket options */
  896. socket = socket_new();
  897. if (socket < 0)
  898. {
  899. return -1;
  900. }
  901. /* get sal socket object by socket descriptor */
  902. sock = sal_get_socket(socket);
  903. if (sock == RT_NULL)
  904. {
  905. socket_delete(socket);
  906. return -1;
  907. }
  908. /* Initialize sal socket object */
  909. retval = socket_init(domain, type, protocol, &sock);
  910. if (retval < 0)
  911. {
  912. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  913. socket_delete(socket);
  914. return retval;
  915. }
  916. /* valid the network interface socket opreation */
  917. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket);
  918. proto_socket = pf->skt_ops->socket(domain, type, protocol);
  919. if (proto_socket >= 0)
  920. {
  921. #ifdef SAL_USING_TLS
  922. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket))
  923. {
  924. sock->user_data_tls = proto_tls->ops->socket(socket);
  925. if (sock->user_data_tls == RT_NULL)
  926. {
  927. socket_delete(socket);
  928. return -1;
  929. }
  930. }
  931. #endif
  932. sock->user_data = (void *)(size_t)proto_socket;
  933. return sock->socket;
  934. }
  935. socket_delete(socket);
  936. return -1;
  937. }
  938. int sal_socketpair(int domain, int type, int protocol, int *fds)
  939. {
  940. int unix_fd[2];
  941. struct sal_socket *socka;
  942. struct sal_socket *sockb;
  943. struct sal_proto_family *pf;
  944. if (domain == AF_UNIX)
  945. {
  946. /* get the socket object by socket descriptor */
  947. SAL_SOCKET_OBJ_GET(socka, fds[0]);
  948. SAL_SOCKET_OBJ_GET(sockb, fds[1]);
  949. /* valid the network interface socket opreation */
  950. SAL_NETDEV_SOCKETOPS_VALID(socka->netdev, pf, socket);
  951. unix_fd[0] = (int)(size_t)socka->user_data;
  952. unix_fd[1] = (int)(size_t)sockb->user_data;
  953. if (pf->skt_ops->socketpair)
  954. {
  955. return pf->skt_ops->socketpair(domain, type, protocol, unix_fd);
  956. }
  957. }
  958. rt_set_errno(EINVAL);
  959. return -1;
  960. }
  961. int sal_closesocket(int socket)
  962. {
  963. struct sal_socket *sock;
  964. struct sal_proto_family *pf;
  965. int error = 0;
  966. /* get the socket object by socket descriptor */
  967. SAL_SOCKET_OBJ_GET(sock, socket);
  968. /* clsoesocket operation not need to vaild network interface status */
  969. /* valid the network interface socket opreation */
  970. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, closesocket);
  971. if (pf->skt_ops->closesocket((int)(size_t)sock->user_data) == 0)
  972. {
  973. #ifdef SAL_USING_TLS
  974. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  975. {
  976. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  977. {
  978. return -1;
  979. }
  980. }
  981. #endif
  982. error = 0;
  983. }
  984. else
  985. {
  986. error = -1;
  987. }
  988. /* delete socket */
  989. socket_delete(socket);
  990. return error;
  991. }
  992. #define ARPHRD_ETHER 1 /* Ethernet 10/100Mbps. */
  993. #define ARPHRD_LOOPBACK 772 /* Loopback device. */
  994. #define IFF_UP 0x1
  995. #define IFF_RUNNING 0x40
  996. #define IFF_NOARP 0x80
  997. int sal_ioctlsocket(int socket, long cmd, void *arg)
  998. {
  999. rt_slist_t *node = RT_NULL;
  1000. struct netdev *netdev = RT_NULL;
  1001. struct netdev *cur_netdev_list = netdev_list;
  1002. struct sal_socket *sock;
  1003. struct sal_proto_family *pf;
  1004. struct sockaddr_in *addr_in = RT_NULL;
  1005. struct sockaddr *addr = RT_NULL;
  1006. ip_addr_t input_ipaddr;
  1007. /* get the socket object by socket descriptor */
  1008. SAL_SOCKET_OBJ_GET(sock, socket);
  1009. struct sal_ifreq *ifr = (struct sal_ifreq *)arg;
  1010. if (ifr != RT_NULL)
  1011. {
  1012. switch (cmd)
  1013. {
  1014. case SIOCGIFADDR:
  1015. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1016. {
  1017. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
  1018. #if NETDEV_IPV4 && NETDEV_IPV6
  1019. addr_in->sin_addr.s_addr = sock->netdev->ip_addr.u_addr.ip4.addr;
  1020. #elif NETDEV_IPV4
  1021. addr_in->sin_addr.s_addr = sock->netdev->ip_addr.addr;
  1022. #elif NETDEV_IPV6
  1023. #error "not only support IPV6"
  1024. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  1025. return 0;
  1026. }
  1027. else
  1028. {
  1029. if (cur_netdev_list == RT_NULL)
  1030. {
  1031. LOG_E("ifconfig: network interface device list error.\n");
  1032. return -1;
  1033. }
  1034. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1035. {
  1036. netdev = rt_list_entry(node, struct netdev, list);
  1037. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1038. {
  1039. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_addr);
  1040. #if NETDEV_IPV4 && NETDEV_IPV6
  1041. addr_in->sin_addr.s_addr = netdev->ip_addr.u_addr.ip4.addr;
  1042. #elif NETDEV_IPV4
  1043. addr_in->sin_addr.s_addr = netdev->ip_addr.addr;
  1044. #elif NETDEV_IPV6
  1045. #error "Do not only support IPV6"
  1046. #endif /* NETDEV_IPV4 && NETDEV_IPV6 */
  1047. return 0;
  1048. }
  1049. }
  1050. return -1;
  1051. }
  1052. case SIOCSIFADDR:
  1053. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1054. {
  1055. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_addr);
  1056. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1057. netdev_set_ipaddr(sock->netdev, &input_ipaddr);
  1058. return 0;
  1059. }
  1060. else
  1061. {
  1062. if (cur_netdev_list == RT_NULL)
  1063. {
  1064. LOG_E("ifconfig: network interface device list error.\n");
  1065. return -1;
  1066. }
  1067. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1068. {
  1069. netdev = rt_list_entry(node, struct netdev, list);
  1070. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1071. {
  1072. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_addr);
  1073. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1074. netdev_set_ipaddr(netdev, &input_ipaddr);
  1075. return 0;
  1076. }
  1077. }
  1078. return -1;
  1079. }
  1080. case SIOCGIFNETMASK:
  1081. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1082. {
  1083. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
  1084. #if NETDEV_IPV4 && NETDEV_IPV6
  1085. addr_in->sin_addr.s_addr = sock->netdev->netmask.u_addr.ip4.addr;
  1086. #elif NETDEV_IPV4
  1087. addr_in->sin_addr.s_addr = sock->netdev->netmask.addr;
  1088. #elif NETDEV_IPV6
  1089. #error "not only support IPV6"
  1090. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  1091. return 0;
  1092. }
  1093. else
  1094. {
  1095. if (cur_netdev_list == RT_NULL)
  1096. {
  1097. LOG_E("ifconfig: network interface device list error.\n");
  1098. return -1;
  1099. }
  1100. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1101. {
  1102. netdev = rt_list_entry(node, struct netdev, list);
  1103. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1104. {
  1105. addr_in = (struct sockaddr_in *)&(ifr->ifr_ifru.ifru_netmask);
  1106. #if NETDEV_IPV4 && NETDEV_IPV6
  1107. addr_in->sin_addr.s_addr = netdev->netmask.u_addr.ip4.addr;
  1108. #elif NETDEV_IPV4
  1109. addr_in->sin_addr.s_addr = netdev->netmask.addr;
  1110. #elif NETDEV_IPV6
  1111. #error "not only support IPV6"
  1112. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  1113. return 0;
  1114. }
  1115. }
  1116. return -1;
  1117. }
  1118. case SIOCSIFNETMASK:
  1119. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1120. {
  1121. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_netmask);
  1122. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1123. netdev_set_netmask(sock->netdev, &input_ipaddr);
  1124. return 0;
  1125. }
  1126. else
  1127. {
  1128. if (cur_netdev_list == RT_NULL)
  1129. {
  1130. LOG_E("ifconfig: network interface device list error.\n");
  1131. return -1;
  1132. }
  1133. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1134. {
  1135. netdev = rt_list_entry(node, struct netdev, list);
  1136. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1137. {
  1138. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_netmask);
  1139. sal_sockaddr_to_ipaddr(addr, &input_ipaddr);
  1140. netdev_set_netmask(netdev, &input_ipaddr);
  1141. return 0;
  1142. }
  1143. }
  1144. return -1;
  1145. }
  1146. case SIOCGIFHWADDR:
  1147. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1148. {
  1149. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_hwaddr);
  1150. #ifdef RT_USING_LWP
  1151. if (!strcmp("lo", sock->netdev->name))
  1152. {
  1153. struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1154. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
  1155. }
  1156. else
  1157. {
  1158. struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1159. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  1160. }
  1161. #endif
  1162. rt_memcpy(addr->sa_data, sock->netdev->hwaddr, sock->netdev->hwaddr_len);
  1163. return 0;
  1164. }
  1165. else
  1166. {
  1167. if (cur_netdev_list == RT_NULL)
  1168. {
  1169. LOG_E("ifconfig: network interface device list error.\n");
  1170. return -1;
  1171. }
  1172. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1173. {
  1174. netdev = rt_list_entry(node, struct netdev, list);
  1175. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1176. {
  1177. addr = (struct sockaddr *)&(ifr->ifr_ifru.ifru_hwaddr);
  1178. #ifdef RT_USING_LWP
  1179. if (!strcmp("lo", netdev->name))
  1180. {
  1181. struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1182. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_LOOPBACK;
  1183. }
  1184. else
  1185. {
  1186. struct musl_ifreq *musl_ifreq_tmp = (struct musl_ifreq *)arg;
  1187. musl_ifreq_tmp->ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  1188. }
  1189. #endif
  1190. rt_memcpy(addr->sa_data, netdev->hwaddr, netdev->hwaddr_len);
  1191. return 0;
  1192. }
  1193. }
  1194. return -1;
  1195. }
  1196. case SIOCGIFMTU:
  1197. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1198. {
  1199. ifr->ifr_ifru.ifru_mtu = sock->netdev->mtu;
  1200. return 0;
  1201. }
  1202. else
  1203. {
  1204. if (cur_netdev_list == RT_NULL)
  1205. {
  1206. LOG_E("ifconfig: network interface device list error.\n");
  1207. return -1;
  1208. }
  1209. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1210. {
  1211. netdev = rt_list_entry(node, struct netdev, list);
  1212. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1213. {
  1214. ifr->ifr_ifru.ifru_mtu = netdev->mtu;
  1215. return 0;
  1216. }
  1217. }
  1218. return -1;
  1219. }
  1220. case SIOCGIFFLAGS:
  1221. if (!strcmp(ifr->ifr_ifrn.ifrn_name, sock->netdev->name))
  1222. {
  1223. uint16_t flags_tmp = 0;
  1224. if (sock->netdev->flags & NETDEV_FLAG_UP)
  1225. flags_tmp = flags_tmp | IFF_UP;
  1226. if (!(sock->netdev->flags & NETDEV_FLAG_ETHARP))
  1227. flags_tmp = flags_tmp | IFF_NOARP;
  1228. flags_tmp = flags_tmp | IFF_RUNNING;
  1229. ifr->ifr_ifru.ifru_flags = flags_tmp;
  1230. return 0;
  1231. }
  1232. else
  1233. {
  1234. if (cur_netdev_list == RT_NULL)
  1235. {
  1236. LOG_E("ifconfig: network interface device list error.\n");
  1237. return -1;
  1238. }
  1239. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1240. {
  1241. netdev = rt_list_entry(node, struct netdev, list);
  1242. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1243. {
  1244. uint16_t flags_tmp = 0;
  1245. if (netdev->flags & NETDEV_FLAG_UP)
  1246. flags_tmp = flags_tmp | IFF_UP;
  1247. if (!(netdev->flags & NETDEV_FLAG_ETHARP))
  1248. flags_tmp = flags_tmp | IFF_NOARP;
  1249. ifr->ifr_ifru.ifru_flags = flags_tmp;
  1250. return 0;
  1251. }
  1252. }
  1253. return -1;
  1254. }
  1255. case SIOCSIFFLAGS:
  1256. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1257. {
  1258. netdev = rt_list_entry(node, struct netdev, list);
  1259. if (!strcmp(ifr->ifr_ifrn.ifrn_name, netdev->name))
  1260. {
  1261. if ((ifr->ifr_ifru.ifru_flags & IFF_UP) == 0)
  1262. {
  1263. netdev_set_down(netdev);
  1264. }
  1265. else
  1266. {
  1267. netdev_set_up(netdev);
  1268. }
  1269. return 0;
  1270. }
  1271. }
  1272. return -1;
  1273. case SIOCGIFCONF:
  1274. {
  1275. struct ifconf *ifconf_tmp;
  1276. ifconf_tmp = (struct ifconf *)arg;
  1277. int count_size = 0;
  1278. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1279. {
  1280. struct sal_ifreq sal_ifreq_temp;
  1281. count_size++;
  1282. netdev = rt_list_entry(node, struct netdev, list);
  1283. rt_strcpy(sal_ifreq_temp.ifr_ifrn.ifrn_name, netdev->name);
  1284. rt_memcpy(ifconf_tmp->ifc_ifcu.ifcu_buf, &sal_ifreq_temp, sizeof(struct sal_ifreq));
  1285. ifconf_tmp->ifc_ifcu.ifcu_buf += sizeof(struct sal_ifreq);
  1286. }
  1287. ifconf_tmp->ifc_len = sizeof(struct sal_ifreq) * count_size;
  1288. ifconf_tmp->ifc_ifcu.ifcu_buf = ifconf_tmp->ifc_ifcu.ifcu_buf - sizeof(struct sal_ifreq) * count_size;
  1289. return 0;
  1290. }
  1291. case SIOCGIFINDEX:
  1292. {
  1293. netdev = netdev_get_by_name(ifr->ifr_ifrn.ifrn_name);
  1294. if (netdev)
  1295. {
  1296. ifr->ifr_ifru.ifru_ivalue = netdev->ifindex;
  1297. return 0;
  1298. }
  1299. return -ENODEV;
  1300. }
  1301. default:
  1302. break;
  1303. }
  1304. }
  1305. /* check the network interface socket opreation */
  1306. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, ioctlsocket);
  1307. return pf->skt_ops->ioctlsocket((int)(size_t)sock->user_data, cmd, arg);
  1308. }
  1309. #ifdef SAL_USING_POSIX
  1310. int sal_poll(struct dfs_file *file, struct rt_pollreq *req)
  1311. {
  1312. struct sal_socket *sock;
  1313. struct sal_proto_family *pf;
  1314. int socket = (int)(size_t)file->vnode->data;
  1315. /* get the socket object by socket descriptor */
  1316. SAL_SOCKET_OBJ_GET(sock, socket);
  1317. /* check the network interface is up status */
  1318. SAL_NETDEV_IS_UP(sock->netdev);
  1319. /* check the network interface socket opreation */
  1320. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll);
  1321. return pf->skt_ops->poll(file, req);
  1322. }
  1323. #endif
  1324. struct hostent *sal_gethostbyname(const char *name)
  1325. {
  1326. struct netdev *netdev = netdev_default;
  1327. struct sal_proto_family *pf;
  1328. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  1329. {
  1330. return pf->netdb_ops->gethostbyname(name);
  1331. }
  1332. else
  1333. {
  1334. /* get the first network interface device with up status */
  1335. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1336. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  1337. {
  1338. return pf->netdb_ops->gethostbyname(name);
  1339. }
  1340. }
  1341. return RT_NULL;
  1342. }
  1343. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  1344. size_t buflen, struct hostent **result, int *h_errnop)
  1345. {
  1346. struct netdev *netdev = netdev_default;
  1347. struct sal_proto_family *pf;
  1348. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  1349. {
  1350. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  1351. }
  1352. else
  1353. {
  1354. /* get the first network interface device with up status */
  1355. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1356. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  1357. {
  1358. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  1359. }
  1360. }
  1361. return -1;
  1362. }
  1363. int sal_getaddrinfo(const char *nodename,
  1364. const char *servname,
  1365. const struct addrinfo *hints,
  1366. struct addrinfo **res)
  1367. {
  1368. struct netdev *netdev = netdev_default;
  1369. struct sal_proto_family *pf;
  1370. int ret = 0;
  1371. rt_uint32_t i = 0;
  1372. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  1373. {
  1374. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  1375. }
  1376. else
  1377. {
  1378. /* get the first network interface device with up status */
  1379. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  1380. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  1381. {
  1382. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  1383. }
  1384. else
  1385. {
  1386. ret = -1;
  1387. }
  1388. }
  1389. if (ret == RT_EOK)
  1390. {
  1391. /*record the netdev and res*/
  1392. for (i = 0; i < SAL_SOCKETS_NUM; i++)
  1393. {
  1394. if (sal_dev_res_tbl[i].res == RT_NULL)
  1395. {
  1396. sal_dev_res_tbl[i].res = *res;
  1397. sal_dev_res_tbl[i].netdev = netdev;
  1398. break;
  1399. }
  1400. }
  1401. RT_ASSERT((i < SAL_SOCKETS_NUM));
  1402. }
  1403. return ret;
  1404. }
  1405. void sal_freeaddrinfo(struct addrinfo *ai)
  1406. {
  1407. struct netdev *netdev = RT_NULL;
  1408. struct sal_proto_family *pf = RT_NULL;
  1409. rt_uint32_t i = 0;
  1410. /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
  1411. for (i = 0; i < SAL_SOCKETS_NUM; i++)
  1412. {
  1413. if (sal_dev_res_tbl[i].res == ai)
  1414. {
  1415. netdev = sal_dev_res_tbl[i].netdev;
  1416. sal_dev_res_tbl[i].res = RT_NULL;
  1417. sal_dev_res_tbl[i].netdev = RT_NULL;
  1418. break;
  1419. }
  1420. }
  1421. RT_ASSERT((i < SAL_SOCKETS_NUM));
  1422. if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
  1423. {
  1424. pf->netdb_ops->freeaddrinfo(ai);
  1425. }
  1426. }