sal_socket.c 45 KB

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