sal_socket.c 48 KB

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